KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map.Entry;
10
11 import javax.servlet.jsp.JspException JavaDoc;
12
13 import fr.improve.struts.taglib.layout.util.LayoutUtils;
14 import fr.improve.struts.taglib.layout.util.Menu;
15 import fr.improve.struts.taglib.layout.util.MenuItem;
16 import fr.improve.struts.taglib.layout.util.TagUtils;
17
18 /**
19  * Menu tag for use with Hiermenus
20  * @author: Jean-NoŽl Ribette
21  */

22 public class DynMenuTag extends PanelTag {
23     protected Menu menu;
24     protected String JavaDoc config = "fr.improve.struts.taglib.layout.DefaultDynMenuConfig";
25     public void addItem(MenuItem item) {
26         if (menu==null) menu = new Menu();
27         menu.addItem(item);
28     }
29     public int doEndLayoutTag() throws JspException JavaDoc {
30         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
31
32         buffer.append("<script>\n");
33         doPrintMenu(buffer, menu, "");
34         buffer.append("</script>\n");
35         buffer.append("<SCRIPT LANGUAGE=\"JavaScript1.2\" SRC=\"config/HM_Loader.js\" TYPE='text/javascript'></SCRIPT>");
36         
37         TagUtils.write(pageContext, buffer.toString());
38         return EVAL_PAGE;
39     }
40 public void doPrintMenu(StringBuffer JavaDoc buffer, Menu menu, String JavaDoc level) throws JspException JavaDoc {
41     Object JavaDoc[] items = menu.getItems();
42
43     HashMap JavaDoc subMenus = new HashMap JavaDoc();
44     
45     buffer.append("HM_Array1");
46     buffer.append(level);
47     buffer.append(" = [\n");
48     if (level.equals("")) doPrintMenuConfig(buffer);
49     else buffer.append("[],\n");
50         
51     for (int i=0;i<items.length;i++) {
52         MenuItem item = (MenuItem) items[i];
53         String JavaDoc link = item.getLink();
54         String JavaDoc key = item.getKey();
55         Menu subMenu = item.getSubMenu();
56
57         if (i!=0) buffer.append(",");
58             
59         buffer.append("[\"");
60         buffer.append(getLabel());
61         buffer.append("\",\"");
62         if (link!=null) buffer.append(link);
63         buffer.append("\",1,0,");
64         if (subMenu==null) buffer.append("0]\n"); else {
65             buffer.append("1]\n");
66             subMenus.put(level + "_" + (i+1),subMenu);
67         }
68                 
69     }
70
71     buffer.append("]\n\n");
72         
73     Set JavaDoc set = subMenus.entrySet();
74     Iterator JavaDoc iterator = set.iterator();
75     while (iterator.hasNext()) {
76         Entry entry = (Entry) iterator.next();
77         String JavaDoc subLevel = (String JavaDoc) entry.getKey();
78         Menu subMenu = (Menu) entry.getValue();
79         doPrintMenu(buffer, subMenu, subLevel);
80     }
81     
82 }
83 // TODO
84
// cache the bundle
85
// get the bundle in function of the skin
86
protected void doPrintMenuConfig(StringBuffer JavaDoc buffer) throws JspException JavaDoc {
87     // get the SKIN
88
String JavaDoc skin = LayoutUtils.getSkin(pageContext.getSession()).getName();
89     
90     // get the config
91
try {
92         ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(config, new Locale JavaDoc(skin,""));
93         buffer.append("[");
94         buffer.append(bundle.getString("width"));
95         buffer.append(",");
96         buffer.append(bundle.getString("left_position"));
97         buffer.append(",");
98         buffer.append(bundle.getString("top_position"));
99         buffer.append(",");
100         buffer.append(bundle.getString("font_color"));
101         buffer.append(",");
102         buffer.append(bundle.getString("mouseover_font_color"));
103         buffer.append(",");
104         buffer.append(bundle.getString("background_color"));
105         buffer.append(",");
106         buffer.append(bundle.getString("mouseover_background_color"));
107         buffer.append(",");
108         buffer.append(bundle.getString("border_color"));
109         buffer.append(",");
110         buffer.append(bundle.getString("separator_color"));
111         buffer.append(",");
112         buffer.append(bundle.getString("top_is_permanent"));
113         buffer.append(",");
114         buffer.append(bundle.getString("top_is_horizontal"));
115         buffer.append(",");
116         buffer.append(bundle.getString("tree_is_horizontal"));
117         buffer.append(",");
118         buffer.append(bundle.getString("position_under"));
119         buffer.append(",");
120         buffer.append(bundle.getString("top_more_images_visible"));
121         buffer.append(",");
122         buffer.append(bundle.getString("tree_more_images_visible"));
123         buffer.append(",");
124         buffer.append(bundle.getString("evaluate_upon_tree_show"));
125         buffer.append(",");
126         buffer.append(bundle.getString("evaluate_upon_tree_hide"));
127         buffer.append("],\n");
128     } catch (MissingResourceException JavaDoc e) {
129         throw new JspException JavaDoc(e.getMessage());
130     }
131 }
132 public int doStartLayoutTag() throws JspException JavaDoc {
133     if (name!=null) try {
134         menu = (Menu) LayoutUtils.getBeanFromPageContext(pageContext, name, property);
135     } catch (ClassCastException JavaDoc e) {
136         throw new JspException JavaDoc("MenuTag: :Object under name=" + name + " property=" + property + " is not of type Menu.");
137     }
138     return EVAL_BODY_INCLUDE;
139 }
140     public void release() {
141         super.release();
142         
143         menu = null;
144         config ="fr.improve.struts.taglib.layout.DefaultDynMenuConfig";
145     }
146 public void setConfig(String JavaDoc config) {
147     this.config = config;
148 }
149 }
150
Popular Tags