1 19 20 package org.netbeans.modules.form.editors2; 21 22 import javax.swing.border.*; 23 import java.util.*; 24 import java.beans.*; 25 import java.lang.reflect.*; 26 27 import org.openide.nodes.*; 28 import org.netbeans.modules.form.*; 29 import org.openide.ErrorManager; 30 31 37 38 public class BorderDesignSupport implements FormDesignValue 39 { 40 private Border theBorder; 41 private boolean borderNeedsUpdate; 42 private boolean propertiesNeedInit; 43 private CreationDescriptor creationDesc; 44 private FormPropertyContext propertyContext = null; 45 private FormProperty[] properties = null; 46 49 public BorderDesignSupport(Class borderClass) 50 throws Exception 51 { 52 creationDesc = CreationFactory.getDescriptor(borderClass); 53 if (creationDesc == null) { 54 creationDesc = new CreationDescriptor(borderClass); 55 CreationFactory.registerDescriptor(creationDesc); 56 } 57 58 theBorder = (Border) CreationFactory.createInstance(borderClass); 59 } 60 61 public BorderDesignSupport(Border border) { 62 creationDesc = CreationFactory.getDescriptor(border.getClass()); 63 if (creationDesc == null) { 64 creationDesc = new CreationDescriptor(border.getClass()); 65 CreationFactory.registerDescriptor(creationDesc); 66 } 67 setBorder(border); 68 } 69 70 public BorderDesignSupport(BorderDesignSupport borderDesignSupport, FormPropertyContext propertyContext) 71 throws Exception 72 { 73 this(borderDesignSupport.getBorderClass()); 74 createProperties(); 75 setPropertyContext(propertyContext); 76 int copyMode = FormUtils.CHANGED_ONLY | FormUtils.DISABLE_CHANGE_FIRING; 77 78 FormUtils.copyProperties(borderDesignSupport.getProperties(), 79 this.properties, 80 copyMode); 81 } 82 83 85 public FormDesignValue copy(FormProperty formProperty) { 86 FormModel formModel = formProperty.getPropertyContext().getFormModel(); 87 try { 88 return new BorderDesignSupport(this, BorderEditor.createFormPropertyContext(formModel)); 89 } catch (Exception ex) { 90 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 91 } 92 return null; 93 } 94 95 public Border getBorder() { 96 if (borderNeedsUpdate) 97 updateBorder(); 98 return theBorder; 99 } 100 101 public void setBorder(Border border) { 102 theBorder = border; 103 if (properties != null) { 104 for (int i=0; i < properties.length; i++) 105 try { 106 properties[i].reinstateProperty(); 107 } 108 catch (IllegalAccessException e1) { 109 } 110 catch (InvocationTargetException e2) { 111 } 112 propertiesNeedInit = false; 113 } 114 else propertiesNeedInit = true; 115 borderNeedsUpdate = false; 116 } 117 118 public Class getBorderClass() { 119 return creationDesc.getDescribedClass(); 120 } 121 122 public String getDisplayName() { 123 return org.openide.util.Utilities.getShortClassName(theBorder.getClass()); 124 } 128 129 132 public void setPropertyContext(FormPropertyContext propertyContext) { 133 if (properties != null && this.propertyContext != propertyContext) { 134 for (int i=0; i < properties.length; i++) 135 if (!properties[i].getValueType().isPrimitive()) 136 properties[i].setPropertyContext(propertyContext); 137 } 138 139 this.propertyContext = propertyContext; 140 } 141 142 public Node.Property[] getProperties() { 144 if (properties == null) 145 createProperties(); 146 return properties; 147 } 148 149 public Node.Property getPropertyOfName(String name) { 150 Node.Property[] props = getProperties(); 151 for (int i=0; i < props.length; i++) 152 if (props[i].getName().equals(name)) 153 return props[i]; 154 155 return null; 156 } 157 158 private void createProperties() { 159 BeanInfo bInfo; 160 try { 161 bInfo = FormUtils.getBeanInfo(theBorder.getClass()); 162 } catch (IntrospectionException ex) { 163 return; 164 } 165 PropertyDescriptor[] props = bInfo.getPropertyDescriptors(); 166 167 ArrayList nodeProps = new ArrayList(); 168 for (int i = 0; i < props.length; i++) { 169 PropertyDescriptor pd = props[i]; 170 if (!pd.isHidden() 171 && (pd.getWriteMethod() != null 172 || CreationFactory.containsProperty(creationDesc, 173 pd.getName()))) 174 { 175 BorderProperty prop = 176 new BorderProperty(pd.getPropertyType().isPrimitive() ? 177 null : propertyContext, 178 pd); 179 180 if (propertiesNeedInit) 181 try { 182 prop.reinstateProperty(); 183 } 184 catch (IllegalAccessException e1) { 185 } 186 catch (InvocationTargetException e2) { 187 } 188 189 nodeProps.add(prop); 190 } 191 } 192 properties = new FormProperty[nodeProps.size()]; 193 nodeProps.toArray(properties); 194 propertiesNeedInit = false; 195 } 196 197 public String getJavaInitializationString() { 198 if (properties == null) 199 createProperties(); 200 201 CreationDescriptor.Creator creator = 202 creationDesc.findBestCreator(properties, 203 CreationDescriptor.CHANGED_ONLY | CreationDescriptor.PLACE_ALL); 204 205 return creator.getJavaCreationCode(properties, Border.class); 206 } 207 208 void updateBorder() { 209 if (properties == null) 210 createProperties(); 211 212 CreationDescriptor.Creator creator = 213 creationDesc.findBestCreator(properties, 214 CreationDescriptor.CHANGED_ONLY | CreationDescriptor.PLACE_ALL); 215 216 try { 217 theBorder = (Border) CreationFactory.createInstance( 218 creationDesc.getDescribedClass(), 219 properties, 220 CreationDescriptor.CHANGED_ONLY | CreationDescriptor.PLACE_ALL); 221 222 FormProperty[] otherProps = CreationFactory.getRemainingProperties( 224 creator, properties); 225 for (int i=0; i < otherProps.length; i++) 226 otherProps[i].reinstateTarget(); 227 } 228 catch (Exception ex) { org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 230 } 231 } 232 233 public Object getDesignValue() { 234 return getBorder(); 235 } 236 237 public String getDescription() { 238 return getDisplayName(); 239 } 240 241 243 public class BorderProperty extends FormProperty { 244 private PropertyDescriptor desc; 245 246 public BorderProperty(FormPropertyContext propertyContext, 247 PropertyDescriptor desc) 248 { 249 super(propertyContext, 250 desc.getName(), 251 desc.getPropertyType(), 252 desc.getDisplayName(), 253 desc.getShortDescription()); 254 255 this.desc = desc; 256 257 if (desc.getWriteMethod() == null) 258 setAccessType(DETACHED_WRITE); 259 else if (desc.getReadMethod() == null) 260 setAccessType(DETACHED_READ); 261 } 262 263 public Object getTargetValue() 264 throws IllegalAccessException , InvocationTargetException 265 { 266 Method readMethod = desc.getReadMethod(); 267 return readMethod.invoke(theBorder, new Object [0]); 268 } 269 270 public void setTargetValue(Object value) 271 throws IllegalAccessException , IllegalArgumentException , 272 InvocationTargetException 273 { 274 Method writeMethod = desc.getWriteMethod(); 275 writeMethod.invoke(theBorder, new Object [] { value }); 276 } 277 278 protected Object getRealValue(Object value) { 279 Object realValue = super.getRealValue(value); 280 281 if (realValue == FormDesignValue.IGNORED_VALUE 282 && "title".equals(desc.getName())) realValue = ((FormDesignValue)value).getDescription(); 284 285 return realValue; 286 } 287 288 public boolean supportsDefaultValue () { 289 return true; 290 } 291 292 public Object getDefaultValue() { 293 Method readMethod = desc.getReadMethod(); 294 Object value = null; 295 if (readMethod != null) 296 try { 297 value = readMethod.invoke( 298 BeanSupport.getDefaultInstance(theBorder.getClass()), 299 new Object [0]); 300 } 301 catch (Exception ex) { } 303 return value; 304 } 305 306 public PropertyEditor getExpliciteEditor() { 307 try { 308 return desc.createPropertyEditor(theBorder); 309 } 310 catch (Exception ex) { 311 ex.printStackTrace(); 312 return null; 313 } 314 } 315 316 protected Method getWriteMethod() { 317 return desc.getWriteMethod(); 318 } 319 320 protected void propertyValueChanged(Object old, Object current) { 321 super.propertyValueChanged(old, current); 322 borderNeedsUpdate = (getAccessType() & DETACHED_WRITE) != 0; 323 } 324 } 325 } 326 | Popular Tags |