1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.sail.core.util;
24
25 import java.awt.Image;
26 import java.beans.BeanInfo;
27 import java.beans.IntrospectionException;
28 import java.beans.Introspector;
29 import java.beans.beancontext.BeanContext;
30 import java.beans.beancontext.BeanContextChild;
31 import java.beans.beancontext.BeanContextServices;
32 import java.io.PrintStream;
33 import java.net.URL;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.HashSet;
37 import java.util.logging.Level;
38 import java.util.logging.Logger;
39
40 import net.sf.sail.common.beansupport.IIconProvider;
41
42
43
44
45
46
47
48
49 public class SailBeanUtils {
50
51
52
53 private static final Logger logger = Logger.getLogger(SailBeanUtils.class
54 .getName());
55
56 public static void printTree(PrintStream out, int indent,
57 Collection<?> collection) {
58 for (int i = 0; i < indent; i += 1)
59 out.print(" ");
60 out.println(collection);
61 for (Object object : collection) {
62 if (object instanceof Collection)
63 printTree(out, indent + 4, (Collection<?>) object);
64 else {
65 for (int i = 0; i < indent; i += 1)
66 out.print(" ");
67 out.print("\\ (");
68 out.print(object.getClass().getSimpleName());
69 out.print(") ");
70 out.println(object);
71 }
72 }
73 }
74
75 public static <T> T firstChildOfType(BeanContext bc, Class<T> type) {
76 Collection<T> collection = SailBeanUtils.childrenOfType(bc, type);
77 return collection.iterator().next();
78 }
79
80 public static <T> Collection<T> childrenOfType(BeanContext bc, Class<T> type) {
81 Collection<T> found = new HashSet<T>();
82 findChildrenOfType(bc, type, found);
83 return Collections.unmodifiableCollection(found);
84 }
85
86 @SuppressWarnings("unchecked")
87 static <T> void findChildrenOfType(BeanContext bc, Class<T> type,
88 Collection<T> found) {
89 for (Object object : bc) {
90 if (type.isInstance(object)) {
91 final T typedObject = (T) object;
92 found.add(typedObject);
93 }
94 if (object instanceof BeanContext) {
95 BeanContext child = (BeanContext) object;
96 findChildrenOfType(child, type, found);
97 }
98 }
99 }
100
101 public static Image getLargeBeanIcon(Object bean) {
102 if (bean instanceof IIconProvider) {
103 URL iconUrl = ((IIconProvider) bean)
104 .getIcon(BeanInfo.ICON_COLOR_32x32);
105 if (iconUrl != null) {
106 return java.awt.Toolkit.getDefaultToolkit().getImage(iconUrl);
107 }
108 }
109
110 try {
111 BeanInfo info = Introspector.getBeanInfo(bean.getClass());
112 return info.getIcon(BeanInfo.ICON_COLOR_32x32);
113 } catch (IntrospectionException e) {
114 logger.log(Level.SEVERE, "Class -", e);
115 return null;
116 }
117 }
118
119 public static Image getSmallBeanIcon(Object bean) {
120 if (bean instanceof IIconProvider) {
121 URL iconUrl = ((IIconProvider) bean)
122 .getIcon(BeanInfo.ICON_COLOR_16x16);
123 if (iconUrl != null) {
124 return java.awt.Toolkit.getDefaultToolkit().getImage(iconUrl);
125 }
126 }
127
128 try {
129 BeanInfo info = Introspector.getBeanInfo(bean.getClass());
130 Image icon = info.getIcon(BeanInfo.ICON_COLOR_16x16);
131 return icon;
132 } catch (IntrospectionException e) {
133 logger.log(Level.SEVERE, "Class -", e);
134 return null;
135 }
136 }
137
138 @Deprecated
139 public static String shortNameOf(Class<?> clazz) {
140 return clazz.getSimpleName();
141 }
142
143 public static BeanContextServices findServicesContextOf(
144 BeanContextChild child) {
145 BeanContext bc = child.getBeanContext();
146
147 if (bc == null)
148 throw new RuntimeException("BeanContextChild has no bean context");
149
150 if (!(bc instanceof BeanContextServices))
151 throw new RuntimeException(
152 "BeanContextChild within a non BCS context");
153
154 BeanContextServices bcs = (BeanContextServices) bc;
155 return bcs;
156 }
157
158
159
160
161
162
163
164 public static BeanContext resolveBeanContext(Object source) {
165 if (source instanceof BeanContextChild) {
166 BeanContextChild bcc = (BeanContextChild) source;
167 BeanContext pod = bcc.getBeanContext();
168 return pod;
169 }
170 return null;
171 }
172
173
174
175
176
177
178 @Deprecated
179 public static BeanContext resolvePod(Object source) {
180 return SailBeanUtils.resolvePod(source);
181 }
182
183 }