View Javadoc

1   /*
2    * ListMenuDisplayer.java
3    *
4    * Created on December 7, 2002, 12:35 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  
15  
16  /**
17   *
18   * @author  <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
19   * @version 1.0
20   */
21  public class ListMenuDisplayer extends MessageResourcesMenuDisplayer {
22      //~ Instance fields ========================================================
23  
24      //~ Methods ================================================================
25  
26      public void init(PageContext pageContext, MenuDisplayerMapping mapping) {
27          super.init(pageContext, mapping);
28          String id = (String) pageContext.getAttribute("menuId");
29  
30          try {
31              out.println(displayStrings.getMessage("lmd.begin",
32                  (id != null) ?  "id=\"" + id + "\" " : ""));
33          } catch (Exception e) {
34              log.error(e.getMessage());
35          }
36      }
37  
38      public void display(MenuComponent menu) throws JspException, IOException {
39          if (isAllowed(menu)) {
40              out.println(displayStrings.getMessage("lmd.menu.top"));
41              displayComponents(menu, 0);
42              out.println(displayStrings.getMessage("lmd.menu.bottom"));
43          }
44      }
45  
46      protected void displayComponents(MenuComponent menu, int level)
47      throws JspException, IOException {
48          MenuComponent[] components = menu.getMenuComponents();
49  
50          if (components.length > 0) {
51              // eliminate spaces in string used for Id
52              String domId = StringUtils.deleteWhitespace(getMessage(menu.getName()));
53              // added to create a unique id everytime
54              domId += ((int) (1000*Math.random()));
55  
56              String menuClass = "menu";
57  
58              if (level >= 1) {
59                  menuClass = "submenu";
60              }
61  
62              // if there is a location/page/action tag on base item use it
63              if (menu.getUrl() != null ){
64                  out.println(displayStrings.getMessage("lmd.menu.actuator.link",
65                              domId, getMessage(menu.getTitle()), menuClass,
66                              getMessage(menu.getUrl())));
67              } else {
68                  out.println(displayStrings.getMessage("lmd.menu.actuator.top",
69                          domId,
70                          getMessage(menu.getTitle()),
71                          menuClass));
72              }
73              
74              for (int i = 0; i < components.length; i++) {
75                  // check the permissions on this component
76                  if (isAllowed(components[i])) {
77                      if (components[i].getMenuComponents().length > 0) {
78                          out.println("<li>");
79  
80                          displayComponents(components[i], level + 1);
81  
82                          out.println(displayStrings.getMessage("lmd.menu.actuator.bottom"));
83                      } else {
84                          out.println(displayStrings.getMessage("lmd.menu.item",
85                                                                components[i].getUrl(),
86                                                                super.getMenuToolTip(components[i]),
87                                                                getExtra(components[i]),
88                                                                this.getMessage(components[i].getTitle())));
89                      }
90                  }
91              }
92  
93              // close the </ul> for the top menu
94              if (menuClass.equals("menu")) {
95                  out.println("</ul>");
96              }
97          } else {
98              if (menu.getParent() == null) {
99                  out.println(displayStrings.getMessage("lmd.menu.standalone",
100                                                       menu.getUrl(),
101                                                       super.getMenuToolTip(menu),
102                                                       getExtra(menu),
103                                                       getMessage(menu.getTitle())));
104             } else {
105                 out.println(displayStrings.getMessage("lmd.menu.item",
106                                                       menu.getUrl(),
107                                                       super.getMenuToolTip(menu),
108                                                       getExtra(menu),
109                                                       getMessage(menu.getTitle())));
110             }
111         }
112     }
113 
114     /**
115      * This will output the ending HTML code to close tags from the beginning
116      * @param context the current pageContext
117      */
118     public void end(PageContext context) {
119         try {
120             out.print(displayStrings.getMessage("lmd.end"));
121         } catch (Exception e) {
122             log.error(e.getMessage());
123         }
124     }
125 
126     public String getExtra(MenuComponent menu) {
127         StringBuffer extra = new StringBuffer();
128         if (menu.getTarget() != null) {
129             extra.append(" target=\"").append(menu.getTarget()).append("\"");
130         }
131         if (menu.getOnclick() != null) {
132             extra.append(" onclick=\"").append(menu.getOnclick()).append("\"");
133         }
134         if (menu.getOnmouseover() != null) {
135             extra.append(" onmouseover=\"").append(menu.getOnmouseover()).append("\"");
136         }
137         if (menu.getOnmouseout() != null) {
138             extra.append(" onmouseout=\"").append(menu.getOnmouseout()).append("\"");
139         }
140         if (menu.getWidth() != null) {
141             extra.append(" style=\"width: " + menu.getWidth() + "px\"");
142         }
143         return (extra.length() > 0) ? extra.toString() : "";
144     }
145 }