KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > ext > HtmlSelectOneMenu


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.ext;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Map JavaDoc;
41
42 import javax.faces.component.UIComponent;
43 import javax.faces.component.UISelectItem;
44 import javax.faces.component.UISelectItems;
45 import javax.faces.context.FacesContext;
46 import javax.faces.el.ValueBinding;
47 import javax.faces.model.SelectItem;
48
49 import com.icesoft.faces.component.CSS_DEFAULT;
50 import com.icesoft.faces.component.IceExtended;
51 import com.icesoft.faces.component.ext.taglib.Util;
52 import com.icesoft.faces.context.BridgeFacesContext;
53 import com.icesoft.faces.context.effects.CurrentStyle;
54 import com.icesoft.faces.context.effects.Effect;
55 import com.icesoft.faces.context.effects.JavascriptContext;
56
57
58 /**
59  * This is an extension of javax.faces.component.html.HtmlSelectOneMenu, which
60  * provides some additional behavior to this component such as: <ul> <li>default
61  * classes for enabled and disabled state</li> <li>provides partial submit
62  * mechanism</li> <li>changes the component's enabled and rendered state based
63  * on the authentication</li> <li>adds effects to the component</li> <ul>
64  */

65 public class HtmlSelectOneMenu
66         extends javax.faces.component.html.HtmlSelectOneMenu
67         implements IceExtended {
68
69     public static final String JavaDoc COMPONENT_TYPE =
70             "com.icesoft.faces.HtmlSelectOneMenu";
71     public static final String JavaDoc RENDERER_TYPE = "com.icesoft.faces.Menu";
72     private static final boolean DEFAULT_VISIBLE = true;
73     private String JavaDoc styleClass = null;
74     private Boolean JavaDoc partialSubmit = null;
75     private String JavaDoc enabledOnUserRole = null;
76     private String JavaDoc renderedOnUserRole = null;
77     private Effect effect;
78     private Boolean JavaDoc visible = null;
79     private Effect onclickeffect;
80     private Effect ondblclickeffect;
81     private Effect onmousedowneffect;
82     private Effect onmouseupeffect;
83     private Effect onmousemoveeffect;
84     private Effect onmouseovereffect;
85     private Effect onmouseouteffect;
86
87     private Effect onkeypresseffect;
88     private Effect onkeydowneffect;
89     private Effect onkeyupeffect;
90
91
92     private CurrentStyle currentStyle;
93
94     private Effect onchangeeffect;
95
96     public HtmlSelectOneMenu() {
97         super();
98         setRendererType(RENDERER_TYPE);
99     }
100
101     /**
102      * This method is used to communicate a focus request from the application
103      * to the client browser.
104      */

105     public void requestFocus() {
106         ((BridgeFacesContext) FacesContext.getCurrentInstance())
107                 .setFocusId("null");
108         JavascriptContext.focus(FacesContext.getCurrentInstance(),
109                                 this.getClientId(
110                                         FacesContext.getCurrentInstance()));
111     }
112
113     public SelectItem[] getSelectItems() {
114         List JavaDoc selectItems = new ArrayList JavaDoc();
115         Iterator JavaDoc children = this.getChildren().iterator();
116
117         while (children.hasNext()) {
118             UIComponent child = (UIComponent) children.next();
119             if (child instanceof UISelectItem) {
120                 Object JavaDoc selectItemValue = ((UISelectItem) child).getValue();
121                 if (selectItemValue != null &&
122                     selectItemValue instanceof SelectItem) {
123                     selectItems.add(selectItemValue);
124                 } else {
125                     //If user defines only one member, either itemValue or itemLabel
126
//The default implementation throws a null pointer exception.
127
//So here we are identifying, if either itemValue or itemLabel is found,
128
//Assigned its value to the other member
129
assignDataIfNull(child);
130                     selectItems.add(
131                             new SelectItem(
132                                     ((UISelectItem) child).getItemValue(),
133                                     ((UISelectItem) child).getItemLabel(),
134                                     ((UISelectItem) child).getItemDescription(),
135                                     ((UISelectItem) child).isItemDisabled()));
136                 }
137             } else if (child instanceof UISelectItems) {
138                 Object JavaDoc selectItemsValue = ((UISelectItems) child).getValue();
139
140                 if (selectItemsValue != null) {
141                     if (selectItemsValue instanceof SelectItem) {
142                         selectItems.add(selectItemsValue);
143                     } else if (selectItemsValue instanceof Collection JavaDoc) {
144                         Iterator JavaDoc selectItemsIterator =
145                                 ((Collection JavaDoc) selectItemsValue).iterator();
146                         while (selectItemsIterator.hasNext()) {
147                             selectItems.add(selectItemsIterator.next());
148                         }
149                     } else if (selectItemsValue instanceof SelectItem[]) {
150                         SelectItem selectItemArray[] =
151                                 (SelectItem[]) selectItemsValue;
152                         for (int i = 0; i < selectItemArray.length; i++) {
153                             selectItems.add(selectItemArray[i]);
154                         }
155                     } else if (selectItemsValue instanceof Map JavaDoc) {
156                         Iterator JavaDoc selectItemIterator =
157                                 ((Map JavaDoc) selectItemsValue).keySet().iterator();
158                         while (selectItemIterator.hasNext()) {
159                             Object JavaDoc nextKey = selectItemIterator.next();
160                             if (nextKey != null) {
161                                 Object JavaDoc nextValue =
162                                         ((Map JavaDoc) selectItemsValue).get(nextKey);
163                                 if (nextValue != null) {
164                                     selectItems.add(
165                                             new SelectItem(
166                                                     nextValue.toString(),
167                                                     nextKey.toString()));
168                                 }
169                             }
170                         }
171                     } else if (selectItemsValue instanceof String JavaDoc[]) {
172                         String JavaDoc stringItemArray[] = (String JavaDoc[]) selectItemsValue;
173                         for (int i = 0; i < stringItemArray.length; i++) {
174                             selectItems.add(new SelectItem(stringItemArray[i]));
175                         }
176                     }
177                 }
178             }
179         }
180         return (SelectItem[]) selectItems
181                 .toArray(new SelectItem[selectItems.size()]);
182     }
183
184     static void assignDataIfNull(Object JavaDoc selectItem) {
185         UISelectItem uiSelectItem = (UISelectItem) selectItem;
186         if (uiSelectItem.getItemValue() == null) {
187             if (uiSelectItem.getItemLabel() != null) {
188                 uiSelectItem.setItemValue(uiSelectItem.getItemLabel());
189             }
190         }
191         if (uiSelectItem.getItemLabel() == null) {
192             if (uiSelectItem.getItemValue() != null) {
193                 uiSelectItem
194                         .setItemLabel(uiSelectItem.getItemValue().toString());
195             }
196         }
197     }
198
199     public void setValueBinding(String JavaDoc s, ValueBinding vb) {
200         if (s != null && s.indexOf("effect") != -1) {
201             // If this is an effect attribute make sure Ice Extras is included
202
JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
203                                          getFacesContext());
204         }
205         super.setValueBinding(s, vb);
206     }
207
208     /**
209      * <p>Set the value of the <code>visible</code> property.</p>
210      */

