1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
package org.asyrinx.brownie.tapestry.util;
|
9
|
|
|
10
|
|
import java.util.Collection;
|
11
|
|
import java.util.Iterator;
|
12
|
|
import java.util.Map;
|
13
|
|
|
14
|
|
import org.apache.commons.collections.Predicate;
|
15
|
|
import org.apache.commons.logging.Log;
|
16
|
|
import org.apache.commons.logging.LogFactory;
|
17
|
|
import org.apache.tapestry.IAsset;
|
18
|
|
import org.apache.tapestry.IComponent;
|
19
|
|
import org.apache.tapestry.IForm;
|
20
|
|
import org.apache.tapestry.IPage;
|
21
|
|
import org.apache.tapestry.form.IFormComponent;
|
22
|
|
import org.apache.tapestry.html.Shell;
|
23
|
|
import org.asyrinx.brownie.core.lang.ArrayUtils;
|
24
|
|
import org.asyrinx.brownie.core.lang.StringUtils;
|
25
|
|
|
26
|
|
|
27
|
|
|
28
|
|
|
29
|
|
public class ComponentUtils {
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
0
|
private ComponentUtils() {
|
35
|
0
|
super();
|
36
|
|
}
|
37
|
|
|
38
|
|
private static Log log = LogFactory.getLog(ComponentUtils.class);
|
39
|
|
|
40
|
0
|
public static IComponent getTopContainer(IComponent target) {
|
41
|
0
|
if (target == null)
|
42
|
0
|
return null;
|
43
|
0
|
if (target.getContainer() != null)
|
44
|
0
|
return getTopContainer(target.getContainer());
|
45
|
|
else
|
46
|
0
|
return target;
|
47
|
|
}
|
48
|
|
|
49
|
0
|
public static IComponent lookFor(IComponent target, Predicate filter) {
|
50
|
0
|
if (target == null)
|
51
|
0
|
return null;
|
52
|
0
|
final Map targetComponents = target.getComponents();
|
53
|
0
|
final Iterator iterator = targetComponents.values().iterator();
|
54
|
0
|
while (iterator.hasNext()) {
|
55
|
0
|
final Object component = iterator.next();
|
56
|
0
|
if (!(component instanceof IComponent))
|
57
|
0
|
continue;
|
58
|
0
|
if (filter.evaluate(component))
|
59
|
0
|
return (IComponent) component;
|
60
|
|
}
|
61
|
0
|
return null;
|
62
|
|
}
|
63
|
|
|
64
|
0
|
public static IComponent lookForByClass(IComponent target, Class class1) {
|
65
|
0
|
return lookFor(target, new ClassMatch(class1));
|
66
|
|
}
|
67
|
|
|
68
|
0
|
public static IComponent lookForById(IComponent target, String componentId) {
|
69
|
0
|
return lookFor(target, new IdMatch(componentId));
|
70
|
|
}
|
71
|
|
|
72
|
0
|
public static IComponent searchBottom(IComponent target, Predicate filter) {
|
73
|
0
|
if (target == null)
|
74
|
0
|
return null;
|
75
|
0
|
IComponent result = lookFor(target, filter);
|
76
|
0
|
if (result != null)
|
77
|
0
|
return result;
|
78
|
0
|
final Iterator iterator = target.getComponents().values().iterator();
|
79
|
0
|
while (iterator.hasNext()) {
|
80
|
0
|
final IComponent subcomponent = (IComponent) iterator.next();
|
81
|
0
|
result = searchBottom(subcomponent, filter);
|
82
|
0
|
if (result != null)
|
83
|
0
|
return result;
|
84
|
|
}
|
85
|
0
|
return null;
|
86
|
|
}
|
87
|
|
|
88
|
0
|
public static IComponent searchBottomById(IComponent target,
|
89
|
|
String componentId) {
|
90
|
0
|
return searchBottom(target, new IdMatch(componentId));
|
91
|
|
}
|
92
|
|
|
93
|
0
|
public static IComponent searchBottomByClass(IComponent target, Class clazz) {
|
94
|
0
|
return searchBottom(target, new ClassMatch(clazz));
|
95
|
|
}
|
96
|
|
|
97
|
0
|
public static IComponent searchUpper(IComponent target, Predicate filter) {
|
98
|
0
|
if (target == null)
|
99
|
0
|
return null;
|
100
|
0
|
final IComponent parent = target.getContainer();
|
101
|
0
|
if (parent == null)
|
102
|
0
|
return null;
|
103
|
0
|
if (filter.evaluate(parent))
|
104
|
0
|
return parent;
|
105
|
|
else
|
106
|
0
|
return searchUpper(parent, filter);
|
107
|
|
}
|
108
|
|
|
109
|
0
|
public static IComponent searchUpperById(IComponent target,
|
110
|
|
String componentId) {
|
111
|
0
|
return searchUpper(target, new IdMatch(componentId));
|
112
|
|
}
|
113
|
|
|
114
|
0
|
public static IComponent searchUpperByClass(IComponent target, Class clazz) {
|
115
|
0
|
return searchUpper(target, new ClassMatch(clazz));
|
116
|
|
}
|
117
|
|
|
118
|
|
|
119
|
|
|
120
|
|
|
121
|
|
|
122
|
|
|
123
|
|
|
124
|
|
|
125
|
0
|
public static String getElementId(IComponent client, String id) {
|
126
|
0
|
final IPage page = client.getPage();
|
127
|
0
|
final IComponent component = ComponentUtils.searchBottomById(page, id);
|
128
|
0
|
final IFormComponent formComponent = (component instanceof IFormComponent) ? (IFormComponent) component
|
129
|
|
: null;
|
130
|
0
|
if (formComponent == null)
|
131
|
0
|
return null;
|
132
|
0
|
final IForm form = formComponent.getForm();
|
133
|
0
|
return form.getElementId(formComponent);
|
134
|
|
}
|
135
|
|
|
136
|
0
|
public static String getNameOnForm(IComponent component) {
|
137
|
0
|
if (component instanceof IFormComponent)
|
138
|
0
|
return ((IFormComponent) component).getName();
|
139
|
|
else
|
140
|
0
|
return null;
|
141
|
|
}
|
142
|
|
|
143
|
0
|
public static void addStylesheet(IComponent component, String assetName) {
|
144
|
0
|
addStylesheet(component, component.getAsset(assetName));
|
145
|
|
}
|
146
|
|
|
147
|
0
|
public static void addStylesheet(IComponent component, IAsset asset) {
|
148
|
0
|
final Shell shell = (Shell) ComponentUtils.searchBottomByClass(
|
149
|
|
ComponentUtils.getTopContainer(component), Shell.class);
|
150
|
0
|
addStylesheet(shell, asset);
|
151
|
|
}
|
152
|
|
|
153
|
0
|
public static void addStylesheet(Shell shell, IAsset asset) {
|
154
|
0
|
if (shell == null) {
|
155
|
0
|
log.warn("failed to addStylesheet because Shell is null");
|
156
|
0
|
return;
|
157
|
|
}
|
158
|
0
|
final Object original = shell.getStylesheets();
|
159
|
0
|
final Collection newStylesheets;
|
160
|
0
|
if (original instanceof Object[]) {
|
161
|
0
|
newStylesheets = ArrayUtils.toArrayList((Object[]) original);
|
162
|
0
|
} else if (original instanceof Collection) {
|
163
|
0
|
newStylesheets = (Collection) original;
|
164
|
|
} else {
|
165
|
0
|
return;
|
166
|
|
}
|
167
|
0
|
newStylesheets.add(asset);
|
168
|
|
}
|
169
|
|
|
170
|
|
}
|
171
|
|
|
172
|
|
class IdMatch implements Predicate {
|
173
|
|
|
174
|
0
|
public IdMatch(String componentId) {
|
175
|
0
|
super();
|
176
|
0
|
this.componentId = componentId;
|
177
|
|
}
|
178
|
|
|
179
|
|
final String componentId;
|
180
|
|
|
181
|
|
|
182
|
|
|
183
|
|
|
184
|
0
|
public boolean evaluate(Object object) {
|
185
|
0
|
if (!(object instanceof IComponent))
|
186
|
0
|
return false;
|
187
|
0
|
final IComponent component = (IComponent) object;
|
188
|
0
|
if (StringUtils.isEmpty(component.getId()))
|
189
|
0
|
return false;
|
190
|
0
|
return component.getId().equals(this.componentId);
|
191
|
|
}
|
192
|
|
|
193
|
|
}
|
194
|
|
|
195
|
|
class ClassMatch implements Predicate {
|
196
|
|
|
197
|
0
|
public ClassMatch(Class clazz) {
|
198
|
0
|
super();
|
199
|
0
|
this.clazz = clazz;
|
200
|
|
}
|
201
|
|
|
202
|
|
final Class clazz;
|
203
|
|
|
204
|
|
|
205
|
|
|
206
|
|
|
207
|
0
|
public boolean evaluate(Object object) {
|
208
|
0
|
return clazz.isInstance(object);
|
209
|
|
}
|
210
|
|
|
211
|
|
}
|