View Javadoc

1   /*
2    * CoolMenuDisplayer4.java
3    *
4    * Created on December 7, 2002, 12:22 AM
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      //~ Static fields/initializers =============================================
23  
24      /*Variables for each menu item: (** means that they have to be specified!)
25         0 name: The name of the item. This must be unique for each item. Do not use spaces or strange characters in this one! **
26         1 parent_name: The name of the menuitem you want this to "connect" to. This will be a submenu of the item that have the name you place in here. ** for all other then the topitems
27         2 text: The text you want in the item. ** (except if you use images)
28         3 link: The page you want this item to link to.
29         4 target: The target window or frame you want the link to go to (Default is same window if you're not using frames, and the mainframe if you're using frames)
30         width: The width of the element. If not specified it will get the default width specified above.
31         height: The height of the element. If not specified it will get the default height specified above.
32         5 img1: The "off" image for element if you want to use images.
33         6 img2: The image that appears onmouseover if using images.
34         7 regClass: The CSS class for a cell when it's not selected.
35         8 overClass: The CSS class for a cell when it's moused over.
36         align: The alignment for this item.
37         rows: The number of rows for this item to expand.
38         9 nolink:  If you have items that are meant to be info items only or something like that you can set this value to 1 and the item will not get a hand cursor and/or a link (the events will not work either)
39         10 onclick: If you want something to happen when the element is clicked (different from going to a link) spesifiy it here.
40         11 onmouseover: This will happen when you mouseover the element. Could be status text, another imageswap or whatever.
41         12 onmouseout: This will happen when you mouseout the element.
42         Remember you can have as many levels/sublevels as you want. Just make sure you spesify the correct "parent" for each item.
43         To set styles for each level see above.
44       */
45  
46      // oCMenu(name, parent_name, text, link, target, width, height, 
47      // regImage, overImage, regClass, overClass , align, rows, nolink, onclick, onmouseover, onmouseout) 
48  
49      /** main message format of the menu.  only 10 args max in jdk1.3 :( */
50  
51      // the zero (0) below before the number 5 is for the nolink attribute, since I'm 
52      // hiding menus when they're not allowed, this is always 0.
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 = "    "; // four spaces
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      //~ Methods ================================================================
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         // fix image HTML to escape double quotes for JavaScript
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 }