KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > renderkit > RendererUtils


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 org.apache.myfaces.renderkit;
17
18 import org.apache.myfaces.util.HashMapUtils;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import javax.faces.FacesException;
24 import javax.faces.render.Renderer;
25 import javax.faces.component.*;
26 import javax.faces.component.html.HtmlInputText;
27 import javax.faces.context.FacesContext;
28 import javax.faces.convert.Converter;
29 import javax.faces.convert.ConverterException;
30 import javax.faces.el.ValueBinding;
31 import javax.faces.el.PropertyNotFoundException;
32 import javax.faces.model.SelectItem;
33 import java.io.IOException JavaDoc;
34 import java.lang.reflect.Array JavaDoc;
35 import java.util.*;
36
37 /**
38  * @author Manfred Geiler (latest modification by $Author: manolito $)
39  * @version $Revision: 1.23 $ $Date: 2005/04/06 10:21:55 $
40  * $Log: RendererUtils.java,v $
41  * Revision 1.23 2005/04/06 10:21:55 manolito
42  * MYFACES-149 fix for NullPointerException in _SharedRendererUtils.getConvertedUISelectManyValue
43  *
44  * Revision 1.22 2005/01/26 13:27:16 mmarinschek
45  * The x:message tags are now extended to use the column-name as a label for all inputs in an x:dataTable, without having to specify additional information.
46  *
47  * Revision 1.21 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.20 2005/01/18 22:43:05 svieujot
51  * Fix some bugs where converter wasn't used to determine selected values.
52  * This caused for examples the list, checkbox and radio based components to bug when the backing bean value type is a primitive.
53  *
54  * Revision 1.19 2005/01/09 18:15:12 mmarinschek
55  * small changes - better error handling, label renderer supports more hooks for sub-classes
56  *
57  * Revision 1.18 2005/01/07 01:54:35 svieujot
58  * radioRenderer wasn't looking at the submitted value.
59  *
60  * Revision 1.17 2005/01/05 23:06:43 svieujot
61  * Fix bug : checkbox wasn't looking at the submitted value.
62  *
63  * Revision 1.16 2004/12/14 14:12:33 mmarinschek
64  * PR:
65  * Obtained from:
66  * Submitted by:
67  * Reviewed by:
68  *
69  * Revision 1.15 2004/12/09 12:18:43 mmarinschek
70  * changes in Calendar-Renderer to check for submitted-value first
71  *
72  * Revision 1.14 2004/10/13 11:51:01 matze
73  * renamed packages to org.apache
74  *
75  * Revision 1.13 2004/09/06 08:41:49 tinytoony
76  * changes to calendar - rendering wrong weekday, check output-text behavior
77  *
78  * Revision 1.12 2004/07/01 22:01:18 mwessendorf
79  * ASF switch
80  *
81  * Revision 1.11 2004/06/23 12:42:26 tinytoony
82  * just a tad more information ;)
83  *
84  * Revision 1.10 2004/05/18 14:31:40 manolito
85  * user role support completely moved to components source tree
86  *
87  * Revision 1.9 2004/04/28 10:38:33 tinytoony
88  * child is of type added to exception message
89  *
90  * Revision 1.8 2004/04/28 10:37:14 tinytoony
91  * child is of type added to exception message
92  *
93  * Revision 1.7 2004/04/28 08:17:11 tinytoony
94  * child is of type added to exception message
95  *
96  * Revision 1.6 2004/04/07 08:21:10 manolito
97  * handling of select items with label == null
98  *
99  * Revision 1.5 2004/04/06 15:33:21 manolito
100  * getStringValue must return submitted value if any
101  *
102  */

