1 /***
2 * Copyright (C) 2005 The Java Community
3 *
4 * This program is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation; either version 2 of the License, or (at your option) any later
7 * version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 * details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place, Suite 330, Boston, MA 02111-1307 USA.
17 *
18 */
19 package org.bejug.javacareers.common.view.jsf.utils;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.faces.application.Application;
29 import javax.faces.component.UIComponent;
30 import javax.faces.component.UIForm;
31 import javax.faces.component.UISelectItem;
32 import javax.faces.component.UISelectItems;
33 import javax.faces.component.ValueHolder;
34 import javax.faces.context.FacesContext;
35 import javax.faces.convert.Converter;
36 import javax.faces.convert.ConverterException;
37 import javax.faces.el.ValueBinding;
38 import javax.faces.model.SelectItem;
39
40 /***
41 * @author : Peter Symoens (Last modified by $Author: shally $)
42 * @version $Revision: 1.5 $ - $Date: 2005/12/21 11:38:42 $:
43 */
44 public class RendererHelper {
45
46 /***
47 * @param context is context
48 * @param component is component
49 * @param submittedValue is submitted value
50 * @return submitted value
51 * @throws ConverterException in case of ConverterException
52 */
53 public static Object getConvertedValue(FacesContext context,
54 UIComponent component, Object submittedValue)
55 throws ConverterException {
56 if (submittedValue instanceof String) {
57 Converter converter = getConverter(context, component);
58 if (converter != null) {
59 return converter.getAsObject(context, component,
60 (String) submittedValue);
61 }
62 }
63 return submittedValue;
64 }
65
66 /***
67 * @param context is the context
68 * @param component is the component
69 * @return converter
70 */
71 public static Converter getConverter(FacesContext context,
72 UIComponent component) {
73 if (!(component instanceof ValueHolder)){
74 return null;
75 }
76 ValueHolder holder = (ValueHolder) component;
77
78 Converter converter = holder.getConverter();
79 if (converter != null){
80 return converter;
81 }
82
83 ValueBinding valueBinding = component.getValueBinding("value");
84 if (valueBinding == null){
85 return null;
86 }
87
88 Class targetType = valueBinding.getType(context);
89 if (targetType == null){
90 return null;
91 }
92
93
94
95 Application app = context.getApplication();
96 return app.createConverter(targetType);
97 }
98
99 /***
100 * @param context is the context
101 * @param component is the component
102 * @return client id found
103 */
104 public static String getFormId(FacesContext context, UIComponent component) {
105 UIComponent parent = component;
106 while (!(parent instanceof UIForm)){
107 parent = parent.getParent();
108 }
109 return parent.getClientId(context);
110 }
111
112 /***
113 * @param component to be checked
114 * @return list of items selected
115 */
116 public static List getSelectItems(UIComponent component) {
117 ArrayList list = new ArrayList();
118 Iterator children = component.getChildren().iterator();
119 while (children.hasNext()) {
120 UIComponent child = (UIComponent) children.next();
121
122 if (child instanceof UISelectItem) {
123 Object value = ((UISelectItem) child).getValue();
124 if (value == null) {
125 UISelectItem item = (UISelectItem) child;
126 list.add(new SelectItem(item.getItemValue(), item
127 .getItemLabel(), item.getItemDescription(), item
128 .isItemDisabled()));
129 } else if (value instanceof SelectItem) {
130 list.add(value);
131 }
132 } else if (child instanceof UISelectItems) {
133 Object value = ((UISelectItems) child).getValue();
134 if (value instanceof SelectItem){
135 list.add(value);
136 }
137 else if (value instanceof SelectItem[]){
138 list.addAll(Arrays.asList((SelectItem[]) value));
139 }
140 else if (value instanceof Collection){
141 list.addAll((Collection) value);
142 }
143 else if (value instanceof Map) {
144 Iterator entries = ((Map) value).entrySet().iterator();
145 while (entries.hasNext()) {
146 Map.Entry entry = (Map.Entry) entries.next();
147 list.add(new SelectItem(entry.getKey(), ""
148 + entry.getValue()));
149 }
150 }
151 }
152 }
153 return list;
154 }
155 }
156
157 /***
158 * $Log: RendererHelper.java,v $
159 * Revision 1.5 2005/12/21 11:38:42 shally
160 * *** empty log message ***
161 *
162 * Revision 1.4 2005/12/20 15:36:47 shally
163 * CheckStyle and PMD changes.
164 *
165 * Revision 1.3 2005/12/08 14:53:46 shally
166 * Opkuis voor checkstyle.
167 *
168 * Revision 1.2 2005/08/30 13:07:46 psong09
169 * renamed author: psong09
170 *
171 * Revision 1.1 2005/08/24 12:32:12 psong09
172 * Added spinner component and renamed commentAction
173 *
174 */