KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.CoreUtils;
39
40 import javax.faces.component.UIComponent;
41 import javax.faces.component.UIParameter;
42 import javax.faces.el.MethodBinding;
43 import javax.faces.el.ValueBinding;
44 import javax.faces.event.ActionListener;
45 import java.util.Iterator JavaDoc;
46 import java.util.List JavaDoc;
47
48
49 /**
50  * MenuItem is a JSF component class that represent an ICEfaces menuItem.
51  * <p>MenuItem components are the menu items contained by a menuBar. The action
52  * and actionListener attributes operate in the same way as the standard
53  * component attributes of the same name. The MenuItem component is only used in
54  * the static approach to defining the heirarchy of menu items.
55  * <p/>
56  * This component extends the ICEfaces MenuItemBase component.
57  * <p/>
58  * By default the MenuItem is rendered by the "com.icesoft.faces.View" renderer
59  * type.
60  *
61  * @author Chris Brown
62  * @author gmccleary
63  * @version 1.1
64  */

65 public class MenuItem extends MenuItemBase {
66
67     private String JavaDoc icon;
68     private Object JavaDoc value;
69     private String JavaDoc link;
70     private String JavaDoc target;
71     private MethodBinding action = null;
72     private MethodBinding actionListener = null;
73     private Boolean JavaDoc disabled = null;
74     private String JavaDoc enabledOnUserRole = null;
75     private String JavaDoc renderedOnUserRole = null;
76
77     /**
78      * String constant defining default menu icon img
79      */

80     public static final String JavaDoc DEFAULT_ICON =
81             "/xmlhttp/css/xp/css-images/menu_blank_icon.gif";
82     private static final String JavaDoc DEFAULT_VALUE = "menu item default";
83     private static final String JavaDoc DEFAULT_LINK = "#";
84
85     public MenuItem() {
86     }
87
88     /* (non-Javadoc)
89      * @see javax.faces.component.UIComponent#getRendererType()
90      */

91     public String JavaDoc getRendererType() {
92         return "com.icesoft.faces.View";
93     }
94
95     public String JavaDoc getComponentType() {
96         return "com.icesoft.faces.MenuNode";
97     }
98
99     /**
100      * <p>Return the value of the <code>COMPONENT_FAMILY</code> of this
101      * component.</p>
102      */

103     public String JavaDoc getFamily() {
104         return "com.icesoft.faces.MenuNode";
105     }
106
107     // convenience getters / setters
108

109     /**
110      * <p>Return the value of the <code>icon</code> property.</p>
111      */

112     public String JavaDoc getIcon() {
113         if (icon != null) {
114             return icon;
115         }
116         ValueBinding vb = getValueBinding("icon");
117         if (vb != null) {
118             return (String JavaDoc) vb.getValue(getFacesContext());
119         }
120         return CoreUtils.resolveResourceURL(getFacesContext(), DEFAULT_ICON);
121     }
122
123     /**
124      * <p>Set the value of the <code>icon</code> property.</p>
125      */

126     public void setIcon(String JavaDoc iconValue) {
127         icon = iconValue;
128     }
129
130     /**
131      * <p>Return the value of the <code>value</code> property.</p>
132      */

133     public Object JavaDoc getValue() {
134         if (value != null) {
135             return value;
136         }
137         ValueBinding vb = getValueBinding("value");
138         if (vb != null) {
139             return (String JavaDoc) vb.getValue(getFacesContext());
140         }
141         return DEFAULT_VALUE;
142     }
143
144     /**
145      * <p>Set the value of the <code>value</code> property.</p>
146      */

147     public void setValue(Object JavaDoc value) {
148         this.value = value;
149     }
150
151     /**
152      * <p>Return the value of the <code>link</code> property.</p>
153      */

154     public String JavaDoc getLink() {
155         if (link != null) {
156             return link;
157         }
158         ValueBinding vb = getValueBinding("link");
159         if (vb != null) {
160             return (String JavaDoc) vb.getValue(getFacesContext());
161         }
162         return DEFAULT_LINK;
163     }
164
165     /**
166      * <p>Set the value of the <code>link</code> property.</p>
167      */

168     public void setLink(String JavaDoc linkValue) {
169         this.link = linkValue;
170     }
171
172     /**
173      * <p>Return the value of the <code>target</code> property.</p>
174      */

175     public String JavaDoc getTarget() {
176         if (target != null) {
177             return target;
178         }
179         ValueBinding vb = getValueBinding("target");
180         if (vb != null) {
181             return (String JavaDoc) vb.getValue(getFacesContext());
182         }
183         return null;
184     }
185
186     /**
187      * <p>Set the value of the <code>target</code> property.</p>
188      */

189     public void setTarget(String JavaDoc target) {
190         this.target = target;
191     }
192
193     /**
194      * <p>Return the value of the <code>action</code> property.</p>
195      */

196     public MethodBinding getAction() {
197         // Superclass getAction() is smart enough to handle JSF 1.1 and 1.2,
198
// getting Action MethodBinding or MethodExpression
199
MethodBinding actionMB = super.getAction();
200         if(actionMB != null)
201             return actionMB;
202         return action;
203     }
204
205     /**
206      * <p>Set the value of the <code>action</code> property.</p>
207      */

208     public void setAction(MethodBinding action) {
209         this.action = action;
210     }
211
212     /**
213      * <p>Return the value of the <code>actionListener</code> property.</p>
214      */

215     public MethodBinding getActionListener() {
216         return actionListener;
217     }
218
219     /**
220      * <p>Set the value of the <code>actionListener</code> property.</p>
221      */

222     public void setActionListener(MethodBinding actionListener) {
223         this.actionListener = actionListener;
224     }
225     
226     public boolean hasActionOrActionListener() {
227         if( getAction() != null )
228             return true;
229         if( getActionListener() != null )
230             return true;
231         ActionListener[] actionListeners = getActionListeners();
232         if( actionListeners != null && actionListeners.length > 0 )
233             return true;
234         return false;
235     }
236
237
238     /**
239      * <p>Set the value of the <code>disabled</code> property.</p>
240      */

241     public void setDisabled(boolean disabled) {
242         this.disabled = new Boolean JavaDoc(disabled);
243         ValueBinding vb = getValueBinding("disabled");
244         if (vb != null) {
245             vb.setValue(getFacesContext(), this.disabled);
246             this.disabled = null;
247         }
248     }
249
250     public boolean getDisabled() {
251         return isDisabled();
252     }
253
254     /**
255      * <p>Return the value of the <code>disabled</code> property.</p>
256      */

257     public boolean isDisabled() {
258         if (!Util.isEnabledOnUserRole(this)) {
259             return true;
260         }
261
262         if (disabled != null) {
263             return disabled.booleanValue();
264         }
265         ValueBinding vb = getValueBinding("disabled");
266         Boolean JavaDoc v =
267                 vb != null ? (Boolean JavaDoc) vb.getValue(getFacesContext()) : null;
268         return v != null ? v.booleanValue() : false;
269     }
270
271     void addParameter(UIComponent link) {
272         List JavaDoc children = getChildren();
273
274         for (int i = 0; i < children.size(); i++) {
275             UIComponent nextChild = (UIComponent) children.get(i);
276             if (nextChild instanceof UIParameter) {
277                 UIParameter param = new UIParameter();
278                 param.setName(((UIParameter) nextChild).getName());
279                 param.setValue(((UIParameter) nextChild).getValue());
280                 link.getChildren().add(param);
281             }
282         }
283     }
284
285     boolean isChildrenMenuItem() {
286         Iterator JavaDoc children = getChildren().iterator();
287         while (children.hasNext()) {
288             UIComponent child = (UIComponent) children.next();
289             if (child instanceof MenuItem) {
290                 return true;
291             }
292         }
293         return false;
294     }
295
296
297     /**
298      * <p>Set the value of the <code>enabledOnUserRole</code> property.</p>
299      *
300      * @param enabledOnUserRole
301      */

302     public void setEnabledOnUserRole(String JavaDoc enabledOnUserRole) {
303         this.enabledOnUserRole = enabledOnUserRole;
304     }
305
306     /**
307      * <p>Return the value of the <code>enabledOnUserRole</code> property.</p>
308      *
309      * @return String enabledOnUserRole
310      */

311     public String JavaDoc getEnabledOnUserRole() {
312         if (enabledOnUserRole != null) {
313             return enabledOnUserRole;
314         }
315         ValueBinding vb = getValueBinding("enabledOnUserRole");
316         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) : null;
317     }
318
319     /**
320      * <p>Set the value of the <code>renderedOnUserRole</code> property.</p>
321      *
322      * @param renderedOnUserRole
323      */

