1
2
3
4
5
6 package net.sf.navigator.displayer;
7
8 import java.io.IOException;
9 import java.text.MessageFormat;
10
11 import javax.servlet.jsp.JspException;
12 import javax.servlet.jsp.PageContext;
13
14 import net.sf.navigator.menu.MenuComponent;
15
16
17 /**
18 *
19 * @author ssayles
20 */
21 public class CoolMenuDisplayer extends MessageResourcesMenuDisplayer {
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 /** main message format of the menu. only 10 args max in jdk1.3 :( */
49 private static MessageFormat menuMessage =
50 new MessageFormat(
51 "oCMenu.makeMenu(''{0}'',''{1}'',''{2}'',''{3}'',''{4}'','''',''''," +
52 "''{5}'',''{6}'',{7},{8},{9},");
53 private static final String SCRIPT_START =
54 "<script type=\"text/javascript\">\n";
55 private static final String SCRIPT_END = "</script>\n";
56 private static final String END_STATEMENT =
57 "\noCMenu.makeStyle(); oCMenu.construct()\n";
58 private static final String TOP_IMAGE = "cmTopMenuImage";
59 private static final String SUB_IMAGE = "cmSubMenuImage";
60 private static final String BGCOL_ON = "cmBGColorOn";
61 private static final String BGCOL_OFF = "cmBGColorOff";
62 private static final String TXTCOL = "cmTxtColor";
63 private static final String HOVER = "cmHoverColor";
64 private static final String DIS_BGCOL_ON = "cmDisBGColorOn";
65 private static final String DIS_BGCOL_OFF = "cmDisBGColorOff";
66 private static final String DIS_TXTCOL = "cmDisTxtColor";
67 private static final String DIS_HOVER = "cmDisHoverColor";
68
69
70
71 public void init(PageContext context, MenuDisplayerMapping mapping) {
72 super.init(context, mapping);
73
74 try {
75 out.print(SCRIPT_START);
76 } catch (Exception e) {}
77 }
78
79 /**
80 * Prints the appropriate javascript for CoolMenu using \
81 * <code>menuMessage</code> as the format.
82 */
83 public void display(MenuComponent menu) throws JspException, IOException {
84 StringBuffer sb = new StringBuffer();
85 buildMenuString(menu, sb, isAllowed(menu));
86 out.print(sb);
87 }
88
89 /**
90 * This will output the ending javascript statements defined in
91 * <code>END_STATEMENT</code> and <code>SCRIPT_END</code>
92 */
93 public void end(PageContext context) {
94 try {
95 out.print(END_STATEMENT);
96 out.print(SCRIPT_END);
97 } catch (Exception e) {}
98 }
99
100 protected void buildMenuString(MenuComponent menu, StringBuffer sb,
101 boolean allowed) {
102 sb.append(menuMessage.format(getArgs(menu, allowed)));
103 sb.append((allowed) ? HOVER : DIS_HOVER);
104 sb.append(",'");
105 sb.append((menu.getOnclick() == null) ? EMPTY : menu.getOnclick());
106 sb.append("')\n");
107
108 MenuComponent[] subMenus = menu.getMenuComponents();
109
110 if (subMenus.length > 0) {
111 for (int i = 0; i < subMenus.length; i++) {
112 buildMenuString(subMenus[i], sb,
113 (allowed) ? isAllowed(subMenus[i]) : allowed);
114 }
115 }
116 }
117
118 protected String[] getArgs(MenuComponent menu, boolean allowed) {
119 String[] args = new String[10];
120 args[0] = menu.getName();
121 args[1] = getParentName(menu);
122 args[2] = getTitle(menu);
123 args[3] =
124 (menu.getUrl() == null) ? EMPTY
125 : ((allowed) ? menu.getUrl() : EMPTY);
126 args[4] = getTarget(menu);
127 args[5] = EMPTY;
128 args[6] = EMPTY;
129 args[7] = (allowed) ? BGCOL_OFF : DIS_BGCOL_OFF;
130 args[8] = (allowed) ? BGCOL_ON : DIS_BGCOL_ON;
131 args[9] = (allowed) ? TXTCOL : DIS_TXTCOL;
132
133 return args;
134 }
135
136 /**
137 * Return a translated title for the menu item that will contain
138 * the <code>TOP_IMAGE</code> javscript variable if it is a parent
139 * menu with sub menus, or the <code>SUB_IMAGE</code> variable if it
140 * is a sub menu with sub menus.
141 */
142 protected String getTitle(MenuComponent menu) {
143 boolean hasSubMenus = false;
144 String title = getMessage(menu.getTitle());
145
146 if (menu.getMenuComponents().length > 0) {
147 hasSubMenus = true;
148
149 if (menu.getParent() == null) {
150 title = title + "'+" + TOP_IMAGE + "+'";
151 } else {
152 title = title + "'+" + SUB_IMAGE + "+'";
153 }
154 }
155
156 return title;
157 }
158
159 protected String getParentName(MenuComponent menu) {
160 String name = null;
161
162 if (menu.getParent() == null) {
163 name = "";
164 } else {
165 name = menu.getParent().getName();
166 }
167
168 return name;
169 }
170
171 protected String getTarget(MenuComponent menu) {
172 String theTarget = super.getTarget(menu);
173
174 if (theTarget == null) {
175 theTarget = EMPTY;
176 }
177
178 return theTarget;
179 }
180 }