1 4 package test.javabean; 5 6 import java.awt.Image ; 7 import java.beans.BeanDescriptor ; 8 import java.beans.BeanInfo ; 9 import java.beans.Introspector ; 10 import java.beans.IntrospectionException ; 11 import java.beans.PropertyDescriptor ; 12 import java.beans.ParameterDescriptor ; 13 import java.beans.MethodDescriptor ; 14 import java.beans.SimpleBeanInfo ; 15 import java.lang.reflect.Method ; 16 import java.util.ResourceBundle ; 17 import java.util.Vector ; 18 19 29 30 public class SimpleI18NBeanBeanInfo extends SimpleBeanInfo 31 { 32 33 protected BeanDescriptor bd = new BeanDescriptor (test.javabean.SimpleI18NBean.class); 34 35 protected Image iconMono16; 36 37 protected Image iconColor16 = loadImage("/toolbarButtonGraphics/general/Stop16.gif"); 38 39 protected Image iconMono32; 40 41 protected Image iconColor32; 42 43 private final static ResourceBundle res = ResourceBundle.getBundle("test.javabean.SimpleI18NBeanBeanInfo"); 44 45 46 public SimpleI18NBeanBeanInfo() throws java.beans.IntrospectionException 47 { 48 bd.setName("SimpleI18NBean"); 50 51 bd.setDisplayName(res.getString("bean.displayName")); 52 bd.setShortDescription(res.getString("bean.shortDescription")); 53 54 bd.setValue("literal","A sample attribute"); 55 bd.setValue("expression",new StringBuffer ()); 56 57 Class infoSourceClass = getBeanDescriptor().getBeanClass().isInterface() 58 ? Object .class : getBeanDescriptor().getBeanClass().getSuperclass(); 59 BeanInfo info = Introspector.getBeanInfo(infoSourceClass); 60 String order = info.getBeanDescriptor().getValue("propertyorder") == null 61 ? "" : (String ) info.getBeanDescriptor().getValue("propertyorder"); 62 PropertyDescriptor [] pd = getPropertyDescriptors(); 63 for (int i = 0; i != pd.length; i++) 64 { 65 if (order.indexOf(pd[i].getName()) == -1) 66 { 67 order = order + (order.length() == 0 ? "" : ":") + pd[i].getName(); 68 } 69 } 70 getBeanDescriptor().setValue("propertyorder", order); 71 } 72 73 78 public BeanInfo [] getAdditionalBeanInfo() 79 { 80 Vector bi = new Vector (); 81 BeanInfo [] biarr = null; 82 try 83 { 84 } 85 catch (Exception e) 86 { 87 } 89 return biarr; 90 } 91 92 97 public BeanDescriptor getBeanDescriptor() 98 { 99 return bd; 100 } 101 102 107 public int getDefaultPropertyIndex() 108 { 109 String defName = ""; 110 if (defName.equals("")) 111 { 112 return -1; 113 } 114 PropertyDescriptor [] pd = getPropertyDescriptors(); 115 for (int i = 0; i < pd.length; i++) 116 { 117 if (pd[i].getName().equals(defName)) 118 { 119 return i; 120 } 121 } 122 return -1; 123 } 124 125 131 public Image getIcon(int type) 132 { 133 if (type == BeanInfo.ICON_COLOR_16x16) 134 { 135 return iconColor16; 136 } 137 if (type == BeanInfo.ICON_MONO_16x16) 138 { 139 return iconMono16; 140 } 141 if (type == BeanInfo.ICON_COLOR_32x32) 142 { 143 return iconColor32; 144 } 145 if (type == BeanInfo.ICON_MONO_32x32) 146 { 147 return iconMono32; 148 } 149 return null; 150 } 151 152 157 public PropertyDescriptor [] getPropertyDescriptors() 158 { 159 162 Vector descriptors = new Vector (); 166 167 try 168 { 169 PropertyDescriptor descriptor = new PropertyDescriptor ("flag", test.javabean.SimpleBean.class, "isFlag", "setFlag"); 170 descriptor.setDisplayName(res.getString("property.flag.displayName")); 171 descriptor.setShortDescription(res.getString("property.flag.shortDescription")); 172 descriptor.setHidden(false); 173 descriptor.setBound(true); 174 descriptors.add(descriptor); 175 } 176 catch (Exception ex) 177 { 178 ex.printStackTrace(); 179 } 180 181 try 182 { 183 PropertyDescriptor descriptor = new PropertyDescriptor ("name", test.javabean.SimpleBean.class, "getName", "setName"); 184 descriptor.setDisplayName(res.getString("property.name.displayName")); 185 descriptor.setShortDescription(res.getString("property.name.shortDescription")); 186 descriptors.add(descriptor); 187 } 188 catch (Exception ex) 189 { 190 ex.printStackTrace(); 191 } 192 193 try 194 { 195 PropertyDescriptor descriptor = new PropertyDescriptor ("count", test.javabean.SimpleBean.class, "getCount", null); 197 descriptor.setDisplayName(res.getString("property.count.displayName")); 198 descriptor.setShortDescription(res.getString("property.count.shortDescription")); 199 descriptors.add(descriptor); 200 } 201 catch (Exception ex) 202 { 203 ex.printStackTrace(); 204 } 205 206 try 207 { 208 PropertyDescriptor descriptor = new PropertyDescriptor ("parent", test.javabean.SimpleBean.class, "getParent", null); 209 descriptor.setDisplayName(res.getString("property.parent.displayName")); 210 descriptor.setShortDescription(res.getString("property.parent.shortDescription")); 211 descriptors.add(descriptor); 212 } 213 catch (Exception ex) 214 { 215 ex.printStackTrace(); 216 } 217 return (PropertyDescriptor []) descriptors.toArray(new PropertyDescriptor [descriptors.size()]); 218 } 219 220 225 public MethodDescriptor [] getMethodDescriptors() { 226 Vector descriptors = new Vector (); 227 MethodDescriptor descriptor = null; 228 Method [] m; 229 Method method; 230 231 try { 232 m = Class.forName("test.javabean.SimpleI18NBean").getMethods(); 233 } catch (ClassNotFoundException e) { 234 return new MethodDescriptor [0]; 235 } 236 237 method = null; 238 for (int j = 0; j != m.length; j++) { 239 if (m[j].getName().equals("myFirstMethod")) { 240 method = m[j]; 241 break; 242 } 243 } 244 245 if (method != null) { 246 ParameterDescriptor [] pd = new ParameterDescriptor [method.getParameterTypes().length]; 247 int pidx = 0; 248 249 if (pidx == 0) { 250 descriptor = new MethodDescriptor (method); 251 } else { 252 descriptor = new MethodDescriptor (method, pd); 253 } 254 descriptor.setName("myFirstMethod"); 255 descriptor.setDisplayName(res.getString("method.myFirstMethod.displayName")); 256 descriptor.setDisplayName(res.getString("method.myFirstMethod.shortDescription")); 257 258 descriptors.add(descriptor); 259 } 260 261 method = null; 262 for (int j = 0; j != m.length; j++) { 263 if (m[j].getName().equals("mySecondMethod")) { 264 method = m[j]; 265 break; 266 } 267 } 268 269 if (method != null) { 270 ParameterDescriptor [] pd = new ParameterDescriptor [method.getParameterTypes().length]; 271 int pidx = 0; 272 273 pd[pidx] = new ParameterDescriptor (); 274 pd[pidx].setName("param1"); 275 pd[pidx].setDisplayName(res.getString("method.mySecondMethod.param1.displayName")); 276 277 pidx++; 278 279 pd[pidx] = new ParameterDescriptor (); 280 pd[pidx].setName("param2"); 281 pd[pidx].setDisplayName(res.getString("method.mySecondMethod.param2.displayName")); 282 283 pidx++; 284 285 pd[pidx] = new ParameterDescriptor (); 286 pd[pidx].setName("param1"); 287 pd[pidx].setDisplayName(res.getString("method.mySecondMethod.param1.displayName")); 288 289 pidx++; 290 291 pd[pidx] = new ParameterDescriptor (); 292 pd[pidx].setName("param2"); 293 pd[pidx].setDisplayName(res.getString("method.mySecondMethod.param2.displayName")); 294 295 pidx++; 296 297 if (pidx == 0) { 298 descriptor = new MethodDescriptor (method); 299 } else { 300 descriptor = new MethodDescriptor (method, pd); 301 } 302 descriptor.setName("mySecondMethod"); 303 descriptor.setDisplayName(res.getString("method.mySecondMethod.displayName")); 304 descriptor.setDisplayName(res.getString("method.mySecondMethod.shortDescription")); 305 306 descriptors.add(descriptor); 307 } 308 309 return (MethodDescriptor []) descriptors.toArray(new MethodDescriptor [descriptors.size()]); 310 } 311 } 312 | Popular Tags |