KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > component > UISelectMany


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package javax.faces.component;
17
18 import javax.faces.application.FacesMessage;
19 import javax.faces.context.FacesContext;
20 import javax.faces.convert.ConverterException;
21 import javax.faces.el.ValueBinding;
22 import javax.faces.event.ValueChangeEvent;
23 import javax.faces.render.Renderer;
24 import java.lang.reflect.Array JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * see Javadoc of JSF Specification
29  *
30  * @author Manfred Geiler (latest modification by $Author: manolito $)
31  * @version $Revision: 1.18 $ $Date: 2005/04/06 10:21:55 $
32  * $Log: UISelectMany.java,v $
33  * Revision 1.18 2005/04/06 10:21:55 manolito
34  * MYFACES-149 fix for NullPointerException in _SharedRendererUtils.getConvertedUISelectManyValue
35  *
36  * Revision 1.17 2005/03/04 00:41:40 mmarinschek
37  * fixed myfaces-
38  * 116
39  *
40  * Revision 1.16 2005/03/04 00:28:45 mmarinschek
41  * Changes in configuration due to missing Attribute/Property classes for the converter; not building in the functionality yet except for part of the converter properties
42  *
43  * Revision 1.15 2005/01/23 22:06:33 svieujot
44  * Bugfix : When no value were submitted, default values were set.
45  * This caused a bug when the component wasn't displayed (for example if it was in a TabPanel's Tab that wasn't displayed).
46  * It was reseting the backend bean's value, and sometime causing Null Pointer Exceptions.
47  *
48  * Revision 1.14 2005/01/22 16:47:17 mmarinschek
49  * fixing bug with validation not called if the submitted value is empty; an empty string is submitted instead if the component is enabled.
50  *
51  * Revision 1.13 2004/12/07 21:33:31 matzew
52  * closing MYFACES-6, thanks to Heath Borders-Wing for patching it!
53  *
54  * Revision 1.12 2004/07/01 22:00:50 mwessendorf
55  * ASF switch
56  *
57  * Revision 1.11 2004/06/14 12:55:21 manolito
58  * Added missing CVS Log comment
59  *
60  */