103 public class RendererUtils
104 {
105     private static final Log log = LogFactory.getLog(RendererUtils.class);
106
107     public static final String JavaDoc SELECT_ITEM_LIST_ATTR = RendererUtils.class.getName() + ".LIST";
108     public static final String JavaDoc EMPTY_STRING = new String JavaDoc();
109
110     public static String JavaDoc getPathToComponent(UIComponent component)
111     {
112         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
113
114         if(component == null)
115         {
116             buf.append("{Component-Path : ");
117             buf.append("[null]}");
118             return buf.toString();
119         }
120
121         getPathToComponent(component,buf);
122
123         buf.insert(0,"{Component-Path : ");
124         buf.append("}");
125
126         return buf.toString();
127     }
128
129     private static void getPathToComponent(UIComponent component, StringBuffer JavaDoc buf)
130     {
131         if(component == null)
132             return;
133
134         StringBuffer JavaDoc intBuf = new StringBuffer JavaDoc();
135
136         intBuf.append("[Class: ");
137         intBuf.append(component.getClass().getName());
138         if(component instanceof UIViewRoot)
139         {
140             intBuf.append(",ViewId: ");
141             intBuf.append(((UIViewRoot) component).getViewId());
142         }
143         else
144         {
145             intBuf.append(",Id: ");
146             intBuf.append(component.getId());
147         }
148         intBuf.append("]");
149
150         buf.insert(0,intBuf);
151
152         if(component!=null)
153         {
154             getPathToComponent(component.getParent(),buf);
155         }
156     }
157
158     public static String JavaDoc getConcatenatedId(FacesContext context, UIComponent container,
159                                            String JavaDoc clientId)
160     {
161         UIComponent child = container.findComponent(clientId);
162
163         if(child == null)
164                 return clientId;
165
166         return getConcatenatedId(context, child);
167     }
168
169     public static String JavaDoc getConcatenatedId(FacesContext context, UIComponent component)
170     {
171         if (context == null) throw new NullPointerException JavaDoc("context");
172
173         StringBuffer JavaDoc idBuf = new StringBuffer JavaDoc();
174
175         idBuf.append(component.getId());
176
177         UIComponent parent = null;
178
179         while((parent = component.getParent())!=null)
180         {
181             if(parent instanceof NamingContainer)
182             {
183                 idBuf.insert(0,NamingContainer.SEPARATOR_CHAR);
184                 idBuf.insert(0,parent.getId());
185             }
186         }
187
188         return idBuf.toString();
189     }
190
191     public static Boolean JavaDoc getBooleanValue(UIComponent component)
192     {
193         if (!(component instanceof ValueHolder))
194         {
195             throw new IllegalArgumentException JavaDoc("Component : "+getPathToComponent(component)
196                     +"is not a ValueHolder");
197         }
198
199         if (component instanceof EditableValueHolder)
200         {
201             Object JavaDoc submittedValue = ((EditableValueHolder)component).getSubmittedValue();
202             if (submittedValue != null)
203             {
204                 if (submittedValue instanceof Boolean JavaDoc)
205                 {
206                     return (Boolean JavaDoc)submittedValue;
207                 }
208                 else
209                 {
210                     throw new IllegalArgumentException JavaDoc("Expected submitted value of type Boolean for component : "+
211                             getPathToComponent(component));
212                 }
213             }
214         }
215
216         Object JavaDoc value = ((ValueHolder)component).getValue();
217
218         if (value==null || value instanceof Boolean JavaDoc)
219         {
220             return (Boolean JavaDoc) value;
221         }
222         else
223         {
224             throw new IllegalArgumentException JavaDoc("Expected submitted value of type Boolean for Component : "+
225                     getPathToComponent(component));
226         }
227     }
228     
229     public static Date getDateValue(UIComponent component)
230     {
231         if (!(component instanceof ValueHolder))
232         {
233             throw new IllegalArgumentException JavaDoc("Component : "+
234                     getPathToComponent(component)+"is not a ValueHolder");
235         }
236
237         if (component instanceof EditableValueHolder)
238         {
239             Object JavaDoc submittedValue = ((EditableValueHolder)component).getSubmittedValue();
240             if (submittedValue != null)
241             {
242                 if (submittedValue instanceof Date)
243                 {
244                     return (Date)submittedValue;
245                 }
246                 else
247                 {
248                     throw new IllegalArgumentException JavaDoc(
249                             "Expected submitted value of type Date for component : "+
250                             getPathToComponent(component));
251                 }
252             }
253         }
254
255         Object JavaDoc value = ((ValueHolder)component).getValue();
256
257         if (value==null || value instanceof Date)
258         {
259             return (Date) value;
260         }
261         else
262         {
263             throw new IllegalArgumentException JavaDoc("Expected submitted value of type Date for component : "
264                 +getPathToComponent(component));
265         }
266     }
267
268
269     public static String JavaDoc getStringValue(FacesContext facesContext,
270                                         UIComponent component)
271     {
272         try
273         {
274             if (!(component instanceof ValueHolder))
275             {
276                 throw new IllegalArgumentException JavaDoc("Component : "+getPathToComponent(component)+"is not a ValueHolder");
277             }
278
279             if (component instanceof EditableValueHolder)
280             {
281                 Object JavaDoc submittedValue = ((EditableValueHolder)component).getSubmittedValue();
282                 if (submittedValue != null)
283                 {
284                     if (submittedValue instanceof String JavaDoc)
285                     {
286                         return (String JavaDoc)submittedValue;
287                     }
288                     else
289                     {
290                         throw new IllegalArgumentException JavaDoc("Expected submitted value of type String for component : "
291                             +getPathToComponent(component));
292                     }
293                 }
294             }
295
296             Object JavaDoc value = ((ValueHolder)component).getValue();
297
298             Converter converter = ((ValueHolder)component).getConverter();
299             if (converter == null && value != null)
300             {
301                 if (value instanceof String JavaDoc)
302                 {
303                     return (String JavaDoc) value;
304                 }
305
306                 try
307                 {
308                     converter = facesContext.getApplication().createConverter(value.getClass());
309                 }
310                 catch (FacesException e)
311                 {
312                     log.error("No converter for class " + value.getClass().getName() + " found (component id=" + component.getId() + ").");
313                     // converter stays null
314
}
315             }
316
317             if (converter == null)
318             {
319                 if (value == null)
320                 {
321                     return "";
322                 }
323                 else
324                 {
325                     return value.toString();
326                 }
327             }
328             else
329             {
330                 return converter.getAsString(facesContext, component, value);
331             }
332         }
333         catch(PropertyNotFoundException ex)
334         {
335             log.error("Property not found - called by component : "+getPathToComponent(component));
336
337             throw ex;
338         }
339     }
340
341     /**
342      * See JSF Spec. 8.5 Table 8-1
343      * @param value
344      * @return
345      */

346     public static boolean isDefaultAttributeValue(Object JavaDoc value)
347     {
348         if (value == null)
349         {
350             return true;
351         }
352         else if (value instanceof Boolean JavaDoc)
353         {
354             return ((Boolean JavaDoc)value).booleanValue() == false;
355         }
356         else if (value instanceof Number JavaDoc)
357         {
358             if (value instanceof Integer JavaDoc)
359             {
360                 return ((Number JavaDoc)value).intValue() == Integer.MIN_VALUE;
361             }
362             else if (value instanceof Double JavaDoc)
363             {
364                 return ((Number JavaDoc)value).doubleValue() == Double.MIN_VALUE;
365             }
366             else if (value instanceof Long JavaDoc)
367             {
368                 return ((Number JavaDoc)value).longValue() == Long.MIN_VALUE;
369             }
370             else if (value instanceof Byte JavaDoc)
371             {
372                 return ((Number JavaDoc)value).byteValue() == Byte.MIN_VALUE;
373             }
374             else if (value instanceof Float JavaDoc)
375             {
376                 return ((Number JavaDoc)value).floatValue() == Float.MIN_VALUE;
377             }
378             else if (value instanceof Short JavaDoc)
379             {
380                 return ((Number JavaDoc)value).shortValue() == Short.MIN_VALUE;
381             }
382         }
383         return false;
384     }
385     
386     /**
387      * Find the proper Converter for the given UIOutput component.
388      * @return the Converter or null if no Converter specified or needed
389      * @throws FacesException if the Converter could not be created
390      */

391     public static Converter findUIOutputConverter(FacesContext facesContext,
392                                                   UIOutput component)
393             throws FacesException
394     {
395         return _SharedRendererUtils.findUIOutputConverter(facesContext, component);
396     }
397
398
399     /**
400      * Find proper Converter for the entries in the associated List or Array of
401      * the given UISelectMany as specified in API Doc of UISelectMany.
402      * @return the Converter or null if no Converter specified or needed
403      * @throws FacesException if the Converter could not be created
404      */

405     public static Converter findUISelectManyConverter(FacesContext facesContext,
406                                                       UISelectMany component)
407     {
408         Converter converter = component.getConverter();
409         if (converter != null) return converter;
410
411         //Try to find out by value binding
412
ValueBinding vb = component.getValueBinding("value");
413         if (vb == null) return null;
414
415         Class JavaDoc valueType = vb.getType(facesContext);
416         if (valueType == null) return null;
417
418         if (List.class.isAssignableFrom(valueType))
419         {
420             //According to API Doc of UISelectMany the assumed entry type for a List is String
421
//--> no converter needed
422
return null;
423         }
424
425         if (!valueType.isArray())
426         {
427             throw new IllegalArgumentException JavaDoc("ValueBinding for UISelectMany : "+getPathToComponent(component)+" must be of type List or Array");
428         }
429
430         Class JavaDoc arrayComponentType = valueType.getComponentType();
431         if (String JavaDoc.class.equals(arrayComponentType)) return null; //No converter needed for String type
432
if (Object JavaDoc.class.equals(arrayComponentType)) return null; //There is no converter for Object class
433

434         try
435         {
436             return facesContext.getApplication().createConverter(arrayComponentType);
437         }
438         catch (FacesException e)
439         {
440             log.error("No Converter for type " + arrayComponentType.getName() + " found", e);
441             return null;
442         }
443     }
444
445
446     public static void checkParamValidity(FacesContext facesContext, UIComponent uiComponent, Class JavaDoc compClass)
447     {
448         if(facesContext == null)
449             throw new NullPointerException JavaDoc("facesContext may not be null");
450         if(uiComponent == null)
451             throw new NullPointerException JavaDoc("uiComponent may not be null");
452
453         //if (compClass != null && !(compClass.isAssignableFrom(uiComponent.getClass())))
454
// why isAssignableFrom with additional getClass method call if isInstance does the same?
455
if (compClass != null && !(compClass.isInstance(uiComponent)))
456         {
457             throw new IllegalArgumentException JavaDoc("uiComponent : "+getPathToComponent(uiComponent)+
458                     " is not instance of "+compClass.getName()+" as it should be");
459         }
460     }
461
462
463     public static void renderChildren(FacesContext facesContext, UIComponent component)
464             throws IOException JavaDoc
465     {
466         if (component.getChildCount() > 0)
467         {
468             for (Iterator it = component.getChildren().iterator(); it.hasNext(); )
469             {
470                 UIComponent child = (UIComponent)it.next();
471                 renderChild(facesContext, child);
472             }
473         }
474     }
475
476
477     public static void renderChild(FacesContext facesContext, UIComponent child)
478             throws IOException JavaDoc
479     {
480         if (!child.isRendered())
481         {
482             return;
483         }
484         
485         child.encodeBegin(facesContext);
486         if (child.getRendersChildren())
487         {
488             child.encodeChildren(facesContext);
489         }
490         else
491         {
492             renderChildren(facesContext, child);
493         }
494         child.encodeEnd(facesContext);
495     }
496
497
498
499     /**
500      * @param uiSelectOne
501      * @return List of SelectItem Objects
502      */

503     public static List getSelectItemList(UISelectOne uiSelectOne)
504     {
505         return internalGetSelectItemList(uiSelectOne);
506     }
507
508     /**
509      * @param uiSelectMany
510      * @return List of SelectItem Objects
511      */

512     public static List getSelectItemList(UISelectMany uiSelectMany)
513     {
514         return internalGetSelectItemList(uiSelectMany);
515     }
516
517     private static List internalGetSelectItemList(UIComponent uiComponent)
518     {
519         /* TODO: Shall we cache the list in a component attribute?
520         ArrayList list = (ArrayList)uiComponent.getAttributes().get(SELECT_ITEM_LIST_ATTR);
521         if (list != null)
522         {
523             return list;
524         }
525          */

526
527         List list = new ArrayList(uiComponent.getChildCount());
528         for (Iterator children = uiComponent.getChildren().iterator(); children.hasNext(); )
529         {
530             UIComponent child = (UIComponent)children.next();
531             if (child instanceof UISelectItem)
532             {
533                 Object JavaDoc value = ((UISelectItem) child).getValue();
534                 if (value != null)
535                 {
536                     //get SelectItem from model via value binding
537
if (!(value instanceof SelectItem))
538                     {
539                         ValueBinding binding = ((UISelectItem) child).getValueBinding("value");
540                         throw new IllegalArgumentException JavaDoc("Value binding '"+(binding==null?null:binding.getExpressionString())
541                                 +"' of UISelectItem : " + getPathToComponent(child) + " does not reference an Object of type SelectItem");
542                     }
543                     list.add(value);
544                 }
545                 else
546                 {
547                     Object JavaDoc itemValue = ((UISelectItem)child).getItemValue();
548                     String JavaDoc label = ((UISelectItem)child).getItemLabel();
549                     String JavaDoc description = ((UISelectItem)child).getItemDescription();
550                     boolean disabled = ((UISelectItem)child).isItemDisabled();
551                     if (label == null)
552                     {
553                         list.add(new SelectItem(itemValue, itemValue.toString(), description, disabled));
554                     }
555                     else
556                     {
557                         list.add(new SelectItem(itemValue, label, description, disabled));
558                     }
559                 }
560             }
561             else if (child instanceof UISelectItems)
562             {
563                 UISelectItems items = ((UISelectItems) child);
564
565                 Object JavaDoc value = items.getValue(); // TODO : Check here for getSubmittedValue.
566

567                 if (value instanceof SelectItem)
568                 {
569                     list.add(value);
570                 }
571                 else if (value instanceof SelectItem[])
572                 {
573                     for (int i = 0; i < ((SelectItem[])value).length; i++)
574                     {
575                         list.add(((SelectItem[])value)[i]);
576                     }
577                 }
578                 else if (value instanceof Collection)
579                 {
580                     for (Iterator it = ((Collection)value).iterator(); it.hasNext();)
581                     {
582                         Object JavaDoc item = it.next();
583                         if (!(item instanceof SelectItem))
584                         {
585                             ValueBinding binding = items.getValueBinding("value");
586                             throw new IllegalArgumentException JavaDoc("Collection referenced by UISelectItems with binding '"+
587                                     binding.getExpressionString()+"' and Component-Path : " + getPathToComponent(child) + " does not contain Objects of type SelectItem");
588                         }
589                         list.add(item);
590                     }
591                 }
592                 else if (value instanceof Map)
593                 {
594                     for (Iterator it = ((Map)value).entrySet().iterator(); it.hasNext();)
595                     {
596                         Map.Entry entry = (Map.Entry)it.next();
597                         list.add(new SelectItem(entry.getValue(), entry.getKey().toString()));
598                     }
599                 }
600                 else
601                 {
602                     ValueBinding binding = items.getValueBinding("value");
603
604                     throw new IllegalArgumentException JavaDoc("Value binding '"+
605                             (binding==null?null:binding.getExpressionString())+"'of UISelectItems with component-path " + getPathToComponent(child) + " does not reference an Object of type SelectItem, SelectItem[], Collection or Map but of type : "+((value==null)?null:value.getClass().getName()));
606                 }
607             }
608             else
609             {
610                 //todo: may other objects than selectItems be nested or not?
611
//log.error("Invalid component : " + getPathToComponent(child) + " : must be UISelectItem or UISelectItems, is of type : "+((child==null)?"null":child.getClass().getName()));
612
}
613         }
614
615         return list;
616     }
617
618
619     /**
620      * Convenient utility method that returns the currently submitted values of
621      * a UISelectMany component as a Set, of which the contains method can then be
622      * easily used to determine if a select item is currently selected.
623      * Calling the contains method of this Set with the renderable (String converted) item value
624      * as argument returns true if this item is selected.
625      * @param uiSelectMany
626      * @return Set containing all currently selected values
627      */

628     public static Set getSubmittedValuesAsSet(FacesContext context, UIComponent component, Converter converter, UISelectMany uiSelectMany)
629     {
630         Object JavaDoc submittedValues = uiSelectMany.getSubmittedValue();
631         if (submittedValues == null)
632         {
633             return null;
634         }
635
636         return internalSubmittedOrSelectedValuesAsSet(context, component, converter, uiSelectMany, submittedValues);
637     }
638
639
640     /**
641      * Convenient utility method that returns the currently selected values of
642      * a UISelectMany component as a Set, of which the contains method can then be
643      * easily used to determine if a value is currently selected.
644      * Calling the contains method of this Set with the item value
645      * as argument returns true if this item is selected.
646      * @param uiSelectMany
647      * @return Set containing all currently selected values
648      */

649     public static Set getSelectedValuesAsSet(FacesContext context, UIComponent component, Converter converter, UISelectMany uiSelectMany)
650     {
651         Object JavaDoc selectedValues = uiSelectMany.getValue();
652
653         return internalSubmittedOrSelectedValuesAsSet(context, component, converter, uiSelectMany, selectedValues);
654     }
655     
656     
657     /**
658      * Convenient utility method that returns the currently given value as String,
659      * using the given converter.
660      * Especially usefull for dealing with primitive types.
661      */

662     public static String JavaDoc getConvertedStringValue(FacesContext context,
663             UIComponent component, Converter converter, Object JavaDoc value) {
664         if (converter == null) {
665             if (value == null) {
666                 return "";
667             } else if (value instanceof String JavaDoc) {
668                 return (String JavaDoc) value;
669             } else {
670                 throw new IllegalArgumentException JavaDoc(
671                         "Value is no String and component "
672                                 + component.getClientId(context)
673                                 + " does not have a Converter");
674             }
675         }
676
677         return converter.getAsString(context, component, value);
678     }
679     
680     
681     /**
682      * Convenient utility method that returns the currently given SelectItem value
683      * as String, using the given converter.
684      * Especially usefull for dealing with primitive types.
685      */

686     public static String JavaDoc getConvertedStringValue(FacesContext context,
687             UIComponent component, Converter converter, SelectItem selectItem) {
688         return getConvertedStringValue(context, component, converter, selectItem.getValue());
689     }
690
691     
692     private static Set internalSubmittedOrSelectedValuesAsSet(FacesContext context,
693             UIComponent component, Converter converter, UISelectMany uiSelectMany,
694             Object JavaDoc values)
695     {
696         if (values == null || values == EMPTY_STRING)
697         {
698             return Collections.EMPTY_SET;
699         }
700         else if (values instanceof Object JavaDoc[])
701         {
702             //Object array
703
Object JavaDoc[] ar = (Object JavaDoc[])values;
704             if (ar.length == 0)
705             {
706                 return Collections.EMPTY_SET;
707             }
708
709             HashSet set = new HashSet(HashMapUtils.calcCapacity(ar.length));
710             for (int i = 0; i < ar.length; i++)
711             {
712                 set.add( getConvertedStringValue(context, component, converter, ar[i]) );
713             }
714             return set;
715         }
716         else if (values.getClass().isArray())
717         {
718             //primitive array
719
int len = Array.getLength(values);
720             HashSet set = new HashSet(HashMapUtils.calcCapacity(len));
721             for (int i = 0; i < len; i++)
722             {
723                 set.add( getConvertedStringValue(context, component, converter, Array.get(values,i)) );
724             }
725             return set;
726         }
727         else if (values instanceof List)
728         {
729             List lst = (List)values;
730             if (lst.size() == 0)
731             {
732                 return Collections.EMPTY_SET;
733             }
734             else
735             {
736                 HashSet set = new HashSet(HashMapUtils.calcCapacity(lst.size()));
737                 for(Iterator i =lst.iterator(); i.hasNext(); )
738                     set.add( getConvertedStringValue(context, component, converter, i.next()) );
739                 
740                 return set;
741             }
742         }
743         else
744         {
745             throw new IllegalArgumentException JavaDoc("Value of UISelectMany component with path : " + getPathToComponent(uiSelectMany) + " is not of type Array or List");
746         }
747     }
748
749
750
751     public static Object JavaDoc getConvertedUIOutputValue(FacesContext facesContext,
752                                                    UIOutput output,
753                                                    Object JavaDoc submittedValue)
754         throws ConverterException
755     {
756         if (submittedValue!=null && !(submittedValue instanceof String JavaDoc))
757         {
758             throw new IllegalArgumentException JavaDoc("Submitted value of type String for component : "+
759                     getPathToComponent(output)+"expected");
760         }
761
762         Converter converter;
763         try
764         {
765             converter = findUIOutputConverter(facesContext, output);
766         }
767         catch (FacesException e)
768         {
769             throw new ConverterException(e);
770         }
771
772         if (converter == null)
773         {
774             //No conversion needed
775
return submittedValue;
776         }
777         else
778         {
779             //Conversion
780
return converter.getAsObject(facesContext, output, (String JavaDoc)submittedValue);
781         }
782     }
783
784
785     public static Object JavaDoc getConvertedUISelectManyValue(FacesContext facesContext,
786                                                        UISelectMany selectMany,
787                                                        Object JavaDoc submittedValue)
788             throws ConverterException
789     {
790         if (submittedValue == null)
791         {
792             return null;
793         }
794         else
795         {
796             if (!(submittedValue instanceof String JavaDoc[]))
797             {
798                 throw new ConverterException("Submitted value of type String[] for component : "
799                                              + getPathToComponent(selectMany) + "expected");
800             }
801         }
802         return _SharedRendererUtils.getConvertedUISelectManyValue(facesContext,
803                                                                   selectMany,
804                                                                   (String JavaDoc[])submittedValue);
805     }
806
807
808     public static boolean getBooleanAttribute(UIComponent component,
809                                               String JavaDoc attrName,
810                                               boolean defaultValue)
811     {
812         Boolean JavaDoc b = (Boolean JavaDoc)component.getAttributes().get(attrName);
813         return b != null ? b.booleanValue() : defaultValue;
814     }
815     
816     public static int getIntegerAttribute(UIComponent component,
817                                           String JavaDoc attrName,
818                                           int defaultValue)
819     {
820         Integer JavaDoc i = (Integer JavaDoc)component.getAttributes().get(attrName);
821         return i != null ? i.intValue() : defaultValue;
822     }
823
824     public static UIForm findParentForm(UIComponentBase comp)
825     {
826         UIComponent parent = comp.getParent();
827         while (parent != null)
828         {
829             if (parent instanceof UIForm)
830             {
831                 return (UIForm)parent;
832             }
833             parent = parent.getParent();
834         }
835         return null;
836     }
837
838     public static void copyHtmlInputTextAttributes(HtmlInputText src, HtmlInputText dest)
839     {
840         dest.setId(src.getId());
841         dest.setImmediate(src.isImmediate());
842         dest.setTransient(src.isTransient());
843         dest.setAccesskey(src.getAccesskey());
844         dest.setAlt(src.getAlt());
845         dest.setConverter(src.getConverter());
846         dest.setDir(src.getDir());
847         dest.setDisabled(src.isDisabled());
848         dest.setLang(src.getLang());
849         dest.setLocalValueSet(src.isLocalValueSet());
850         dest.setMaxlength(src.getMaxlength());
851         dest.setOnblur(src.getOnblur());
852         dest.setOnchange(src.getOnchange());
853         dest.setOnclick(src.getOnclick());
854         dest.setOndblclick(src.getOndblclick());
855         dest.setOnfocus(src.getOnfocus());
856         dest.setOnkeydown(src.getOnkeydown());
857         dest.setOnkeypress(src.getOnkeypress());
858         dest.setOnkeyup(src.getOnkeyup());
859         dest.setOnmousedown(src.getOnmousedown());
860         dest.setOnmousemove(src.getOnmousemove());
861         dest.setOnmouseout(src.getOnmouseout());
862         dest.setOnmouseover(src.getOnmouseover());
863         dest.setOnmouseup(src.getOnmouseup());
864         dest.setOnselect(src.getOnselect());
865         dest.setReadonly(src.isReadonly());
866         dest.setRendered(src.isRendered());
867         dest.setRequired(src.isRequired());
868         dest.setSize(src.getSize());
869         dest.setStyle(src.getStyle());
870         dest.setStyleClass(src.getStyleClass());
871         dest.setTabindex(src.getTabindex());
872         dest.setTitle(src.getTitle());
873         dest.setValidator(src.getValidator());
874     }
875 }
876
Popular Tags