KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > menubar > MenuBar


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.component.menubar;
35
36 import com.icesoft.faces.component.CSS_DEFAULT;
37 import com.icesoft.faces.component.ext.taglib.Util;
38 import com.icesoft.faces.context.effects.JavascriptContext;
39 import com.icesoft.faces.util.CoreUtils;
40
41 import javax.faces.component.NamingContainer;
42 import javax.faces.component.UICommand;
43 import javax.faces.component.UIComponent;
44 import javax.faces.context.FacesContext;
45 import javax.faces.el.ValueBinding;
46 import javax.faces.event.AbortProcessingException;
47 import javax.faces.event.FacesEvent;
48
49
50 /**
51  * MenuBar is a JSF component class representing the ICEfaces menu bar. <p>The
52  * menuBar component provides a robust menu system that supports:
53  * <p/>
54  * 1. Nested child menuItem and menuItemSeparator components. Support for
55  * menuItemCheckbox and menuItemRadio components are planned for a future
56  * release.<br/> 2. Horizontal (default) and Vertical menu orientations. Defines
57  * whether the submenus of the top-level menu items appear beside or below the
58  * top-level menu items.<br/> 3. Definition of the heirarchy of menu items and
59  * their submenus in one of two ways:<br/> - by using a binding to a bean method
60  * that returns a (potentially) dynamic heirarchy of menu items.<br/> - by
61  * statically defining the heirarchy in the JSPX page.<br/> 4. The action
62  * attribute of the contained menuItem tags or instances can be defined to
63  * indicate a string or a backing bean method that can be used in application
64  * navigation.<br/> 5. The actionListener attribute of the contained menuItem
65  * tags or instances can be defined to indicate an actionListener that resides
66  * in a backing bean.<br/>
67  * <p/>
68  * This component extends the JSF UICommand component and implements the
69  * NamingContainer interface.
70  * <p/>
71  * By default the MenuBar is rendered by the "com.icesoft.faces.View" renderer
72  * type.
73  *
74  * @author Chris Brown
75  * @author gmccleary
76  * @version 1.1
77  */

78 public class MenuBar extends UICommand implements NamingContainer {
79
80     // default style classes
81
private static final String JavaDoc DEFAULT_IMAGEDIR = "/xmlhttp/css/xp/css-images/";
82     /**
83      * String constant vertical orientation
84      */

85     public static final String JavaDoc ORIENTATION_VERTICAL = "vertical";
86     /**
87      * String contant horizontal orientation
88      */

89     public static final String JavaDoc ORIENTATION_HORIZONTAL = "horizontal";
90     /**
91      * String contant default orientation
92      */

93     public static final String JavaDoc DEFAULT_ORIENTATION = ORIENTATION_HORIZONTAL;
94     /**
95      * String constant menu id prefix
96      */

97     public static final String JavaDoc ID_PREFIX = "menu-";
98
99     private String JavaDoc imageDir;
100     private String JavaDoc orientation; // horizontal | vertical ; default = horizontal
101
private String JavaDoc style;
102     private String JavaDoc renderedOnUserRole = null;
103     private Boolean JavaDoc noIcons;
104
105     /**
106      * default no args constructor
107      */

108     public MenuBar() {
109         JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
110                                      FacesContext.getCurrentInstance());
111     }
112
113     /**
114      * <p>Return the value of the <code>COMPONENT_FAMILY</code> of this
115      * component.</p>
116      *
117      * @return String component family
118      */

119     public String JavaDoc getFamily() {
120         return "com.icesoft.faces.Menu";
121     }
122
123     /**
124      * @return String component type
125      */

126     public String JavaDoc getComponentType() {
127         return "com.icesoft.faces.Menu";
128     }
129
130     /* (non-Javadoc)
131      * @see javax.faces.component.UIComponentBase#getRendererType()
132      */

