KickJava   Java API By Example, From Geeks To Geeks.

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


1 package fr.improve.struts.taglib.layout;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Locale JavaDoc;
6 import java.util.MissingResourceException JavaDoc;
7 import java.util.ResourceBundle JavaDoc;
8 import java.util.Set JavaDoc;
9 import java.util.Vector JavaDoc;
10 import java.util.Map.Entry;
11
12 import javax.servlet.jsp.JspException JavaDoc;
13 import javax.servlet.jsp.PageContext JavaDoc;
14
15 import fr.improve.struts.taglib.layout.menu.MenuComponent;
16 import fr.improve.struts.taglib.layout.util.LayoutUtils;
17 import fr.improve.struts.taglib.layout.util.MenuInterface;
18 import fr.improve.struts.taglib.layout.util.TagUtils;
19
20 /**
21  * Menu tag for use with Hiermenus, compatible with Scott Sayles menu framework
22  * PENDING:
23  * 1. use the Layout.properties file instaed of a custom file.
24  * 2. use a free menu system instead of HierMenus.
25  * @author: Jean-NoŽl Ribette
26  */

27 public class DynMenuTag2 extends PanelTag implements MenuInterface{
28     protected Vector JavaDoc menus = new Vector JavaDoc();
29     protected String JavaDoc config = "fr.improve.struts.taglib.layout.DefaultDynMenuConfig";
30
31     protected String JavaDoc ID = "fr.improve.struts.taglib.layout.DynMenuTag2.ID";
32     // hack to give each menu a different id.
33
protected int menuNumber;
34     
35     protected String JavaDoc top;
36     protected String JavaDoc left;
37     
38     protected boolean includeScript = false;
39     protected static final String JavaDoc SCRIPT_KEY = "fr.improve.struts.taglib.layout.DynMenuTag2.SCRIPT_KEY";
40     
41     public static void includeScriptCode(StringBuffer JavaDoc in_buffer, PageContext JavaDoc in_pg) {
42         if (in_pg.getRequest().getAttribute(SCRIPT_KEY)!=null) {
43             in_buffer.append("<SCRIPT LANGUAGE=\"JavaScript1.2\" SRC=\"");
44             in_buffer.append(LayoutUtils.getSkin(in_pg.getSession()).getConfigDirectory(in_pg.getRequest()));
45             in_buffer.append("/HM_Loader.js\" TYPE='text/javascript'></SCRIPT>\n");
46             in_pg.getRequest().removeAttribute(SCRIPT_KEY);
47         }
48     }
49
50     public void addItem(MenuComponent item) {
51         menus.add(item);
52     }
53     public int doEndLayoutTag() throws JspException JavaDoc {
54         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
55         
56         buffer.append("<script LANGUAGE='JavaScript1.2' TYPE='text/javascript'>\n");
57
58         doPrintMenu(buffer, (MenuComponent[])menus.toArray(new MenuComponent[menus.size()]), "");
59         buffer.append("</script>\n");
60         
61         pageContext.getRequest().setAttribute(SCRIPT_KEY, "");
62         
63         if (includeScript) {
64             includeScriptCode(buffer, pageContext);
65         }
66             
67         TagUtils.write(pageContext,buffer.toString());
68
69         pageContext.getRequest().setAttribute(ID, new Integer JavaDoc(menuNumber+1));
70
71         // Reset the tag.
72
menus.removeAllElements();
73         
74         return EVAL_PAGE;
75     }
76     
77     public void release() {
78         super.release();
79         config ="fr.improve.struts.taglib.layout.DefaultDynMenuConfig";
80         left = null;
81         top = null;
82         includeScript = false;
83     }
84 public void doPrintMenu(StringBuffer JavaDoc buffer, MenuComponent[] menus, String JavaDoc level) throws JspException JavaDoc {
85     HashMap JavaDoc subMenusMap = new HashMap JavaDoc();
86     
87     buffer.append("HM_Array");
88     buffer.append(menuNumber);
89     buffer.append(level);
90     buffer.append(" = [\n");
91     if (level.equals("")) doPrintMenuConfig(buffer);
92     else buffer.append("[],\n");
93         
94     for (int i=0;i<menus.length;i++) {
95         MenuComponent item = (MenuComponent) menus[i];
96         String JavaDoc link = item.getLocation();
97         String JavaDoc forward = item.getForward();
98         String JavaDoc page = item.getPage();
99         String JavaDoc target = item.getTarget();
100         String JavaDoc action = item.getAction();
101         /* String*/ key = item.getTitle();
102         MenuComponent[] subMenus = item.getMenuComponents();
103
104         if (i!=0) buffer.append(",");
105             
106         buffer.append("[\"");
107         buffer.append(getLabel());
108         buffer.append("\",\"");
109         if (link!=null || page!=null || forward!=null || action!=null) {
110             if (target!=null) {
111                 buffer.append("javascript:window.open('").append(LayoutUtils.computeURL(pageContext, forward, link, page, action, null, null, null, false, null)).append("','").append(target);
112                 buffer.append("','')");
113             } else {
114                 buffer.append(LayoutUtils.computeURL(pageContext, forward, link, page, action, null, null, null, false, null));
115             }
116         }
117         buffer.append("\",1,0,");
118         if (subMenus==null || subMenus.length==0) buffer.append("0]\n"); else {
119             buffer.append("1]\n");
120             subMenusMap.put(level + "_" + (i+1),subMenus);
121         }
122                 
123     }
124
125     buffer.append("]\n\n");
126         
127     Set JavaDoc set = subMenusMap.entrySet();
128     Iterator JavaDoc iterator = set.iterator();
129     while (iterator.hasNext()) {
130         Entry entry = (Entry) iterator.next();
131         String JavaDoc subLevel = (String JavaDoc) entry.getKey();
132         MenuComponent[] subMenus = (MenuComponent[]) entry.getValue();
133         doPrintMenu(buffer, subMenus, subLevel);
134     }
135     
136 }
137 protected void doPrintMenuConfig(StringBuffer JavaDoc buffer) throws JspException JavaDoc {
138     // get the SKIN
139
String JavaDoc skin = LayoutUtils.getSkin(pageContext.getSession()).getCssFileName();
140     skin = skin.substring(0, skin.length()-4);
141
142     // check the top & left position are numbers
143
try {
144         if (left!=null) {
145             Integer.parseInt(left);
146         }
147         if (top!=null) {
148             Integer.parseInt(top);
149         }
150     } catch (NumberFormatException JavaDoc e) {
151         JspException JavaDoc jspe = new JspException JavaDoc("dynMenu: left or top position is not a valid number");
152         TagUtils.saveException(pageContext, jspe);
153         throw jspe;
154     }
155     
156     // get the config
157
try {
158         ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(config, new Locale JavaDoc(skin,""));
159         buffer.append("[");
160         buffer.append(bundle.getString("width"));
161         buffer.append(",");
162         buffer.append(left !=null ? left : bundle.getString("left_position"));
163         buffer.append(",");
164         buffer.append(top !=null ? top : bundle.getString("top_position"));
165         buffer.append(",");
166         buffer.append(bundle.getString("font_color"));
167         buffer.append(",");
168         buffer.append(bundle.getString("mouseover_font_color"));
169         buffer.append(",");
170         buffer.append(bundle.getString("background_color"));
171         buffer.append(",");
172         buffer.append(bundle.getString("mouseover_background_color"));
173         buffer.append(",");
174         buffer.append(bundle.getString("border_color"));
175         buffer.append(",");
176         buffer.append(bundle.getString("separator_color"));
177         buffer.append(",");
178         buffer.append(bundle.getString("top_is_permanent"));
179         buffer.append(",");
180         buffer.append(bundle.getString("top_is_horizontal"));
181         buffer.append(",");
182         buffer.append(bundle.getString("tree_is_horizontal"));
183         buffer.append(",");
184         buffer.append(bundle.getString("position_under"));
185         buffer.append(",");
186         buffer.append(bundle.getString("top_more_images_visible"));
187         buffer.append(",");
188         buffer.append(bundle.getString("tree_more_images_visible"));
189         buffer.append(",");
190         buffer.append(bundle.getString("evaluate_upon_tree_show"));
191         buffer.append(",");
192         buffer.append(bundle.getString("evaluate_upon_tree_hide"));
193         buffer.append("],\n");
194     } catch (MissingResourceException JavaDoc e) {
195         JspException JavaDoc jspe = new JspException JavaDoc("dynMenu: missing attribute " + e.getMessage());
196         TagUtils.saveException(pageContext, jspe);
197         throw jspe;
198     }
199     
200 }
201     public int doStartLayoutTag() throws JspException JavaDoc {
202         Integer JavaDoc lc_int = (Integer JavaDoc) pageContext.getRequest().getAttribute(ID);
203         if (lc_int==null) {
204             menuNumber = 1;
205         } else {
206             menuNumber = lc_int.intValue();
207         }
208         return EVAL_BODY_INCLUDE;
209     }
210     public void setConfig(String JavaDoc config) {
211         this.config = config;
212     }
213     public void setLeft(String JavaDoc left) {
214         this.left = left;
215     }
216     public void setTop(String JavaDoc top) {
217         this.top = top;
218     }
219     public void setIncludeScript(boolean in_b) {
220         includeScript = in_b;
221     }
222
223 }
224
Popular Tags