1 19 package org.openide.explorer.propertysheet; 20 21 import java.beans.FeatureDescriptor ; 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.beans.PropertyVetoException ; 25 import java.beans.VetoableChangeListener ; 26 import java.beans.VetoableChangeSupport ; 27 28 29 87 public class PropertyEnv { 88 89 public static final String PROP_STATE = "state"; 91 95 public static final Object STATE_VALID = "valid"; 97 102 public static final Object STATE_NEEDS_VALIDATION = "needs_validation"; 104 109 public static final Object STATE_INVALID = "invalid"; static final String PROP_CHANGE_IMMEDIATE = "changeImmediate"; static final FeatureDescriptor dummyDescriptor = new FeatureDescriptor (); 112 113 114 private FeatureDescriptor featureDescriptor = dummyDescriptor; 115 116 117 private Object [] beans; 118 119 120 private Object state = STATE_VALID; 121 122 123 private VetoableChangeSupport support; 124 125 126 private PropertyChangeSupport change; 127 128 134 private boolean changeImmediate = true; 135 InplaceEditor.Factory factory = null; 136 boolean editable = true; 137 138 142 PropertyEnv() { 143 } 144 145 148 public Object [] getBeans() { 149 return beans; 150 } 151 152 155 void setBeans(Object [] beans) { 156 this.beans = beans; 157 } 158 159 164 public FeatureDescriptor getFeatureDescriptor() { 165 return featureDescriptor; 166 } 167 168 173 void setFeatureDescriptor(FeatureDescriptor desc) { 174 if (desc == null) { 175 throw new IllegalArgumentException ("Cannot set FeatureDescriptor to null."); } 177 178 this.featureDescriptor = desc; 179 180 if (featureDescriptor != null) { 181 Object obj = featureDescriptor.getValue(PROP_CHANGE_IMMEDIATE); 182 183 if (obj instanceof Boolean ) { 184 setChangeImmediate(((Boolean ) obj).booleanValue()); 185 } 186 } 187 } 188 189 191 193 194 196 202 public void setState(Object newState) { 203 if (getState().equals(newState)) { 204 return; 206 } 207 208 try { 209 getSupport().fireVetoableChange(PROP_STATE, getState(), newState); 210 state = newState; 211 212 getChange().firePropertyChange(PROP_STATE, null, newState); 214 } catch (PropertyVetoException pve) { 215 PropertyDialogManager.notify(pve); 217 } 218 } 219 220 221 String silentlySetState(Object newState, Object newValue) { 222 if (getState().equals(newState)) { 223 return null; 225 } 226 227 try { 228 getSupport().fireVetoableChange(PROP_STATE, getState(), newState); 229 state = newState; 230 231 getChange().firePropertyChange(PROP_STATE, null, newState); 233 } catch (PropertyVetoException pve) { 234 pve.printStackTrace(); 236 237 String name = (getFeatureDescriptor() == null) ? null : getFeatureDescriptor().getDisplayName(); 238 239 return PropUtils.findLocalizedMessage(pve, newValue, name); 240 } 241 242 return null; 243 } 244 245 250 public Object getState() { 251 return state; 252 } 253 254 262 public void addVetoableChangeListener(VetoableChangeListener l) { 263 getSupport().addVetoableChangeListener(l); 264 } 265 266 271 public void addPropertyChangeListener(PropertyChangeListener l) { 272 getChange().addPropertyChangeListener(l); 273 } 274 275 278 public void removeVetoableChangeListener(VetoableChangeListener l) { 279 getSupport().removeVetoableChangeListener(l); 280 } 281 282 286 public void removePropertyChangeListener(PropertyChangeListener l) { 287 getChange().removePropertyChangeListener(l); 288 } 289 290 293 boolean isChangeImmediate() { 294 return changeImmediate; 295 } 296 297 300 void setChangeImmediate(boolean changeImmediate) { 301 this.changeImmediate = changeImmediate; 302 } 303 304 307 private synchronized VetoableChangeSupport getSupport() { 308 if (support == null) { 309 support = new VetoableChangeSupport (this); 310 } 311 312 return support; 313 } 314 315 318 private synchronized PropertyChangeSupport getChange() { 319 if (change == null) { 320 change = new PropertyChangeSupport (this); 321 } 322 323 return change; 324 } 325 326 335 public void registerInplaceEditorFactory(InplaceEditor.Factory factory) { 336 this.factory = factory; 337 } 338 339 InplaceEditor getInplaceEditor() { 340 InplaceEditor result; 341 342 if (factory != null) { 343 result = factory.getInplaceEditor(); 344 } else { 345 result = null; 346 } 347 348 return result; 349 } 350 351 void setEditable(boolean editable) { 352 this.editable = editable; 353 } 354 355 boolean isEditable() { 356 return editable; 357 } 358 359 public String toString() { 360 StringBuffer sb = new StringBuffer (); 361 sb.append(getClass().getName()); 362 sb.append("@"); sb.append(System.identityHashCode(this)); 364 sb.append("[state="); sb.append( 366 (state == STATE_NEEDS_VALIDATION) ? "STATE_NEEDS_VALIDATION" 367 : ((state == STATE_INVALID) ? "STATE_INVALID" : "STATE_VALID") 368 ); sb.append(", "); 371 if (factory != null) { 372 sb.append("InplaceEditorFactory=" + factory.getClass().getName()); sb.append(", "); } 375 376 sb.append("editable="); sb.append(editable); 378 sb.append(", isChangeImmediate="); sb.append(isChangeImmediate()); 380 sb.append(", featureDescriptor="); sb.append(getFeatureDescriptor().getDisplayName()); 382 383 return sb.toString(); 384 } 385 } 386 | Popular Tags |