KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > navmenu > jscookmenu > HtmlJSCookMenuRenderer


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.navmenu.jscookmenu;
17
18 import org.apache.myfaces.component.html.util.AddResource;
19 import org.apache.myfaces.custom.navmenu.NavigationMenuItem;
20 import org.apache.myfaces.custom.navmenu.NavigationMenuUtils;
21 import org.apache.myfaces.custom.navmenu.UINavigationMenuItem;
22 import org.apache.myfaces.el.SimpleActionMethodBinding;
23 import org.apache.myfaces.renderkit.RendererUtils;
24 import org.apache.myfaces.renderkit.html.HtmlRenderer;
25 import org.apache.myfaces.renderkit.html.util.DummyFormResponseWriter;
26 import org.apache.myfaces.renderkit.html.util.DummyFormUtils;
27 import org.apache.myfaces.renderkit.html.util.JavascriptUtils;
28
29 import javax.faces.component.UIComponent;
30 import javax.faces.context.FacesContext;
31 import javax.faces.context.ResponseWriter;
32 import javax.faces.el.MethodBinding;
33 import javax.faces.el.ValueBinding;
34 import javax.faces.event.ActionEvent;
35 import javax.faces.webapp.UIComponentTag;
36 import java.io.IOException JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * @author Thomas Spiegl (latest modification by $Author: schof $)
42  * @version $Revision: 1.15 $ $Date: 2005/04/13 13:52:01 $
43  * $Log: HtmlJSCookMenuRenderer.java,v $
44  * Revision 1.15 2005/04/13 13:52:01 schof
45  * Fixes MYFACES-185 (patch submitted by Martin Bosak)
46  *
47  * Revision 1.14 2005/04/12 17:47:51 schof
48  * Fixes MYFACES-182 (Thanks to David Heffelfinger for reporting and fixing.)
49  *
50  * Revision 1.13 2005/04/08 13:05:59 schof
51  * Fixes MyFaces-20 (Patch by Martin Bosak)
52  *
53  * Revision 1.12 2004/12/27 04:11:11 mmarinschek
54  * Data Table stores the state of facets of children; script tag is rendered with type attribute instead of language attribute, popup works better as a column in a data table
55  *
56  * Revision 1.11 2004/12/24 14:49:29 svieujot
57  * Upgrade the navmenu component to use the Extensions filter.
58  *
59  * Revision 1.10 2004/12/13 23:14:37 oros
60  * fix #1044663: handle enabledOnUserRole/visibleOnUserRole, disabled menu items are rendered with null actions
61  *
62  * Revision 1.9 2004/10/13 11:50:57 matze
63  * renamed packages to org.apache
64  *
65  * Revision 1.8 2004/10/05 15:11:43 manolito
66  * #1020264 x:navigationMenuItem icon problem
67  *
68  * Revision 1.7 2004/07/16 13:06:30 manolito
69  * encode javascript strings for jscook menu labels
70  *
71  * Revision 1.6 2004/07/05 08:28:24 royalts
72  * added example for <x:navigationMenuItems>
73  *
74  * Revision 1.5 2004/07/01 21:53:09 mwessendorf
75  * ASF switch
76  *
77  * Revision 1.4 2004/06/25 10:58:43 royalts
78  * fixed bug 979038
79  *
80  * Revision 1.3 2004/06/23 14:17:31 royalts
81  * no message
82  *
83  * Revision 1.2 2004/06/23 13:50:18 royalts
84  * no message
85  *
86  * Revision 1.1 2004/06/23 13:44:31 royalts
87  * no message
88  *
89  */

