1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
package darrylbu.util; |
7 |
|
|
8 |
|
import java.awt.Component; |
9 |
|
import java.awt.Container; |
10 |
|
import java.lang.reflect.InvocationTargetException; |
11 |
|
import java.lang.reflect.Method; |
12 |
|
import java.util.ArrayList; |
13 |
|
import java.util.Collections; |
14 |
|
import java.util.HashMap; |
15 |
|
import java.util.HashSet; |
16 |
|
import java.util.List; |
17 |
|
import java.util.Map; |
18 |
|
import java.util.Set; |
19 |
|
|
20 |
|
import javax.swing.JComponent; |
21 |
|
import javax.swing.UIDefaults; |
22 |
|
import javax.swing.UIManager; |
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
@author |
28 |
|
|
|
|
| 0% |
Uncovered Elements: 144 (144) |
Complexity: 49 |
Complexity Density: 0.52 |
|
29 |
|
public final class SwingUtils |
30 |
|
{ |
31 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
32 |
0 |
private SwingUtils()... |
33 |
|
{ |
34 |
0 |
throw new Error("SwingUtils is just a container for static methods"); |
35 |
|
} |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
@param |
49 |
|
|
50 |
|
@param |
51 |
|
|
52 |
|
@return |
53 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
54 |
0 |
public static <T extends JComponent> List<T> getDescendantsOfType(... |
55 |
|
Class<T> clazz, Container container) |
56 |
|
{ |
57 |
0 |
return getDescendantsOfType(clazz, container, true); |
58 |
|
} |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
@param |
70 |
|
|
71 |
|
@param |
72 |
|
|
73 |
|
@param |
74 |
|
|
75 |
|
|
76 |
|
@return |
77 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
78 |
0 |
public static <T extends JComponent> List<T> getDescendantsOfType(... |
79 |
|
Class<T> clazz, Container container, boolean nested) |
80 |
|
{ |
81 |
0 |
List<T> tList = new ArrayList<T>(); |
82 |
0 |
for (Component component : container.getComponents()) |
83 |
|
{ |
84 |
0 |
if (clazz.isAssignableFrom(component.getClass())) |
85 |
|
{ |
86 |
0 |
tList.add(clazz.cast(component)); |
87 |
|
} |
88 |
0 |
if (nested || !clazz.isAssignableFrom(component.getClass())) |
89 |
|
{ |
90 |
0 |
tList.addAll(SwingUtils.<T> getDescendantsOfType(clazz, |
91 |
|
(Container) component, nested)); |
92 |
|
} |
93 |
|
} |
94 |
0 |
return tList; |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
@param |
107 |
|
|
108 |
|
@param |
109 |
|
|
110 |
|
@param |
111 |
|
|
112 |
|
|
113 |
|
@param |
114 |
|
|
115 |
|
@return |
116 |
|
@throws |
117 |
|
|
118 |
|
|
119 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
120 |
0 |
public static <T extends JComponent> T getDescendantOfType(Class<T> clazz,... |
121 |
|
Container container, String property, Object value) |
122 |
|
throws IllegalArgumentException |
123 |
|
{ |
124 |
0 |
return getDescendantOfType(clazz, container, property, value, true); |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
@param |
134 |
|
|
135 |
|
@param |
136 |
|
|
137 |
|
@param |
138 |
|
|
139 |
|
|
140 |
|
@param |
141 |
|
|
142 |
|
@param |
143 |
|
|
144 |
|
|
145 |
|
@return |
146 |
|
@throws |
147 |
|
|
148 |
|
|
149 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
150 |
0 |
public static <T extends JComponent> T getDescendantOfType(Class<T> clazz,... |
151 |
|
Container container, String property, Object value, |
152 |
|
boolean nested) throws IllegalArgumentException |
153 |
|
{ |
154 |
0 |
List<T> list = getDescendantsOfType(clazz, container, nested); |
155 |
0 |
return getComponentFromList(clazz, list, property, value); |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
@param |
167 |
|
|
168 |
|
@param |
169 |
|
|
170 |
|
@return |
171 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
172 |
0 |
public static <T extends JComponent> List<T> getDescendantsOfClass(... |
173 |
|
Class<T> clazz, Container container) |
174 |
|
{ |
175 |
0 |
return getDescendantsOfClass(clazz, container, true); |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
@param |
185 |
|
|
186 |
|
@param |
187 |
|
|
188 |
|
@param |
189 |
|
|
190 |
|
|
191 |
|
@return |
192 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
193 |
0 |
public static <T extends JComponent> List<T> getDescendantsOfClass(... |
194 |
|
Class<T> clazz, Container container, boolean nested) |
195 |
|
{ |
196 |
0 |
List<T> tList = new ArrayList<T>(); |
197 |
0 |
for (Component component : container.getComponents()) |
198 |
|
{ |
199 |
0 |
if (clazz.equals(component.getClass())) |
200 |
|
{ |
201 |
0 |
tList.add(clazz.cast(component)); |
202 |
|
} |
203 |
0 |
if (nested || !clazz.equals(component.getClass())) |
204 |
|
{ |
205 |
0 |
tList.addAll(SwingUtils.<T> getDescendantsOfClass(clazz, |
206 |
|
(Container) component, nested)); |
207 |
|
} |
208 |
|
} |
209 |
0 |
return tList; |
210 |
|
} |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
@param |
223 |
|
|
224 |
|
@param |
225 |
|
|
226 |
|
@param |
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
@param |
231 |
|
|
232 |
|
@return |
233 |
|
|
234 |
|
@throws |
235 |
|
|
236 |
|
|
237 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
238 |
0 |
public static <T extends JComponent> T getDescendantOfClass(... |
239 |
|
Class<T> clazz, Container container, String property, |
240 |
|
Object value) throws IllegalArgumentException |
241 |
|
{ |
242 |
0 |
return getDescendantOfClass(clazz, container, property, value, true); |
243 |
|
} |
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
@param |
253 |
|
|
254 |
|
@param |
255 |
|
|
256 |
|
@param |
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
@param |
261 |
|
|
262 |
|
@param |
263 |
|
|
264 |
|
|
265 |
|
@return |
266 |
|
|
267 |
|
@throws |
268 |
|
|
269 |
|
|
270 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
271 |
0 |
public static <T extends JComponent> T getDescendantOfClass(... |
272 |
|
Class<T> clazz, Container container, String property, |
273 |
|
Object value, boolean nested) throws IllegalArgumentException |
274 |
|
{ |
275 |
0 |
List<T> list = getDescendantsOfClass(clazz, container, nested); |
276 |
0 |
return getComponentFromList(clazz, list, property, value); |
277 |
|
} |
278 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 7 |
Complexity Density: 0.44 |
|
279 |
0 |
private static <T extends JComponent> T getComponentFromList(... |
280 |
|
Class<T> clazz, List<T> list, String property, Object value) |
281 |
|
throws IllegalArgumentException |
282 |
|
{ |
283 |
0 |
T retVal = null; |
284 |
0 |
Method method = null; |
285 |
0 |
try |
286 |
|
{ |
287 |
0 |
method = clazz.getMethod("get" + property); |
288 |
|
} catch (NoSuchMethodException ex) |
289 |
|
{ |
290 |
0 |
try |
291 |
|
{ |
292 |
0 |
method = clazz.getMethod("is" + property); |
293 |
|
} catch (NoSuchMethodException ex1) |
294 |
|
{ |
295 |
0 |
throw new IllegalArgumentException("Property " + property |
296 |
|
+ " not found in class " + clazz.getName()); |
297 |
|
} |
298 |
|
} |
299 |
0 |
try |
300 |
|
{ |
301 |
0 |
for (T t : list) |
302 |
|
{ |
303 |
0 |
Object testVal = method.invoke(t); |
304 |
0 |
if (equals(value, testVal)) |
305 |
|
{ |
306 |
0 |
return t; |
307 |
|
} |
308 |
|
} |
309 |
|
} catch (InvocationTargetException ex) |
310 |
|
{ |
311 |
0 |
throw new IllegalArgumentException("Error accessing property " |
312 |
|
+ property + " in class " + clazz.getName()); |
313 |
|
} catch (IllegalAccessException ex) |
314 |
|
{ |
315 |
0 |
throw new IllegalArgumentException("Property " + property |
316 |
|
+ " cannot be accessed in class " + clazz.getName()); |
317 |
|
} catch (SecurityException ex) |
318 |
|
{ |
319 |
0 |
throw new IllegalArgumentException("Property " + property |
320 |
|
+ " cannot be accessed in class " + clazz.getName()); |
321 |
|
} |
322 |
0 |
return retVal; |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
@param |
330 |
|
|
331 |
|
@param |
332 |
|
|
333 |
|
@return |
334 |
|
|
335 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
336 |
0 |
public static boolean equals(Object obj1, Object obj2)... |
337 |
|
{ |
338 |
0 |
return obj1 == null ? obj2 == null : obj1.equals(obj2); |
339 |
|
} |
340 |
|
|
341 |
|
|
342 |
|
|
343 |
|
|
344 |
|
|
345 |
|
|
346 |
|
|
347 |
|
|
348 |
|
|
349 |
|
|
350 |
|
@param |
351 |
|
|
352 |
|
@param |
353 |
|
|
354 |
|
@return |
355 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
356 |
0 |
public static Map<JComponent, List<JComponent>> getComponentMap(... |
357 |
|
JComponent container, boolean nested) |
358 |
|
{ |
359 |
0 |
HashMap<JComponent, List<JComponent>> retVal = new HashMap<JComponent, List<JComponent>>(); |
360 |
0 |
for (JComponent component : getDescendantsOfType(JComponent.class, |
361 |
|
container, false)) |
362 |
|
{ |
363 |
0 |
if (!retVal.containsKey(container)) |
364 |
|
{ |
365 |
0 |
retVal.put(container, new ArrayList<JComponent>()); |
366 |
|
} |
367 |
0 |
retVal.get(container).add(component); |
368 |
0 |
if (nested) |
369 |
|
{ |
370 |
0 |
retVal.putAll(getComponentMap(component, nested)); |
371 |
|
} |
372 |
|
} |
373 |
0 |
return retVal; |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
|
380 |
|
@param |
381 |
|
|
382 |
|
@return |
383 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
384 |
0 |
public static UIDefaults getUIDefaultsOfClass(Class clazz)... |
385 |
|
{ |
386 |
0 |
String name = clazz.getName(); |
387 |
0 |
name = name.substring(name.lastIndexOf(".") + 2); |
388 |
0 |
return getUIDefaultsOfClass(name); |
389 |
|
} |
390 |
|
|
391 |
|
|
392 |
|
|
393 |
|
|
394 |
|
|
395 |
|
@param |
396 |
|
|
397 |
|
@return |
398 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 4 |
Complexity Density: 0.36 |
|
399 |
0 |
public static UIDefaults getUIDefaultsOfClass(String className)... |
400 |
|
{ |
401 |
0 |
UIDefaults retVal = new UIDefaults(); |
402 |
0 |
UIDefaults defaults = UIManager.getLookAndFeelDefaults(); |
403 |
0 |
List<?> listKeys = Collections.list(defaults.keys()); |
404 |
0 |
for (Object key : listKeys) |
405 |
|
{ |
406 |
0 |
if (key instanceof String && ((String) key).startsWith(className)) |
407 |
|
{ |
408 |
0 |
String stringKey = (String) key; |
409 |
0 |
String property = stringKey; |
410 |
0 |
if (stringKey.contains(".")) |
411 |
|
{ |
412 |
0 |
property = stringKey.substring(stringKey.indexOf(".") + 1); |
413 |
|
} |
414 |
0 |
retVal.put(property, defaults.get(key)); |
415 |
|
} |
416 |
|
} |
417 |
0 |
return retVal; |
418 |
|
} |
419 |
|
|
420 |
|
|
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
@param |
425 |
|
|
426 |
|
@param |
427 |
|
|
428 |
|
@return |
429 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
430 |
0 |
public static Object getUIDefaultOfClass(Class clazz, String property)... |
431 |
|
{ |
432 |
0 |
Object retVal = null; |
433 |
0 |
UIDefaults defaults = getUIDefaultsOfClass(clazz); |
434 |
0 |
List<Object> listKeys = Collections.list(defaults.keys()); |
435 |
0 |
for (Object key : listKeys) |
436 |
|
{ |
437 |
0 |
if (key.equals(property)) |
438 |
|
{ |
439 |
0 |
return defaults.get(key); |
440 |
|
} |
441 |
0 |
if (key.toString().equalsIgnoreCase(property)) |
442 |
|
{ |
443 |
0 |
retVal = defaults.get(key); |
444 |
|
} |
445 |
|
} |
446 |
0 |
return retVal; |
447 |
|
} |
448 |
|
|
449 |
|
|
450 |
|
|
451 |
|
|
452 |
|
static Set<String> setExclude = new HashSet<String>(); |
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
453 |
0 |
static... |
454 |
|
{ |
455 |
0 |
setExclude.add("getFocusCycleRootAncestor"); |
456 |
0 |
setExclude.add("getAccessibleContext"); |
457 |
0 |
setExclude.add("getColorModel"); |
458 |
0 |
setExclude.add("getGraphics"); |
459 |
0 |
setExclude.add("getGraphicsConfiguration"); |
460 |
|
} |
461 |
|
|
462 |
|
|
463 |
|
|
464 |
|
|
465 |
|
|
466 |
|
|
467 |
|
|
468 |
|
|
469 |
|
@param |
470 |
|
|
471 |
|
@return |
472 |
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 11 |
Complexity Density: 0.79 |
|
473 |
0 |
public static Map<Object, Object> getProperties(JComponent component)... |
474 |
|
{ |
475 |
0 |
Map<Object, Object> retVal = new HashMap<Object, Object>(); |
476 |
0 |
Class<?> clazz = component.getClass(); |
477 |
0 |
Method[] methods = clazz.getMethods(); |
478 |
0 |
Object value = null; |
479 |
0 |
for (Method method : methods) |
480 |
|
{ |
481 |
0 |
if (method.getName().matches("^(is|get).*") |
482 |
|
&& method.getParameterTypes().length == 0) |
483 |
|
{ |
484 |
0 |
try |
485 |
|
{ |
486 |
0 |
Class returnType = method.getReturnType(); |
487 |
0 |
if (returnType != void.class |
488 |
|
&& !returnType.getName().startsWith("[") |
489 |
|
&& !setExclude.contains(method.getName())) |
490 |
|
{ |
491 |
0 |
String key = method.getName(); |
492 |
0 |
value = method.invoke(component); |
493 |
0 |
if (value != null && !(value instanceof Component)) |
494 |
|
{ |
495 |
0 |
retVal.put(key, value); |
496 |
|
} |
497 |
|
} |
498 |
|
|
499 |
|
} catch (IllegalAccessException ex) |
500 |
|
{ |
501 |
|
} catch (IllegalArgumentException ex) |
502 |
|
{ |
503 |
|
} catch (InvocationTargetException ex) |
504 |
|
{ |
505 |
|
} |
506 |
|
} |
507 |
|
} |
508 |
0 |
return retVal; |
509 |
|
} |
510 |
|
|
511 |
|
|
512 |
|
|
513 |
|
|
514 |
|
|
515 |
|
@param |
516 |
|
|
517 |
|
@return |
518 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
519 |
0 |
public static <T extends JComponent> Class getJClass(T component)... |
520 |
|
{ |
521 |
0 |
Class<?> clazz = component.getClass(); |
522 |
0 |
while (!clazz.getName().matches("javax.swing.J[^.]*$")) |
523 |
|
{ |
524 |
0 |
clazz = clazz.getSuperclass(); |
525 |
|
} |
526 |
0 |
return clazz; |
527 |
|
} |
528 |
|
} |