211     public void setVisible(boolean visible) {
212         this.visible = Boolean.valueOf(visible);
213     }
214
215     /**
216      * <p>Return the value of the <code>visible</code> property.</p>
217      */

218     public boolean getVisible() {
219         if (visible != null) {
220             return visible.booleanValue();
221         }
222         ValueBinding vb = getValueBinding("visible");
223         Boolean JavaDoc boolVal =
224                 vb != null ? (Boolean JavaDoc) vb.getValue(getFacesContext()) : null;
225         return boolVal != null ? boolVal.booleanValue() : DEFAULT_VISIBLE;
226     }
227
228     /**
229      * <p>Set the value of the <code>effect</code> property.</p>
230      */

231     public void setEffect(Effect effect) {
232         this.effect = effect;
233         JavascriptContext
234                 .includeLib(JavascriptContext.ICE_EXTRAS, getFacesContext());
235     }
236
237     /**
238      * <p>Return the value of the <code>effect</code> property.</p>
239      */

240     public Effect getEffect() {
241         if (effect != null) {
242             return effect;
243         }
244         ValueBinding vb = getValueBinding("effect");
245         return vb != null ? (Effect) vb.getValue(getFacesContext()) : null;
246     }
247
248     /**
249      * <p>Set the value of the <code>visible</code> property.</p>
250      */

251     public void setPartialSubmit(boolean partialSubmit) {
252         this.partialSubmit = Boolean.valueOf(partialSubmit);
253     }
254
255     /**
256      * <p>Return the value of the <code>partialSubmit</code> property.</p>
257      */