90 public class HtmlJSCookMenuRenderer
91     extends HtmlRenderer
92 {
93     //private static final Log log = LogFactory.getLog(HtmlJSCookMenuRenderer.class);
94

95     private static final String JavaDoc JSCOOK_ACTION_PARAM = "jscook_action";
96
97     public void decode(FacesContext context, UIComponent component)
98     {
99         RendererUtils.checkParamValidity(context, component, HtmlCommandJSCookMenu.class);
100
101         Map JavaDoc parameter = context.getExternalContext().getRequestParameterMap();
102         String JavaDoc actionParam = (String JavaDoc)parameter.get(JSCOOK_ACTION_PARAM);
103         if (actionParam != null && !actionParam.trim().equals("") &&
104                 !actionParam.trim().equals("null"))
105         {
106             String JavaDoc compId = component.getId();
107             int idx = actionParam.indexOf(':');
108             if (idx == -1) {
109                 return;
110             }
111             String JavaDoc actionId = actionParam.substring(0, idx);
112             if (! compId.equals(actionId)) {
113                 return;
114             }
115             actionParam = actionParam.substring(idx + 1);
116             actionParam = decodeValueBinding(actionParam, context);
117             MethodBinding mb;
118             if (UIComponentTag.isValueReference(actionParam))
119             {
120                 mb = context.getApplication().createMethodBinding(actionParam, null);
121             }
122             else
123             {
124                 mb = new SimpleActionMethodBinding(actionParam);
125             }
126             ((HtmlCommandJSCookMenu)component).setAction(mb);
127
128             component.queueEvent(new ActionEvent(component));
129         }
130     }
131
132     private String JavaDoc decodeValueBinding(String JavaDoc actionParam, FacesContext context)
133     {
134         int idx = actionParam.indexOf(";#{");
135         if (idx == -1) {
136             return actionParam;
137         }
138         
139         String JavaDoc newActionParam = actionParam.substring(0, idx);
140         String JavaDoc vbParam = actionParam.substring(idx + 1);
141         
142         idx = vbParam.indexOf('=');
143         if (idx == -1) {
144             return newActionParam;
145         }
146         String JavaDoc vbExpressionString = vbParam.substring(0, idx);
147         String JavaDoc vbValue = vbParam.substring(idx + 1);
148         
149         ValueBinding vb =
150             context.getApplication().createValueBinding(vbExpressionString);
151         vb.setValue(context, vbValue);
152         
153         return newActionParam;
154     }
155     
156     public boolean getRendersChildren()
157     {
158         return true;
159     }
160
161     public void encodeChildren(FacesContext context, UIComponent component) throws IOException JavaDoc
162     {
163         RendererUtils.checkParamValidity(context, component, HtmlCommandJSCookMenu.class);
164
165         List JavaDoc list = NavigationMenuUtils.getNavigationMenuItemList(component);
166         if (list.size() > 0)
167         {
168             List JavaDoc uiNavMenuItemList = component.getChildren();
169             DummyFormResponseWriter dummyFormResponseWriter = DummyFormUtils.getDummyFormResponseWriter(context);
170             dummyFormResponseWriter.addDummyFormParameter(JSCOOK_ACTION_PARAM);
171             dummyFormResponseWriter.setWriteDummyForm(true);
172
173             String JavaDoc myId = component.getId();
174             
175             ResponseWriter writer = context.getResponseWriter();
176
177             writer.write("\n<script type=\"text/javascript\"><!--\n" +
178                          "var myMenu =\n[");
179             encodeNavigationMenuItems(context, writer,
180                                       (NavigationMenuItem[]) list.toArray(new NavigationMenuItem[list.size()]),
181                                       uiNavMenuItemList,
182                                       myId);
183
184             writer.write("];\n" +
185                          "--></script>\n");
186         }
187     }
188
189     private void encodeNavigationMenuItems(FacesContext context,
190                                            ResponseWriter writer,
191                                            NavigationMenuItem[] items,
192                                            List JavaDoc uiNavMenuItemList,
193                                            String JavaDoc menuId)
194         throws IOException JavaDoc
195     {
196         for (int i = 0; i < items.length; i++)
197         {
198             NavigationMenuItem item = (NavigationMenuItem)items[i];
199             Object JavaDoc tempObj = null;
200             UINavigationMenuItem uiNavMenuItem = null;
201             try {
202                 tempObj = uiNavMenuItemList.get(i);
203             } catch (IndexOutOfBoundsException JavaDoc e) {
204             }
205             if (tempObj != null) {
206                 if (tempObj instanceof UINavigationMenuItem) {
207                     uiNavMenuItem = (UINavigationMenuItem) tempObj;
208                 }
209             }
210
211             if (! item.isRendered()) {
212                 continue;
213             }
214
215             if (i > 0)
216             {
217                 writer.write(",\n");
218             }
219
220             if (item.isSplit())
221             {
222                 writer.write("_cmSplit,");
223             }
224
225             writer.write("[");
226             if (item.getIcon() != null)
227             {
228                 String JavaDoc iconSrc = context.getApplication().getViewHandler().getResourceURL(context, item.getIcon());
229                 writer.write("'<img SRC=\"");
230                 writer.write(context.getExternalContext().encodeResourceURL(iconSrc));
231                 writer.write("\"/>'");
232             }
233             else
234             {
235                 writer.write("''");
236             }
237             writer.write(", '");
238             writer.write(JavascriptUtils.encodeString(item.getLabel()));
239             writer.write("', ");
240             if (item.getAction() != null && ! item.isDisabled())
241             {
242                 writer.write("'");
243                 writer.write(menuId);
244                 writer.write(':');
245                 writer.write(item.getAction());
246                 if (uiNavMenuItem != null) {
247                     encodeValueBinding(writer, uiNavMenuItem, item);
248                 }
249                 writer.write("'");
250             }
251             else
252             {
253                 writer.write("null");
254             }
255             writer.write(", '#', null");
256
257             if (item.isRendered() && ! item.isDisabled()) {
258                 // render children only if parent is visible/enabled
259
NavigationMenuItem[] menuItems = item.getNavigationMenuItems();
260                 if (menuItems != null && menuItems.length > 0)
261                 {
262                     writer.write(",");
263                     if (uiNavMenuItem != null)
264                     {
265                         encodeNavigationMenuItems(context, writer, menuItems,
266                                 uiNavMenuItem.getChildren(), menuId);
267                     }
268                 }
269             };
270             writer.write("]");
271         }
272     }
273
274     private void encodeValueBinding(ResponseWriter writer, UINavigationMenuItem uiNavMenuItem,
275             NavigationMenuItem item) throws IOException JavaDoc
276     {
277         ValueBinding vb = uiNavMenuItem.getValueBinding("NavMenuItemValue");
278         if (vb == null) {
279             return;
280         }
281         String JavaDoc vbExpression = vb.getExpressionString();
282         if (vbExpression == null) {
283             return;
284         }
285         Object JavaDoc tempObj = item.getValue();
286         if (tempObj == null) {
287             return;
288         }
289         
290         writer.write(";");
291         writer.write(vbExpression);
292         writer.write("=");
293         writer.write(tempObj.toString());
294     }
295     
296     public void encodeEnd(FacesContext context, UIComponent component) throws IOException JavaDoc
297     {
298         RendererUtils.checkParamValidity(context, component, HtmlCommandJSCookMenu.class);
299         HtmlCommandJSCookMenu menu = (HtmlCommandJSCookMenu)component;
300         
301         AddResource.addJavaScriptToHeader(NavigationMenuItem.class, "jscookmenu/JSCookMenu.js", context);
302
303         AddResource.addJavaScriptToHeader(NavigationMenuItem.class, "jscookmenu/ThemeOffice/theme.js", context);
304         AddResource.addStyleSheet(NavigationMenuItem.class, "jscookmenu/ThemeOffice/theme.css", context);
305
306         AddResource.addJavaScriptToHeader(NavigationMenuItem.class, "jscookmenu/ThemeMiniBlack/theme.js", context);
307         AddResource.addStyleSheet(NavigationMenuItem.class, "jscookmenu/ThemeMiniBlack/theme.css", context);
308
309         AddResource.addJavaScriptToHeader(NavigationMenuItem.class, "jscookmenu/ThemeIE/theme.js", context);
310         AddResource.addStyleSheet(NavigationMenuItem.class, "jscookmenu/ThemeIE/theme.css", context);
311
312         AddResource.addJavaScriptToHeader(NavigationMenuItem.class, "jscookmenu/ThemePanel/theme.js", context);
313         AddResource.addStyleSheet(NavigationMenuItem.class, "jscookmenu/ThemePanel/theme.css", context);
314         
315         ResponseWriter writer = context.getResponseWriter();
316
317         String JavaDoc menuId = component.getClientId(context).replaceAll(":","_") + "_menu";
318
319         while(menuId.startsWith("_"))
320         {
321             menuId = menuId.substring(1);
322         }
323
324         writer.write("<div id=\"" + menuId + "\"></div>\n" +
325                      "<script type=\"text/javascript\"><!--\n" +
326                      "\tcmDraw ('" + menuId + "', myMenu, '" + menu.getLayout() + "', cm" + menu.getTheme() + ", '" + menu.getTheme() + "');\n" +
327                      "--></script>\n");
328     }
329
330 }
331
Popular Tags