KickJava   Java API By Example, From Geeks To Geeks.

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


1 package fr.improve.struts.taglib.layout;
2
3 import javax.servlet.jsp.JspException JavaDoc;
4
5 import fr.improve.struts.taglib.layout.menu.MenuComponent;
6 import fr.improve.struts.taglib.layout.util.LayoutUtils;
7 import fr.improve.struts.taglib.layout.util.Menu;
8 import fr.improve.struts.taglib.layout.util.MenuItem;
9 import fr.improve.struts.taglib.layout.util.TagUtils;
10
11 /**
12  * Insert the type's description here.
13  * Creation date: (23/03/01 10:09:16)
14  * @author: Jean-NoŽl Ribette
15  */

16 public class MenuTag extends PanelTag {
17     protected Menu menu;
18 public void addItem(MenuItem item) {
19         if (menu==null) menu = new Menu();
20         menu.addItem(item);
21 }
22 public int doEndLayoutTag() throws JspException JavaDoc {
23     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
24     doStartPanel(buffer);
25     doBeforeBody(buffer);
26
27     buffer.append("<tr><td><table border=0 cellpadding=10><tr><td><table border=0>");
28     doPrintMenu(buffer,menu,1);
29     buffer.append("</table></td></tr></td></tr></table>");
30     
31     doAfterBody(buffer);
32     doEndPanel(buffer);
33     
34     TagUtils.write(pageContext, buffer.toString());
35     
36     return EVAL_PAGE;
37 }
38 protected void doPrintMenu(
39     StringBuffer JavaDoc buffer,
40     MenuComponent menu,
41     int level)
42     throws JspException JavaDoc {
43
44     String JavaDoc link = menu.getLocation();
45     String JavaDoc key = menu.getTitle();
46
47     if (link == null) {
48         buffer.append("<tr><td class=");
49         buffer.append(styleClass);
50         buffer.append(" onClick=\"changeMenu('");
51         buffer.append(key);
52         buffer.append("')\" style=\"cursor:hand\">");
53         if (level == 2)
54             buffer.append("&nbsp;&middot;&nbsp;");
55         if (level == 3)
56             buffer.append("&nbsp;&nbsp;&nbsp;>&nbsp;");
57         buffer.append(getLabel());
58         buffer.append("</td></tr>");
59     } else {
60         buffer.append("<tr><td align=left class=");
61         buffer.append(styleClass);
62         buffer.append(">");
63         if (level == 2)
64             buffer.append("&nbsp;&middot;&nbsp;");
65         if (level == 3)
66             buffer.append("&nbsp;&nbsp;&nbsp;>&nbsp;");
67         buffer.append("<a HREF=\"");
68         buffer.append(link);
69         buffer.append("\">");
70         buffer.append(getLabel());
71         buffer.append("</a></td></tr>");
72     }
73
74     MenuComponent[] subMenus = menu.getMenuComponents();
75     for (int i = 0; i < subMenus.length; i++) {
76         MenuComponent subMenu = subMenus[i];
77         buffer.append("<tr id=");
78         buffer.append(key);
79         buffer.append("><td><table border=0 cellspacing=0 cellpadding=0>");
80         buffer.append(
81             "<script language=\"JavaScript\">initMenu('" + key + "');</script>\n");
82         doPrintMenu(buffer, subMenu, level + 1);
83         buffer.append("</table></td></tr>");
84     }
85     buffer.append("\n");
86
87 }
88 protected void doPrintMenu(StringBuffer JavaDoc buffer, Menu menu, int level) throws JspException JavaDoc {
89     Object JavaDoc[] items = menu.getItems();
90     
91     for (int i=0;i<items.length;i++) {
92         MenuItem item = (MenuItem) items[i];
93         String JavaDoc link = item.getLink();
94         String JavaDoc key = item.getKey();
95         Menu subMenu = item.getSubMenu();
96     
97         if (link==null) {
98             buffer.append("<tr><td class=");
99             buffer.append(styleClass);
100             buffer.append(" onClick=\"changeMenu('");
101             buffer.append(key);
102             buffer.append("')\" style=\"cursor:hand\">");
103             if (level==2) buffer.append("&nbsp;&middot;&nbsp;");
104             if (level==3) buffer.append("&nbsp;&nbsp;&nbsp;>&nbsp;");
105             buffer.append(getLabel());
106             buffer.append("</td></tr>");
107         } else {
108             buffer.append("<tr><td align=left class=");
109             buffer.append(styleClass);
110             buffer.append(">");
111             if (level==2) buffer.append("&nbsp;&middot;&nbsp;");
112             if (level==3) buffer.append("&nbsp;&nbsp;&nbsp;>&nbsp;");
113             buffer.append("<a HREF=\"");
114             buffer.append(link);
115             buffer.append("\">");
116             buffer.append(getLabel());
117             buffer.append("</a></td></tr>");
118         }
119
120         if (subMenu!=null) {
121             buffer.append("<tr id=");
122             buffer.append(key);
123             buffer.append("><td><table border=0 cellspacing=0 cellpadding=0>");
124             buffer.append("<script language=\"JavaScript\">initMenu('" + key +"');</script>\n");
125             doPrintMenu(buffer, subMenu, level+1);
126             buffer.append("</table></td></tr>");
127         }
128         
129         buffer.append("\n");
130     }
131
132 }
133 public int doStartLayoutTag() throws JspException JavaDoc {
134     if (name!=null) try {
135         menu = (Menu) LayoutUtils.getBeanFromPageContext(pageContext, name, property);
136     } catch (ClassCastException JavaDoc e) {
137         throw new JspException JavaDoc("MenuTag: :Object under name=" + name + " property=" + property + " is not of type Menu.");
138     }
139     
140     
141     return EVAL_BODY_INCLUDE;
142 }
143     public void release() {
144         super.release();
145         menu = null;
146     }
147 }
148
Popular Tags