258     public boolean getPartialSubmit() {
259         if (partialSubmit != null) {
260             return partialSubmit.booleanValue();
261         }
262         ValueBinding vb = getValueBinding("partialSubmit");
263         Boolean JavaDoc boolVal =
264                 vb != null ? (Boolean JavaDoc) vb.getValue(getFacesContext()) : null;
265         return boolVal != null ? boolVal.booleanValue() :
266                Util.isParentPartialSubmit(this);
267     }
268
269     /**
270      * <p>Set the value of the <code>enabledOnUserRole</code> property.</p>
271      */

272     public void setEnabledOnUserRole(String JavaDoc enabledOnUserRole) {
273         this.enabledOnUserRole = enabledOnUserRole;
274     }
275
276     /**
277      * <p>Return the value of the <code>enabledOnUserRole</code> property.</p>
278      */

279     public String JavaDoc getEnabledOnUserRole() {
280         if (enabledOnUserRole != null) {
281             return enabledOnUserRole;
282         }
283         ValueBinding vb = getValueBinding("enabledOnUserRole");
284         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) : null;
285     }
286
287     /**
288      * <p>Set the value of the <code>renderedOnUserRole</code> property.</p>
289      */

290     public void setRenderedOnUserRole(String JavaDoc renderedOnUserRole) {
291         this.renderedOnUserRole = renderedOnUserRole;
292     }
293
294     /**
295      * <p>Return the value of the <code>renderedOnUserRole</code> property.</p>
296      */

297     public String JavaDoc getRenderedOnUserRole() {
298         if (renderedOnUserRole != null) {
299             return renderedOnUserRole;
300         }
301         ValueBinding vb = getValueBinding("renderedOnUserRole");
302         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) : null;
303     }
304
305     /**
306      * <p>Return the value of the <code>rendered</code> property.</p>
307      */

308     public boolean isRendered() {
309         if (!Util.isRenderedOnUserRole(this)) {
310             return false;
311         }
312         return super.isRendered();
313     }
314
315     /**
316      * <p>Set the value of the <code>styleClass</code> property.</p>
317      */

318     public void setStyleClass(String JavaDoc styleClass) {
319         this.styleClass = styleClass;
320     }
321
322     /**
323      * <p>Return the value of the <code>styleClass</code> property.</p>
324      */

325     public String JavaDoc getStyleClass() {
326         return Util.getQualifiedStyleClass(this,
327                 styleClass,
328                 CSS_DEFAULT.SELECT_ONE_MENU_DEFAULT_STYLE_CLASS,
329                 "styleClass",
330                 isDisabled());
331     }
332
333     /**
334      * <p>Return the value of the <code>disabled</code> property.</p>
335      */

336     public boolean isDisabled() {
337         if (!Util.isEnabledOnUserRole(this)) {
338             return true;
339         } else {
340             return super.isDisabled();
341         }
342     }
343
344
345     /**
346      * <p>Return the value of the <code>onchangeeffect</code> property.</p>
347      */

348     public Effect getOnchangeeffect() {
349         if (onchangeeffect != null) {
350             return onchangeeffect;
351         }
352         ValueBinding vb = getValueBinding("onchangeeffect");
353
354         return vb != null ? (Effect) vb.getValue(getFacesContext()) : null;
355     }
356
357     /**
358      * <p>Set the value of the <code>onchangeeffect</code> property.</p>
359      */

360     public void setOnchangeeffect(Effect onchangeeffect) {
361         this.onchangeeffect = onchangeeffect;
362         JavascriptContext
363                 .includeLib(JavascriptContext.ICE_EXTRAS, getFacesContext());
364     }
365
366
367     /**
368      * <p>Return the value of the <code>currentStyle</code> property.</p>
369      */

370     public CurrentStyle getCurrentStyle() {
371         return currentStyle;
372     }
373
374     /**
375      * <p>Set the value of the <code>currentStyle</code> property.</p>
376      */

377     public void setCurrentStyle(CurrentStyle currentStyle) {
378         this.currentStyle = currentStyle;
379     }
380
381
382
383
384
385
386     /**
387      * <p>Set the value of the <code>onclickeffect</code> property.</p>
388      */

389     public void setOnclickeffect(Effect onclickeffect) {
390         this.onclickeffect = onclickeffect;
391         JavascriptContext
392                 .includeLib(JavascriptContext.ICE_EXTRAS, getFacesContext());
393     }
394
395     /**
396      * <p>Return the value of the <code>ondblclickeffect</code> property.</p>
397      */

398     public Effect getOndblclickeffect() {
399         if (ondblclickeffect != null) {
400             return ondblclickeffect;
401         }
402         ValueBinding vb = getValueBinding("ondblclickeffect");
403
404         return vb != null ? (Effect) vb.getValue(getFacesContext()) : null;
405     }
406
407     /**
408      * <p>Set the value of the <code>ondblclickeffect</code> property.</p>
409      */

