1 19 20 package org.netbeans.modules.form; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.lang.reflect.Method ; 25 import java.lang.ref.Reference ; 26 import java.lang.ref.SoftReference ; 27 import java.util.*; 28 import org.netbeans.modules.form.fakepeer.FakePeerSupport; 29 import org.openide.util.Utilities; 30 31 37 public class BeanSupport 38 { 39 public static final Object NO_VALUE = new Object (); 40 41 44 private static Map errorEmptyMap = new HashMap(3); 45 private static Map valuesCache = new HashMap(30); 46 private static Map instancesCache = new HashMap(30); 47 private static Map deviationsCache = new HashMap(); 48 49 52 59 public static Object createBeanInstance(Class beanClass) { 60 try { 61 return CreationFactory.createDefaultInstance(beanClass); 62 } 63 catch (Exception ex) { 64 if (Boolean.getBoolean("netbeans.debug.exceptions")) ex.printStackTrace(); 66 System.err.println("[WARNING] BeanSupport cannot create default instance of: "+beanClass.getName()); 67 return null; 68 } 69 } 70 71 72 80 public static Object getDefaultInstance(Class beanClass) { 81 Object defInstance = instancesCache.get(beanClass); 82 if (defInstance == null) { 83 defInstance = createBeanInstance(beanClass); 84 if (defInstance instanceof Component) { 85 FakePeerSupport.attachFakePeer((Component)defInstance); 86 if (defInstance instanceof Container) 87 FakePeerSupport.attachFakePeerRecursively( 88 (Container)defInstance); 89 } 90 91 if (defInstance instanceof javax.swing.JTextField ) { 94 Object [] values = new Object [2]; 95 javax.swing.JTextField jtf = (javax.swing.JTextField ) defInstance; 96 values[0] = jtf.getBackground(); 97 jtf.setEditable(false); 98 values[1] = jtf.getBackground(); 99 jtf.setEditable(true); 100 101 Map deviationMap = new HashMap(); 102 deviationMap.put( 103 "background", new DefaultValueDeviation(values) { 105 Object getValue(Object beanInstance) { 106 return ((javax.swing.JTextField )beanInstance).isEditable() ? 107 this.values[0] : this.values[1]; 108 } 109 } 110 ); 111 112 deviationsCache.put(beanClass, deviationMap); 113 } 114 115 instancesCache.put(beanClass, defInstance); 116 } 117 return defInstance; 118 } 119 120 135 136 public static Map getDefaultPropertyValues(Class beanClass) { 137 Map defValues =(Map) valuesCache.get(beanClass); 138 if (defValues == null) { 139 Object beanInstance = getDefaultInstance(beanClass); 140 if (beanInstance == null) 141 return errorEmptyMap; 142 defValues = getPropertyValues(beanInstance); 143 valuesCache.put(beanClass, defValues); 144 } 145 return defValues; 146 } 147 148 166 public static Object getDefaultPropertyValue(Object bean, 167 String propertyName) 168 { 169 Map deviationMap = (Map) deviationsCache.get(bean.getClass()); 170 if (deviationMap != null) { 171 DefaultValueDeviation deviation = (DefaultValueDeviation) 172 deviationMap.get(propertyName); 173 if (deviation != null) 174 return deviation.getValue(bean); 175 } 176 177 Map valuesMap = getDefaultPropertyValues(bean.getClass()); 178 Object value = valuesMap.get(propertyName); 179 return value != null || valuesMap.containsKey(propertyName) ? 180 value : NO_VALUE; 181 } 182 183 189 public static Map getPropertyValues(Object beanInstance) { 190 if (beanInstance == null) { 191 return errorEmptyMap; 192 } 193 194 BeanInfo info; 195 try { 196 info = FormUtils.getBeanInfo(beanInstance.getClass()); 197 } catch (IntrospectionException ex) { 198 return errorEmptyMap; 199 } 200 PropertyDescriptor[] properties = info.getPropertyDescriptors(); 201 HashMap defaultValues = new HashMap(properties.length * 2); 202 203 for (int i = 0; i < properties.length; i++) { 204 defaultValues.put(properties[i].getName(), NO_VALUE); 205 206 Method readMethod = properties[i].getReadMethod(); 207 if (readMethod != null) { 208 try { 209 Object value = readMethod.invoke(beanInstance, new Object [0]); 210 defaultValues.put(properties[i].getName(), value); 211 } catch (Exception e) { 212 } 213 } 214 } 215 216 return defaultValues; 217 } 218 219 223 public static Image getBeanIcon(Class beanClass, int iconType) { 224 return getIconForDefault(beanClass); 225 } 226 227 233 public static Class getAdapterForListener(Class listener) { 234 if (java.awt.event.ComponentListener .class.equals(listener)) 235 return java.awt.event.ComponentAdapter .class; 236 else if (java.awt.event.ContainerListener .class.equals(listener)) 237 return java.awt.event.ContainerAdapter .class; 238 else if (java.awt.event.FocusListener .class.equals(listener)) 239 return java.awt.event.FocusAdapter .class; 240 else if (java.awt.event.KeyListener .class.equals(listener)) 241 return java.awt.event.KeyAdapter .class; 242 else if (java.awt.event.MouseListener .class.equals(listener)) 243 return java.awt.event.MouseAdapter .class; 244 else if (java.awt.event.MouseMotionListener .class.equals(listener)) 245 return java.awt.event.MouseMotionAdapter .class; 246 else if (java.awt.event.WindowListener .class.equals(listener)) 247 return java.awt.event.WindowAdapter .class; 248 else return null; } 250 251 254 static Reference <Map<String ,Object >> imageCache; 255 256 private static synchronized Image getIconForDefault(Class klass) { 257 Map<String ,Object > icons; 258 if ((imageCache == null) || ((icons = imageCache.get()) == null)) { 259 icons = createImageCache(); 260 imageCache = new SoftReference <Map<String ,Object >>(icons); 261 } 262 263 String name = klass.getName(); 264 Object img = icons.get(name); 265 266 if (img == null) { 267 return null; 268 } 269 270 if (img instanceof Image) { 271 return (Image) img; 272 } else { 273 Image image = Utilities.loadImage((String )img); 274 icons.put(name, image); 275 return image; 276 } 277 } 278 279 private static Map<String , Object > createImageCache() { 280 Map<String , Object > map = new HashMap<String , Object >(); 281 282 map.put("java.awt.Label", "org/netbeans/modules/form/beaninfo/awt/label.gif"); map.put("java.awt.Button", "org/netbeans/modules/form/beaninfo/awt/button.gif"); map.put("java.awt.TextField", "org/netbeans/modules/form/beaninfo/awt/textfield.gif"); map.put("java.awt.TextArea", "org/netbeans/modules/form/beaninfo/awt/textarea.gif"); map.put("java.awt.Checkbox", "org/netbeans/modules/form/beaninfo/awt/checkbox.gif"); map.put("java.awt.Choice", "org/netbeans/modules/form/beaninfo/awt/choice.gif"); map.put("java.awt.List", "org/netbeans/modules/form/beaninfo/awt/list.gif"); map.put("java.awt.Scrollbar", "org/netbeans/modules/form/beaninfo/awt/scrollbar.gif"); map.put("java.awt.ScrollPane", "org/netbeans/modules/form/beaninfo/awt/scrollpane.gif"); map.put("java.awt.Panel", "org/netbeans/modules/form/beaninfo/awt/panel.gif"); map.put("java.awt.Canvas", "org/netbeans/modules/form/beaninfo/awt/canvas.gif"); map.put("java.awt.MenuBar", "org/netbeans/modules/form/beaninfo/awt/menubar.gif"); map.put("java.awt.PopupMenu", "org/netbeans/modules/form/beaninfo/awt/popupmenu.gif"); map.put("java.awt.Menu", "org/netbeans/modules/form/resources/menu.gif"); map.put("java.awt.MenuItem", "org/netbeans/modules/form/resources/menuItem.gif"); map.put("java.awt.CheckboxMenuItem", "org/netbeans/modules/form/resources/menuItemCheckbox.gif"); map.put("org.netbeans.modulres.form.Separator", "org/netbeans/modules/form/resources/menuSeparator.gif"); 300 map.put("java.applet.Applet", "org/netbeans/modules/form/resources/applet.gif"); map.put("java.awt.Dialog", "org/netbeans/modules/form/resources/dialog.gif"); map.put("java.awt.Frame", "org/netbeans/modules/form/resources/frame.gif"); 304 return map; 305 } 306 307 private static abstract class DefaultValueDeviation { 308 protected Object [] values; 309 DefaultValueDeviation(Object [] values) { 310 this.values = values; 311 } 312 abstract Object getValue(Object beanInstance); 313 } 314 } 315 | Popular Tags |