001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package javax.faces.component;
030:
031: import java.util.*;
032:
033: import javax.el.*;
034: import javax.faces.context.*;
035: import javax.faces.model.*;
036:
037: public class UISelectItem extends UIComponentBase {
038: public static final String COMPONENT_FAMILY = "javax.faces.SelectItem";
039: public static final String COMPONENT_TYPE = "javax.faces.SelectItem";
040:
041: private static final HashMap<String, PropEnum> _propMap = new HashMap<String, PropEnum>();
042:
043: private String _itemDescription;
044: private ValueExpression _itemDescriptionExpr;
045:
046: private Boolean _itemDisabled;
047: private ValueExpression _itemDisabledExpr;
048:
049: private Boolean _itemEscaped;
050: private ValueExpression _itemEscapedExpr;
051:
052: private String _itemLabel;
053: private ValueExpression _itemLabelExpr;
054:
055: private Object _itemValue;
056: private ValueExpression _itemValueExpr;
057:
058: private Object _value;
059: private ValueExpression _valueExpr;
060:
061: public UISelectItem() {
062: }
063:
064: /**
065: * Returns the component family, used to select the renderer.
066: */
067: public String getFamily() {
068: return COMPONENT_FAMILY;
069: }
070:
071: //
072: // properties
073: //
074:
075: public String getItemDescription() {
076: if (_itemDescription != null)
077: return _itemDescription;
078: else if (_itemDescriptionExpr != null)
079: return Util.evalString(_itemDescriptionExpr,
080: getFacesContext());
081: else
082: return null;
083: }
084:
085: public void setItemDescription(String value) {
086: _itemDescription = value;
087: }
088:
089: public boolean isItemDisabled() {
090: if (_itemDisabled != null)
091: return _itemDisabled;
092: else if (_itemDisabledExpr != null)
093: return Util.evalBoolean(_itemDisabledExpr,
094: getFacesContext());
095: else
096: return false;
097: }
098:
099: public void setItemDisabled(boolean value) {
100: _itemDisabled = value;
101: }
102:
103: public boolean isItemEscaped() {
104: if (_itemEscaped != null)
105: return _itemEscaped;
106: else if (_itemEscapedExpr != null)
107: return Util
108: .evalBoolean(_itemEscapedExpr, getFacesContext());
109: else
110: return true;
111: }
112:
113: public void setItemEscaped(boolean value) {
114: _itemEscaped = value;
115: }
116:
117: public String getItemLabel() {
118: if (_itemLabel != null)
119: return _itemLabel;
120: else if (_itemLabelExpr != null)
121: return Util.evalString(_itemLabelExpr, getFacesContext());
122: else
123: return null;
124: }
125:
126: public void setItemLabel(String value) {
127: _itemLabel = value;
128: }
129:
130: public Object getItemValue() {
131: if (_itemValue != null)
132: return _itemValue;
133: else if (_itemValueExpr != null)
134: return Util.eval(_itemValueExpr, getFacesContext());
135: else
136: return null;
137: }
138:
139: public void setItemValue(Object value) {
140: _itemValue = value;
141: }
142:
143: public Object getValue() {
144: if (_value != null)
145: return _value;
146: else if (_valueExpr != null)
147: return Util.eval(_valueExpr, getFacesContext());
148: else
149: return null;
150: }
151:
152: public void setValue(Object value) {
153: _value = value;
154: }
155:
156: /**
157: * Returns the value expression with the given name.
158: */
159: @Override
160: public ValueExpression getValueExpression(String name) {
161: PropEnum prop = _propMap.get(name);
162:
163: if (prop != null) {
164: switch (prop) {
165: case ITEM_DESCRIPTION:
166: return _itemDescriptionExpr;
167: case ITEM_DISABLED:
168: return _itemDisabledExpr;
169: case ITEM_ESCAPED:
170: return _itemEscapedExpr;
171: case ITEM_LABEL:
172: return _itemLabelExpr;
173: case ITEM_VALUE:
174: return _itemValueExpr;
175: case VALUE:
176: return _valueExpr;
177: }
178: }
179:
180: return super .getValueExpression(name);
181: }
182:
183: /**
184: * Sets the value expression with the given name.
185: */
186: @Override
187: public void setValueExpression(String name, ValueExpression expr) {
188: PropEnum prop = _propMap.get(name);
189:
190: if (prop != null) {
191: switch (prop) {
192: case ITEM_DESCRIPTION:
193: if (expr != null && expr.isLiteralText()) {
194: _itemDescription = String.valueOf(expr
195: .getValue(null));
196: return;
197: } else
198: _itemDescriptionExpr = expr;
199: break;
200:
201: case ITEM_DISABLED:
202: if (expr != null && expr.isLiteralText()) {
203: _itemDisabled = Util.booleanValueOf(expr
204: .getValue(null));
205: return;
206: } else
207: _itemDisabledExpr = expr;
208: break;
209:
210: case ITEM_ESCAPED:
211: if (expr != null && expr.isLiteralText()) {
212: _itemEscaped = Util.booleanValueOf(expr
213: .getValue(null));
214: return;
215: } else
216: _itemEscapedExpr = expr;
217: break;
218:
219: case ITEM_LABEL:
220: if (expr != null && expr.isLiteralText()) {
221: _itemLabel = String.valueOf(expr.getValue(null));
222: return;
223: } else
224: _itemLabelExpr = expr;
225: break;
226:
227: case ITEM_VALUE:
228: if (expr != null && expr.isLiteralText()) {
229: _itemValue = expr.getValue(null);
230: return;
231: } else
232: _itemValueExpr = expr;
233: break;
234:
235: case VALUE:
236: if (expr != null && expr.isLiteralText()) {
237: _value = expr.getValue(null);
238: return;
239: } else
240: _valueExpr = expr;
241: break;
242: }
243: }
244:
245: super .setValueExpression(name, expr);
246: }
247:
248: //
249: // state
250: //
251:
252: public Object saveState(FacesContext context) {
253: Object[] state = new Object[] { super .saveState(context),
254: _itemDescription, _itemDisabled, _itemEscaped,
255: _itemLabel, _itemValue, _value };
256:
257: return state;
258: }
259:
260: public void restoreState(FacesContext context, Object value) {
261: Object[] state = (Object[]) value;
262:
263: super .restoreState(context, state[0]);
264:
265: _itemDescription = (String) state[1];
266: _itemDisabled = (Boolean) state[2];
267: _itemEscaped = (Boolean) state[3];
268: _itemLabel = (String) state[4];
269: _itemValue = state[5];
270: _value = state[6];
271: }
272:
273: private enum PropEnum {
274: ITEM_DESCRIPTION, ITEM_DISABLED, ITEM_ESCAPED, ITEM_LABEL, ITEM_VALUE, VALUE,
275: }
276:
277: static {
278: _propMap.put("itemDescription", PropEnum.ITEM_DESCRIPTION);
279: _propMap.put("itemDisabled", PropEnum.ITEM_DISABLED);
280: _propMap.put("itemEscaped", PropEnum.ITEM_ESCAPED);
281: _propMap.put("itemLabel", PropEnum.ITEM_LABEL);
282: _propMap.put("itemValue", PropEnum.ITEM_VALUE);
283: _propMap.put("value", PropEnum.VALUE);
284: }
285: }
|