1 /* -------------------------------------------------------------------
2 * Java source file for the class JBeansBeanInfo
3 *
4 * Copyright (c), 2002, Masahiro Takatsuka.
5 * All Rights Researved.
6 *
7 * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8 * $Author: takatsukam $
9 *
10 * $Date: 2003/07/25 04:51:44 $
11 *
12 * $Id: JBeansBeanInfo.java,v 1.1.1.1 2003/07/25 04:51:44 takatsukam Exp $
13 *
14 * Reference: Document no:
15 * ___ ___
16 *
17 * To Do:
18 * ___
19 *
20 ------------------------------------------------------------------- */
21
22 /* --------------------------- Package ---------------------------- */
23 package net.jbeans.bean;
24
25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */
26 import java.awt.*;
27 import java.beans.*;
28
29 /*====================================================================
30 Implementation of class JBeansBeanInfo
31 ====================================================================*/
32 /***
33 * JBeansBeanInfo adds few more helper methods to java.beans.SimpleBeanInfo.
34 *
35 * @version $Revision: 1.1.1.1 $
36 * @author Masahiro Takatsuka (masa@jbeans.net)
37 * @see SimpleBeanInfo
38 */
39
40 public abstract class JBeansBeanInfo extends SimpleBeanInfo {
41 public static final String BOUND = "bound";
42 public static final String CONSTRAINED = "constrained";
43 public static final String PROPERTYEDITORCLASS = "propertyEditorClass";
44 public static final String READMETHOD = "readMethod";
45 public static final String WRITEMETHOD = "writeMethod";
46 public static final String DISPLAYNAME = "displayName";
47 public static final String EXPERT = "expert";
48 public static final String HIDDEN = "hidden";
49 public static final String PREFERRED = "preferred";
50 public static final String SHORTDESCRIPTION = "shortDescription";
51 public static final String CUSTOMIZERCLASS = "customizerClass";
52
53 public JBeansBeanInfo() {
54 }
55
56 protected void throwError(Exception exception, String s) {
57 throw new Error(exception.toString() + " " + s);
58 }
59
60 private void initFeatureDescriptor(FeatureDescriptor featuredescriptor, String s, Object obj) {
61 if (DISPLAYNAME.equals(s)) {
62 featuredescriptor.setDisplayName((String)obj);
63 }
64 if (EXPERT.equals(s)) {
65 featuredescriptor.setExpert(((Boolean)obj).booleanValue());
66 }
67 if (HIDDEN.equals(s)) {
68 featuredescriptor.setHidden(((Boolean)obj).booleanValue());
69 }
70 if(PREFERRED.equals(s)) {
71 featuredescriptor.setPreferred(((Boolean)obj).booleanValue());
72 if (PREFERRED.equals(s)) {
73 featuredescriptor.setValue(s, obj);
74 }
75 } else if (SHORTDESCRIPTION.equals(s)) {
76 featuredescriptor.setShortDescription((String)obj);
77 } else {
78 featuredescriptor.setValue(s, obj);
79 }
80 }
81
82 public PropertyDescriptor createPropertyDescriptor(Class class1, String s, Object aobj[]) {
83 PropertyDescriptor propertydescriptor = null;
84 try {
85 propertydescriptor = new PropertyDescriptor(s, class1);
86 } catch(IntrospectionException introspectionexception) {
87 try {
88 propertydescriptor = createReadOnlyPropertyDescriptor(s, class1);
89 } catch(IntrospectionException introspectionexception1) {
90 throwError(introspectionexception1, "Can't create PropertyDescriptor for " + s + " ");
91 }
92 }
93 for (int i = 0; i < aobj.length; i += 2) {
94 String s1 = (String)aobj[i];
95 Object obj = aobj[i + 1];
96 if (BOUND.equals(s1)){
97 propertydescriptor.setBound(((Boolean)obj).booleanValue());
98 } else if (CONSTRAINED.equals(s1)) {
99 propertydescriptor.setConstrained(((Boolean)obj).booleanValue());
100 } else if (PROPERTYEDITORCLASS.equals(s1)) {
101 propertydescriptor.setPropertyEditorClass((Class)obj);
102 } else if(READMETHOD.equals(s1)) {
103 String s2 = (String)obj;
104 java.lang.reflect.Method method;
105 try {
106 method = class1.getMethod(s2, new Class[0]);
107 } catch(Exception exception) {
108 throwError(exception, class1 + " no such method as \"" + s2 + "\"");
109 }
110 } else if(WRITEMETHOD.equals(s1)) {
111 String s3 = (String)obj;
112 try {
113 Class class2 = propertydescriptor.getPropertyType();
114 java.lang.reflect.Method method1 = class1.getMethod(s3, new Class[] {class2});
115 } catch(Exception exception1) {
116 throwError(exception1, class1 + " no such method as \"" + s3 + "\"");
117 }
118 } else {
119 initFeatureDescriptor(propertydescriptor, s1, obj);
120 }
121 }
122
123 return propertydescriptor;
124 }
125
126 public BeanDescriptor createBeanDescriptor(Class class1, Object aobj[]) {
127 Class class2 = null;
128 for (int i = 0; i < aobj.length; i += 2) {
129 if (!CUSTOMIZERCLASS.equals((String)aobj[i])) {
130 continue;
131 }
132 class2 = (Class)aobj[i + 1];
133 break;
134 }
135
136 BeanDescriptor beandescriptor = new BeanDescriptor(class1, class2);
137 for (int j = 0; j < aobj.length; j += 2) {
138 String s = (String)aobj[j];
139 Object obj = aobj[j + 1];
140 initFeatureDescriptor(beandescriptor, s, obj);
141 }
142
143 return beandescriptor;
144 }
145
146 public int getDefaultPropertyIndex() {
147 return 0;
148 }
149
150 public BeanInfo[] getAdditionalBeanInfo() {
151 Class class1 = getBeanDescriptor().getBeanClass().getSuperclass();
152 BeanInfo beaninfo = null;
153 try {
154 beaninfo = Introspector.getBeanInfo(class1);
155 } catch(IntrospectionException introspectionexception) { }
156 if (beaninfo != null) {
157 BeanInfo abeaninfo[] = new BeanInfo[1];
158 abeaninfo[0] = beaninfo;
159 return abeaninfo;
160 } else {
161 return null;
162 }
163 }
164
165 /***
166 * Utility method to take a string and convert it to normal Java Class
167 * name capitalization. This normally means converting the first
168 * character from lower case to upper case.
169 *
170 * @param name The string to be capitalized.
171 * @return The capitalized version of the string.
172 */
173 private final String capitalize(String name) {
174 if (name == null || name.length() == 0) {
175 return name;
176 }
177 char chars[] = name.toCharArray();
178 chars[0] = Character.toUpperCase(chars[0]);
179 return new String(chars);
180 }
181
182 private PropertyDescriptor createReadOnlyPropertyDescriptor(String s, Class class1) throws IntrospectionException {
183 java.lang.reflect.Method method = null;
184 String s1 = capitalize(s);
185 Class aclass[] = new Class[0];
186 try {
187 method = class1.getMethod("is" + s1, aclass);
188 } catch(Exception exception) { }
189 if (method == null) {
190 try {
191 method = class1.getMethod("get" + s1, aclass);
192 } catch(Exception exception1) { }
193 }
194 if (method != null) {
195 return new PropertyDescriptor(s, method, null);
196 }
197 try {
198 Class aclass1[] = new Class[1];
199 aclass1[0] = Integer.TYPE;
200 method = class1.getMethod("get" + s1, aclass1);
201 } catch(NoSuchMethodException nosuchmethodexception) {
202 throw new IntrospectionException("cannot find accessor method for " + s + " property.");
203 }
204 return new IndexedPropertyDescriptor(s, null, null, method, null);
205 }
206
207 /***
208 * Returns a icons of the specified size.
209 * <PRE>
210 * </PRE>
211 *
212 * @param iconKind an ID (ICON_COLOR_16x16 or ICON_COLOR_32x32)
213 * indicating the size of the icon.
214 * @return Image
215 */
216 public Image getIcon(int iconKind) {
217 return null;
218 }
219
220 /***
221 * This is a utility method to help in loading icon images.
222 * It takes the name of a resource file associated with the
223 * current object's class file and loads an image object
224 * from that file. Typically images will be GIFs.
225 * <p>
226 * @param resourceName A pathname relative to the directory
227 * holding the class file of the current class. For example,
228 * "wombat.gif".
229 * @return an image object. May be null if the load failed.
230 */
231 public java.awt.Image loadImage(final String resourceName, final Class beanClass) {
232 try {
233 java.awt.image.ImageProducer ip = (java.awt.image.ImageProducer)
234 java.security.AccessController.doPrivileged(
235 new java.security.PrivilegedAction() {
236 public Object run() {
237 java.net.URL url;
238 if ((url = beanClass.getResource(resourceName)) == null) {
239 return null;
240 } else {
241 try {
242 return url.getContent();
243 } catch (java.io.IOException ioe) {
244 return null;
245 }
246 }
247 }
248 });
249
250 if (ip == null)
251 return null;
252 java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();
253 return tk.createImage(ip);
254 } catch (Exception ex) {
255 return null;
256 }
257 }
258 }
This page was automatically generated by Maven