1 19 24 package org.openide.explorer.propertysheet; 25 26 import org.openide.nodes.Node; 27 import org.openide.nodes.Node.Property; 28 import org.openide.nodes.Node.PropertySet; 29 import org.openide.nodes.PropertySupport; 30 31 import java.beans.BeanDescriptor ; 32 import java.beans.BeanInfo ; 33 import java.beans.FeatureDescriptor ; 34 import java.beans.Introspector ; 35 import java.beans.PropertyDescriptor ; 36 import java.beans.PropertyEditor ; 37 38 import java.lang.reflect.Constructor ; 39 import java.lang.reflect.InvocationTargetException ; 40 import java.util.logging.Level ; 41 import java.util.logging.Logger ; 42 import org.openide.util.Exceptions; 43 44 45 49 class ModelProperty extends Property { 50 PropertyModel mdl; 51 private boolean valueSet = false; 52 53 54 private ModelProperty(PropertyModel pm) { 55 super(pm.getPropertyType()); 56 this.mdl = pm; 57 58 if (mdl instanceof ExPropertyModel) { 59 FeatureDescriptor fd = ((ExPropertyModel) mdl).getFeatureDescriptor(); 60 Boolean result = (Boolean ) fd.getValue("canEditAsText"); 62 if (result != null) { 63 this.setValue("canEditAsText", result); 64 } 65 } 66 67 } 70 71 Object [] getBeans() { 72 if (mdl instanceof ExPropertyModel) { 73 return ((ExPropertyModel) mdl).getBeans(); 74 } 75 76 return null; 77 } 78 79 public PropertyEditor getPropertyEditor() { 80 if (mdl.getPropertyEditorClass() != null) { 81 try { 82 Constructor c = mdl.getPropertyEditorClass().getConstructor(); 85 c.setAccessible(true); 86 87 return (PropertyEditor ) c.newInstance(new Object [0]); 88 } catch (Exception e) { 89 Exceptions.printStackTrace(e); 90 91 return new PropUtils.NoPropertyEditorEditor(); 92 } 93 } 94 95 return super.getPropertyEditor(); 96 } 97 98 static Property toProperty(PropertyModel mdl) { 99 if (mdl instanceof NodePropertyModel) { 100 return ((NodePropertyModel) mdl).getProperty(); 102 } else if (mdl instanceof DefaultPropertyModel) { 103 return new DPMWrapper((DefaultPropertyModel) mdl); 104 } else if ( 105 mdl instanceof ExPropertyModel && 106 ((ExPropertyModel) mdl).getFeatureDescriptor() instanceof PropertyDescriptor 107 ) { 108 Object [] beans = ((ExPropertyModel) mdl).getBeans(); 109 110 if (beans.length == 1) { 111 return new DPMWrapper( 112 (PropertyDescriptor ) ((ExPropertyModel) mdl).getFeatureDescriptor(), 113 ((ExPropertyModel) mdl).getBeans() 114 ); 115 } else { 116 return new ModelProperty(mdl); 117 } 118 } else if (mdl instanceof ExPropertyModel && 119 ((ExPropertyModel) mdl).getFeatureDescriptor() instanceof Property) { 120 UnsupportedOperationException uoe = new UnsupportedOperationException ( 121 "PropertyPanel now supports direct" + " use of Node.Property objects. Please do not use " + 122 "ExPropertyModel if you only need to wrap a Node.Property " + 123 "object. PropertyModel will be deprecated soon." 124 ); Logger.getLogger(ModelProperty.class.getName()).log(Level.WARNING, null, uoe); 126 127 return (Property) ((ExPropertyModel) mdl).getFeatureDescriptor(); 128 } else if (mdl != null) { 129 return new ModelProperty(mdl); 130 } else { 131 return new EmptyProperty(); 133 } 134 } 135 136 137 static Property toProperty(Node[] n, String name) throws ClassCastException , NullPointerException { 138 Class clazz = null; 140 if (n.length == 0) { 141 throw new NullPointerException ( 143 "Cannot find a property in an array" + " of 0 properties. Looking for " + name 144 ); 145 } 146 147 for (int i = 0; i < n.length; i++) { 148 Property p = findProperty(n[i], name); 149 150 if (p == null) { 151 throw new NullPointerException ( 152 "Node " + n[i].getDisplayName() + " does not contain a property " + name 153 ); 154 } else if (clazz == null) { 155 clazz = p.getValueType(); 156 } else { 157 if (p.getValueType() != clazz) { 158 throw new ClassCastException ( 159 "Found a property named " + n + " but it is of class " + p.getValueType().getName() + " not " + 160 clazz.getName() 161 ); 162 } 163 } 164 } 165 166 ProxyNode pn = new ProxyNode(n); 167 Property p = findProperty(pn, name); 168 169 if (p != null) { 170 return p; 171 } 172 173 throw new NullPointerException ( 175 "Found properties named " + name + " but ProxyNode did not contain one with this name. This should " + 176 "be impossible; probably someone has modified ProxyNode" 177 ); 178 } 179 180 181 static Property findProperty(Node n, String name) throws NullPointerException { 182 PropertySet[] ps = n.getPropertySets(); 183 184 for (int j = 0; j < ps.length; j++) { 185 Property p = findProperty(ps[j], name); 186 187 if (p != null) { 188 return p; 189 } 190 } 191 192 return null; 193 } 194 195 196 private static Property findProperty(PropertySet set, String name) { 197 Property[] p = set.getProperties(); 198 199 for (int i = 0; i < p.length; i++) { 200 if (p[i].getName().equals(name)) { 201 return p[i]; 202 } 203 } 204 205 return null; 206 } 207 208 public boolean canRead() { 209 return true; } 211 212 public boolean canWrite() { 213 return true; 214 } 215 216 public Object getValue() throws IllegalAccessException , InvocationTargetException { 217 return mdl.getValue(); 218 } 219 220 public void setValue(Object val) throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 221 mdl.setValue(val); 223 valueSet = true; 224 } 225 226 230 FeatureDescriptor getFeatureDescriptor() { 231 if (mdl instanceof ExPropertyModel) { 232 return ((ExPropertyModel) mdl).getFeatureDescriptor(); 233 } else { 234 return this; 235 } 236 } 237 238 private static String findDisplayNameFor(Object o) { 239 try { 240 if (o == null) { 241 return null; 242 } 243 244 if (o instanceof Node.Property) { 245 return ((Node.Property) o).getDisplayName(); 246 } 247 248 BeanInfo bi = Introspector.getBeanInfo(o.getClass()); 249 250 if (bi != null) { 251 BeanDescriptor bd = bi.getBeanDescriptor(); 252 253 if (bd != null) { 254 return bd.getDisplayName(); 255 } 256 } 257 } catch (Exception e) { 258 } 260 261 return null; 262 } 263 264 266 static class DPMWrapper extends PropertySupport.Reflection { 267 PropertyDescriptor descriptor; 268 PropertyModel mdl; 269 private String beanName = null; 270 271 public DPMWrapper(DefaultPropertyModel mdl) { 272 super( 273 mdl.bean, mdl.getPropertyType(), ((PropertyDescriptor ) mdl.getFeatureDescriptor()).getReadMethod(), 274 ((PropertyDescriptor ) mdl.getFeatureDescriptor()).getWriteMethod() 275 ); 276 descriptor = ((PropertyDescriptor ) mdl.getFeatureDescriptor()); 277 278 this.mdl = mdl; 282 beanName = findDisplayNameFor(mdl.bean); 283 } 284 285 public DPMWrapper(PropertyDescriptor desc, Object [] instances) { 286 super(instances[0], desc.getPropertyType(), desc.getReadMethod(), desc.getWriteMethod()); 287 descriptor = desc; 288 289 if ((instances != null) && (instances.length == 1)) { 290 beanName = findDisplayNameFor(instances[0]); 291 } 292 293 } 295 296 public String getBeanName() { 297 return beanName; 298 } 299 300 Object [] getBeans() { 301 if (mdl instanceof DefaultPropertyModel) { 302 return ((DefaultPropertyModel) mdl).getBeans(); 303 } 304 305 return null; 306 } 307 308 312 FeatureDescriptor getFeatureDescriptor() { 313 return descriptor; 314 } 315 316 public String getDisplayName() { 317 return descriptor.getDisplayName(); 318 } 319 320 public String getShortDescription() { 321 return descriptor.getShortDescription(); 322 } 323 324 public Object getValue(String key) { 325 Object result = descriptor.getValue(key); 326 327 if (result == null) { 328 result = super.getValue(key); 329 } 330 331 return result; 332 } 333 334 public void setValue(String key, Object val) { 335 descriptor.setValue(key, val); 336 } 337 338 public PropertyEditor getPropertyEditor() { 339 Class edClass; 340 341 if (mdl != null) { 342 edClass = mdl.getPropertyEditorClass(); 343 } else { 344 edClass = descriptor.getPropertyEditorClass(); 345 } 346 347 if (edClass != null) { 348 try { 351 return (PropertyEditor ) edClass.newInstance(); 353 } catch (Exception e) { 354 } 356 } 357 358 return super.getPropertyEditor(); 359 } 360 } 361 362 364 private static class EmptyProperty extends Property { 365 public EmptyProperty() { 366 super(Object .class); 367 } 368 369 public boolean canRead() { 370 return true; 371 } 372 373 public boolean canWrite() { 374 return false; 375 } 376 377 public Object getValue() throws IllegalAccessException , InvocationTargetException { 378 return ""; 379 } 380 381 public void setValue(Object val) 382 throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 383 } 385 386 public PropertyEditor getPropertyEditor() { 387 return new PropUtils.NoPropertyEditorEditor(); 388 } 389 } 390 } 391 | Popular Tags |