410     public void setOndblclickeffect(Effect ondblclickeffect) {
411         this.ondblclickeffect = ondblclickeffect;
412         JavascriptContext
413                 .includeLib(JavascriptContext.ICE_EXTRAS, getFacesContext());
414     }
415
416     /**
417      * <p>Return the value of the <code>onmousedowneffect</code> property.</p>
418      */

419     public Effect getOnmousedowneffect() {
420         if (onmousedowneffect != null) {
421             return onmousedowneffect;
422         }
423         ValueBinding vb = getValueBinding("onmousedowneffect");
424
425         return vb != null ? (Effect) vb.getValue(getFacesContext()) : null;
426     }
427
428     /**
429      * <p>Set the value of the <code>onmousedowneffect</code> property.</p>
430      */

431     public void setOnmousedowneffect(Effect onmousedowneffect) {
432         this.onmousedowneffect = onmousedowneffect;
433         JavascriptContext
434                 .includeLib(JavascriptContext.ICE_EXTRAS, getFacesContext());
435     }
436
437     /**
438      * <p>Return the value of the <code>onmouseupeffect</code> property.</p>
439      */

440     public Effect getOnmouseupeffect() {
441         if (onmouseupeffect != null) {
442             return onmouseupeffect;
443         }
444         ValueBinding vb = getValueBinding("onmouseupeffect");
445
446         return vb != null ? (Effect) vb.getValue(getFacesContext()) : null;
447     }
448
449     /**
450      * <p>Set the value of the <code>onmouseupeffect</code> property.</p>
451      */

452     public void setOnmouseupeffect(Effect onmouseupeffect) {
453         this.onmouseupeffect = onmouseupeffect;
454         JavascriptContext
455                 .includeLib(JavascriptContext.ICE_EXTRAS, getFacesContext());
456     }
457
458     /**
459      * <p>Return the value of the <code>onmousemoveeffect</code> property.</p>
460      */

461     public Effect getOnmousemoveeffect() {
462         if (onmousemoveeffect != null) {
463             return onmousemoveeffect;
464         }
465         ValueBinding vb = getValueBinding("onmousemoveeffect");
466
467         return vb != null ? (Effect) vb.getValue(getFacesContext()) : null;
468     }
469
470     /**
471      * <p>Set the value of the <code>onmousemoveeffect</code> property.</p>
472      */

473     public void setOnmousemoveeffect(Effect onmousemoveeffect) {
474         this.onmousemoveeffect = onmousemoveeffect;
475         JavascriptContext
476                 .includeLib(JavascriptContext.ICE_EXTRAS, getFacesContext());
477     }
478
479     /**
480      * <p>Return the value of the <code>onmouseovereffect</code> property.</p>
481      */

482     public Effect getOnmouseovereffect() {
483         if (onmouseovereffect != null) {
484             return onmouseovereffect;
485         }
486         ValueBinding vb = getValueBinding("onmouseovereffect");
487
488         return vb != null ? (Effect) vb.getValue(getFacesContext()) : null;
489     }
490
491     /**
492      * <p>Set the value of the <code>onmouseovereffect</code> property.</p>
493      */

494     public void setOnmouseovereffect(Effect onmouseovereffect) {
495         this.onmouseovereffect = onmouseovereffect;
496         JavascriptContext
497                 .includeLib(JavascriptContext.ICE_EXTRAS, getFacesContext());
498     }
499
500     /**
501      * <p>Return the value of the <code>onmouseouteffect</code> property.</p>
502      */

503     public Effect getOnmouseouteffect() {
504         if (onmouseouteffect != null) {
505             return onmouseouteffect;
506         }
507         ValueBinding vb = getValueBinding("onmouseouteffect");
508
509         return vb != null ? (Effect) vb.getValue(getFacesContext()) : null;
510     }
511
512     /**
513      * <p>Set the value of the <code>onmouseouteffect</code> property.</p>
514      */

515     public void setOnmouseouteffect(Effect onmouseouteffect) {
516         this.onmouseouteffect = onmouseouteffect;
517         JavascriptContext
518                 .includeLib(JavascriptContext.ICE_EXTRAS, getFacesContext());
519     }
520
521
522     /**
523      * <p>Return the value of the <code>onkeypresseffect</code> property.</p>
524      */

