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.application.*;
035: import javax.faces.context.*;
036: import javax.faces.model.SelectItem;
037: import javax.faces.model.SelectItemGroup;
038:
039: public class UISelectMany extends UIInput {
040: public static final String COMPONENT_FAMILY = "javax.faces.SelectMany";
041: public static final String COMPONENT_TYPE = "javax.faces.SelectMany";
042: public static final String INVALID_MESSAGE_ID = "javax.faces.component.UISelectMany.INVALID";
043:
044: public UISelectMany() {
045: setRendererType("javax.faces.Listbox");
046: }
047:
048: /**
049: * Returns the component family, used to select the renderer.
050: */
051: public String getFamily() {
052: return COMPONENT_FAMILY;
053: }
054:
055: //
056: // properties
057: //
058:
059: public Object[] getSelectedValues() {
060: Object value = super .getValue();
061:
062: if (value instanceof String)
063: return new Object[] { value };
064: else
065: return (Object[]) value;
066: }
067:
068: public void setSelectedValues(Object[] value) {
069: super .setValue(value);
070: }
071:
072: /**
073: * Returns the value expression with the given name.
074: */
075: @Override
076: public ValueExpression getValueExpression(String name) {
077: if ("selectedValues".equals(name))
078: return super .getValueExpression("value");
079: else
080: return super .getValueExpression(name);
081: }
082:
083: /**
084: * Sets the value expression with the given name.
085: */
086: @Override
087: public void setValueExpression(String name, ValueExpression expr) {
088: if ("selectedValues".equals(name)) {
089: super .setValueExpression("value", expr);
090: } else
091: super .setValueExpression(name, expr);
092: }
093:
094: //
095: // validate
096: //
097:
098: public void validateValue(FacesContext context, Object value) {
099: super .validateValue(context, value);
100:
101: if (!isValid())
102: return;
103:
104: boolean hasValue = false;
105:
106: ValueExpression ve = getValueExpression("value");
107:
108: Class type = null;
109: if (ve != null) {
110: type = ve.getType(context.getELContext());
111: if (type != null)
112: type = type.getComponentType();
113: }
114:
115: ExpressionFactory exprFactory = context.getApplication()
116: .getExpressionFactory();
117:
118: if (value instanceof Object[]) {
119: Object[] values = (Object[]) value;
120:
121: for (int i = 0; i < values.length; i++) {
122: hasValue = UISelectOne.matchChildren(exprFactory, this ,
123: values[i], type);
124:
125: if (!hasValue)
126: break;
127: }
128: }
129:
130: if (!hasValue) {
131: setValid(false);
132:
133: String summary = Util
134: .l10n(
135: context,
136: INVALID_MESSAGE_ID,
137: "{0}: Validation Error: UISelectMany value {1} is not valid.",
138: Util.getLabel(context, this ), value);
139:
140: String detail = summary;
141:
142: FacesMessage msg = new FacesMessage(summary, detail);
143:
144: context.addMessage(getClientId(context), msg);
145: }
146: }
147:
148: @Override
149: protected boolean compareValues(Object oldValue, Object newValue) {
150: if (oldValue == newValue)
151: return false;
152: else if (oldValue == null || newValue == null)
153: return true;
154:
155: Object[] oldValues = (Object[]) oldValue;
156: Object[] newValues = (Object[]) newValue;
157:
158: if (oldValues.length != newValues.length)
159: return true;
160:
161: for (int i = 0; i < oldValues.length; i++) {
162: if (!oldValues[i].equals(newValues[i]))
163: return true;
164: }
165:
166: return false;
167: }
168: }
|