324     public void setRenderedOnUserRole(String JavaDoc renderedOnUserRole) {
325         this.renderedOnUserRole = renderedOnUserRole;
326     }
327
328     /**
329      * <p>Return the value of the <code>renderedOnUserRole</code> property.</p>
330      *
331      * @return String renderedOnUserRole
332      */

333     public String JavaDoc getRenderedOnUserRole() {
334         if (renderedOnUserRole != null) {
335             return renderedOnUserRole;
336         }
337         ValueBinding vb = getValueBinding("renderedOnUserRole");
338         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) : null;
339     }
340
341     /**
342      * <p>Return the value of the <code>rendered</code> property.</p>
343      *
344      * @return boolean rendered
345      */

346     public boolean isRendered() {
347         if (!Util.isRenderedOnUserRole(this)) {
348             return false;
349         }
350         return super.isRendered();
351     }
352     
353     private String JavaDoc styleClass;
354     /**
355      * <p>Set the value of the <code>styleClass</code> property.</p>
356      *
357      * @param styleClass
358      */

359     public void setStyleClass(String JavaDoc styleClass) {
360         this.styleClass = styleClass;
361     }
362
363     /**
364      * <p>Return the value of the <code>styleClass</code> property.</p>
365      *
366      * @return String styleClass
367      */

368     public String JavaDoc getStyleClass() {
369         return Util.getQualifiedStyleClass(this,
370                 styleClass,
371                 CSS_DEFAULT.MENU_ITEM_STYLE,
372                 "styleClass",
373                 isDisabled());
374     }
375     
376     public String JavaDoc getLabelStyleClass() {
377         return Util.getQualifiedStyleClass(this,
378                     CSS_DEFAULT.MENU_ITEM_LABEL_STYLE, isDisabled());
379     }
380     
381     public String JavaDoc getImageStyleClass() {
382         return Util.getQualifiedStyleClass(this,
383                 CSS_DEFAULT.MENU_ITEM_IMAGE_STYLE);
384     }
385     
386     String JavaDoc getUserDefinedStyleClass(String JavaDoc parentClass, String JavaDoc subClass) {
387         String JavaDoc disSuffix = isDisabled()? "-dis" : "";
388         
389         if (styleClass != null) {
390             return parentClass + disSuffix + " " +styleClass + subClass + disSuffix;
391         }
392         return parentClass + disSuffix;
393     }
394 }
395
Popular Tags