525     public Effect getOnkeypresseffect() {
526         if (onkeypresseffect != null) {
527             return onkeypresseffect;
528         }
529         ValueBinding vb = getValueBinding("onkeypresseffect");
530
531         return vb != null ? (Effect) vb.getValue(getFacesContext()) : null;
532     }
533
534     /**
535      * <p>Set the value of the <code>onkeypresseffect</code> property.</p>
536      */

537     public void setOnkeypresseffect(Effect onkeypresseffect) {
538         this.onkeypresseffect = onkeypresseffect;
539         JavascriptContext
540                 .includeLib(JavascriptContext.ICE_EXTRAS, getFacesContext());
541     }
542
543
544
545
546
547
548
549
550
551     /**
552      * <p>Return the value of the <code>onkeydowneffect</code> property.</p>
553      */

554     public Effect getOnkeydowneffect() {
555         if (onkeydowneffect != null) {
556             return onkeydowneffect;
557         }
558         ValueBinding vb = getValueBinding("onkeydowneffect");
559
560         return vb != null ? (Effect) vb.getValue(getFacesContext()) : null;
561     }
562
563     /**
564      * <p>Set the value of the <code>onkeydowneffect</code> property.</p>
565      */

566     public void setOnkeydowneffect(Effect onkeydowneffect) {
567         this.onkeydowneffect = onkeydowneffect;
568         JavascriptContext
569                 .includeLib(JavascriptContext.ICE_EXTRAS, getFacesContext());
570     }
571
572     /**
573      * <p>Return the value of the <code>onkeyupeffect</code> property.</p>
574      */

575     public Effect getOnkeyupeffect() {
576         if (onkeyupeffect != null) {
577             return onkeyupeffect;
578         }
579         ValueBinding vb = getValueBinding("onkeyupeffect");
580
581         return vb != null ? (Effect) vb.getValue(getFacesContext()) : null;
582     }
583
584     /**
585      * <p>Set the value of the <code>onkeyupeffect</code> property.</p>
586      */

587     public void setOnkeyupeffect(Effect onkeyupeffect) {
588         this.onkeyupeffect = onkeyupeffect;
589         JavascriptContext
590                 .includeLib(JavascriptContext.ICE_EXTRAS, getFacesContext());
591     }
592
593     /**
594      * <p>Return the value of the <code>onkeyupeffect</code> property.</p>
595      */

596     public Effect getOnclickeffect() {
597         if (onclickeffect != null) {
598             return onclickeffect;
599         }
600         ValueBinding vb = getValueBinding("onclickeffect");
601
602         return vb != null ? (Effect) vb.getValue(getFacesContext()) : null;
603     }
604
605     private String JavaDoc autocomplete;
606
607     /**
608      * <p>Set the value of the <code>autocomplete</code> property.</p>
609      */

610     public void setAutocomplete(String JavaDoc autocomplete) {
611         this.autocomplete = autocomplete;
612     }
613
614     /**
615      * <p>Return the value of the <code>autocomplete</code> property.</p>
616      */

617     public String JavaDoc getAutocomplete() {
618         if (autocomplete != null) {
619             return autocomplete;
620         }
621         ValueBinding vb = getValueBinding("autocomplete");
622         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) : null;
623     }
624
625
626     /**
627      * <p>Gets the state of the instance as a <code>Serializable</code>
628      * Object.</p>
629      */

630     public Object JavaDoc saveState(FacesContext context) {
631         Object JavaDoc values[] = new Object JavaDoc[21];
632         values[0] = super.saveState(context);
633         values[1] = partialSubmit;
634         values[2] = enabledOnUserRole;
635         values[3] = renderedOnUserRole;
636         values[4] = styleClass;
637         values[5] = effect;
638         values[13] = onchangeeffect;
639         values[19] = currentStyle;
640         values[20] = visible;
641         return ((Object JavaDoc) (values));
642     }
643
644     /**
645      * <p>Perform any processing required to restore the state from the entries
646      * in the state Object.</p>
647      */

648     public void restoreState(FacesContext context, Object JavaDoc state) {
649         Object JavaDoc values[] = (Object JavaDoc[]) state;
650         super.restoreState(context, values[0]);
651         partialSubmit = (Boolean JavaDoc) values[1];
652         enabledOnUserRole = (String JavaDoc) values[2];
653         renderedOnUserRole = (String JavaDoc) values[3];
654         styleClass = (String JavaDoc) values[4];
655         effect = (Effect) values[5];
656         onchangeeffect = (Effect) values[13];
657         currentStyle = (CurrentStyle) values[19];
658         visible = (Boolean JavaDoc) values[20];
659     }
660 }
661
662
663
Popular Tags