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 SimpleBeanBeanInfo extends SimpleBeanInfo 31 { 32 33 protected BeanDescriptor bd = new BeanDescriptor (test.javabean.SimpleBean.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 44 public SimpleBeanBeanInfo() throws java.beans.IntrospectionException 45 { 46 bd.setName("SimpleBean"); 48 49 bd.setDisplayName("Simple Bean"); 50 bd.setShortDescription("Simple example of JavaBean BeanInfo generation"); 51 52 bd.setValue("literal","A sample attribute"); 53 bd.setValue("expression",new StringBuffer ()); 54 55 Class infoSourceClass = getBeanDescriptor().getBeanClass().isInterface() 56 ? Object .class : getBeanDescriptor().getBeanClass().getSuperclass(); 57 BeanInfo info = Introspector.getBeanInfo(infoSourceClass); 58 String order = info.getBeanDescriptor().getValue("propertyorder") == null 59 ? "" : (String ) info.getBeanDescriptor().getValue("propertyorder"); 60 PropertyDescriptor [] pd = getPropertyDescriptors(); 61 for (int i = 0; i != pd.length; i++) 62 { 63 if (order.indexOf(pd[i].getName()) == -1) 64 { 65 order = order + (order.length() == 0 ? "" : ":") + pd[i].getName(); 66 } 67 } 68 getBeanDescriptor().setValue("propertyorder", order); 69 } 70 71 76 public BeanInfo [] getAdditionalBeanInfo() 77 { 78 Vector bi = new Vector (); 79 BeanInfo [] biarr = null; 80 try 81 { 82 } 83 catch (Exception e) 84 { 85 } 87 return biarr; 88 } 89 90 95 public BeanDescriptor getBeanDescriptor() 96 { 97 return bd; 98 } 99 100 105 public int getDefaultPropertyIndex() 106 { 107 String defName = ""; 108 if (defName.equals("")) 109 { 110 return -1; 111 } 112 PropertyDescriptor [] pd = getPropertyDescriptors(); 113 for (int i = 0; i < pd.length; i++) 114 { 115 if (pd[i].getName().equals(defName)) 116 { 117 return i; 118 } 119 } 120 return -1; 121 } 122 123 129 public Image getIcon(int type) 130 { 131 if (type == BeanInfo.ICON_COLOR_16x16) 132 { 133 return iconColor16; 134 } 135 if (type == BeanInfo.ICON_MONO_16x16) 136 { 137 return iconMono16; 138 } 139 if (type == BeanInfo.ICON_COLOR_32x32) 140 { 141 return iconColor32; 142 } 143 if (type == BeanInfo.ICON_MONO_32x32) 144 { 145 return iconMono32; 146 } 147 return null; 148 } 149 150 155 public PropertyDescriptor [] getPropertyDescriptors() 156 { 157 160 Vector descriptors = new Vector (); 164 165 try 166 { 167 PropertyDescriptor descriptor = new PropertyDescriptor ("flag", test.javabean.SimpleBean.class, "isFlag", "setFlag"); 168 descriptor.setDisplayName("Class"); 169 descriptor.setShortDescription("Class of the entry"); 170 descriptor.setHidden(false); 171 descriptor.setBound(true); 172 descriptors.add(descriptor); 173 } 174 catch (Exception ex) 175 { 176 ex.printStackTrace(); 177 } 178 179 try 180 { 181 PropertyDescriptor descriptor = new PropertyDescriptor ("name", test.javabean.SimpleBean.class, "getName", "setName"); 182 descriptor.setDisplayName("Name"); 183 descriptor.setShortDescription("Name of the entry"); 184 descriptors.add(descriptor); 185 } 186 catch (Exception ex) 187 { 188 ex.printStackTrace(); 189 } 190 191 try 192 { 193 PropertyDescriptor descriptor = new PropertyDescriptor ("count", test.javabean.SimpleBean.class, "getCount", null); 195 descriptor.setDisplayName("Message(s)"); 196 descriptor.setShortDescription("Number of messages in Queue"); 197 descriptors.add(descriptor); 198 } 199 catch (Exception ex) 200 { 201 ex.printStackTrace(); 202 } 203 204 try 205 { 206 PropertyDescriptor descriptor = new PropertyDescriptor ("parent", test.javabean.SimpleBean.class, "getParent", null); 207 descriptor.setDisplayName("Full Path"); 208 descriptor.setShortDescription("Absolute path of the context"); 209 descriptors.add(descriptor); 210 } 211 catch (Exception ex) 212 { 213 ex.printStackTrace(); 214 } 215 return (PropertyDescriptor []) descriptors.toArray(new PropertyDescriptor [descriptors.size()]); 216 } 217 218 223 public MethodDescriptor [] getMethodDescriptors() { 224 Vector descriptors = new Vector (); 225 MethodDescriptor descriptor = null; 226 Method [] m; 227 Method method; 228 229 try { 230 m = Class.forName("test.javabean.SimpleBean").getMethods(); 231 } catch (ClassNotFoundException e) { 232 return new MethodDescriptor [0]; 233 } 234 235 method = null; 236 for (int j = 0; j != m.length; j++) { 237 if (m[j].getName().equals("myFirstMethod")) { 238 method = m[j]; 239 break; 240 } 241 } 242 243 if (method != null) { 244 ParameterDescriptor [] pd = new ParameterDescriptor [method.getParameterTypes().length]; 245 int pidx = 0; 246 247 if (pidx == 0) { 248 descriptor = new MethodDescriptor (method); 249 } else { 250 descriptor = new MethodDescriptor (method, pd); 251 } 252 descriptor.setName("myFirstMethod"); 253 descriptor.setDisplayName("My First Method"); 254 descriptor.setShortDescription("Example of method without parameters"); 255 256 descriptors.add(descriptor); 257 } 258 259 method = null; 260 for (int j = 0; j != m.length; j++) { 261 if (m[j].getName().equals("mySecondMethod")) { 262 method = m[j]; 263 break; 264 } 265 } 266 267 if (method != null) { 268 ParameterDescriptor [] pd = new ParameterDescriptor [method.getParameterTypes().length]; 269 int pidx = 0; 270 271 pd[pidx] = new ParameterDescriptor (); 272 pd[pidx].setName("param1"); 273 pd[pidx].setDisplayName("Parameter 1"); 274 275 pidx++; 276 277 pd[pidx] = new ParameterDescriptor (); 278 pd[pidx].setName("param2"); 279 pd[pidx].setDisplayName("Parameter 2"); 280 281 pidx++; 282 283 if (pidx == 0) { 284 descriptor = new MethodDescriptor (method); 285 } else { 286 descriptor = new MethodDescriptor (method, pd); 287 } 288 descriptor.setName("mySecondMethod"); 289 descriptor.setDisplayName("My Second Method"); 290 descriptor.setShortDescription("Example of method with parameters"); 291 292 descriptors.add(descriptor); 293 } 294 295 return (MethodDescriptor []) descriptors.toArray(new MethodDescriptor [descriptors.size()]); 296 } 297 } 298 | Popular Tags |