1 package net.sf.navigator.displayer;
2
3 import java.io.IOException;
4 import java.text.MessageFormat;
5
6 import javax.servlet.jsp.JspException;
7 import javax.servlet.jsp.PageContext;
8
9 import net.sf.navigator.displayer.MenuDisplayerMapping;
10 import net.sf.navigator.displayer.MessageResourcesMenuDisplayer;
11 import net.sf.navigator.menu.MenuComponent;
12 import org.apache.commons.lang.StringUtils;
13
14 /**
15 * Create the javascript required for the Xtree menu functionality
16 *
17 */
18 public class XtreeMenuDisplayer extends MessageResourcesMenuDisplayer {
19
20
21
22
23
24
25
26
27
28
29
30 private static MessageFormat newWebFXTreeMessage = new MessageFormat("var {0} = new WebFXTree(''{2}'');");
31 private static MessageFormat newWebFXTreeItemMessage = new MessageFormat("var {0} = new WebFXTreeItem(''{2}'');");
32 private static MessageFormat newWebFXTreeItemMessage1 = new MessageFormat("var {0} = new WebFXTreeItem(''{2}'',''{3}'');");
33 private static MessageFormat addMessage = new MessageFormat("{1}.add({0});");
34 private static MessageFormat writeMessage = new MessageFormat("document.write({0});");
35 private static String parent = "";
36
37 private static final String END_STATEMENT_START = "document.write(";
38 private static final String END_STATEMENT_END = ");";
39 private static final String TAB = " ";
40 private static final String SCRIPT_START = "\n<script type=\"text/javascript\">\n<!--";
41 private static final String SCRIPT_END = "//-->\n</script>\n";
42
43
44
45 public void init(PageContext context, MenuDisplayerMapping mapping) {
46 super.init(context, mapping);
47
48 try {
49 out.print(SCRIPT_START);
50 } catch (Exception e) {
51 }
52 }
53
54 public void display(MenuComponent menu) throws JspException, IOException {
55 StringBuffer sb = new StringBuffer();
56 buildMenuString(menu, sb, isAllowed(menu));
57 out.print("\n" + TAB + sb);
58 out.print(TAB + END_STATEMENT_START + parent + END_STATEMENT_END);
59 }
60
61 public void end(PageContext context) {
62 try {
63 out.print(SCRIPT_END);
64 } catch (Exception e) {
65 }
66 }
67
68 protected void buildMenuString(MenuComponent menu, StringBuffer sb, boolean allowed) {
69 if (allowed) {
70 String[] args = getArgs(menu);
71 if (args[1] == null || args[1].equals("")) {
72 sb.append(newWebFXTreeMessage.format(args) + "\n" + TAB + TAB);
73 parent = args[0];
74 } else if (args[3] == null || args[3].equals("")) {
75 sb.append(newWebFXTreeItemMessage.format(args) + "\n" + TAB + TAB);
76 sb.append(addMessage.format(args) + "\n" + TAB + TAB);
77 } else {
78 sb.append(newWebFXTreeItemMessage1.format(args) + "\n" + TAB + TAB);
79 sb.append(addMessage.format(args) + "\n" + TAB + TAB);
80 }
81
82 MenuComponent[] subMenus = menu.getMenuComponents();
83
84 if (subMenus.length > 0) {
85 for (int i = 0; i < subMenus.length; i++) {
86 buildMenuString(subMenus[i], sb, (allowed) ? isAllowed(subMenus[i]) : allowed);
87 }
88 }
89 }
90 }
91
92 protected String[] getArgs(MenuComponent menu) {
93 String[] args = new String[4];
94 args[0] = menu.getName();
95 args[1] = getParentName(menu);
96 args[2] = getMessage(menu.getTitle());
97 args[3] = (menu.getUrl() == null) ? EMPTY : menu.getUrl();
98
99 return args;
100 }
101
102 protected String getParentName(MenuComponent menu) {
103 String name = null;
104
105 if (menu.getParent() == null) {
106 name = "";
107 } else {
108 name = menu.getParent().getName();
109 }
110
111 return name;
112 }
113
114 protected String getTarget(MenuComponent menu) {
115 String theTarget = super.getTarget(menu);
116
117 if (theTarget == null) {
118 theTarget = EMPTY;
119 }
120
121 return theTarget;
122 }
123 }