1 19 package org.openide.explorer.propertysheet; 20 21 22 import java.beans.*; 23 24 import java.lang.reflect.*; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 28 29 37 public @Deprecated class DefaultPropertyModel extends Object implements ExPropertyModel, PropertyChangeListener { 38 39 Object bean; 40 41 42 String propertyName; 44 46 47 private PropertyChangeSupport support; 48 49 50 private PropertyDescriptor prop; 51 52 53 private Method readMethod; 54 55 56 private Method writeMethod; 57 58 59 private Class propertyTypeClass; 60 61 62 private boolean donotfire = false; 63 64 71 public DefaultPropertyModel(Object bean, String propertyName) 72 throws IllegalArgumentException { 73 this(bean, findInfo(bean, propertyName)); 74 } 75 76 98 public DefaultPropertyModel(Object bean, PropertyDescriptor descr) { 99 this.bean = bean; 100 this.propertyName = descr.getName(); 101 support = new PropertyChangeSupport(this); 102 103 this.prop = descr; 104 this.propertyTypeClass = descr.getPropertyType(); 105 this.readMethod = descr.getReadMethod(); 106 this.writeMethod = descr.getWriteMethod(); 107 108 try { 109 try { 110 Method addList = bean.getClass().getMethod( 113 "addPropertyChangeListener", new Class [] { String .class, PropertyChangeListener.class } 114 ); 116 addList.invoke( 117 bean, new Object [] { propertyName, org.openide.util.WeakListeners.propertyChange(this, bean) } 118 ); 119 } catch (NoSuchMethodException nsme) { 120 try { 121 Method addList = bean.getClass().getMethod( 122 "addPropertyChangeListener", new Class [] { PropertyChangeListener.class } 123 ); 125 addList.invoke(bean, new Object [] { org.openide.util.WeakListeners.propertyChange(this, bean) }); 126 } catch (NoSuchMethodException nosme) { 127 } 129 } 130 } catch (Exception e) { 131 Logger.getLogger(DefaultPropertyModel.class.getName()).log(Level.WARNING, null, e); 132 } 133 } 134 135 141 private static PropertyDescriptor findInfo(Object bean, String name) 142 throws IllegalArgumentException { 143 try { 144 BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass()); 145 PropertyDescriptor[] descr = beanInfo.getPropertyDescriptors(); 146 147 for (int i = 0; i < descr.length; i++) { 148 if (descr[i].getName().equals(name)) { 149 return descr[i]; 150 } 151 } 152 153 throw new IllegalArgumentException ("No property named " + name + " in class " + bean.getClass() ); 155 } catch (IntrospectionException e) { 156 throw (IllegalArgumentException ) new IllegalArgumentException (e.toString()).initCause(e); 157 } 158 } 159 160 163 public Class getPropertyType() { 164 return propertyTypeClass; 165 } 166 167 169 public Object getValue() throws InvocationTargetException { 170 try { 171 return (readMethod == null) ? null : readMethod.invoke(bean, new Object [] { }); 172 } catch (IllegalAccessException e) { 173 Logger.getLogger(DefaultPropertyModel.class.getName()).log(Level.WARNING, null, e); 174 throw new InvocationTargetException(e); 175 } 176 } 177 178 182 public void setValue(Object v) throws InvocationTargetException { 183 try { 184 if (writeMethod != null) { 185 donotfire = true; 186 writeMethod.invoke(bean, new Object [] { v }); 187 donotfire = false; 188 } 189 } catch (IllegalAccessException e) { 190 Logger.getLogger(DefaultPropertyModel.class.getName()).log(Level.WARNING, null, e); 191 throw new InvocationTargetException(e); 192 } 193 } 194 195 197 public void addPropertyChangeListener(PropertyChangeListener l) { 198 support.addPropertyChangeListener(l); 199 } 200 201 203 public void removePropertyChangeListener(PropertyChangeListener l) { 204 support.removePropertyChangeListener(l); 205 } 206 207 208 public void propertyChange(PropertyChangeEvent evt) { 209 if (propertyName.equals(evt.getPropertyName()) && (!donotfire)) { 210 support.firePropertyChange(PROP_VALUE, evt.getOldValue(), evt.getNewValue()); 211 } 212 } 213 214 217 public Class getPropertyEditorClass() { 218 return prop.getPropertyEditorClass(); 219 } 220 221 225 public Object [] getBeans() { 226 return new Object [] { bean }; 227 } 228 229 233 public FeatureDescriptor getFeatureDescriptor() { 234 return prop; 235 } 236 } 237 | Popular Tags |