1
2
3
4
5
6 package net.sf.navigator.displayer;
7
8 import net.sf.navigator.menu.MenuComponent;
9 import org.apache.commons.lang.StringUtils;
10
11 import javax.servlet.jsp.JspException;
12 import javax.servlet.jsp.PageContext;
13 import java.io.IOException;
14 import java.text.MessageFormat;
15
16
17 /**
18 *
19 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
20 */
21 public class CoolMenuDisplayer4 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
49 /** main message format of the menu. only 10 args max in jdk1.3 :( */
50
51
52
53 private static MessageFormat menuMessage =
54 new MessageFormat(
55 ".makeMenu(''{0}'',''{1}'',''{2}'',''{3}'',''{4}'',''{5}'',''''," +
56 "'''','''','''','''',''{6}'','''',0,''{7}'',''{8}'',''{9}'');");
57 private static final String TAB = " ";
58 private static final String SCRIPT_START =
59 "\n<script type=\"text/javascript\">\n<!--";
60 private static final String SCRIPT_END = "//-->\n</script>\n";
61 private static final String END_STATEMENT = ".construct();\n";
62 private String menuId;
63
64
65
66 public void init(PageContext pageContext, MenuDisplayerMapping mapping) {
67 super.init(pageContext, mapping);
68 menuId = (String) pageContext.getAttribute("menuId");
69
70 try {
71 out.print(SCRIPT_START);
72 } catch (Exception e) {
73 log.error(e.getMessage());
74 }
75 }
76
77 /**
78 * Prints the appropriate javascript for CoolMenu using \
79 * <code>menuMessage</code> as the format.
80 */
81 public void display(MenuComponent menu) throws JspException, IOException {
82 StringBuffer sb = new StringBuffer();
83 buildMenuString(menu, sb, isAllowed(menu));
84 out.print("\n" + TAB + sb);
85 }
86
87 /**
88 * This will output the ending javascript statements defined in
89 * <code>END_STATEMENT</code> and <code>SCRIPT_END</code>
90 */
91 public void end(PageContext context) {
92 try {
93 out.print(TAB + getMenuName() + END_STATEMENT);
94 out.print(SCRIPT_END);
95 } catch (Exception e) {
96 log.error(e.getMessage());
97 } finally {
98 this.menuId = null;
99 }
100 }
101
102 protected void buildMenuString(MenuComponent menu, StringBuffer sb, boolean allowed) {
103 if (allowed) {
104 sb.append(getMenuName()).append(menuMessage.format(getArgs(menu))).append("\n").append(TAB).append(TAB);
105
106 MenuComponent[] subMenus = menu.getMenuComponents();
107
108 if (subMenus.length > 0) {
109 for (int i = 0; i < subMenus.length; i++) {
110 buildMenuString(subMenus[i], sb, isAllowed(subMenus[i]));
111 }
112 }
113 }
114 }
115
116 protected String[] getArgs(MenuComponent menu) {
117 String[] args = new String[10];
118 args[0] = menu.getName();
119 args[1] = getParentName(menu);
120 args[2] =
121 (menu.getImage() != null)
122 ? (displayStrings.getMessage("cm.image", menu.getImage()) + " " +
123 getMessage(menu.getTitle())) : getMessage(menu.getTitle());
124 args[3] = (menu.getUrl() == null) ? EMPTY : menu.getUrl();
125 args[4] = getTarget(menu);
126 args[5] = (menu.getWidth() == null) ? EMPTY : menu.getWidth();
127 args[6] = (menu.getAlign() == null) ? EMPTY : menu.getAlign();
128 args[7] = (menu.getOnclick() == null) ? EMPTY : menu.getOnclick();
129 args[8] =
130 (menu.getOnmouseover() == null) ? EMPTY : menu.getOnmouseover();
131 args[9] = (menu.getOnmouseout() == null) ? EMPTY : menu.getOnmouseout();
132
133
134 args[2] = StringUtils.replace(args[2], "\"", "\\\"");
135
136 return args;
137 }
138
139
140 protected String getParentName(MenuComponent menu) {
141 String name;
142
143 if (menu.getParent() == null) {
144 name = "";
145 } else {
146 name = menu.getParent().getName();
147 }
148
149 return name;
150 }
151
152 protected String getTarget(MenuComponent menu) {
153 String theTarget = super.getTarget(menu);
154
155 if (theTarget == null) {
156 theTarget = EMPTY;
157 }
158
159 return theTarget;
160 }
161
162 private String getMenuName() {
163 return "oCMenu" + ((menuId != null) ? menuId : "");
164 }
165 }