1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.custom.util;
21
22 import javax.faces.component.UIColumn;
23 import javax.faces.component.UIComponent;
24 import javax.faces.component.UIParameter;
25 import javax.faces.component.html.HtmlDataTable;
26 import javax.faces.component.html.HtmlMessages;
27 import javax.faces.context.FacesContext;
28 import javax.faces.lifecycle.LifecycleFactory;
29 import javax.faces.webapp.FacesServlet;
30 import javax.servlet.ServletContext;
31
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Map;
37
38
39
40
41
42
43 public final class ComponentUtils
44 {
45
46 private ComponentUtils() {
47
48 }
49
50
51
52
53
54
55
56
57
58 public static UIComponent findComponentByClientId(FacesContext context,
59 UIComponent root, String clientId) {
60 UIComponent component = null;
61 for (int i = 0; i < root.getChildCount() && component == null; i++) {
62 UIComponent child = (UIComponent) root.getChildren().get(i);
63 component = findComponentByClientId(context, child, clientId);
64 }
65 if (root.getId() != null) {
66 if (component == null && root.getClientId(context).equals(clientId)) {
67 component = root;
68 }
69 }
70 return component;
71 }
72
73
74
75
76
77
78
79
80
81
82
83 public static UIComponent findComponentById(FacesContext context,
84 UIComponent root, String id) {
85 UIComponent component = null;
86 for (int i = 0; i < root.getChildCount() && component == null; i++) {
87 UIComponent child = (UIComponent) root.getChildren().get(i);
88 component = findComponentById(context, child, id);
89 }
90
91
92 if (root.getId() != null) {
93 if (component == null && root.getId().equals(id)) {
94 component = root;
95 }
96 }
97 return component;
98 }
99
100 public static UIComponent findFirstMessagesComponent(FacesContext context,
101 UIComponent base) {
102 if (base == null) {
103 return null;
104 }
105
106 if (base instanceof HtmlMessages) {
107 return base;
108 }
109
110 Iterator iterChildren = base.getFacetsAndChildren();
111 while (iterChildren.hasNext()) {
112 UIComponent child = (UIComponent) iterChildren.next();
113
114 UIComponent found = findFirstMessagesComponent(context, child);
115 if (found != null) {
116 return found;
117 }
118 }
119
120 return null;
121 }
122
123 private static boolean isDecorated(UIComponent component, String attribute,
124 String value) {
125 String attributeValue = (String) component.getAttributes().get(
126 attribute);
127
128 if (attributeValue == null || attributeValue.indexOf(value) == -1)
129 return false;
130 else
131 return true;
132 }
133
134
135
136
137
138
139
140
141
142
143
144 public static void decorateEventAttribute(UIComponent component,
145 String attribute, String value) {
146 if (isDecorated(component, attribute, value))
147 return;
148
149 String attributeValue = (String) component.getAttributes().get(
150 attribute);
151
152 if (attributeValue == null)
153 component.getAttributes().put(attribute, value);
154 else if (attributeValue.endsWith(";"))
155 component.getAttributes().put(attribute, attributeValue + value);
156 else
157 component.getAttributes().put(attribute,
158 attributeValue + ";" + value);
159 }
160
161
162
163
164
165
166
167 public static Map getParameterMap(UIComponent component) {
168 Map result = new HashMap();
169 for (Iterator iter = component.getChildren().iterator(); iter.hasNext();) {
170 UIComponent child = (UIComponent) iter.next();
171 if (child instanceof UIParameter) {
172 UIParameter uiparam = (UIParameter) child;
173 Object value = uiparam.getValue();
174 if (value != null) {
175 result.put(uiparam.getName(), value);
176 }
177 }
178 }
179 return result;
180 }
181
182
183
184
185
186
187
188 public static String getLifecycleId(ServletContext context) {
189 String lifecycleId = context
190 .getInitParameter(FacesServlet.LIFECYCLE_ID_ATTR);
191 return lifecycleId != null ? lifecycleId
192 : LifecycleFactory.DEFAULT_LIFECYCLE;
193 }
194
195
196
197
198
199
200
201 public static List getHTMLDataTableColumns(HtmlDataTable table) {
202 List columns = new ArrayList();
203
204 for (int i = 0; i < table.getChildCount(); i++) {
205 UIComponent child = (UIComponent) table.getChildren().get( i );
206 if (child instanceof UIColumn) {
207 columns.add( child );
208 }
209 }
210 return columns;
211 }
212 }