1 19 20 21 package org.netbeans.modules.form; 22 23 import java.awt.*; 24 import java.beans.*; 25 26 import org.openide.explorer.propertysheet.*; 27 import org.openide.explorer.propertysheet.editors.*; 28 29 38 39 public class RADConnectionPropertyEditor 40 implements PropertyEditor, 41 FormAwareEditor, 42 XMLPropertyEditor, 43 NamedPropertyEditor 44 { 45 protected PropertyChangeSupport support; 46 private Class propertyType; 47 private FormModel formModel = null; 48 private RADConnectionDesignValue designValue = null; 49 private Object realValue = null; 50 51 52 public RADConnectionPropertyEditor(Class propertyType) { 53 support = new PropertyChangeSupport(this); 54 this.propertyType = propertyType; 55 } 56 57 62 public void setFormModel(FormModel model) { 63 formModel = model; 64 } 65 66 69 public Object getValue() { 70 71 return designValue != null ? designValue : realValue; 72 } 73 74 public void setValue(Object value) { 75 76 77 if (value instanceof RADConnectionDesignValue) { 78 designValue =(RADConnectionDesignValue)value; 79 } else { 80 designValue = null; 81 realValue = value; 82 } 83 support.firePropertyChange("", null, null); } 85 86 public void setAsText(String string) { 87 } 88 89 public String getAsText() { 90 return null; 91 } 92 93 public String [] getTags() { 94 return null; 95 } 96 97 public boolean isPaintable() { 98 return true; 99 } 100 101 public void paintValue(Graphics g, Rectangle rectangle) { 102 FontMetrics fm = g.getFontMetrics(); 103 g.drawString(getValueString(), rectangle.x, 104 rectangle.y + (rectangle.height - fm.getHeight()) / 2 + fm.getAscent()); 105 } 106 107 public boolean supportsCustomEditor() { 108 return true; 109 } 110 111 public java.awt.Component getCustomEditor() { 112 ParametersPicker pp = new ParametersPicker(formModel, propertyType); 113 pp.setPropertyValue(designValue, realValue); 114 return pp; 115 } 116 117 public String getJavaInitializationString() { 118 if (designValue != null) { 119 if (designValue.needsInit) 120 designValue.initialize(); 121 122 if (designValue.type == RADConnectionDesignValue.TYPE_VALUE) { 123 if ("java.lang.String".equals(designValue.requiredTypeName)) return "\""+designValue.value+"\""; else if ("long".equals(designValue.requiredTypeName)) return designValue.value+"L"; else if ("float".equals(designValue.requiredTypeName)) return designValue.value+"F"; else if ("double".equals(designValue.requiredTypeName)) return designValue.value+"D"; else if ("char".equals(designValue.requiredTypeName)) return "\'"+designValue.value+"\'"; else return designValue.value; 134 } 135 else if (designValue.type == RADConnectionDesignValue.TYPE_CODE) 136 return designValue.userCode; 137 else { 138 if (designValue.radComponent == null 139 || designValue.radComponent.getCodeExpression() == null) 140 return null; 142 if (designValue.type == RADConnectionDesignValue.TYPE_PROPERTY) { 143 PropertyDescriptor pd = designValue.getProperty(); 144 if (pd == null) return null; else { 146 if (designValue.radComponent == formModel.getTopRADComponent()) { 147 return pd.getReadMethod().getName() + "()"; } else { 149 return designValue.radComponent.getName() + "." + pd.getReadMethod().getName() + "()"; } 151 } 152 } 153 else if (designValue.type == RADConnectionDesignValue.TYPE_METHOD) { 154 if (designValue.radComponent == formModel.getTopRADComponent()) { 155 return designValue.methodName + "()"; } else { 157 return designValue.radComponent.getName() + "." + designValue.methodName + "()"; } 159 } 160 else if (designValue.type == RADConnectionDesignValue.TYPE_BEAN) { 161 if (designValue.radComponent == formModel.getTopRADComponent()) { 162 return "this"; } else { 164 return designValue.radComponent.getName(); 165 } 166 } 167 } 168 } 169 return null; 170 } 171 172 public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) { 173 support.addPropertyChangeListener(propertyChangeListener); 174 } 175 176 public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) { 177 support.removePropertyChangeListener(propertyChangeListener); 178 } 179 180 183 184 public String getDisplayName() { 185 return FormUtils.getBundleString("CTL_RADConn_DisplayName"); } 187 188 private String getValueString() { 190 String str; 191 if (designValue != null) { 192 str = designValue.getName(); 193 } 194 else if (realValue != null) { 195 if (realValue instanceof Number 196 || realValue instanceof Boolean 197 || realValue instanceof String 198 || realValue instanceof Character ) 199 str = realValue.toString(); 200 else 201 str = realValue.getClass().isArray() ? 202 "[" + FormUtils.getBundleString("CTL_ArrayOf") + " " + realValue.getClass().getComponentType().getName() + "]" : 205 "["+org.openide.util.Utilities.getShortClassName(realValue.getClass())+"]"; } 207 else str = "null"; 209 return str; 210 } 211 212 213 216 public static class RADConnectionDesignValue implements FormDesignValue { public final static int TYPE_PROPERTY = 0; 218 public final static int TYPE_METHOD = 1; 219 public final static int TYPE_CODE = 2; 220 public final static int TYPE_VALUE = 3; 221 public final static int TYPE_BEAN = 4; 222 223 224 int type; 225 226 private transient RADComponent radComponent = null; String radComponentName = null; 229 private transient MethodDescriptor method = null; String methodName = null; private transient PropertyDescriptor property = null; String propertyName = null; String userCode = null; String value = null; String requiredTypeName = null; 237 transient private boolean needsInit = false; transient private FormModel formModel; 240 static final long serialVersionUID =147134837271021412L; 241 RADConnectionDesignValue(RADComponent comp) { 242 radComponent = comp; 243 radComponentName = radComponent.getName(); 244 type = TYPE_BEAN; 245 } 246 247 RADConnectionDesignValue(RADComponent comp, MethodDescriptor md) { 248 radComponent = comp; 249 radComponentName = radComponent.getName(); 250 method = md; 251 methodName = md.getName(); 252 type = TYPE_METHOD; 253 } 254 255 RADConnectionDesignValue(RADComponent comp, PropertyDescriptor pd) { 256 radComponent = comp; 257 radComponentName = radComponent.getName(); 258 property = pd; 259 propertyName = pd.getName(); 260 type = TYPE_PROPERTY; 261 } 262 263 RADConnectionDesignValue(String reqTypeName, String valueText) { 264 this.requiredTypeName = reqTypeName; 265 this.value = valueText; 266 type = TYPE_VALUE; 267 } 268 269 private RADConnectionDesignValue(String compName, int valueType, String name, FormModel manager) { 270 radComponentName = compName; 271 formModel = manager; 272 if (valueType == TYPE_PROPERTY) { 273 needsInit = true; 274 type = TYPE_PROPERTY; 275 propertyName = name; 276 } else if (valueType == TYPE_METHOD) { 277 needsInit = true; 278 type = TYPE_METHOD; 279 methodName = name; 280 } else if (valueType == TYPE_BEAN) { 281 needsInit = true; 282 type = TYPE_BEAN; 283 } else throw new IllegalArgumentException (); 284 } 285 286 public RADConnectionDesignValue(Class requiredType, String valueText) { 287 this.requiredTypeName = requiredType.getName(); 288 this.value = valueText; 289 type = TYPE_VALUE; 290 } 291 292 public RADConnectionDesignValue(String userCode) { 293 this.userCode = userCode; 294 type = TYPE_CODE; 295 } 296 297 public FormDesignValue copy(FormProperty formProperty) { 298 switch(type) { 299 case TYPE_CODE: 300 return new RADConnectionDesignValue(userCode); 301 case TYPE_VALUE: 302 return new RADConnectionDesignValue(requiredTypeName, value); 303 } 304 return null; 305 } 306 307 String getName() { 308 if (needsInit) 309 initialize(); 310 311 if (type == TYPE_VALUE) 312 return FormUtils.getFormattedBundleString("FMT_VALUE_CONN", new Object [] { value }); 314 else if (type == TYPE_CODE) 315 return FormUtils.getBundleString("CTL_CODE_CONN"); else { 317 if (radComponent == null || radComponent.getCodeExpression() == null) 318 return FormUtils.getBundleString("CTL_CONNECTION_INVALID"); 320 if (radComponent == null) 321 return null; 322 323 if (type == TYPE_PROPERTY) 324 return FormUtils.getFormattedBundleString( 325 "FMT_PROPERTY_CONN", new Object [] { radComponent.getName(), propertyName }); 327 else if (type == TYPE_METHOD) 328 return FormUtils.getFormattedBundleString( 329 "FMT_METHOD_CONN", new Object [] { radComponent.getName(), methodName }); 331 else if (type == TYPE_BEAN) 332 return FormUtils.getFormattedBundleString( 333 "FMT_BEAN_CONN", new Object [] { radComponent.getName() }); 335 } 336 337 throw new IllegalStateException (); 338 } 339 340 public PropertyDescriptor getProperty() { 341 if (needsInit) { 342 if (!initialize()) return null; 343 } 344 return property; 345 } 346 347 public MethodDescriptor getMethod() { 348 if (needsInit) { 349 if (!initialize()) return null; 350 } 351 return method; 352 } 353 354 public String getCode() { 355 if (needsInit) { 356 if (!initialize()) return null; 357 } 358 return userCode; 359 } 360 361 public String getValue() { 362 if (needsInit) { 363 if (!initialize()) return null; 364 } 365 return value; 366 } 367 368 public RADComponent getRADComponent() { 369 if (needsInit) { 370 if (!initialize()) return null; 371 } 372 return radComponent; 373 } 374 375 private boolean initialize() { 376 boolean retVal = false; 377 radComponent = formModel.findRADComponent(radComponentName); 378 if (radComponent != null) { 379 if (type == TYPE_BEAN) { retVal = true; 381 } else if (type == TYPE_PROPERTY) { PropertyDescriptor[] componentsProps = radComponent.getBeanInfo().getPropertyDescriptors(); 383 for (int i = 0; i < componentsProps.length; i++) { 384 if (componentsProps[i].getName().equals(propertyName)) { 385 property = componentsProps[i]; 386 retVal = true; 387 break; 388 } 389 } } else { MethodDescriptor[] componentMethods = radComponent.getBeanInfo().getMethodDescriptors(); 392 for (int i = 0; i < componentMethods.length; i++) { 393 if (componentMethods[i].getName().equals(methodName)) { 394 method = componentMethods[i]; 395 retVal = true; 396 break; 397 } 398 } } 400 } if (retVal) needsInit = false; 402 return retVal; 403 } 404 405 413 public Object getDesignValue() { 419 switch (type) { 420 case TYPE_PROPERTY: 421 try { 422 Object value = getProperty().getReadMethod().invoke(getRADComponent().getBeanInstance(), new Object [0]); 423 return value; 424 } catch (Exception e) { 425 return FormDesignValue.IGNORED_VALUE; 427 } 428 case TYPE_METHOD: 429 try { 430 Object value = getMethod().getMethod().invoke(getRADComponent().getBeanInstance(), new Object [0]); 431 return value; 432 } catch (Exception e) { 433 return FormDesignValue.IGNORED_VALUE; 435 } 436 case TYPE_VALUE: 437 return parseValue(requiredTypeName, value); 438 case TYPE_BEAN: 439 RADComponent comp = getRADComponent(); 440 return (comp == null) ? FormDesignValue.IGNORED_VALUE : comp.getBeanInstance(); 441 case TYPE_CODE: 442 return FormDesignValue.IGNORED_VALUE; 443 default: 444 return FormDesignValue.IGNORED_VALUE; 445 } 446 } 447 448 public String getDescription() { 449 return getName(); 450 } 451 452 454 public int getType() { 455 return type; 456 } 457 } 459 private static Object parseValue(String typeName, String value) { 460 try { 461 if ("java.lang.String".equals(typeName)) { return value; 463 } else if ("int".equals(typeName)) { return Integer.valueOf(value); 465 } else if ("short".equals(typeName)) { return Short.valueOf(value); 467 } else if ("long".equals(typeName)) { return Long.valueOf(value); 469 } else if ("byte".equals(typeName)) { return Byte.valueOf(value); 471 } else if ("float".equals(typeName)) { return Float.valueOf(value); 473 } else if ("double".equals(typeName)) { return Double.valueOf(value); 475 } else if ("boolean".equals(typeName)) { return Boolean.valueOf(value); 477 } else if ("char".equals(typeName)) { if (value.length() > 0) return new Character (value.charAt(0)); 479 } 480 return FormDesignValue.IGNORED_VALUE; 481 } catch (Exception e) { 482 return FormDesignValue.IGNORED_VALUE; 484 } 485 } 486 487 490 public static final String XML_CONNECTION = "Connection"; 492 public static final String ATTR_TYPE = "type"; public static final String ATTR_COMPONENT = "component"; public static final String ATTR_NAME = "name"; public static final String ATTR_CODE = "code"; public static final String ATTR_VALUE = "value"; public static final String ATTR_REQUIRED_TYPE = "valueType"; 499 public static final String VALUE_VALUE = "value"; public static final String VALUE_PROPERTY = "property"; public static final String VALUE_METHOD = "method"; public static final String VALUE_BEAN = "bean"; public static final String VALUE_CODE = "code"; 505 511 public void readFromXML(org.w3c.dom.Node element) throws java.io.IOException { 512 if (!XML_CONNECTION.equals(element.getNodeName())) { 513 throw new java.io.IOException (); 514 } 515 org.w3c.dom.NamedNodeMap attributes = element.getAttributes(); 516 try { 517 String typeString = attributes.getNamedItem(ATTR_TYPE).getNodeValue(); 518 if (VALUE_VALUE.equals(typeString)) { 519 String value = attributes.getNamedItem(ATTR_VALUE).getNodeValue(); 520 String valueType = attributes.getNamedItem(ATTR_REQUIRED_TYPE).getNodeValue(); 521 setValue(new RADConnectionDesignValue(valueType, value)); 522 523 } else if (VALUE_PROPERTY.equals(typeString)) { 524 String component = attributes.getNamedItem(ATTR_COMPONENT).getNodeValue(); 525 String name = attributes.getNamedItem(ATTR_NAME).getNodeValue(); 526 setValue(new RADConnectionDesignValue(component, RADConnectionDesignValue.TYPE_PROPERTY, name, formModel)); 528 } else if (VALUE_METHOD.equals(typeString)) { 529 String component = attributes.getNamedItem(ATTR_COMPONENT).getNodeValue(); 530 String name = attributes.getNamedItem(ATTR_NAME).getNodeValue(); 531 setValue(new RADConnectionDesignValue(component, RADConnectionDesignValue.TYPE_METHOD, name, formModel)); 533 } else if (VALUE_BEAN.equals(typeString)) { 534 String component = attributes.getNamedItem(ATTR_COMPONENT).getNodeValue(); 535 setValue(new RADConnectionDesignValue(component, RADConnectionDesignValue.TYPE_BEAN, null, formModel)); 537 } else { 538 String code = attributes.getNamedItem(ATTR_CODE).getNodeValue(); 539 setValue(new RADConnectionDesignValue(code)); 540 } 541 } catch (NullPointerException e) { 542 if (System.getProperty("netbeans.debug.exceptions") != null) { 543 e.printStackTrace(); 544 } 545 throw new java.io.IOException (); 546 } 547 } 548 549 555 556 public org.w3c.dom.Node storeToXML(org.w3c.dom.Document doc) { 557 if (designValue == null) 558 return null; 559 560 String componentName = designValue.radComponent != null ? 561 designValue.radComponent.getName() : 562 designValue.radComponentName; 563 564 if (componentName == null && designValue.radComponent != null) 565 return null; 567 org.w3c.dom.Element el = doc.createElement(XML_CONNECTION); 568 String typeString; 569 switch (designValue.type) { 570 case RADConnectionDesignValue.TYPE_VALUE: typeString = VALUE_VALUE; break; 571 case RADConnectionDesignValue.TYPE_PROPERTY: typeString = VALUE_PROPERTY; break; 572 case RADConnectionDesignValue.TYPE_METHOD: typeString = VALUE_METHOD; break; 573 case RADConnectionDesignValue.TYPE_BEAN: typeString = VALUE_BEAN; break; 574 case RADConnectionDesignValue.TYPE_CODE: 575 default: 576 typeString = VALUE_CODE; break; 577 } 578 el.setAttribute(ATTR_TYPE, typeString); 579 switch (designValue.type) { 580 case RADConnectionDesignValue.TYPE_VALUE: 581 el.setAttribute(ATTR_VALUE, designValue.value); 582 el.setAttribute(ATTR_REQUIRED_TYPE, designValue.requiredTypeName); 583 break; 584 case RADConnectionDesignValue.TYPE_PROPERTY: 585 el.setAttribute(ATTR_COMPONENT, componentName); 586 el.setAttribute(ATTR_NAME, designValue.propertyName); 587 break; 588 case RADConnectionDesignValue.TYPE_METHOD: 589 el.setAttribute(ATTR_COMPONENT, componentName); 590 el.setAttribute(ATTR_NAME, designValue.methodName); 591 break; 592 case RADConnectionDesignValue.TYPE_BEAN: 593 el.setAttribute(ATTR_COMPONENT, componentName); 594 break; 595 case RADConnectionDesignValue.TYPE_CODE: 596 el.setAttribute(ATTR_CODE, designValue.userCode); 597 break; 598 } 599 600 return el; 601 } 602 } 603 | Popular Tags |