61 public class UISelectMany
62         extends UIInput
63 {
64     public static final String JavaDoc INVALID_MESSAGE_ID = "javax.faces.component.UISelectMany.INVALID";
65
66     public Object JavaDoc[] getSelectedValues()
67     {
68         return (Object JavaDoc[])getValue();
69     }
70
71     public void setSelectedValues(Object JavaDoc[] selectedValues)
72     {
73         setValue(selectedValues);
74     }
75
76     public ValueBinding getValueBinding(String JavaDoc name)
77     {
78         if (name == null) throw new NullPointerException JavaDoc("name");
79         if (name.equals("selectedValues"))
80         {
81             return super.getValueBinding("value");
82         }
83         else
84         {
85             return super.getValueBinding(name);
86         }
87     }
88
89     public void setValueBinding(String JavaDoc name,
90                                 ValueBinding binding)
91     {
92         if (name == null) throw new NullPointerException JavaDoc("name");
93         if (name.equals("selectedValues"))
94         {
95             super.setValueBinding("value", binding);
96         }
97         else
98         {
99             super.setValueBinding(name, binding);
100         }
101     }
102
103     /**
104      * @return true if Objects are different (!)
105      */

106     protected boolean compareValues(Object JavaDoc previous,
107                                     Object JavaDoc value)
108     {
109         if (previous == null)
110         {
111             // one is null, the other not
112
return value != null;
113         }
114         else if (value == null)
115         {
116             // one is null, the other not
117
return previous != null;
118         }
119         else
120         {
121             if (previous instanceof Object JavaDoc[] &&
122                 value instanceof Object JavaDoc[])
123             {
124                 return compareObjectArrays((Object JavaDoc[])previous, (Object JavaDoc[])value);
125             }
126             else if (previous instanceof List JavaDoc &&
127                      value instanceof List JavaDoc)
128             {
129                 return compareLists((List JavaDoc)previous, (List JavaDoc)value);
130             }
131             else if (previous.getClass().isArray() &&
132                      value.getClass().isArray())
133             {
134                 return comparePrimitiveArrays(previous, value);
135             }
136             else
137             {
138                 //Objects have different classes
139
return true;
140             }
141         }
142     }
143
144     private boolean compareObjectArrays(Object JavaDoc[] previous,
145                                         Object JavaDoc[] value)
146     {
147         int length = ((Object JavaDoc[])value).length;
148         if (((Object JavaDoc[])previous).length != length)
149         {
150             //different length
151
return true;
152         }
153
154         boolean[] scoreBoard = new boolean[length];
155         for (int i = 0; i < length; i++)
156         {
157             Object JavaDoc p = previous[i];
158             boolean found = false;
159             for (int j = 0; j < length; j++)
160             {
161                 if (scoreBoard[j] == false)
162                 {
163                     Object JavaDoc v = value[j];
164                     if ((p == null && v == null) ||
165                         (p != null && v != null && p.equals(v)))
166                     {
167                         scoreBoard[j] = true;
168                         found = true;
169                         break;
170                     }
171                 }
172             }
173             if (!found)
174             {
175                 return true; //current element of previous array not found in new array
176
}
177         }
178
179         return false; // arrays are identical
180
}
181
182     private boolean compareLists(List JavaDoc previous, List JavaDoc value)
183     {
184         int length = value.size();
185         if (previous.size() != length)
186         {
187             //different length
188
return true;
189         }
190
191         boolean[] scoreBoard = new boolean[length];
192         for (int i = 0; i < length; i++)
193         {
194             Object JavaDoc p = previous.get(i);
195             boolean found = false;
196             for (int j = 0; j < length; j++)
197             {
198                 if (scoreBoard[j] == false)
199                 {
200                     Object JavaDoc v = value.get(j);
201                     if ((p == null && v == null) ||
202                         (p != null && v != null && p.equals(v)))
203                     {
204                         scoreBoard[j] = true;
205                         found = true;
206                         break;
207                     }
208                 }
209             }
210             if (!found)
211             {
212                 return true; //current element of previous List not found in new List
213
}
214         }
215
216         return false; // Lists are identical
217
}
218
219     private boolean comparePrimitiveArrays(Object JavaDoc previous, Object JavaDoc value)
220     {
221         int length = Array.getLength(value);
222         if (Array.getLength(previous) != length)
223         {
224             //different length
225
return true;
226         }
227
228         boolean[] scoreBoard = new boolean[length];
229         for (int i = 0; i < length; i++)
230         {
231             Object JavaDoc p = Array.get(previous, i);
232             boolean found = false;
233             for (int j = 0; j < length; j++)
234             {
235                 if (scoreBoard[j] == false)
236                 {
237                     Object JavaDoc v = Array.get(value, j);
238                     if ((p == null && v == null) ||
239                         (p != null && v != null && p.equals(v)))
240                     {
241                         scoreBoard[j] = true;
242                         found = true;
243                         break;
244                     }
245                 }
246             }
247             if (!found)
248             {
249                 return true; //current element of previous array not found in new array
250
}
251         }
252
253         return false; // arrays are identical
254
}
255
256     protected void validateValue(FacesContext context, Object JavaDoc convertedValue)
257     {
258         boolean empty =
259             convertedValue == null
260                 || ((convertedValue instanceof Object JavaDoc[]) && (((Object JavaDoc[]) convertedValue).length == 0))
261                 || ((convertedValue instanceof List JavaDoc) && ((List JavaDoc) convertedValue).isEmpty());
262         if (isRequired() && empty)
263         {
264             _MessageUtils.addErrorMessage(context, this, REQUIRED_MESSAGE_ID,new Object JavaDoc[]{getId()});
265             setValid(false);
266             return;
267         }
268
269         if (!empty)
270         {
271             _ComponentUtils.callValidators(context, this, convertedValue);
272         }
273
274         //TODO: see javadoc: iterate through UISelectItem and UISelectItems and check
275
//current values against these items
276
}
277
278     /**
279      * First part is identical to super.validate except the empty condition.
280      * Second part: iterate through UISelectItem and UISelectItems and check
281      * current values against these items
282      */

283     public void validate(FacesContext context)
284     {
285         // TODO : Setting the submitted value to null in the super class causes a bug, if set to null, you'll get the following error :
286
// java.lang.NullPointerException at org.apache.myfaces.renderkit._SharedRendererUtils.getConvertedUISelectManyValue(_SharedRendererUtils.java:118)
287
super.validate(context);
288     }
289
290
291     protected Object JavaDoc getConvertedValue(FacesContext context, Object JavaDoc submittedValue)
292     {
293         try
294         {
295             Renderer renderer = getRenderer(context);
296             if (renderer != null)
297             {
298                 return renderer.getConvertedValue(context, this, submittedValue);
299             }
300             else if (submittedValue == null)
301             {
302                 return null;
303             }
304             else if (submittedValue instanceof String JavaDoc[])
305             {
306                 return _SharedRendererUtils.getConvertedUISelectManyValue(context, this,
307                                                                           (String JavaDoc[])submittedValue);
308             }
309         }
310         catch (ConverterException e)
311         {
312             FacesMessage facesMessage = e.getFacesMessage();
313             if (facesMessage != null)
314             {
315                 context.addMessage(getClientId(context), facesMessage);
316             }
317             else
318             {
319                 _MessageUtils.addErrorMessage(context, this, CONVERSION_MESSAGE_ID,new Object JavaDoc[]{getId()});
320             }
321             setValid(false);
322         }
323         return submittedValue;
324     }
325
326
327
328
329     //------------------ GENERATED CODE BEGIN (do not modify!) --------------------
330

331     public static final String JavaDoc COMPONENT_TYPE = "javax.faces.SelectMany";
332     public static final String JavaDoc COMPONENT_FAMILY = "javax.faces.SelectMany";
333     private static final String JavaDoc DEFAULT_RENDERER_TYPE = "javax.faces.Listbox";
334
335
336     public UISelectMany()
337     {
338         setRendererType(DEFAULT_RENDERER_TYPE);
339     }
340
341     public String JavaDoc getFamily()
342     {
343         return COMPONENT_FAMILY;
344     }
345
346
347     //------------------ GENERATED CODE END ---------------------------------------
348
}
349
Popular Tags