133     public String JavaDoc getRendererType() {
134         return "com.icesoft.faces.View";
135     }
136
137     /**
138      * Return the value of the noIcons property. A return value of "true"
139      * indicates that noIcons should be rendered on all of the MenuBar
140      * subMenus.
141      *
142      * @return String value of Boolean noIcons
143      */

144     public String JavaDoc getNoIcons() {
145
146         if (noIcons != null) {
147             return noIcons.toString();
148         }
149         ValueBinding vb = getValueBinding("noIcons");
150         if (vb != null) {
151             return vb.getValue(getFacesContext()).toString();
152         }
153         return String.valueOf(false);
154     }
155
156     /**
157      * Set the value of the noIcons property. Setting this value to "true" will
158      * cause all subMenus of this MenuBar to be rendered without icons and
159      * spacers.
160      *
161      * @param b
162      */

163     public void setNoIcons(String JavaDoc b) {
164         noIcons = new Boolean JavaDoc(b);
165     }
166
167     /**
168      * <p>Set the value of the <code>style</code> property.</p>
169      *
170      * @param style
171      */

172     public void setStyle(String JavaDoc style) {
173         this.style = style;
174     }
175
176     /**
177      * <p>Return the value of the <code>style</code> property.</p>
178      *
179      * @return String style
180      */

181     public String JavaDoc getStyle() {
182         if (style != null) {
183             return style;
184         }
185         ValueBinding vb = getValueBinding("style");
186         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) : null;
187     }
188
189     // GETTERS AND SETTERS
190

191     /**
192      * <p>Return the value of the <code>imageDir</code> property.</p>
193      *
194      * @return String imageDir
195      */

196     public String JavaDoc getImageDir() {
197         if (imageDir != null) {
198             return imageDir;
199         }
200         ValueBinding vb = getValueBinding("imageDir");
201         if (vb != null) {
202             return (String JavaDoc) vb.getValue(getFacesContext());
203         }
204         return CoreUtils.resolveResourceURL(getFacesContext(), DEFAULT_IMAGEDIR);
205     }
206
207     /**
208      * <p>Set the value of the <code>imageDir</code> property.</p>
209      *
210      * @param imageDir
211      */

212     public void setImageDir(String JavaDoc imageDir) {
213         this.imageDir = imageDir;
214     }
215
216     /**
217      * <p>Return the value of the <code>orientation</code> property.</p>
218      *
219      * @return String orientation
220      */

221     public String JavaDoc getOrientation() {
222         if (orientation != null) {
223             return orientation;
224         }
225         ValueBinding vb = getValueBinding("orientation");
226         if (vb != null) {
227             return (String JavaDoc) vb.getValue(getFacesContext());
228         }
229         return DEFAULT_ORIENTATION;
230     }
231
232     /**
233      * <p>Set the value of the <code>orientation</code> property.</p>
234      *
235      * @param orient
236      */

237     public void setOrientation(String JavaDoc orient) {
238         orientation = orient;
239     }
240
241     /* (non-Javadoc)
242      * @see javax.faces.component.UIComponent#processDecodes(javax.faces.context.FacesContext)
243      */

244     public void processDecodes(FacesContext context) {
245         if (context == null) {
246             throw new NullPointerException JavaDoc("context");
247         }
248         if (!isRendered()) {
249             return;
250         }
251
252         decodeRecursive(this, context);
253         try {
254             decode(context);
255         } catch (RuntimeException JavaDoc e) {
256             context.renderResponse();
257             throw e;
258         }
259     }
260
261     /**
262      * @param component
263      * @param context
264      */

265     private void decodeRecursive(UIComponent component, FacesContext context) {
266         for (int i = 0; i < component.getChildCount(); i++) {
267             UIComponent next = (UIComponent) component.getChildren().get(i);
268             next.processDecodes(context);
269             decodeRecursive(next, context);
270         }
271     }
272
273     /* (non-Javadoc)
274      * @see javax.faces.component.UIComponent#queueEvent(javax.faces.event.FacesEvent)
275      */

