1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package javax.faces.internal; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.Iterator; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import javax.faces.application.FacesMessage; |
23 | |
import javax.faces.component.UIComponent; |
24 | |
import javax.faces.component.UIForm; |
25 | |
import javax.faces.component.UIInput; |
26 | |
import javax.faces.context.FacesContext; |
27 | |
import javax.faces.el.EvaluationException; |
28 | |
import javax.faces.el.MethodBinding; |
29 | |
import javax.faces.render.RenderKit; |
30 | |
import javax.faces.render.Renderer; |
31 | |
import javax.faces.validator.Validator; |
32 | |
import javax.faces.validator.ValidatorException; |
33 | |
|
34 | |
import org.seasar.framework.util.StringUtil; |
35 | |
import org.seasar.teeda.core.JsfConstants; |
36 | |
import org.seasar.teeda.core.exception.TagNotFoundRuntimeException; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class UIComponentUtil { |
43 | |
|
44 | |
public static boolean isDisabled(UIComponent component) { |
45 | 142 | return getPrimitiveBooleanAttribute(component, |
46 | |
JsfConstants.DISABLED_ATTR); |
47 | |
} |
48 | |
|
49 | |
public static String getStringAttribute(UIComponent component, String name) { |
50 | 434 | return (String) component.getAttributes().get(name); |
51 | |
} |
52 | |
|
53 | |
public static boolean getPrimitiveBooleanAttribute(UIComponent component, |
54 | |
String name) { |
55 | 302 | Object value = component.getAttributes().get(name); |
56 | 302 | if (value == null) { |
57 | 1 | return false; |
58 | |
} |
59 | 301 | return ((Boolean) value).booleanValue(); |
60 | |
} |
61 | |
|
62 | |
public static int getPrimitiveIntAttribute(UIComponent component, |
63 | |
String name) { |
64 | 33 | Object value = component.getAttributes().get(name); |
65 | 33 | if (value == null) { |
66 | 0 | return 0; |
67 | |
} |
68 | 33 | return ((Integer) value).intValue(); |
69 | |
} |
70 | |
|
71 | |
protected static String getNormalizeId(UIComponent component) { |
72 | 0 | String id = component.getId(); |
73 | 0 | if (StringUtil.isEmpty(id)) { |
74 | 0 | return id; |
75 | |
} |
76 | 0 | int indexOf = id.indexOf("-"); |
77 | 0 | if (indexOf < 0) { |
78 | 0 | return id; |
79 | |
} |
80 | 0 | return id.substring(0, indexOf); |
81 | |
} |
82 | |
|
83 | |
public static String getLabel(UIComponent component) { |
84 | 89 | String label = (String) component.getAttributes().get( |
85 | |
JsfConstants.LABEL_ATTR); |
86 | 89 | if (label != null) { |
87 | 20 | return label; |
88 | |
} |
89 | 69 | label = LabelUtil.getLabelValue(component.getId()); |
90 | 69 | if (label != null) { |
91 | 1 | return label; |
92 | |
} |
93 | 68 | String title = (String) component.getAttributes().get( |
94 | |
JsfConstants.TITLE_ATTR); |
95 | 68 | if (title != null) { |
96 | 0 | return title; |
97 | |
} |
98 | 68 | return component.getId(); |
99 | |
} |
100 | |
|
101 | |
public static void callValidators(FacesContext context, UIInput input, |
102 | |
Object convertedValue) { |
103 | 4 | Validator[] validators = input.getValidators(); |
104 | 5 | for (int i = 0; i < validators.length; ++i) { |
105 | 1 | Validator validator = validators[i]; |
106 | |
try { |
107 | 1 | validator.validate(context, input, convertedValue); |
108 | 1 | } catch (ValidatorException e) { |
109 | 1 | input.setValid(false); |
110 | 1 | FacesMessage facesMessage = e.getFacesMessage(); |
111 | 1 | if (facesMessage != null) { |
112 | 1 | context |
113 | |
.addMessage(input.getClientId(context), |
114 | |
facesMessage); |
115 | |
} |
116 | 0 | } |
117 | |
} |
118 | 4 | MethodBinding validatorBinding = input.getValidator(); |
119 | 4 | if (validatorBinding != null) { |
120 | |
try { |
121 | 3 | validatorBinding.invoke(context, new Object[] { context, input, |
122 | |
convertedValue }); |
123 | 2 | } catch (EvaluationException e) { |
124 | 2 | input.setValid(false); |
125 | 2 | Throwable cause = e.getCause(); |
126 | 2 | if (cause instanceof ValidatorException) { |
127 | 1 | FacesMessage facesMessage = ((ValidatorException) cause) |
128 | |
.getFacesMessage(); |
129 | 1 | if (facesMessage != null) { |
130 | 1 | context.addMessage(input.getClientId(context), |
131 | |
facesMessage); |
132 | |
} |
133 | |
} else { |
134 | 1 | throw e; |
135 | |
} |
136 | 1 | } |
137 | |
} |
138 | 3 | } |
139 | |
|
140 | |
public static UIForm findParentForm(final UIComponent component) { |
141 | 21 | final UIComponent parent = findParentOrNull(component, UIForm.class); |
142 | 20 | if (parent == null) { |
143 | 1 | throw new TagNotFoundRuntimeException("form"); |
144 | |
} |
145 | 19 | return (UIForm) parent; |
146 | |
} |
147 | |
|
148 | |
public static UIComponent findParent(final UIComponent component, |
149 | |
final Class parentClass) { |
150 | 0 | final UIComponent parent = findParentOrNull(component, parentClass); |
151 | 0 | if (parent != null) { |
152 | 0 | return parent; |
153 | |
} |
154 | |
|
155 | 0 | throw new IllegalArgumentException("parent element not found [" + |
156 | |
parentClass.getName() + "]"); |
157 | |
} |
158 | |
|
159 | |
public static UIComponent findParent(final UIComponent component, |
160 | |
final Class parentClass, final String id) { |
161 | 0 | final UIComponent parent = findParentOrNull(component, parentClass, id); |
162 | 0 | if (parent != null) { |
163 | 0 | return parent; |
164 | |
} |
165 | |
|
166 | 0 | throw new IllegalArgumentException("parent element not found [" + id + |
167 | |
"]"); |
168 | |
} |
169 | |
|
170 | |
private static UIComponent findParentOrNull(final UIComponent component, |
171 | |
final Class parentClass) { |
172 | 20 | for (UIComponent parent = component.getParent(); parent != null; parent = parent |
173 | |
.getParent()) { |
174 | 19 | if (parentClass.isInstance(parent)) { |
175 | 19 | return parent; |
176 | |
} |
177 | |
} |
178 | 1 | return null; |
179 | |
} |
180 | |
|
181 | |
private static UIComponent findParentOrNull(final UIComponent component, |
182 | |
final Class parentClass, final String id) { |
183 | 0 | for (UIComponent parent = component.getParent(); parent != null; parent = parent |
184 | |
.getParent()) { |
185 | 0 | if (parentClass.isInstance(parent) && |
186 | |
id.equals(getNormalizeId(parent))) { |
187 | 0 | return parent; |
188 | |
} |
189 | |
} |
190 | 0 | return null; |
191 | |
} |
192 | |
|
193 | |
public static UIComponent findDescendant(UIComponent component, Class clazz) { |
194 | 3 | List children = component.getChildren(); |
195 | 3 | for (Iterator i = children.iterator(); i.hasNext();) { |
196 | 3 | UIComponent child = (UIComponent) i.next(); |
197 | 3 | if (clazz.isInstance(child)) { |
198 | 1 | return child; |
199 | |
} |
200 | 2 | UIComponent descendant = findDescendant(child, clazz); |
201 | 2 | if (descendant != null) { |
202 | 0 | return descendant; |
203 | |
} |
204 | |
} |
205 | 2 | return null; |
206 | |
} |
207 | |
|
208 | |
public static List collectDescendants(UIComponent component, Class clazz) { |
209 | 5 | List list = new ArrayList(); |
210 | 5 | List children = component.getChildren(); |
211 | 5 | for (Iterator i = children.iterator(); i.hasNext();) { |
212 | 4 | UIComponent child = (UIComponent) i.next(); |
213 | 4 | if (clazz.isInstance(child)) { |
214 | 3 | list.add(child); |
215 | |
} |
216 | 4 | List l = collectDescendants(child, clazz); |
217 | 4 | if (l != null) { |
218 | 4 | list.addAll(l); |
219 | |
} |
220 | |
} |
221 | 5 | return list; |
222 | |
} |
223 | |
|
224 | |
public static Renderer getRenderer(FacesContext context, |
225 | |
UIComponent component) { |
226 | 716 | String rendererType = component.getRendererType(); |
227 | 716 | if (rendererType == null) { |
228 | 213 | return null; |
229 | |
} |
230 | 503 | RenderKit renderKit = RenderKitUtil.getRenderKit(context); |
231 | 503 | return renderKit.getRenderer(component.getFamily(), rendererType); |
232 | |
} |
233 | |
} |