1 19 20 package org.netbeans.modules.form; 21 22 import java.util.*; 23 import java.beans.*; 24 import javax.accessibility.*; 25 26 import org.openide.nodes.*; 27 28 import org.netbeans.modules.form.layoutdesign.*; 29 import org.netbeans.modules.form.layoutsupport.*; 30 import org.openide.util.NbBundle; 31 32 36 37 public class RADVisualComponent extends RADComponent { 38 private static final String PROP_LAYOUT_COMPONENT_HORIZONTAL_SIZE = "layoutComponentHorizontalSize"; private static final String PROP_LAYOUT_COMPONENT_VERTICAL_SIZE = "layoutComponentVerticalSize"; private static final String PROP_LAYOUT_COMPONENT_HORIZONTAL_RESIZABLE = "layoutComponentHorizontalResizable"; private static final String PROP_LAYOUT_COMPONENT_VERTICAL_RESIZABLE = "layoutComponentVerticalResizable"; 43 46 private HashMap constraints = new HashMap(); 48 50 private Node.Property[] constraintsProperties; 51 private ConstraintsListenerConvertor constraintsListener; 52 53 private MetaAccessibleContext accessibilityData; 54 private FormProperty[] accessibilityProperties; 55 56 59 65 69 80 81 84 85 89 public final RADVisualContainer getParentContainer() { 90 return (RADVisualContainer) getParentComponent(); 91 } 92 93 94 public final int getComponentIndex() { 95 RADVisualContainer parent = (RADVisualContainer) getParentComponent(); 96 return parent != null ? parent.getIndexOf(this) : -1; 97 } 99 100 final LayoutSupportManager getParentLayoutSupport() { 101 RADVisualContainer parent = (RADVisualContainer) getParentComponent(); 102 return parent != null ? parent.getLayoutSupport() : null; 103 } 104 105 108 110 public void setLayoutConstraints(Class layoutDelegateClass, 111 LayoutConstraints constr) 112 { 113 if (constr != null) 114 constraints.put(layoutDelegateClass.getName(), constr); 115 } 116 117 119 public LayoutConstraints getLayoutConstraints(Class layoutDelegateClass) { 120 return (LayoutConstraints) 121 constraints.get(layoutDelegateClass.getName()); 122 } 123 124 HashMap getConstraintsMap() { 125 return constraints; 126 } 127 128 void setConstraintsMap(Map map) { 129 constraints.putAll(map); 130 } 131 132 135 protected void createPropertySets(List propSets) { 136 super.createPropertySets(propSets); 137 138 if (constraintsProperties == null) 139 createConstraintsProperties(); 140 141 if (constraintsProperties != null && constraintsProperties.length > 0) 142 propSets.add(propSets.size() - 1, 143 new Node.PropertySet("layout", FormUtils.getBundleString("CTL_LayoutTab"), FormUtils.getBundleString("CTL_LayoutTabHint")) { 147 public Node.Property[] getProperties() { 148 return getConstraintsProperties(); 149 } 150 }); 151 152 if (accessibilityProperties == null) 153 createAccessibilityProperties(); 154 155 if (accessibilityProperties.length > 0) 156 propSets.add(new Node.PropertySet( 157 "accessibility", FormUtils.getBundleString("CTL_AccessibilityTab"), FormUtils.getBundleString("CTL_AccessibilityTabHint")) { 161 public Node.Property[] getProperties() { 162 return getAccessibilityProperties(); 163 } 164 }); 165 } 166 167 public Node.Property getPropertyByName(String name, 168 Class propertyType, 169 boolean fromAll) 170 { 171 if (fromAll && accessibilityProperties == null) 172 createAccessibilityProperties(); 173 return super.getPropertyByName(name, propertyType, fromAll); 174 } 175 176 180 218 219 protected void clearProperties() { 220 super.clearProperties(); 221 constraintsProperties = null; 222 accessibilityData = null; 223 accessibilityProperties = null; 224 } 225 226 229 public Node.Property[] getConstraintsProperties() { 230 if (constraintsProperties == null) 231 createConstraintsProperties(); 232 return constraintsProperties; 233 } 234 235 public void resetConstraintsProperties() { 236 if (constraintsProperties != null) { 237 for (int i=0; i < constraintsProperties.length; i++) 238 nameToProperty.remove(constraintsProperties[i].getName()); 239 240 constraintsProperties = null; 241 propertySets = null; 242 243 RADComponentNode node = getNodeReference(); 244 if (node != null) 245 node.fireComponentPropertySetsChange(); 246 } 247 } 248 249 private void createConstraintsProperties() { 250 constraintsProperties = null; 251 252 LayoutSupportManager layoutSupport = getParentLayoutSupport(); 253 if (layoutSupport != null) { 254 LayoutConstraints constr = layoutSupport.getConstraints(this); 255 if (constr != null) 256 constraintsProperties = constr.getProperties(); 257 } else if (getParentContainer() != null) { 258 LayoutComponent component = getFormModel().getLayoutModel().getLayoutComponent(getId()); 259 if (component == null) return; constraintsProperties = new Node.Property[] { 261 new LayoutComponentSizeProperty(component, LayoutConstants.HORIZONTAL), 262 new LayoutComponentSizeProperty(component, LayoutConstants.VERTICAL), 263 new LayoutComponentResizableProperty(component, LayoutConstants.HORIZONTAL), 264 new LayoutComponentResizableProperty(component, LayoutConstants.VERTICAL) 265 }; 266 component.addPropertyChangeListener(new PropertyChangeListener() { 267 public void propertyChange(PropertyChangeEvent evt) { 268 RADComponentNode node = getNodeReference(); 269 if (node != null) { 270 String propName = evt.getPropertyName(); 271 if (LayoutConstants.PROP_HORIZONTAL_PREF_SIZE.equals(propName)) { 272 node.firePropertyChangeHelper(PROP_LAYOUT_COMPONENT_HORIZONTAL_SIZE, null, null); 273 } else if (LayoutConstants.PROP_VERTICAL_PREF_SIZE.equals(propName)) { 274 node.firePropertyChangeHelper(PROP_LAYOUT_COMPONENT_VERTICAL_SIZE, null, null); 275 } else if (LayoutConstants.PROP_HORIZONTAL_MAX_SIZE.equals(propName)) { 276 node.firePropertyChangeHelper(PROP_LAYOUT_COMPONENT_HORIZONTAL_RESIZABLE, null, null); 277 } else if (LayoutConstants.PROP_VERTICAL_MAX_SIZE.equals(propName)) { 278 node.firePropertyChangeHelper(PROP_LAYOUT_COMPONENT_VERTICAL_RESIZABLE, null, null); 279 } 280 } 281 } 282 }); 283 } 284 285 if (constraintsProperties == null) { 286 constraintsProperties = NO_PROPERTIES; 287 return; 288 } 289 290 for (int i=0; i < constraintsProperties.length; i++) { 291 if (constraintsProperties[i] instanceof FormProperty) { 292 FormProperty prop = (FormProperty)constraintsProperties[i]; 293 294 prop.addVetoableChangeListener(getConstraintsListener()); 296 prop.addPropertyChangeListener(getConstraintsListener()); 297 prop.addValueConvertor(getConstraintsListener()); 298 299 prop.setPropertyContext( 300 new RADProperty.RADPropertyContext(this)); 301 302 if (isReadOnly() || !isValid()) { 303 int type = prop.getAccessType() | FormProperty.NO_WRITE; 304 prop.setAccessType(type); 305 } 306 nameToProperty.put(prop.getName(), prop); 307 } 308 } 309 } 310 311 private ConstraintsListenerConvertor getConstraintsListener() { 312 if (constraintsListener == null) 313 constraintsListener = new ConstraintsListenerConvertor(); 314 return constraintsListener; 315 } 316 317 private class ConstraintsListenerConvertor implements VetoableChangeListener, 318 PropertyChangeListener, FormProperty.ValueConvertor 319 { 320 public void vetoableChange(PropertyChangeEvent ev) 321 throws PropertyVetoException 322 { 323 Object source = ev.getSource(); 324 String eventName = ev.getPropertyName(); 325 if (source instanceof FormProperty 326 && (FormProperty.PROP_VALUE.equals(eventName) 327 || FormProperty.PROP_VALUE_AND_EDITOR.equals(eventName))) 328 { 329 i18nPropertyChanged(ev); 330 331 LayoutSupportManager layoutSupport = getParentLayoutSupport(); 332 int index = getComponentIndex(); 333 LayoutConstraints constraints = 334 layoutSupport.getConstraints(index); 335 336 ev = new PropertyChangeEvent(constraints, 337 ((FormProperty)source).getName(), 338 ev.getOldValue(), 339 ev.getNewValue()); 340 341 layoutSupport.componentLayoutChanged(index, ev); 342 } 343 } 344 345 public void propertyChange(PropertyChangeEvent ev) { 346 Object source = ev.getSource(); 347 if (source instanceof FormProperty 348 && FormProperty.CURRENT_EDITOR.equals(ev.getPropertyName())) 349 { 350 LayoutSupportManager layoutSupport = getParentLayoutSupport(); 351 int index = getComponentIndex(); 352 LayoutConstraints constraints = 353 layoutSupport.getConstraints(index); 354 355 ev = new PropertyChangeEvent(constraints, null, null, null); 356 357 try { 358 layoutSupport.componentLayoutChanged(index, ev); 359 } 360 catch (PropertyVetoException ex) {} } 362 } 363 364 public Object convert(Object value, FormProperty property) { 365 return i18nPropertyConvert(value, property); 366 } 367 } 368 369 372 public FormProperty[] getAccessibilityProperties() { 373 if (accessibilityProperties == null) 374 createAccessibilityProperties(); 375 return accessibilityProperties; 376 } 377 378 private void createAccessibilityProperties() { 379 Object comp = getBeanInstance(); 380 if (comp instanceof Accessible 381 && ((Accessible)comp).getAccessibleContext() != null) 382 { 383 if (accessibilityData == null) 384 accessibilityData = new MetaAccessibleContext(); 385 accessibilityProperties = accessibilityData.getProperties(); 386 387 for (int i=0; i < accessibilityProperties.length; i++) { 388 FormProperty prop = accessibilityProperties[i]; 389 setPropertyListener(prop); 390 prop.setPropertyContext( 391 new RADProperty.RADPropertyContext(this)); 392 nameToProperty.put(prop.getName(), prop); 393 } 394 } 395 else { 396 accessibilityData = null; 397 accessibilityProperties = NO_PROPERTIES; 398 } 399 } 400 401 private class MetaAccessibleContext { 402 private Object accName = BeanSupport.NO_VALUE; 403 private Object accDescription = BeanSupport.NO_VALUE; 404 private Object accParent = BeanSupport.NO_VALUE; 405 406 private FormProperty[] properties; 407 408 FormProperty[] getProperties() { 409 if (properties == null) { 410 properties = new FormProperty[] { 411 new FormProperty( 412 "AccessibleContext.accessibleName", String .class, 414 FormUtils.getBundleString("PROP_AccessibleName"), FormUtils.getBundleString("PROP_AccessibleName")) { 417 public Object getTargetValue() { 418 return accName != BeanSupport.NO_VALUE ? 419 accName : getDefaultValue(); 420 } 421 public void setTargetValue(Object value) { 422 accName = (String ) value; 423 } 424 public boolean supportsDefaultValue () { 425 return true; 426 } 427 public Object getDefaultValue() { 428 return getAccessibleContext().getAccessibleName(); 429 } 430 public void restoreDefaultValue() 431 throws IllegalAccessException , 432 java.lang.reflect.InvocationTargetException 433 { 434 super.restoreDefaultValue(); 435 accName = BeanSupport.NO_VALUE; 436 } 437 String getPartialSetterCode(String javaInitStr) { 438 return "getAccessibleContext().setAccessibleName(" + javaInitStr + ")"; } 441 }, 442 443 new FormProperty( 444 "AccessibleContext.accessibleDescription", String .class, 446 FormUtils.getBundleString("PROP_AccessibleDescription"), FormUtils.getBundleString("PROP_AccessibleDescription")) { 449 public Object getTargetValue() { 450 return accDescription != BeanSupport.NO_VALUE ? 451 accDescription : getDefaultValue(); 452 } 453 public void setTargetValue(Object value) { 454 accDescription = (String ) value; 455 } 456 public boolean supportsDefaultValue () { 457 return true; 458 } 459 public Object getDefaultValue() { 460 return getAccessibleContext().getAccessibleDescription(); 461 } 462 public void restoreDefaultValue() 463 throws IllegalAccessException , 464 java.lang.reflect.InvocationTargetException 465 { 466 super.restoreDefaultValue(); 467 accDescription = BeanSupport.NO_VALUE; 468 } 469 String getPartialSetterCode(String javaInitStr) { 470 return 471 "getAccessibleContext().setAccessibleDescription(" + javaInitStr + ")"; } 474 }, 475 476 new FormProperty( 477 "AccessibleContext.accessibleParent", Accessible.class, 479 FormUtils.getBundleString("PROP_AccessibleParent"), FormUtils.getBundleString("PROP_AccessibleParent")) { 482 public Object getTargetValue() { 483 return accParent != BeanSupport.NO_VALUE ? 484 accParent : getDefaultValue(); 485 } 486 public void setTargetValue(Object value) { 487 accParent = value; 488 } 489 public boolean supportsDefaultValue () { 490 return true; 491 } 492 public Object getDefaultValue() { 493 Object acP = getAccessibleContext() 494 .getAccessibleParent(); 495 if (acP != null) { 496 RADVisualContainer metacont = getParentContainer(); 497 if (metacont != null) { 498 Object cont = metacont.getContainerDelegate( 499 metacont.getBeanInstance()); 500 if (cont == acP) 501 return metacont; 502 } 503 } 504 return acP; 505 } 506 public void restoreDefaultValue() 507 throws IllegalAccessException , 508 java.lang.reflect.InvocationTargetException 509 { 510 super.restoreDefaultValue(); 511 accParent = BeanSupport.NO_VALUE; 512 } 513 public PropertyEditor getExpliciteEditor() { 514 return new AccessibleParentEditor(); 515 } 516 String getPartialSetterCode(String javaInitStr) { 517 return javaInitStr == null ? null : 518 "getAccessibleContext().setAccessibleParent(" + javaInitStr + ")"; } 521 } 522 }; 523 } 524 return properties; 525 } 526 527 private AccessibleContext getAccessibleContext() { 528 return ((Accessible)getBeanInstance()).getAccessibleContext(); 529 } 530 } 531 532 public static class AccessibleParentEditor extends ComponentChooserEditor { 533 public AccessibleParentEditor() { 534 super(); 535 setBeanTypes(new Class [] { Accessible.class }); 536 } 537 538 public String getDisplayName() { 539 return NbBundle.getBundle(getClass()).getString("CTL_AccessibleParentEditor_DisplayName"); } 541 } 542 543 546 private class LayoutComponentSizeProperty extends PropertySupport.ReadWrite { 547 private LayoutComponent component; 548 private int dimension; 549 550 private LayoutComponentSizeProperty(LayoutComponent component, int dimension) { 551 super(dimension == LayoutConstants.HORIZONTAL ? PROP_LAYOUT_COMPONENT_HORIZONTAL_SIZE 552 : PROP_LAYOUT_COMPONENT_VERTICAL_SIZE, Integer .class, null, null); 553 boolean horizontal = dimension == LayoutConstants.HORIZONTAL; 554 setDisplayName(FormUtils.getBundleString(horizontal ? 555 "PROP_LAYOUT_COMPONENT_HORIZONTAL_SIZE" : "PROP_LAYOUT_COMPONENT_VERTICAL_SIZE")); setShortDescription(FormUtils.getBundleString(horizontal ? 557 "HINT_LAYOUT_COMPONENT_HORIZONTAL_SIZE" : "HINT_LAYOUT_COMPONENT_VERTICAL_SIZE")); this.component = component; 559 this.dimension = dimension; 560 setValue("canEditAsText", Boolean.TRUE); } 562 563 public void setValue(Object value) { 564 if (!(value instanceof Integer )) 565 throw new IllegalArgumentException (); 566 567 Integer oldValue = (Integer )getValue(); 568 Integer newValue = (Integer )value; 569 LayoutModel layoutModel = getFormModel().getLayoutModel(); 570 LayoutInterval interval = component.getLayoutInterval(dimension); 571 Object layoutUndoMark = layoutModel.getChangeMark(); 572 javax.swing.undo.UndoableEdit ue = layoutModel.getUndoableEdit(); 573 boolean autoUndo = true; 574 try { 575 layoutModel.setIntervalSize(interval, interval.getMinimumSize(false), newValue.intValue(), interval.getMaximumSize(false)); 576 getNodeReference().firePropertyChangeHelper( 577 getName(), oldValue, newValue); 578 autoUndo = false; 579 } finally { 580 getFormModel().fireContainerLayoutChanged(getParentContainer(), null, null, null); 581 if (!layoutUndoMark.equals(layoutModel.getChangeMark())) { 582 getFormModel().addUndoableEdit(ue); 583 } 584 if (autoUndo) { 585 getFormModel().forceUndoOfCompoundEdit(); 586 } 587 } 588 } 589 590 public Object getValue() { 591 int size = component.getLayoutInterval(dimension).getPreferredSize(false); 592 return new Integer (size); 593 } 594 595 public boolean supportsDefaultValue() { 596 return true; 597 } 598 599 public void restoreDefaultValue() { 600 setValue(new Integer (LayoutConstants.NOT_EXPLICITLY_DEFINED)); 601 } 602 603 public boolean isDefaultValue() { 604 return ((Integer )getValue()).intValue() == LayoutConstants.NOT_EXPLICITLY_DEFINED; 605 } 606 607 public PropertyEditor getPropertyEditor() { 608 return new PropertyEditorSupport() { 609 private String notExplicitelyDefined = FormUtils.getBundleString("VALUE_SizeNotExplicitelyDefined"); 611 public String [] getTags() { 612 return new String [] {notExplicitelyDefined}; 613 } 614 615 public String getAsText() { 616 Integer value = (Integer )getValue(); 617 if (value.intValue() == LayoutConstants.NOT_EXPLICITLY_DEFINED) { 618 return notExplicitelyDefined; 619 } else { 620 return value.toString(); 621 } 622 } 623 624 public void setAsText(String str) { 625 if (notExplicitelyDefined.equals(str)) { 626 setValue(new Integer (LayoutConstants.NOT_EXPLICITLY_DEFINED)); 627 } else { 628 try { 629 setValue(new Integer (Integer.parseInt(str))); 630 } 631 catch (NumberFormatException e) {} } 633 } 634 }; 635 } 636 637 public boolean canWrite() { 638 return !isReadOnly(); 639 } 640 641 } 642 643 646 private class LayoutComponentResizableProperty extends PropertySupport.ReadWrite { 647 private LayoutComponent component; 648 private int dimension; 649 650 private LayoutComponentResizableProperty(LayoutComponent component, int dimension) { 651 super(dimension == LayoutConstants.HORIZONTAL ? PROP_LAYOUT_COMPONENT_HORIZONTAL_RESIZABLE 652 : PROP_LAYOUT_COMPONENT_VERTICAL_RESIZABLE, Boolean .class, null, null); 653 boolean horizontal = dimension == LayoutConstants.HORIZONTAL; 654 setDisplayName(FormUtils.getBundleString(horizontal ? 655 "PROP_LAYOUT_COMPONENT_HORIZONTAL_RESIZABLE" : "PROP_LAYOUT_COMPONENT_VERTICAL_RESIZABLE")); setShortDescription(FormUtils.getBundleString(horizontal ? 657 "HINT_LAYOUT_COMPONENT_HORIZONTAL_RESIZABLE" : "HINT_LAYOUT_COMPONENT_VERTICAL_RESIZABLE")); this.component = component; 659 this.dimension = dimension; 660 } 661 662 public void setValue(Object value) { 663 if (!(value instanceof Boolean )) 664 throw new IllegalArgumentException (); 665 666 Boolean oldValue = (Boolean )getValue(); 667 Boolean newValue = (Boolean )value; 668 boolean resizable = newValue.booleanValue(); 669 LayoutModel layoutModel = getFormModel().getLayoutModel(); 670 LayoutInterval interval = component.getLayoutInterval(dimension); 671 Object layoutUndoMark = layoutModel.getChangeMark(); 672 javax.swing.undo.UndoableEdit ue = layoutModel.getUndoableEdit(); 673 boolean autoUndo = true; 674 try { 675 layoutModel.setIntervalSize(interval, 676 resizable ? LayoutConstants.NOT_EXPLICITLY_DEFINED : LayoutConstants.USE_PREFERRED_SIZE, 677 interval.getPreferredSize(false), 678 resizable ? Short.MAX_VALUE : LayoutConstants.USE_PREFERRED_SIZE); 679 getNodeReference().firePropertyChangeHelper( 680 getName(), oldValue, newValue); 681 autoUndo = false; 682 } finally { 683 getFormModel().fireContainerLayoutChanged(getParentContainer(), null, null, null); 684 if (!layoutUndoMark.equals(layoutModel.getChangeMark())) { 685 getFormModel().addUndoableEdit(ue); 686 } 687 if (autoUndo) { 688 getFormModel().forceUndoOfCompoundEdit(); 689 } 690 } 691 } 692 693 public Object getValue() { 694 int pref = component.getLayoutInterval(dimension).getPreferredSize(false); 695 int max = component.getLayoutInterval(dimension).getMaximumSize(false); 696 return Boolean.valueOf((max != pref) && (max != LayoutConstants.USE_PREFERRED_SIZE)); 697 } 698 699 public boolean supportsDefaultValue() { 700 return true; 701 } 702 703 public void restoreDefaultValue() { 704 setValue(Boolean.FALSE); 705 } 706 707 public boolean isDefaultValue() { 708 return getValue().equals(Boolean.FALSE); 709 } 710 711 public boolean canWrite() { 712 return !isReadOnly(); 713 } 714 715 } 716 717 } 718 | Popular Tags |