276     public void queueEvent(FacesEvent event) {
277         super.queueEvent(event);
278     }
279
280     /* (non-Javadoc)
281      * @see javax.faces.component.UIComponent#broadcast(javax.faces.event.FacesEvent)
282      */

283     public void broadcast(FacesEvent event) throws AbortProcessingException {
284         super.broadcast(event);
285         return;
286
287     }
288
289     /**
290      * <p>Set the value of the <code>renderedOnUserRole</code> property.</p>
291      *
292      * @param renderedOnUserRole
293      */

294     public void setRenderedOnUserRole(String JavaDoc renderedOnUserRole) {
295         this.renderedOnUserRole = renderedOnUserRole;
296     }
297
298     /**
299      * <p>Return the value of the <code>renderedOnUserRole</code> property.</p>
300      *
301      * @return String renderedOnUserRole
302      */

303     public String JavaDoc getRenderedOnUserRole() {
304         if (renderedOnUserRole != null) {
305             return renderedOnUserRole;
306         }
307         ValueBinding vb = getValueBinding("renderedOnUserRole");
308         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) : null;
309     }
310
311     /**
312      * <p>Return the value of the <code>rendered</code> property.</p>
313      *
314      * @return boolean rendered
315      */

316     public boolean isRendered() {
317         if (!Util.isRenderedOnUserRole(this)) {
318             return false;
319         }
320         return super.isRendered();
321     }
322     
323     private String JavaDoc styleClass;
324     /**
325      * <p>Set the value of the <code>styleClass</code> property.</p>
326      *
327      * @param styleClass
328      */

329     public void setStyleClass(String JavaDoc styleClass) {
330         this.styleClass = styleClass;
331     }
332
333     /**
334      * <p>Return the value of the <code>styleClass</code> property.</p>
335      *
336      * @return String styleClass
337      */

338     public String JavaDoc getStyleClass() {
339         String JavaDoc defaultStyle = CSS_DEFAULT.MENU_BAR_STYLE;
340         String JavaDoc userDefinedClass = styleClass;
341         if (MenuBar.ORIENTATION_VERTICAL.equalsIgnoreCase(
342                 getOrientation())){
343             defaultStyle+=CSS_DEFAULT.MENU_BAR_VERTICAL_SUFFIX_STYLE;
344             if (userDefinedClass != null) {
345                 userDefinedClass+=CSS_DEFAULT.MENU_BAR_VERTICAL_SUFFIX_STYLE;
346             }
347         }
348         return Util.getQualifiedStyleClass(this,
349                 userDefinedClass,
350                 defaultStyle,
351                 "styleClass");
352     }
353     
354     public String JavaDoc getItemStyleClass() {
355         return Util.getQualifiedStyleClass(this,
356                                 CSS_DEFAULT.MENU_BAR_ITEM_STYLE);
357     }
358     
359     public String JavaDoc getItemLabelStyleClass() {
360         return Util.getQualifiedStyleClass(this,
361                                 CSS_DEFAULT.MENU_BAR_ITEM_LABEL_STYLE);
362     }
363     
364     public String JavaDoc getItemImageStyleClass() {
365         return Util.getQualifiedStyleClass(this,
366                                 CSS_DEFAULT.MENU_BAR_ITEM_STYLE+
367                                 CSS_DEFAULT.MENU_ITEM_IMAGE_STYLE);
368     }
369     
370     public String JavaDoc getSubMenuStyleClass() {
371         return Util.getQualifiedStyleClass(this,
372                                 CSS_DEFAULT.MENU_BAR_SUB_MENU_STYLE);
373     }
374     
375     public String JavaDoc getSubMenuIndicatorStyleClass() {
376         return Util.getQualifiedStyleClass(this,
377                                 CSS_DEFAULT.MENU_BAR_SUB_MENU_INDICATOR_STYLE);
378     }
379 }
380
381
Popular Tags