1 19 20 21 package org.netbeans.modules.form; 22 23 import java.beans.*; 24 import java.lang.reflect.*; 25 import org.openide.ErrorManager; 26 27 import org.netbeans.modules.form.editors.*; 28 import org.netbeans.modules.form.fakepeer.FakePeerSupport; 29 30 38 public class RADProperty extends FormProperty { 39 40 public static final String SYNTH_PREFIX = "$$$_"; public static final String SYNTH_PRE_CODE = SYNTH_PREFIX + PROP_PRE_CODE + "_"; public static final String SYNTH_POST_CODE = SYNTH_PREFIX + PROP_POST_CODE + "_"; 44 private RADComponent component; 45 private PropertyDescriptor desc; 46 47 RADProperty(RADComponent metacomp, PropertyDescriptor propdesc) { 48 super(new RADPropertyContext(metacomp), 49 propdesc.getName(), 50 propdesc.getPropertyType(), 51 propdesc.getDisplayName(), 52 propdesc.getShortDescription()); 53 54 component = metacomp; 55 desc = propdesc; 56 57 if (desc.getWriteMethod() == null) 58 setAccessType(NO_WRITE); 59 else if (desc.getReadMethod() == null) 60 setAccessType(DETACHED_READ); 61 } 62 63 public RADComponent getRADComponent() { 64 return component; 65 } 66 67 public PropertyDescriptor getPropertyDescriptor() { 68 return desc; 69 } 70 71 73 public Object getTargetValue() throws IllegalAccessException , 74 InvocationTargetException { 75 Method readMethod = desc.getReadMethod(); 76 if (readMethod == null) { 77 throw new IllegalAccessException ("Not a readable property: "+desc.getName()); } 79 return readMethod.invoke(component.getBeanInstance(), new Object [0]); 80 } 81 82 public void setTargetValue(Object value) throws IllegalAccessException , 83 IllegalArgumentException , 84 InvocationTargetException { 85 Method writeMethod = desc.getWriteMethod(); 86 if (writeMethod == null) { 87 throw new IllegalAccessException ("Not a writeable property: "+desc.getName()); } 89 90 Object beanInstance = component.getBeanInstance(); 91 92 java.awt.peer.ComponentPeer scrollbarPeerHack = 98 "setOrientation".equals(writeMethod.getName()) && beanInstance instanceof java.awt.Scrollbar ? 100 FakePeerSupport.detachFakePeer((java.awt.Component )beanInstance) 101 : null; 102 103 try { 104 writeMethod.invoke(component.getBeanInstance(), 106 new Object [] { value }); 107 } 108 catch (InvocationTargetException ex) { 109 String message = FormUtils.getFormattedBundleString( 111 "MSG_ERR_WRITING_TO_PROPERTY", new Object [] { getDisplayName() }); 113 114 Throwable tex = ex.getTargetException(); 115 if(tex instanceof IllegalArgumentException ) { 116 ErrorManager.getDefault().annotate( 117 tex, ErrorManager.WARNING, null, 118 message, null, null); 119 throw (IllegalArgumentException ) tex; 120 } else if(tex instanceof IllegalAccessException ) { 121 ErrorManager.getDefault().annotate( 122 tex, ErrorManager.WARNING, null, 123 message, null, null); 124 throw (IllegalAccessException ) tex; 125 } else if(value==null && tex instanceof NullPointerException ) { 126 IllegalArgumentException iae = new IllegalArgumentException (); 127 ErrorManager.getDefault().annotate( 128 iae, ErrorManager.WARNING, null, 129 message, null, null); 130 throw iae; 131 } 132 133 ErrorManager.getDefault().annotate( 134 ex, ErrorManager.WARNING, null, 135 message, null, null); 136 137 throw ex; 138 } 139 140 if (scrollbarPeerHack != null) FakePeerSupport.attachFakePeer((java.awt.Component )beanInstance, 142 scrollbarPeerHack); 143 } 144 145 public void setValue(Object value) throws IllegalAccessException , 146 IllegalArgumentException , 147 InvocationTargetException { 148 super.setValue(value); 149 150 component.debugChangedValues(); } 152 153 protected Object getRealValue(Object value) { 154 Object realValue = super.getRealValue(value); 155 156 if (realValue == FormDesignValue.IGNORED_VALUE 157 && component.getBeanInstance() instanceof java.awt.Component 158 && "text".equals(desc.getName())) realValue = ((FormDesignValue)value).getDescription(); 160 161 return realValue; 162 } 163 164 public boolean supportsDefaultValue() { 165 return BeanSupport.NO_VALUE != BeanSupport.getDefaultPropertyValue( 166 component.getBeanInstance(), 167 getName()); 168 } 169 170 public Object getDefaultValue() { 171 return BeanSupport.getDefaultPropertyValue(component.getBeanInstance(), 172 getName()); 173 } 174 175 177 public boolean canWrite() { 178 return component.isReadOnly() ? false : super.canWrite(); 179 } 180 181 183 public PropertyEditor getExpliciteEditor() { 184 PropertyEditor prEd = null; 185 186 PropertyDescriptor descriptor = getPropertyDescriptor(); 187 if (descriptor.getPropertyType() == Integer.TYPE 188 && ("mnemonic".equals(descriptor.getName()) || "displayedMnemonic".equals(descriptor.getName()))) { prEd = new MnemonicEditor(); 191 } else { 192 prEd = createEnumEditor(descriptor); 193 } 194 195 if (prEd == null) { 196 try { 197 prEd = desc.createPropertyEditor(component.getBeanInstance()); 198 } 199 catch (Exception ex) { 200 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 201 } 202 } 203 204 return prEd; 205 } 206 207 protected PropertyEditor createEnumEditor(PropertyDescriptor descriptor) { 208 Object [] enumerationValues; 209 210 if (!"debugGraphicsOptions".equals(descriptor.getName()) || !javax.swing.JComponent .class.isAssignableFrom( 212 component.getBeanClass())) 213 { enumerationValues = (Object []) 215 descriptor.getValue("enumerationValues"); } 217 else { enumerationValues = new Object [] { 221 "NONE_OPTION", new Integer (-1), "DebugGraphics.NONE_OPTION", "NO_CHANGES", new Integer (0), "0", "LOG_OPTION", new Integer (1), "DebugGraphics.LOG_OPTION", "FLASH_OPTION", new Integer (2), "DebugGraphics.FLASH_OPTION", "BUFFERED_OPTION", new Integer (4), "DebugGraphics.BUFFERED_OPTION" }; } 227 228 if (enumerationValues == null 229 && "defaultCloseOperation".equals(descriptor.getName()) && (javax.swing.JDialog .class.isAssignableFrom( 231 component.getBeanClass()) 232 || javax.swing.JInternalFrame .class.isAssignableFrom( 233 component.getBeanClass()))) 234 { enumerationValues = new Object [] { 237 "DISPOSE_ON_CLOSE", new Integer (2), "WindowConstants.DISPOSE_ON_CLOSE", "DO_NOTHING_ON_CLOSE", new Integer (0), "WindowConstants.DO_NOTHING_ON_CLOSE", "HIDE_ON_CLOSE", new Integer (1), "WindowConstants.HIDE_ON_CLOSE" }; } 244 245 return enumerationValues != null ? 246 new EnumEditor(enumerationValues) : null; 247 } 248 249 protected Method getWriteMethod() { 250 return desc.getWriteMethod(); 251 } 252 253 public void setPreCode(String value) { 254 if ((preCode == null && value != null) 255 || (preCode != null && !preCode.equals(value))) { 256 Object old = preCode; 257 preCode = value; 258 if (isChangeFiring() && component.getFormModel() != null) 259 component.getFormModel().fireSyntheticPropertyChanged( 260 component, SYNTH_PRE_CODE + getName(), old, value); 261 } 262 } 263 264 public void setPostCode(String value) { 265 if ((postCode == null && value != null) 266 || (postCode != null && !postCode.equals(value))) { 267 Object old = postCode; 268 postCode = value; 269 if (isChangeFiring() && component.getFormModel() != null) 270 component.getFormModel().fireSyntheticPropertyChanged( 271 component, SYNTH_POST_CODE + getName(), old, value); 272 } 273 } 274 275 277 292 293 296 static class RADPropertyContext extends FormPropertyContext.DefaultSupport { 297 RADComponent component; 298 299 RADPropertyContext(RADComponent comp) { 300 component = comp; 301 } 302 303 public FormModel getFormModel() { 304 return component.getFormModel(); 305 } 306 } 307 308 static class FakePropertyDescriptor extends PropertyDescriptor { 312 Class propType; 313 314 FakePropertyDescriptor(String name, Class type) throws IntrospectionException { 315 super(name,null,null); 316 propType = type; 317 } 318 319 public Class getPropertyType() { 320 return propType; 321 } 322 } 323 } 324 | Popular Tags |