1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dishevelled.identify;
25
26 import java.awt.ComponentOrientation;
27 import java.beans.BeanInfo;
28 import java.beans.BeanDescriptor;
29 import java.beans.Introspector;
30 import java.beans.PropertyDescriptor;
31 import java.beans.IntrospectionException;
32
33 import java.lang.reflect.Method;
34 import java.lang.reflect.InvocationTargetException;
35
36 import java.net.URL;
37
38 import javax.swing.JLabel;
39 import javax.swing.LookAndFeel;
40 import javax.swing.UIManager;
41
42 import org.dishevelled.iconbundle.IconBundle;
43 import org.dishevelled.iconbundle.IconTextDirection;
44
45 import org.dishevelled.iconbundle.impl.PNGIconBundle;
46
47
48
49
50
51
52
53 public final class IdentifyUtils
54 {
55
56 private NameStrategy nameStrategy;
57
58
59 private IconBundleStrategy iconBundleStrategy;
60
61
62 private IconBundle defaultIconBundle;
63
64
65 private static IdentifyUtils instance;
66
67
68 private static final String GTK_LOOK_AND_FEEL_CLASS_NAME = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
69
70
71 private static final String JDK15_MAC_OSX_LOOK_AND_FEEL_CLASS_NAME = "apple.laf.AquaLookAndFeel";
72
73
74 private static final String JDK16_MAC_OSX_LOOK_AND_FEEL_CLASS_NAME = "com.apple.laf.AquaLookAndFeel";
75
76
77 private static final String WINDOWS_LOOK_AND_FEEL_CLASS_NAME = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
78
79
80
81
82
83 private IdentifyUtils()
84 {
85 nameStrategy = new DefaultNameStrategy();
86 iconBundleStrategy = new DefaultIconBundleStrategy();
87
88 URL url = getClass().getResource("default.png");
89 defaultIconBundle = new PNGIconBundle(url);
90 }
91
92
93
94
95
96
97
98 public static IdentifyUtils getInstance()
99 {
100 if (instance == null)
101 {
102 instance = new IdentifyUtils();
103 }
104 return instance;
105 }
106
107
108
109
110
111
112
113
114
115
116
117 public static String getNameFor(final Object bean)
118 {
119 return getInstance().getNameStrategy().getNameFor(bean);
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134 public static IconBundle getIconBundleFor(final Object bean)
135 {
136 return getInstance().getIconBundleStrategy().getIconBundleFor(bean);
137 }
138
139
140
141
142
143
144
145 public static boolean isGTKLookAndFeel()
146 {
147 LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
148 if (lookAndFeel == null)
149 {
150 return false;
151 }
152 return GTK_LOOK_AND_FEEL_CLASS_NAME.equals(lookAndFeel.getClass().getName());
153 }
154
155
156
157
158
159
160
161
162 public static boolean isMacOSXLookAndFeel()
163 {
164 LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
165 if (lookAndFeel == null)
166 {
167 return false;
168 }
169 return JDK15_MAC_OSX_LOOK_AND_FEEL_CLASS_NAME.equals(lookAndFeel.getClass().getName())
170 || JDK16_MAC_OSX_LOOK_AND_FEEL_CLASS_NAME.equals(lookAndFeel.getClass().getName());
171 }
172
173
174
175
176
177
178
179 public static boolean isWindowsLookAndFeel()
180 {
181 LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
182 if (lookAndFeel == null)
183 {
184 return false;
185 }
186 return WINDOWS_LOOK_AND_FEEL_CLASS_NAME.equals(lookAndFeel.getClass().getName());
187 }
188
189
190
191
192
193
194
195 public NameStrategy getNameStrategy()
196 {
197 return nameStrategy;
198 }
199
200
201
202
203
204
205
206 public void setNameStrategy(final NameStrategy nameStrategy)
207 {
208 if (nameStrategy == null)
209 {
210 throw new IllegalArgumentException("nameStrategy must not be null");
211 }
212 this.nameStrategy = nameStrategy;
213 }
214
215
216
217
218
219
220
221 public IconBundleStrategy getIconBundleStrategy()
222 {
223 return iconBundleStrategy;
224 }
225
226
227
228
229
230
231
232
233 public void setIconBundleStrategy(final IconBundleStrategy iconBundleStrategy)
234 {
235 if (iconBundleStrategy == null)
236 {
237 throw new IllegalArgumentException("iconBundleStrategy must not be null");
238 }
239 this.iconBundleStrategy = iconBundleStrategy;
240 }
241
242
243
244
245
246
247
248
249 public IconBundle getDefaultIconBundle()
250 {
251 return defaultIconBundle;
252 }
253
254
255
256
257
258
259
260 public void setDefaultIconBundle(final IconBundle defaultIconBundle)
261 {
262 if (defaultIconBundle == null)
263 {
264 throw new IllegalArgumentException("defaultIconBundle must not be null");
265 }
266 this.defaultIconBundle = defaultIconBundle;
267 }
268
269
270
271
272
273
274
275
276
277 interface NameStrategy
278 {
279
280
281
282
283
284
285
286
287
288 String getNameFor(Object bean);
289 }
290
291
292
293
294 interface IconBundleStrategy
295 {
296
297
298
299
300
301
302
303
304
305
306
307
308 IconBundle getIconBundleFor(Object bean);
309 }
310
311
312
313
314
315
316
317
318
319
320 private class DefaultNameStrategy
321 implements NameStrategy
322 {
323
324 @Override
325 public String getNameFor(final Object bean)
326 {
327 if (bean == null)
328 {
329 return "null";
330 }
331 if (bean instanceof Identifiable)
332 {
333 return ((Identifiable) bean).getName();
334 }
335
336 try
337 {
338 BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
339 BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
340
341 if (beanDescriptor instanceof IdentifiableBeanDescriptor)
342 {
343 int namePropertyIndex = ((IdentifiableBeanDescriptor) beanDescriptor).getNamePropertyIndex();
344 if (namePropertyIndex != -1)
345 {
346 PropertyDescriptor namePropertyDescriptor = beanInfo.getPropertyDescriptors()[namePropertyIndex];
347 Method readMethod = namePropertyDescriptor.getReadMethod();
348 String name = (String) readMethod.invoke(bean, new Object[] {});
349 return name;
350 }
351 }
352 }
353 catch (IntrospectionException ie)
354 {
355
356 }
357 catch (IllegalAccessException iae)
358 {
359
360 }
361 catch (InvocationTargetException ite)
362 {
363
364 }
365 return bean.toString();
366 }
367 }
368
369
370
371
372
373
374
375
376
377
378
379
380 private class DefaultIconBundleStrategy
381 implements IconBundleStrategy
382 {
383
384 @Override
385 public IconBundle getIconBundleFor(final Object bean)
386 {
387 if (bean == null)
388 {
389 return null;
390 }
391 if (bean instanceof Identifiable)
392 {
393 return ((Identifiable) bean).getIconBundle();
394 }
395
396 try
397 {
398 BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
399 BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
400
401 if (beanDescriptor instanceof IdentifiableBeanDescriptor)
402 {
403 return ((IdentifiableBeanDescriptor) beanDescriptor).getIconBundle();
404 }
405 }
406 catch (IntrospectionException ie)
407 {
408
409 }
410 return defaultIconBundle;
411 }
412 }
413
414
415
416
417
418
419
420
421
422 static IconTextDirection determineTextDirection(final JLabel label)
423 {
424 ComponentOrientation componentOrientation = label.getComponentOrientation();
425 return componentOrientation.isLeftToRight() ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
426 }
427 }