KickJava   Java API By Example, From Geeks To Geeks.

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


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.Converter;
21 import javax.faces.convert.ConverterException;
22 import javax.faces.el.EvaluationException;
23 import javax.faces.el.MethodBinding;
24 import javax.faces.el.ValueBinding;
25 import javax.faces.event.AbortProcessingException;
26 import javax.faces.event.FacesEvent;
27 import javax.faces.event.ValueChangeEvent;
28 import javax.faces.event.ValueChangeListener;
29 import javax.faces.render.Renderer;
30 import javax.faces.validator.Validator;
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  * see Javadoc of JSF Specification
36  *
37  * @author Manfred Geiler (latest modification by $Author: manolito $)
38  * @version $Revision: 1.17 $ $Date: 2005/04/12 12:41:48 $
39  * $Log: UIInput.java,v $
40  * Revision 1.17 2005/04/12 12:41:48 manolito
41  * PR: MYFACES-179
42  * Submitted by: Mathias Broekelmann
43  *
44  * Revision 1.16 2005/03/04 00:28:45 mmarinschek
45  * 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
46  *
47  * Revision 1.15 2005/01/22 16:47:17 mmarinschek
48  * fixing bug with validation not called if the submitted value is empty; an empty string is submitted instead if the component is enabled.
49  *
50  * Revision 1.14 2004/07/01 22:00:50 mwessendorf
51  * ASF switch
52  *
53  * Revision 1.13 2004/06/07 13:40:38 mwessendorf
54  * solved Feature Request #966892
55  *
56  * Revision 1.12 2004/05/18 10:39:35 manolito
57  * (re)set to valid on decode, so that component automatically gets (re)validated
58  *
59  * Revision 1.11 2004/04/16 15:13:33 manolito
60  * validator attribute support and MethodBinding invoke exception handling fixed
61  *
62  * Revision 1.10 2004/04/06 13:03:35 manolito
63  * x-checked getConvertedValue method in api and impl
64  *
65  */

