View Javadoc

1   /*
2    * CoolMenuDisplayer.java
3    *
4    * Created on March 21, 2002, 5:47 PM
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      //~ Static fields/initializers =============================================
23  
24      /*Variables for each menu item: (** means that they have to be spesified!)
25         0 name: The name of the item. This must be unique for each item. Do not use spaces or strange charachters in this one! **
26         1 parent: 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 spesified it will get the default width spesified above.
31         height: The height of the element. If not spesified it will get the default height spesified 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 bgcoloroff: The background color for this item. If not spesified it will get the default background color spesified above.
35         8 bgcoloron: The "on" background color for this item. If not spesified it will get the default "on" background color spesified above.
36         9 textcolor: The text color for this item. If not spesified it will get the default text color spesified above.
37         10 hovercolor: The "on" text color for this item. If not spesified it will get the default "on" text color spesified above. Netscape4 ignores this
38         11 onclick: If you want something to happen when the element is clicked (different from going to a link) spesifiy it here.
39         onmouseover: This will happen when you mouseover the element. Could be status text, another imageswap or whatever.
40         onmouseout: This will happen when you mouseout the element.
41         Remember you can have as many levels/sublevels as you want. Just make sure you spesify the correct "parent" for each item.
42         To set styles for each level see above.
43       */
44  
45      //oCMenu.makeMenu('top0','',' News','','')
46      //    oCMenu.makeMenu('sub10','top1','New scripts','/scripts/index.asp?show=new')
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      //~ Methods ================================================================
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) { //then us the top image
150                 title = title + "'+" + TOP_IMAGE + "+'";
151             } else { //use the sub menu image
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 }