001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.component.ext;
035:
036: import java.util.ArrayList;
037: import java.util.Collection;
038: import java.util.Iterator;
039: import java.util.List;
040: import java.util.Map;
041:
042: import javax.faces.component.UIComponent;
043: import javax.faces.component.UISelectItem;
044: import javax.faces.component.UISelectItems;
045: import javax.faces.context.FacesContext;
046: import javax.faces.el.ValueBinding;
047: import javax.faces.model.SelectItem;
048:
049: import com.icesoft.faces.component.CSS_DEFAULT;
050: import com.icesoft.faces.component.IceExtended;
051: import com.icesoft.faces.component.ext.taglib.Util;
052: import com.icesoft.faces.context.BridgeFacesContext;
053: import com.icesoft.faces.context.effects.CurrentStyle;
054: import com.icesoft.faces.context.effects.Effect;
055: import com.icesoft.faces.context.effects.JavascriptContext;
056:
057: /**
058: * This is an extension of javax.faces.component.html.HtmlSelectOneMenu, which
059: * provides some additional behavior to this component such as: <ul> <li>default
060: * classes for enabled and disabled state</li> <li>provides partial submit
061: * mechanism</li> <li>changes the component's enabled and rendered state based
062: * on the authentication</li> <li>adds effects to the component</li> <ul>
063: */
064: public class HtmlSelectOneMenu extends
065: javax.faces.component.html.HtmlSelectOneMenu implements
066: IceExtended {
067:
068: public static final String COMPONENT_TYPE = "com.icesoft.faces.HtmlSelectOneMenu";
069: public static final String RENDERER_TYPE = "com.icesoft.faces.Menu";
070: private static final boolean DEFAULT_VISIBLE = true;
071: private String styleClass = null;
072: private Boolean partialSubmit = null;
073: private String enabledOnUserRole = null;
074: private String renderedOnUserRole = null;
075: private Effect effect;
076: private Boolean visible = null;
077: private Effect onclickeffect;
078: private Effect ondblclickeffect;
079: private Effect onmousedowneffect;
080: private Effect onmouseupeffect;
081: private Effect onmousemoveeffect;
082: private Effect onmouseovereffect;
083: private Effect onmouseouteffect;
084:
085: private Effect onkeypresseffect;
086: private Effect onkeydowneffect;
087: private Effect onkeyupeffect;
088:
089: private CurrentStyle currentStyle;
090:
091: private Effect onchangeeffect;
092:
093: public HtmlSelectOneMenu() {
094: super ();
095: setRendererType(RENDERER_TYPE);
096: }
097:
098: /**
099: * This method is used to communicate a focus request from the application
100: * to the client browser.
101: */
102: public void requestFocus() {
103: ((BridgeFacesContext) FacesContext.getCurrentInstance())
104: .setFocusId("null");
105: JavascriptContext.focus(FacesContext.getCurrentInstance(), this
106: .getClientId(FacesContext.getCurrentInstance()));
107: }
108:
109: public SelectItem[] getSelectItems() {
110: List selectItems = new ArrayList();
111: Iterator children = this .getChildren().iterator();
112:
113: while (children.hasNext()) {
114: UIComponent child = (UIComponent) children.next();
115: if (child instanceof UISelectItem) {
116: Object selectItemValue = ((UISelectItem) child)
117: .getValue();
118: if (selectItemValue != null
119: && selectItemValue instanceof SelectItem) {
120: selectItems.add(selectItemValue);
121: } else {
122: //If user defines only one member, either itemValue or itemLabel
123: //The default implementation throws a null pointer exception.
124: //So here we are identifying, if either itemValue or itemLabel is found,
125: //Assigned its value to the other member
126: assignDataIfNull(child);
127: selectItems
128: .add(new SelectItem(((UISelectItem) child)
129: .getItemValue(),
130: ((UISelectItem) child)
131: .getItemLabel(),
132: ((UISelectItem) child)
133: .getItemDescription(),
134: ((UISelectItem) child)
135: .isItemDisabled()));
136: }
137: } else if (child instanceof UISelectItems) {
138: Object selectItemsValue = ((UISelectItems) child)
139: .getValue();
140:
141: if (selectItemsValue != null) {
142: if (selectItemsValue instanceof SelectItem) {
143: selectItems.add(selectItemsValue);
144: } else if (selectItemsValue instanceof Collection) {
145: Iterator selectItemsIterator = ((Collection) selectItemsValue)
146: .iterator();
147: while (selectItemsIterator.hasNext()) {
148: selectItems.add(selectItemsIterator.next());
149: }
150: } else if (selectItemsValue instanceof SelectItem[]) {
151: SelectItem selectItemArray[] = (SelectItem[]) selectItemsValue;
152: for (int i = 0; i < selectItemArray.length; i++) {
153: selectItems.add(selectItemArray[i]);
154: }
155: } else if (selectItemsValue instanceof Map) {
156: Iterator selectItemIterator = ((Map) selectItemsValue)
157: .keySet().iterator();
158: while (selectItemIterator.hasNext()) {
159: Object nextKey = selectItemIterator.next();
160: if (nextKey != null) {
161: Object nextValue = ((Map) selectItemsValue)
162: .get(nextKey);
163: if (nextValue != null) {
164: selectItems.add(new SelectItem(
165: nextValue.toString(),
166: nextKey.toString()));
167: }
168: }
169: }
170: } else if (selectItemsValue instanceof String[]) {
171: String stringItemArray[] = (String[]) selectItemsValue;
172: for (int i = 0; i < stringItemArray.length; i++) {
173: selectItems.add(new SelectItem(
174: stringItemArray[i]));
175: }
176: }
177: }
178: }
179: }
180: return (SelectItem[]) selectItems
181: .toArray(new SelectItem[selectItems.size()]);
182: }
183:
184: static void assignDataIfNull(Object 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.setItemLabel(uiSelectItem.getItemValue()
194: .toString());
195: }
196: }
197: }
198:
199: public void setValueBinding(String 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 boolVal = vb != null ? (Boolean) vb
224: .getValue(getFacesContext()) : null;
225: return boolVal != null ? boolVal.booleanValue()
226: : DEFAULT_VISIBLE;
227: }
228:
229: /**
230: * <p>Set the value of the <code>effect</code> property.</p>
231: */
232: public void setEffect(Effect effect) {
233: this .effect = effect;
234: JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
235: getFacesContext());
236: }
237:
238: /**
239: * <p>Return the value of the <code>effect</code> property.</p>
240: */
241: public Effect getEffect() {
242: if (effect != null) {
243: return effect;
244: }
245: ValueBinding vb = getValueBinding("effect");
246: return vb != null ? (Effect) vb.getValue(getFacesContext())
247: : null;
248: }
249:
250: /**
251: * <p>Set the value of the <code>visible</code> property.</p>
252: */
253: public void setPartialSubmit(boolean partialSubmit) {
254: this .partialSubmit = Boolean.valueOf(partialSubmit);
255: }
256:
257: /**
258: * <p>Return the value of the <code>partialSubmit</code> property.</p>
259: */
260: public boolean getPartialSubmit() {
261: if (partialSubmit != null) {
262: return partialSubmit.booleanValue();
263: }
264: ValueBinding vb = getValueBinding("partialSubmit");
265: Boolean boolVal = vb != null ? (Boolean) vb
266: .getValue(getFacesContext()) : null;
267: return boolVal != null ? boolVal.booleanValue() : Util
268: .isParentPartialSubmit(this );
269: }
270:
271: /**
272: * <p>Set the value of the <code>enabledOnUserRole</code> property.</p>
273: */
274: public void setEnabledOnUserRole(String enabledOnUserRole) {
275: this .enabledOnUserRole = enabledOnUserRole;
276: }
277:
278: /**
279: * <p>Return the value of the <code>enabledOnUserRole</code> property.</p>
280: */
281: public String getEnabledOnUserRole() {
282: if (enabledOnUserRole != null) {
283: return enabledOnUserRole;
284: }
285: ValueBinding vb = getValueBinding("enabledOnUserRole");
286: return vb != null ? (String) vb.getValue(getFacesContext())
287: : null;
288: }
289:
290: /**
291: * <p>Set the value of the <code>renderedOnUserRole</code> property.</p>
292: */
293: public void setRenderedOnUserRole(String renderedOnUserRole) {
294: this .renderedOnUserRole = renderedOnUserRole;
295: }
296:
297: /**
298: * <p>Return the value of the <code>renderedOnUserRole</code> property.</p>
299: */
300: public String getRenderedOnUserRole() {
301: if (renderedOnUserRole != null) {
302: return renderedOnUserRole;
303: }
304: ValueBinding vb = getValueBinding("renderedOnUserRole");
305: return vb != null ? (String) vb.getValue(getFacesContext())
306: : null;
307: }
308:
309: /**
310: * <p>Return the value of the <code>rendered</code> property.</p>
311: */
312: public boolean isRendered() {
313: if (!Util.isRenderedOnUserRole(this )) {
314: return false;
315: }
316: return super .isRendered();
317: }
318:
319: /**
320: * <p>Set the value of the <code>styleClass</code> property.</p>
321: */
322: public void setStyleClass(String styleClass) {
323: this .styleClass = styleClass;
324: }
325:
326: /**
327: * <p>Return the value of the <code>styleClass</code> property.</p>
328: */
329: public String getStyleClass() {
330: return Util.getQualifiedStyleClass(this , styleClass,
331: CSS_DEFAULT.SELECT_ONE_MENU_DEFAULT_STYLE_CLASS,
332: "styleClass", isDisabled());
333: }
334:
335: /**
336: * <p>Return the value of the <code>disabled</code> property.</p>
337: */
338: public boolean isDisabled() {
339: if (!Util.isEnabledOnUserRole(this )) {
340: return true;
341: } else {
342: return super .isDisabled();
343: }
344: }
345:
346: /**
347: * <p>Return the value of the <code>onchangeeffect</code> property.</p>
348: */
349: public Effect getOnchangeeffect() {
350: if (onchangeeffect != null) {
351: return onchangeeffect;
352: }
353: ValueBinding vb = getValueBinding("onchangeeffect");
354:
355: return vb != null ? (Effect) vb.getValue(getFacesContext())
356: : null;
357: }
358:
359: /**
360: * <p>Set the value of the <code>onchangeeffect</code> property.</p>
361: */
362: public void setOnchangeeffect(Effect onchangeeffect) {
363: this .onchangeeffect = onchangeeffect;
364: JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
365: getFacesContext());
366: }
367:
368: /**
369: * <p>Return the value of the <code>currentStyle</code> property.</p>
370: */
371: public CurrentStyle getCurrentStyle() {
372: return currentStyle;
373: }
374:
375: /**
376: * <p>Set the value of the <code>currentStyle</code> property.</p>
377: */
378: public void setCurrentStyle(CurrentStyle currentStyle) {
379: this .currentStyle = currentStyle;
380: }
381:
382: /**
383: * <p>Set the value of the <code>onclickeffect</code> property.</p>
384: */
385: public void setOnclickeffect(Effect onclickeffect) {
386: this .onclickeffect = onclickeffect;
387: JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
388: getFacesContext());
389: }
390:
391: /**
392: * <p>Return the value of the <code>ondblclickeffect</code> property.</p>
393: */
394: public Effect getOndblclickeffect() {
395: if (ondblclickeffect != null) {
396: return ondblclickeffect;
397: }
398: ValueBinding vb = getValueBinding("ondblclickeffect");
399:
400: return vb != null ? (Effect) vb.getValue(getFacesContext())
401: : null;
402: }
403:
404: /**
405: * <p>Set the value of the <code>ondblclickeffect</code> property.</p>
406: */
407: public void setOndblclickeffect(Effect ondblclickeffect) {
408: this .ondblclickeffect = ondblclickeffect;
409: JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
410: getFacesContext());
411: }
412:
413: /**
414: * <p>Return the value of the <code>onmousedowneffect</code> property.</p>
415: */
416: public Effect getOnmousedowneffect() {
417: if (onmousedowneffect != null) {
418: return onmousedowneffect;
419: }
420: ValueBinding vb = getValueBinding("onmousedowneffect");
421:
422: return vb != null ? (Effect) vb.getValue(getFacesContext())
423: : null;
424: }
425:
426: /**
427: * <p>Set the value of the <code>onmousedowneffect</code> property.</p>
428: */
429: public void setOnmousedowneffect(Effect onmousedowneffect) {
430: this .onmousedowneffect = onmousedowneffect;
431: JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
432: getFacesContext());
433: }
434:
435: /**
436: * <p>Return the value of the <code>onmouseupeffect</code> property.</p>
437: */
438: public Effect getOnmouseupeffect() {
439: if (onmouseupeffect != null) {
440: return onmouseupeffect;
441: }
442: ValueBinding vb = getValueBinding("onmouseupeffect");
443:
444: return vb != null ? (Effect) vb.getValue(getFacesContext())
445: : null;
446: }
447:
448: /**
449: * <p>Set the value of the <code>onmouseupeffect</code> property.</p>
450: */
451: public void setOnmouseupeffect(Effect onmouseupeffect) {
452: this .onmouseupeffect = onmouseupeffect;
453: JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
454: getFacesContext());
455: }
456:
457: /**
458: * <p>Return the value of the <code>onmousemoveeffect</code> property.</p>
459: */
460: public Effect getOnmousemoveeffect() {
461: if (onmousemoveeffect != null) {
462: return onmousemoveeffect;
463: }
464: ValueBinding vb = getValueBinding("onmousemoveeffect");
465:
466: return vb != null ? (Effect) vb.getValue(getFacesContext())
467: : 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.includeLib(JavascriptContext.ICE_EXTRAS,
476: 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())
489: : null;
490: }
491:
492: /**
493: * <p>Set the value of the <code>onmouseovereffect</code> property.</p>
494: */
495: public void setOnmouseovereffect(Effect onmouseovereffect) {
496: this .onmouseovereffect = onmouseovereffect;
497: JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
498: getFacesContext());
499: }
500:
501: /**
502: * <p>Return the value of the <code>onmouseouteffect</code> property.</p>
503: */
504: public Effect getOnmouseouteffect() {
505: if (onmouseouteffect != null) {
506: return onmouseouteffect;
507: }
508: ValueBinding vb = getValueBinding("onmouseouteffect");
509:
510: return vb != null ? (Effect) vb.getValue(getFacesContext())
511: : null;
512: }
513:
514: /**
515: * <p>Set the value of the <code>onmouseouteffect</code> property.</p>
516: */
517: public void setOnmouseouteffect(Effect onmouseouteffect) {
518: this .onmouseouteffect = onmouseouteffect;
519: JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
520: getFacesContext());
521: }
522:
523: /**
524: * <p>Return the value of the <code>onkeypresseffect</code> property.</p>
525: */
526: public Effect getOnkeypresseffect() {
527: if (onkeypresseffect != null) {
528: return onkeypresseffect;
529: }
530: ValueBinding vb = getValueBinding("onkeypresseffect");
531:
532: return vb != null ? (Effect) vb.getValue(getFacesContext())
533: : null;
534: }
535:
536: /**
537: * <p>Set the value of the <code>onkeypresseffect</code> property.</p>
538: */
539: public void setOnkeypresseffect(Effect onkeypresseffect) {
540: this .onkeypresseffect = onkeypresseffect;
541: JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
542: getFacesContext());
543: }
544:
545: /**
546: * <p>Return the value of the <code>onkeydowneffect</code> property.</p>
547: */
548: public Effect getOnkeydowneffect() {
549: if (onkeydowneffect != null) {
550: return onkeydowneffect;
551: }
552: ValueBinding vb = getValueBinding("onkeydowneffect");
553:
554: return vb != null ? (Effect) vb.getValue(getFacesContext())
555: : null;
556: }
557:
558: /**
559: * <p>Set the value of the <code>onkeydowneffect</code> property.</p>
560: */
561: public void setOnkeydowneffect(Effect onkeydowneffect) {
562: this .onkeydowneffect = onkeydowneffect;
563: JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
564: getFacesContext());
565: }
566:
567: /**
568: * <p>Return the value of the <code>onkeyupeffect</code> property.</p>
569: */
570: public Effect getOnkeyupeffect() {
571: if (onkeyupeffect != null) {
572: return onkeyupeffect;
573: }
574: ValueBinding vb = getValueBinding("onkeyupeffect");
575:
576: return vb != null ? (Effect) vb.getValue(getFacesContext())
577: : null;
578: }
579:
580: /**
581: * <p>Set the value of the <code>onkeyupeffect</code> property.</p>
582: */
583: public void setOnkeyupeffect(Effect onkeyupeffect) {
584: this .onkeyupeffect = onkeyupeffect;
585: JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
586: getFacesContext());
587: }
588:
589: /**
590: * <p>Return the value of the <code>onkeyupeffect</code> property.</p>
591: */
592: public Effect getOnclickeffect() {
593: if (onclickeffect != null) {
594: return onclickeffect;
595: }
596: ValueBinding vb = getValueBinding("onclickeffect");
597:
598: return vb != null ? (Effect) vb.getValue(getFacesContext())
599: : null;
600: }
601:
602: private String autocomplete;
603:
604: /**
605: * <p>Set the value of the <code>autocomplete</code> property.</p>
606: */
607: public void setAutocomplete(String autocomplete) {
608: this .autocomplete = autocomplete;
609: }
610:
611: /**
612: * <p>Return the value of the <code>autocomplete</code> property.</p>
613: */
614: public String getAutocomplete() {
615: if (autocomplete != null) {
616: return autocomplete;
617: }
618: ValueBinding vb = getValueBinding("autocomplete");
619: return vb != null ? (String) vb.getValue(getFacesContext())
620: : null;
621: }
622:
623: /**
624: * <p>Gets the state of the instance as a <code>Serializable</code>
625: * Object.</p>
626: */
627: public Object saveState(FacesContext context) {
628: Object values[] = new Object[21];
629: values[0] = super .saveState(context);
630: values[1] = partialSubmit;
631: values[2] = enabledOnUserRole;
632: values[3] = renderedOnUserRole;
633: values[4] = styleClass;
634: values[5] = effect;
635: values[13] = onchangeeffect;
636: values[19] = currentStyle;
637: values[20] = visible;
638: return ((Object) (values));
639: }
640:
641: /**
642: * <p>Perform any processing required to restore the state from the entries
643: * in the state Object.</p>
644: */
645: public void restoreState(FacesContext context, Object state) {
646: Object values[] = (Object[]) state;
647: super .restoreState(context, values[0]);
648: partialSubmit = (Boolean) values[1];
649: enabledOnUserRole = (String) values[2];
650: renderedOnUserRole = (String) values[3];
651: styleClass = (String) values[4];
652: effect = (Effect) values[5];
653: onchangeeffect = (Effect) values[13];
654: currentStyle = (CurrentStyle) values[19];
655: visible = (Boolean) values[20];
656: }
657: }
|