66 public class UIInput
67         extends UIOutput
68         implements EditableValueHolder
69 {
70     public static final String JavaDoc CONVERSION_MESSAGE_ID = "javax.faces.component.UIInput.CONVERSION";
71     public static final String JavaDoc REQUIRED_MESSAGE_ID = "javax.faces.component.UIInput.REQUIRED";
72
73     private static final Validator[] EMPTY_VALIDATOR_ARRAY = new Validator[0];
74
75     private Object JavaDoc _submittedValue = null;
76     private boolean _localValueSet = false;
77     private boolean _valid = true;
78     private MethodBinding _validator = null;
79     private MethodBinding _valueChangeListener = null;
80     private List JavaDoc _validatorList = null;
81
82     public Object JavaDoc getSubmittedValue()
83     {
84         return _submittedValue;
85     }
86
87     public void setSubmittedValue(Object JavaDoc submittedValue)
88     {
89         _submittedValue = submittedValue;
90     }
91
92     public void setValue(Object JavaDoc value)
93     {
94         setLocalValueSet(true);
95         super.setValue(value);
96     }
97
98     public boolean isLocalValueSet()
99     {
100         return _localValueSet;
101     }
102
103     public void setLocalValueSet(boolean localValueSet)
104     {
105         _localValueSet = localValueSet;
106     }
107
108     public boolean isValid()
109     {
110         return _valid;
111     }
112
113     public void setValid(boolean valid)
114     {
115         _valid = valid;
116     }
117
118     public MethodBinding getValidator()
119     {
120         return _validator;
121     }
122
123     public void setValidator(MethodBinding validator)
124     {
125         _validator = validator;
126     }
127
128     public MethodBinding getValueChangeListener()
129     {
130         return _valueChangeListener;
131     }
132
133     public void setValueChangeListener(MethodBinding valueChangeListener)
134     {
135         _valueChangeListener = valueChangeListener;
136     }
137
138     public void processDecodes(FacesContext context)
139     {
140         if (context == null) throw new NullPointerException JavaDoc("context");
141         if (!isRendered()) return;
142         super.processDecodes(context);
143         if (isImmediate())
144         {
145             try
146             {
147                 validate(context);
148             }
149             catch (RuntimeException JavaDoc e)
150             {
151                 context.renderResponse();
152                 throw e;
153             }
154             if (!isValid())
155             {
156                 context.renderResponse();
157             }
158         }
159     }
160
161     public void processValidators(FacesContext context)
162     {
163         if (context == null) throw new NullPointerException JavaDoc("context");
164         if (!isRendered()) return;
165         super.processValidators(context);
166         if (!isImmediate())
167         {
168             try
169             {
170                 validate(context);
171             }
172             catch (RuntimeException JavaDoc e)
173             {
174                 context.renderResponse();
175                 throw e;
176             }
177             if (!isValid())
178             {
179                 context.renderResponse();
180             }
181         }
182     }
183
184     public void processUpdates(FacesContext context)
185     {
186         if (context == null) throw new NullPointerException JavaDoc("context");
187         if (!isRendered()) return;
188         super.processUpdates(context);
189         try
190         {
191             updateModel(context);
192         }
193         catch (RuntimeException JavaDoc e)
194         {
195             context.renderResponse();
196             throw e;
197         }
198         if (!isValid())
199         {
200             context.renderResponse();
201         }
202     }
203
204     public void decode(FacesContext context)
205     {
206         //We (re)set to valid, so that component automatically gets (re)validated
207
setValid(true);
208         super.decode(context);
209     }
210
211     public void broadcast(FacesEvent event)
212             throws AbortProcessingException
213     {
214         if (!(event instanceof ValueChangeEvent))
215         {
216             throw new IllegalArgumentException JavaDoc("FacesEvent of class " + event.getClass().getName() + " not supported by UIInput");
217         }
218
219         super.broadcast(event);
220
221         MethodBinding valueChangeListenerBinding = getValueChangeListener();
222         if (valueChangeListenerBinding != null)
223         {
224             try
225             {
226                 valueChangeListenerBinding.invoke(getFacesContext(),
227                                                   new Object JavaDoc[]{event});
228             }
229             catch (EvaluationException e)
230             {
231                 Throwable JavaDoc cause = e.getCause();
232                 if (cause != null && cause instanceof AbortProcessingException)
233                 {
234                     throw (AbortProcessingException)cause;
235                 }
236                 else
237                 {
238                     throw e;
239                 }
240             }
241         }
242     }
243
244     public void updateModel(FacesContext context)
245     {
246         if (!isValid()) return;
247         if (!isLocalValueSet()) return;
248         ValueBinding vb = getValueBinding("value");
249         if (vb == null) return;
250         try
251         {
252             vb.setValue(context, getLocalValue());
253             setValue(null);
254             setLocalValueSet(false);
255         }
256         catch (RuntimeException JavaDoc e)
257         {
258             //Object[] args = {getId()};
259
context.getExternalContext().log(e.getMessage(), e);
260             _MessageUtils.addErrorMessage(context, this,CONVERSION_MESSAGE_ID,new Object JavaDoc[]{getId()});
261             setValid(false);
262         }
263     }
264
265     protected void validateValue(FacesContext context,Object JavaDoc convertedValue)
266     {
267         boolean empty = convertedValue == null ||
268                         (convertedValue instanceof String JavaDoc
269                          && ((String JavaDoc)convertedValue).length() == 0);
270
271         if (isRequired() && empty)
272         {
273             _MessageUtils.addErrorMessage(context, this, REQUIRED_MESSAGE_ID,new Object JavaDoc[]{getId()});
274             setValid(false);
275             return;
276         }
277
278         if (!empty)
279         {
280             _ComponentUtils.callValidators(context, this, convertedValue);
281         }
282
283     }
284
285     public void validate(FacesContext context)
286     {
287         if (context == null) throw new NullPointerException JavaDoc("context");
288         Object JavaDoc submittedValue = getSubmittedValue();
289         if (submittedValue == null) return;
290
291         if(submittedValue instanceof String JavaDoc && ((String JavaDoc) submittedValue).length()==0)
292         {
293             submittedValue = null;
294         }
295
296         Object JavaDoc convertedValue = getConvertedValue(context, submittedValue);
297
298         if (!isValid()) return;
299
300         validateValue(context, convertedValue);
301
302         if (!isValid()) return;
303
304         Object JavaDoc previousValue = getValue();
305         setValue(convertedValue);
306         setSubmittedValue(null);
307         if (compareValues(previousValue, convertedValue))
308         {
309             queueEvent(new ValueChangeEvent(this, previousValue, convertedValue));
310         }
311     }
312
313     protected Object JavaDoc getConvertedValue(FacesContext context, Object JavaDoc submittedValue)
314     {
315         try
316         {
317             Renderer renderer = getRenderer(context);
318             if (renderer != null)
319             {
320                 return renderer.getConvertedValue(context, this, submittedValue);
321             }
322             else if (submittedValue instanceof String JavaDoc)
323             {
324                 Converter converter = _SharedRendererUtils.findUIOutputConverter(context, this);
325                 if (converter != null)
326                 {
327                     return converter.getAsObject(context, this, (String JavaDoc)submittedValue);
328                 }
329             }
330         }
331         catch (ConverterException e)
332         {
333             FacesMessage facesMessage = e.getFacesMessage();
334             if (facesMessage != null)
335             {
336                 context.addMessage(getClientId(context), facesMessage);
337             }
338             else
339             {
340                 _MessageUtils.addErrorMessage(context, this, CONVERSION_MESSAGE_ID,new Object JavaDoc[]{getId()});
341             }
342             setValid(false);
343         }
344         return submittedValue;
345     }
346
347
348
349     protected boolean compareValues(Object JavaDoc previous,
350                                     Object JavaDoc value)
351     {
352         return !((previous == null && value == null) ||
353                  (previous != null && value != null && previous.equals(value)));
354     }
355
356     public void addValidator(Validator validator)
357     {
358         if (validator == null) throw new NullPointerException JavaDoc("validator");
359         if (_validatorList == null)
360         {
361             _validatorList = new ArrayList JavaDoc();
362         }
363         _validatorList.add(validator);
364     }
365
366     public Validator[] getValidators()
367     {
368         return _validatorList != null ?
369                (Validator[])_validatorList.toArray(new Validator[_validatorList.size()]) :
370                EMPTY_VALIDATOR_ARRAY;
371     }
372
373     public void removeValidator(Validator validator)
374     {
375         if (validator == null) throw new NullPointerException JavaDoc("validator");
376         if (_validatorList != null)
377         {
378             _validatorList.remove(validator);
379         }
380     }
381
382     public void addValueChangeListener(ValueChangeListener listener)
383     {
384         addFacesListener(listener);
385     }
386
387     public ValueChangeListener[] getValueChangeListeners()
388     {
389         return (ValueChangeListener[])getFacesListeners(ValueChangeListener.class);
390     }
391
392     public void removeValueChangeListener(ValueChangeListener listener)
393     {
394         removeFacesListener(listener);
395     }
396
397     public Object JavaDoc saveState(FacesContext context)
398     {
399         Object JavaDoc values[] = new Object JavaDoc[9];
400         values[0] = super.saveState(context);
401         values[1] = _immediate;
402         values[2] = Boolean.valueOf(_localValueSet);
403         values[3] = _required;
404         values[4] = _submittedValue;
405         values[5] = Boolean.valueOf(_valid);
406         values[6] = saveAttachedState(context, _validator);
407         values[7] = saveAttachedState(context, _valueChangeListener);
408         values[8] = saveAttachedState(context, _validatorList);
409         return ((Object JavaDoc)(values));
410     }
411
412     public void restoreState(FacesContext context, Object JavaDoc state)
413     {
414         Object JavaDoc values[] = (Object JavaDoc[])state;
415         super.restoreState(context, values[0]);
416         _immediate = (Boolean JavaDoc)values[1];
417         _localValueSet = ((Boolean JavaDoc)values[2]).booleanValue();
418         _required = (Boolean JavaDoc)values[3];
419         _submittedValue = (Object JavaDoc)values[4];
420         _valid = ((Boolean JavaDoc)values[5]).booleanValue();
421         _validator = (MethodBinding)restoreAttachedState(context, values[6]);
422         _valueChangeListener = (MethodBinding)restoreAttachedState(context, values[7]);
423         _validatorList = (List JavaDoc)restoreAttachedState(context, values[8]);
424     }
425
426
427     //------------------ GENERATED CODE BEGIN (do not modify!) --------------------
428

429     public static final String JavaDoc COMPONENT_TYPE = "javax.faces.Input";
430     public static final String JavaDoc COMPONENT_FAMILY = "javax.faces.Input";
431     private static final String JavaDoc DEFAULT_RENDERER_TYPE = "javax.faces.Text";
432     private static final boolean DEFAULT_IMMEDIATE = false;
433     private static final boolean DEFAULT_REQUIRED = false;
434
435     private Boolean JavaDoc _immediate = null;
436     private Boolean JavaDoc _required = null;
437
438     public UIInput()
439     {
440         setRendererType(DEFAULT_RENDERER_TYPE);
441     }
442
443     public String JavaDoc getFamily()
444     {
445         return COMPONENT_FAMILY;
446     }
447
448     public void setImmediate(boolean immediate)
449     {
450         _immediate = Boolean.valueOf(immediate);
451     }
452
453     public boolean isImmediate()
454     {
455         if (_immediate != null) return _immediate.booleanValue();
456         ValueBinding vb = getValueBinding("immediate");
457         Boolean JavaDoc v = vb != null ? (Boolean JavaDoc)vb.getValue(getFacesContext()) : null;
458         return v != null ? v.booleanValue() : DEFAULT_IMMEDIATE;
459     }
460
461     public void setRequired(boolean required)
462     {
463         _required = Boolean.valueOf(required);
464     }
465
466     public boolean isRequired()
467     {
468         if (_required != null) return _required.booleanValue();
469         ValueBinding vb = getValueBinding("required");
470         Boolean JavaDoc v = vb != null ? (Boolean JavaDoc)vb.getValue(getFacesContext()) : null;
471         return v != null ? v.booleanValue() : DEFAULT_REQUIRED;
472     }
473
474
475     //------------------ GENERATED CODE END ---------------------------------------
476
}
477
Popular Tags