KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > MenuTag2


1 package fr.improve.struts.taglib.layout;
2
3 import java.util.Vector JavaDoc;
4
5 import javax.servlet.jsp.JspException JavaDoc;
6
7 import fr.improve.struts.taglib.layout.menu.MenuComponent;
8 import fr.improve.struts.taglib.layout.util.LayoutUtils;
9 import fr.improve.struts.taglib.layout.util.MenuInterface;
10 import fr.improve.struts.taglib.layout.util.TagUtils;
11
12 /**
13  * Display a tree like menu.
14  * Compatible with Scott Sayles menu framework.
15  * @author: Jean-NoŽl Ribette
16  */

17 public class MenuTag2 extends PanelTag implements MenuInterface {
18     protected Vector JavaDoc menus = new Vector JavaDoc();
19
20     /**
21      * Start the tag, do nothing (menu items are not defined yet).
22      */

23     public int doStartLayoutTag() throws JspException JavaDoc {
24         return EVAL_BODY_INCLUDE;
25     }
26     
27     /**
28      * Add a menu item (method specified in MenuInterface).
29      */

30     public void addItem(MenuComponent item) {
31         menus.add(item);
32     }
33
34     /**
35      * End the tag, generate the HTML code for the menu items added.
36      */

37     public int doEndLayoutTag() throws JspException JavaDoc {
38         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
39         // width = "90";
40
doStartPanel(buffer);
41         doBeforeBody(buffer);
42     
43         buffer.append("<tr><td><table border=0 cellpadding=10><tr><td><table border=0>");
44         doPrintMenu(buffer,(MenuComponent[])menus.toArray(new MenuComponent[menus.size()]),1);
45         buffer.append("</table></td></tr></table></td></tr>");
46         
47         doAfterBody(buffer);
48         doEndPanel(buffer);
49     
50         TagUtils.write(pageContext, buffer.toString());
51         menus.removeAllElements();
52         return EVAL_PAGE;
53     }
54     
55     /**
56      * Iterates over the MenuComponent[].
57      */

58     protected void doPrintMenu(StringBuffer JavaDoc buffer, MenuComponent[] menus, int level) throws JspException JavaDoc {
59         for (int i=0;i<menus.length;i++) {
60             // Print the menu item.
61
MenuComponent item = (MenuComponent) menus[i];
62             doPrintMenuItem(buffer, level, item);
63             
64             // Print the sub items.
65
MenuComponent[] subMenus = item.getMenuComponents();
66             String JavaDoc key = item.getTitle();
67             doPrintSubMenu(buffer, level, key, subMenus);
68             
69             buffer.append("\n");
70         }
71     
72     }
73
74     /**
75      * Generate the HTML code for one menu item.
76      */

77     protected void doPrintMenuItem(StringBuffer JavaDoc buffer, int level, MenuComponent item) throws JspException JavaDoc {
78         String JavaDoc link = item.getLocation();
79         String JavaDoc forward = item.getForward();
80         String JavaDoc page = item.getPage();
81         String JavaDoc action = item.getAction();
82         String JavaDoc key = item.getTitle();
83         String JavaDoc image = item.getImage();
84         String JavaDoc target = item.getTarget();
85         String JavaDoc style = item.getStyle();
86         
87         boolean lc_hasLink = link!=null || forward!=null || page!=null || action!=null;
88
89         buffer.append("<tr valign=\"top\"><td class=\"");
90         buffer.append(styleClass);
91         if (lc_hasLink) {
92             buffer.append("\" align=\"left");
93         } else {
94             buffer.append("\" onClick=\"changeMenu('");
95             buffer.append(key);
96             buffer.append("')\" style=\"cursor:hand");
97         }
98         buffer.append("\">");
99         
100         doPrintSpaces(buffer, level);
101         
102         if (lc_hasLink) {
103             doStartLink(buffer, link, forward, page, action, target, style);
104         }
105         
106         if (image!=null) {
107             buffer.append("<img SRC=");
108             buffer.append(image);
109             buffer.append(">&nbsp;");
110         }
111         
112         buffer.append(LayoutUtils.getLabel(pageContext,bundle, key, null, false));
113         if (lc_hasLink) {
114             doEndLink(buffer);
115         }
116         buffer.append("</td></tr>");
117     }
118
119     private void doPrintSpaces(StringBuffer JavaDoc buffer, int level) {
120         switch (level) {
121             case 2: buffer.append("&nbsp;&nbsp;"); break;
122             case 3: buffer.append("&nbsp;&nbsp;&nbsp;>&nbsp;"); break;
123         }
124     }
125     
126     /**
127      * @deprecated.
128      */

129     protected void doStartLink(StringBuffer JavaDoc buffer, String JavaDoc link, String JavaDoc forward, String JavaDoc page, String JavaDoc target, String JavaDoc style) throws JspException JavaDoc {
130         doStartLink(buffer, link, forward, page, null, target, style);
131     }
132
133     protected void doStartLink(StringBuffer JavaDoc buffer, String JavaDoc link, String JavaDoc forward, String JavaDoc page, String JavaDoc action, String JavaDoc target, String JavaDoc style) throws JspException JavaDoc {
134         buffer.append("<a HREF=\"");
135         buffer.append(LayoutUtils.computeURL(pageContext, forward, link, page, action, null,null, null, false, target));
136         if (target!=null) {
137             buffer.append("\" target=\"");
138             buffer.append(target);
139         }
140         if (style!=null) {
141             buffer.append("\" style=\"");
142             buffer.append(style);
143         }
144         buffer.append("\">");
145     }
146     
147     protected void doEndLink(StringBuffer JavaDoc buffer) {
148         buffer.append("</a>");
149     }
150
151     /**
152      * Print the submenu for a menu item.
153      */

154     protected void doPrintSubMenu(StringBuffer JavaDoc in_buffer, int in_level, String JavaDoc in_key, MenuComponent[] in_subMenus) throws JspException JavaDoc {
155         if (in_subMenus!=null && in_subMenus.length!=0) {
156             //buffer.append("<tr><td id=");
157
//buffer.append(key);
158
//buffer.append("b style=\"display:none\"></td><td id=");
159
//buffer.append(key);
160
//buffer.append("><table border=0 cellspacing=0 cellpadding=0>");
161
//buffer.append("<script language=\"JavaScript\">initMenu('" + key +"');</script>\n");
162
//doPrintMenu(buffer, subMenus, level+1);
163
//buffer.append("</table></td></tr>");
164

165             in_buffer.append("<tr valign=top><td><div id=\"");
166             in_buffer.append(in_key);
167             in_buffer.append("b\" style=\"display:none\"></div><div id=\"");
168             in_buffer.append(in_key);
169             in_buffer.append("\"><table border=0 cellspacing=0 cellpadding=0>");
170             in_buffer.append("<script language=\"JavaScript\">initMenu('" + in_key +"');</script>\n");
171             doPrintMenu(in_buffer, in_subMenus, in_level+1);
172             in_buffer.append("</table></div></td></tr>");
173             
174         }
175     }
176 }
177
Popular Tags