1 19 20 package org.netbeans.modules.form; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.io.*; 25 import java.util.*; 26 import java.lang.reflect.Method ; 27 import java.util.List ; 28 import javax.swing.border.TitledBorder ; 29 import java.text.MessageFormat ; 30 31 import org.openide.ErrorManager; 32 import org.openide.util.*; 33 import org.openide.nodes.Node; 34 import org.openide.filesystems.FileObject; 35 36 import org.netbeans.modules.form.project.ClassPathUtils; 37 38 42 43 public class FormUtils 44 { 45 public static final int CHANGED_ONLY = 1; 47 public static final int DISABLE_CHANGE_FIRING = 2; 48 public static final int PASS_DESIGN_VALUES = 4; 49 public static final int DONT_CLONE_VALUES = 8; 50 51 private static final Object CLASS_EXACTLY = new Object (); 52 private static final Object CLASS_AND_SUBCLASSES = new Object (); 53 private static final Object CLASS_AND_SWING_SUBCLASSES = new Object (); 54 55 static final Object PROP_PREFERRED = new Object (); 56 static final Object PROP_NORMAL = new Object (); 57 static final Object PROP_EXPERT = new Object (); 58 static final Object PROP_HIDDEN = new Object (); 59 60 static final String PROP_REQUIRES_PARENT = "thisPropertyRequiresParent"; static final String PROP_REQUIRES_CHILDREN = "thisPropertyRequiresChildren"; 63 65 private static Object [][] propertyCategories = { 66 { "java.awt.Component", CLASS_AND_SUBCLASSES, 67 "locale", PROP_HIDDEN, 68 "locationOnScreen", PROP_HIDDEN, 69 "showing", PROP_HIDDEN }, 70 { "java.awt.Component", CLASS_AND_SWING_SUBCLASSES, 71 "accessibleContext", PROP_HIDDEN, 72 "components", PROP_HIDDEN, 73 "containerListeners", PROP_HIDDEN, 74 "focusTraversalPolicySet", PROP_HIDDEN, 75 "focusCycleRootAncestor", PROP_HIDDEN, 76 "focusOwner", PROP_HIDDEN }, 77 { "java.awt.Container", CLASS_AND_SUBCLASSES, 78 "componentCount", PROP_HIDDEN, 79 "layout", PROP_HIDDEN }, 80 { "javax.swing.JComponent", CLASS_AND_SUBCLASSES, 81 "debugGraphicsOptions", PROP_EXPERT, 82 "preferredSize", PROP_NORMAL, 83 "actionMap", PROP_HIDDEN }, 84 { "javax.swing.JComponent", CLASS_AND_SWING_SUBCLASSES, 85 "graphics", PROP_HIDDEN, 86 "height", PROP_HIDDEN, 87 "inputMap", PROP_HIDDEN, 88 "maximumSizeSet", PROP_HIDDEN, 89 "minimumSizeSet", PROP_HIDDEN, 90 "preferredSizeSet", PROP_HIDDEN, 91 "registeredKeyStrokes", PROP_HIDDEN, 92 "rootPane", PROP_HIDDEN, 93 "topLevelAncestor", PROP_HIDDEN, 94 "validateRoot", PROP_HIDDEN, 95 "visibleRect", PROP_HIDDEN, 96 "width", PROP_HIDDEN, 97 "x", PROP_HIDDEN, 98 "y", PROP_HIDDEN, 99 "ancestorListeners", PROP_HIDDEN, 100 "propertyChangeListeners", PROP_HIDDEN, 101 "vetoableChangeListeners", PROP_HIDDEN, 102 "actionListeners", PROP_HIDDEN, 103 "changeListeners", PROP_HIDDEN, 104 "itemListeners", PROP_HIDDEN, 105 "managingFocus", PROP_HIDDEN, 106 "optimizedDrawingEnabled", PROP_HIDDEN, 107 "paintingTile", PROP_HIDDEN }, 108 { "java.awt.Window", CLASS_AND_SWING_SUBCLASSES, 109 "focusCycleRootAncestor", PROP_HIDDEN, 110 "focusOwner", PROP_HIDDEN, 111 "active", PROP_HIDDEN, 112 "alignmentX", PROP_HIDDEN, 113 "alignmentY", PROP_HIDDEN, 114 "bufferStrategy", PROP_HIDDEN, 115 "focused", PROP_HIDDEN, 116 "graphicsConfiguration", PROP_HIDDEN, 117 "mostRecentFocusOwner", PROP_HIDDEN, 118 "inputContext", PROP_HIDDEN, 119 "ownedWindows", PROP_HIDDEN, 120 "owner", PROP_HIDDEN, 121 "windowFocusListeners", PROP_HIDDEN, 122 "windowListeners", PROP_HIDDEN, 123 "windowStateListeners", PROP_HIDDEN, 124 "warningString", PROP_HIDDEN, 125 "toolkit", PROP_HIDDEN, 126 "focusableWindow", PROP_HIDDEN, 127 "locationRelativeTo", PROP_HIDDEN }, 128 { "javax.swing.text.JTextComponent", CLASS_AND_SUBCLASSES, 129 "document", PROP_PREFERRED, 130 "text", PROP_PREFERRED, 131 "editable", PROP_PREFERRED, 132 "disabledTextColor", PROP_NORMAL, 133 "selectedTextColor", PROP_NORMAL, 134 "selectionColor", PROP_NORMAL, 135 "caretColor", PROP_NORMAL }, 136 { "javax.swing.text.JTextComponent", CLASS_AND_SWING_SUBCLASSES, 137 "actions", PROP_HIDDEN, 138 "caretListeners", PROP_HIDDEN, 139 "inputMethodRequests", PROP_HIDDEN }, 140 { "javax.swing.JTextField", CLASS_AND_SUBCLASSES, 141 "columns", PROP_PREFERRED }, 142 { "javax.swing.JTextField", CLASS_AND_SWING_SUBCLASSES, 143 "horizontalVisibility", PROP_HIDDEN }, 144 { "javax.swing.JFormattedTextField", CLASS_EXACTLY, 145 "formatter", PROP_HIDDEN }, 146 { "javax.swing.JPasswordField", CLASS_EXACTLY, 147 "password", PROP_HIDDEN }, 148 { "javax.swing.JTextArea", CLASS_AND_SUBCLASSES, 149 "columns", PROP_PREFERRED, 150 "rows", PROP_PREFERRED, 151 "lineWrap", PROP_PREFERRED, 152 "wrapStyleWord", PROP_PREFERRED }, 153 { "javax.swing.JEditorPane", CLASS_AND_SUBCLASSES, 154 "border", PROP_PREFERRED, 155 "font", PROP_PREFERRED, 156 "contentType", PROP_PREFERRED, 157 "editorKit", PROP_PREFERRED }, 158 { "javax.swing.JEditorPane", CLASS_AND_SWING_SUBCLASSES, 159 "hyperlinkListeners", PROP_HIDDEN }, 160 { "javax.swing.JTextPane", CLASS_EXACTLY, 161 "characterAttributes", PROP_HIDDEN, 162 "paragraphAttributes", PROP_HIDDEN }, 163 { "javax.swing.JTree", CLASS_AND_SUBCLASSES, 164 "border", PROP_PREFERRED }, 165 { "javax.swing.JTree", CLASS_EXACTLY, 166 "editing", PROP_HIDDEN, 167 "editingPath", PROP_HIDDEN, 168 "selectionCount", PROP_HIDDEN, 169 "selectionEmpty", PROP_HIDDEN, 170 "lastSelectedPathComponent", PROP_HIDDEN, 171 "leadSelectionRow", PROP_HIDDEN, 172 "maxSelectionRow", PROP_HIDDEN, 173 "minSelectionRow", PROP_HIDDEN, 174 "treeExpansionListeners", PROP_HIDDEN, 175 "treeSelectionListeners", PROP_HIDDEN, 176 "treeWillExpandListeners", PROP_HIDDEN }, 177 { "javax.swing.AbstractButton", CLASS_AND_SUBCLASSES, 178 "mnemonic", PROP_PREFERRED, 179 "action", PROP_PREFERRED }, 180 { "javax.swing.AbstractButton", CLASS_AND_SWING_SUBCLASSES, 181 "selectedObjects", PROP_HIDDEN }, 182 { "javax.swing.JToggleButton", CLASS_AND_SUBCLASSES, 183 "icon", PROP_PREFERRED, 184 "selected", PROP_PREFERRED, 185 "buttonGroup", PROP_PREFERRED }, 186 { "javax.swing.JButton", CLASS_AND_SUBCLASSES, 187 "icon", PROP_PREFERRED, 188 "defaultButton", PROP_HIDDEN }, 189 { "javax.swing.JCheckBox", CLASS_EXACTLY, 190 "icon", PROP_NORMAL }, 191 { "javax.swing.JRadioButton", CLASS_EXACTLY, 192 "icon", PROP_NORMAL }, 193 { "javax.swing.JMenuItem", CLASS_AND_SUBCLASSES, 194 "icon", PROP_PREFERRED }, 195 { "javax.swing.JMenuItem", CLASS_AND_SWING_SUBCLASSES, 196 "menuDragMouseListeners", PROP_HIDDEN, 197 "menuKeyListeners", PROP_HIDDEN }, 198 { "javax.swing.JCheckBoxMenuItem", CLASS_AND_SUBCLASSES, 199 "selected", PROP_PREFERRED, 200 "buttonGroup", PROP_PREFERRED, 201 "icon", PROP_NORMAL }, 202 { "javax.swing.JRadioButtonMenuItem", CLASS_AND_SUBCLASSES, 203 "selected", PROP_PREFERRED, 204 "buttonGroup", PROP_PREFERRED, 205 "icon", PROP_NORMAL }, 206 { "javax.swing.JTabbedPane", CLASS_EXACTLY, 207 "selectedComponent", PROP_EXPERT }, 208 { "javax.swing.JSplitPane", CLASS_AND_SUBCLASSES, 209 "dividerLocation", PROP_PREFERRED, 210 "dividerSize", PROP_PREFERRED, 211 "orientation", PROP_PREFERRED, 212 "resizeWeight", PROP_PREFERRED }, 213 { "javax.swing.JSplitPane", CLASS_EXACTLY, 214 "leftComponent", PROP_HIDDEN, 215 "rightComponent", PROP_HIDDEN, 216 "topComponent", PROP_HIDDEN, 217 "bottomComponent", PROP_HIDDEN }, 218 { "javax.swing.JSlider", CLASS_AND_SUBCLASSES, 219 "majorTickSpacing", PROP_PREFERRED, 220 "minorTickSpacing", PROP_PREFERRED, 221 "paintLabels", PROP_PREFERRED, 222 "paintTicks", PROP_PREFERRED, 223 "paintTrack", PROP_PREFERRED, 224 "snapToTicks", PROP_PREFERRED }, 225 { "javax.swing.JLabel", CLASS_AND_SUBCLASSES, 226 "horizontalAlignment", PROP_PREFERRED, 227 "verticalAlignment", PROP_PREFERRED, 228 "displayedMnemonic", PROP_PREFERRED, 229 "labelFor", PROP_PREFERRED }, 230 { "javax.swing.JList", CLASS_AND_SUBCLASSES, 231 "model", PROP_PREFERRED, 232 "border", PROP_PREFERRED, 233 "selectionMode", PROP_PREFERRED }, 234 { "javax.swing.JComboBox", CLASS_AND_SUBCLASSES, 235 "model", PROP_PREFERRED }, 236 { "javax.swing.JComboBox", CLASS_EXACTLY, 237 "popupVisible", PROP_HIDDEN, 238 "popupMenuListeners", PROP_HIDDEN, 239 "selectedObjects", PROP_HIDDEN }, 240 { "javax.swing.Scrollable", CLASS_AND_SWING_SUBCLASSES, 241 "preferredScrollableViewportSize", PROP_HIDDEN, 242 "scrollableTracksViewportWidth", PROP_HIDDEN, 243 "scrollableTracksViewportHeight", PROP_HIDDEN }, 244 { "javax.swing.JScrollBar", CLASS_EXACTLY, 245 "adjustmentListeners", PROP_HIDDEN }, 246 { "javax.swing.JTable", CLASS_AND_SUBCLASSES, 247 "model", PROP_PREFERRED, 248 "border", PROP_PREFERRED }, 249 { "javax.swing.JTable", CLASS_EXACTLY, 250 "editing", PROP_HIDDEN, 251 "editorComponent", PROP_HIDDEN, 252 "selectedColumn", PROP_HIDDEN, 253 "selectedColumnCount", PROP_HIDDEN, 254 "selectedColumns", PROP_HIDDEN, 255 "selectedRow", PROP_HIDDEN, 256 "selectedRowCount", PROP_HIDDEN, 257 "selectedRows", PROP_HIDDEN }, 258 { "javax.swing.JSeparator", CLASS_EXACTLY, 259 "font", PROP_NORMAL }, 260 { "javax.swing.JInternalFrame", CLASS_AND_SUBCLASSES, 261 "defaultCloseOperation", PROP_PREFERRED, 262 "visible", PROP_NORMAL }, 263 { "javax.swing.JInternalFrame", CLASS_EXACTLY, 264 "menuBar", PROP_HIDDEN, 265 "JMenuBar", PROP_HIDDEN, 266 "desktopPane", PROP_HIDDEN, 267 "internalFrameListeners", PROP_HIDDEN, 268 "mostRecentFocusOwner", PROP_HIDDEN, 269 "warningString", PROP_HIDDEN, 270 "closed", PROP_HIDDEN }, 271 { "javax.swing.JMenu", CLASS_EXACTLY, 272 "accelerator", PROP_HIDDEN, 273 "tearOff", PROP_HIDDEN, 274 "menuComponents", PROP_HIDDEN, 275 "menuListeners", PROP_HIDDEN, 276 "popupMenu", PROP_HIDDEN, 277 "topLevelMenu", PROP_HIDDEN }, 278 { "javax.swing.JPopupMenu", CLASS_AND_SWING_SUBCLASSES, 279 "popupMenuListeners", PROP_HIDDEN }, 280 { "java.awt.Frame", CLASS_AND_SWING_SUBCLASSES, 281 "cursorType", PROP_HIDDEN, 282 "menuBar", PROP_HIDDEN }, 283 { "javax.swing.JFrame", CLASS_AND_SUBCLASSES, 284 "title", PROP_PREFERRED }, 285 { "javax.swing.JFrame", CLASS_EXACTLY, 286 "menuBar", PROP_HIDDEN, 287 "layout", PROP_HIDDEN }, 288 { "javax.swing.JDialog", CLASS_AND_SUBCLASSES, 289 "title", PROP_PREFERRED }, 290 { "javax.swing.JDialog", CLASS_EXACTLY, 291 "layout", PROP_HIDDEN }, 292 { "javax.swing.MenuElement", CLASS_AND_SWING_SUBCLASSES, 293 "component", PROP_HIDDEN, 294 "subElements", PROP_HIDDEN }, 295 { "javax.swing.JMenuBar", CLASS_EXACTLY, 296 "helpMenu", PROP_HIDDEN, 297 "menuCount", PROP_HIDDEN, 298 "selected", PROP_HIDDEN }, 299 { "javax.swing.JSpinner", CLASS_AND_SUBCLASSES, 300 "model", PROP_PREFERRED }, 301 { "java.applet.Applet", CLASS_AND_SUBCLASSES, 302 "appletContext", PROP_HIDDEN, 303 "codeBase", PROP_HIDDEN, 304 "documentBase", PROP_HIDDEN }, 305 { "javax.swing.JFileChooser", CLASS_EXACTLY, 306 "acceptAllFileFilter", PROP_HIDDEN, 307 "choosableFileFilters", PROP_HIDDEN } 308 }; 309 310 312 private static Object [][] propertiesAccess = { 313 { "javax.swing.JFrame", CLASS_AND_SUBCLASSES, 314 "defaultCloseOperation", new Integer (FormProperty.DETACHED_WRITE) } 315 }; 316 317 321 private static Object [][] propertyContainerDeps = { 322 { "javax.swing.JTabbedPane", CLASS_AND_SUBCLASSES, 323 "selectedIndex", PROP_REQUIRES_CHILDREN, 324 "selectedComponent", PROP_REQUIRES_CHILDREN }, 325 { "javax.swing.JInternalFrame", CLASS_AND_SUBCLASSES, 326 "maximum", PROP_REQUIRES_PARENT, 327 "icon", PROP_REQUIRES_PARENT } 328 }; 329 330 331 private static Object [][] propertyOrder = { 332 { "javax.swing.text.JTextComponent", 333 "document", "text" }, 334 { "javax.swing.JSpinner", 335 "model", "editor" }, 336 { "javax.swing.AbstractButton", 337 "action", "actionCommand", 338 "action", "enabled", 339 "action", "mnemonic", 340 "action", "icon", 341 "action", "text", 342 "action", "toolTipText" }, 343 { "javax.swing.JMenuItem", 344 "action", "accelerator" }, 345 { "javax.swing.JList", 346 "model", "selectedIndex", 347 "model", "selectedValues" }, 348 { "javax.swing.JComboBox", 349 "model", "selectedIndex", 350 "model", "selectedItem" }, 351 { "java.awt.TextComponent", 352 "text", "selectionStart", 353 "text", "selectionEnd" }, 354 { "javax.swing.JEditorPane", 355 "contentType", "text", 356 "editorKit", "text" } 357 }; 358 359 361 private static String [] forbiddenContainers = { 362 "javax.swing.JLabel", "javax.swing.JButton", "javax.swing.JToggleButton", "javax.swing.JCheckBox", "javax.swing.JRadioButton", "javax.swing.JComboBox", "javax.swing.JList", "javax.swing.JTextField", "javax.swing.JTextArea", "javax.swing.JScrollBar", "javax.swing.JSlider", "javax.swing.JProgressBar", "javax.swing.JFormattedTextField", "javax.swing.JPasswordField", "javax.swing.JSpinner", "javax.swing.JSeparator", "javax.swing.JTextPane", "javax.swing.JEditorPane", "javax.swing.JTree", "javax.swing.JTable", "javax.swing.JOptionPane", "javax.swing.JColorChooser", "javax.swing.JFileChooser", }; 386 387 390 public static ResourceBundle getBundle() { 391 return NbBundle.getBundle(FormUtils.class); 392 } 393 394 public static String getBundleString(String key) { 395 return NbBundle.getBundle(FormUtils.class).getString(key); 396 } 397 398 public static String getFormattedBundleString(String key, 399 Object [] arguments) 400 { 401 ResourceBundle bundle = NbBundle.getBundle(FormUtils.class); 402 return MessageFormat.format(bundle.getString(key), arguments); 403 } 404 405 409 public static Object cloneObject(Object o, FormModel formModel) throws CloneNotSupportedException { 410 if (o == null) return null; 411 412 if ((o instanceof Byte ) || 413 (o instanceof Short ) || 414 (o instanceof Integer ) || 415 (o instanceof Long ) || 416 (o instanceof Float ) || 417 (o instanceof Double ) || 418 (o instanceof Boolean ) || 419 (o instanceof Character ) || 420 (o instanceof String )) { 421 return o; } 423 424 if (o.getClass() == Font.class) { 425 return o; 426 } 427 if ((o.getClass() == TitledBorder .class) 429 && (Utilities.isMac())) { 430 TitledBorder border = (TitledBorder )o; 431 return new TitledBorder ( 432 border.getBorder(), 433 border.getTitle(), 434 border.getTitleJustification(), 435 border.getTitlePosition(), 436 border.getTitleFont(), 437 border.getTitleColor()); 438 } 439 if (o.getClass() == Color.class) 440 return new Color(((Color)o).getRGB()); 441 if (o instanceof Dimension) 442 return new Dimension((Dimension)o); 443 if (o instanceof Point) 444 return new Point((Point)o); 445 if (o instanceof Rectangle) 446 return new Rectangle((Rectangle)o); 447 if (o instanceof Insets) 448 return ((Insets)o).clone(); 449 if (o instanceof GradientPaint) { 450 GradientPaint gp = (GradientPaint)o; 451 return new GradientPaint(gp.getPoint1(), gp.getColor1(), gp.getPoint2(), gp.getColor2(), gp.isCyclic()); 452 } 453 if (o instanceof Serializable) 454 return cloneBeanInstance(o, null, formModel); 455 456 throw new CloneNotSupportedException (); 457 } 458 459 464 public static Object cloneBeanInstance(Object bean, BeanInfo bInfo, FormModel formModel) 465 throws CloneNotSupportedException 466 { 467 if (bean == null) 468 return null; 469 470 if (bean instanceof Serializable) { 471 try { 472 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 473 ObjectOutputStream oos = new ObjectOutputStream(baos); 474 oos.writeObject(bean); 475 oos.close(); 476 477 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 478 return new OIS(bais, bean.getClass().getClassLoader(), formModel).readObject(); 479 } 480 catch (Exception ex) { 481 ErrorManager em = ErrorManager.getDefault(); 482 em.annotate(ex, "Cannot clone "+bean.getClass().getName()); em.notify(ErrorManager.INFORMATIONAL, ex); 484 throw new CloneNotSupportedException (ex.getMessage()); 485 } 486 } 487 488 Object clone; 490 try { 491 clone = CreationFactory.createDefaultInstance(bean.getClass()); 492 if (clone == null) 493 throw new CloneNotSupportedException (); 494 495 if (bInfo == null) 496 bInfo = Utilities.getBeanInfo(bean.getClass()); 497 } 498 catch (Exception ex) { 499 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 500 throw new CloneNotSupportedException (ex.getMessage()); 501 } 502 503 PropertyDescriptor[] pds = bInfo.getPropertyDescriptors(); 505 for (int i=0; i < pds.length; i++) { 506 Method getter = pds[i].getReadMethod(); 507 Method setter = pds[i].getWriteMethod(); 508 if (getter != null && setter != null) { 509 Object propertyValue; 510 try { 511 propertyValue = getter.invoke(bean, new Object [0]); 512 } 513 catch (Exception e1) { continue; 515 } 516 try { 517 propertyValue = cloneObject(propertyValue, formModel); 518 } 519 catch (Exception e2) { } 521 try { 522 setter.invoke(clone, new Object [] { propertyValue }); 523 } 524 catch (Exception e3) { } 526 } 527 } 528 529 return clone; 530 } 531 532 541 public static void copyProperties(Node.Property[] sourceProperties, 542 Node.Property[] targetProperties, 543 int mode) 544 { 545 for (int i=0; i < sourceProperties.length; i++) { 546 Node.Property snProp = sourceProperties[i]; 547 FormProperty sfProp = snProp instanceof FormProperty ? 548 (FormProperty)snProp : null; 549 550 if (sfProp != null 551 && (mode & CHANGED_ONLY) != 0 552 && !sfProp.isChanged()) 553 continue; 555 Node.Property tnProp = targetProperties[i]; 557 if (!tnProp.getName().equals(snProp.getName())) { 558 int j; 559 for (j=0; j < targetProperties.length; j++) { 560 tnProp = targetProperties[i]; 561 if (tnProp.getName().equals(snProp.getName())) 562 break; 563 } 564 if (j == targetProperties.length) 565 continue; } 567 FormProperty tfProp = tnProp instanceof FormProperty ? 568 (FormProperty)tnProp : null; 569 570 try { 571 Object propertyValue = snProp.getValue(); 573 Object copiedValue = propertyValue; 574 if ((mode & DONT_CLONE_VALUES) == 0) { 575 if (!(propertyValue instanceof FormDesignValue)) { 576 try { FormModel formModel = (sfProp == null) ? null : sfProp.getPropertyContext().getFormModel(); 578 copiedValue = FormUtils.cloneObject(propertyValue, formModel); 579 } 580 catch (CloneNotSupportedException ex) {} } 582 else { Object val = ((FormDesignValue)propertyValue).copy(tfProp); 584 if (val != null) 585 copiedValue = val; 586 else if ((mode & PASS_DESIGN_VALUES) == 0) 587 continue; } 589 } 590 591 if (tfProp != null) { 593 boolean firing = tfProp.isChangeFiring(); 594 tfProp.setChangeFiring((mode & DISABLE_CHANGE_FIRING) == 0); 595 tfProp.setValue(copiedValue); 596 tfProp.setChangeFiring(firing); 597 } 598 else tnProp.setValue(copiedValue); 599 600 if (sfProp != null && tfProp != null) { 601 PropertyEditor sPrEd = sfProp.getCurrentEditor(); 603 PropertyEditor tPrEd = tfProp.getCurrentEditor(); 604 if (sPrEd != null 605 && (tPrEd == null 606 || sPrEd.getClass() != tPrEd.getClass()) 607 && (propertyValue == copiedValue 608 || (propertyValue != null && copiedValue != null 609 && propertyValue.getClass() == copiedValue.getClass()))) 610 { 611 tPrEd = sPrEd instanceof RADConnectionPropertyEditor ? 612 new RADConnectionPropertyEditor(tfProp.getValueType()) : 613 (PropertyEditor)CreationFactory.createDefaultInstance( 614 sPrEd.getClass()); 615 tfProp.setCurrentEditor(tPrEd); 616 } 617 } 618 } 619 catch (Exception ex) { ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 621 } 622 } 623 } 624 625 public static void copyPropertiesToBean(RADProperty[] props, 626 Object targetBean, 627 Collection relativeProperties) 628 { 629 for (int i = 0; i < props.length; i++) { 630 RADProperty prop = props[i]; 631 if (!prop.isChanged()) 632 continue; 633 634 try { 635 if (relativeProperties != null) { 636 Object value = prop.getValue(); 637 if (value instanceof RADComponent 638 || value instanceof RADComponent.ComponentReference 639 || (value instanceof RADConnectionPropertyEditor.RADConnectionDesignValue 640 && (((RADConnectionPropertyEditor.RADConnectionDesignValue)value).type 641 == RADConnectionPropertyEditor.RADConnectionDesignValue.TYPE_BEAN))) 642 { 643 relativeProperties.add(prop); 644 continue; 645 } 646 } 647 648 Method writeMethod = prop.getPropertyDescriptor().getWriteMethod(); 649 if (writeMethod == null 650 || !prop.canWriteToTarget() 651 || !writeMethod.getDeclaringClass().isAssignableFrom( 652 targetBean.getClass())) 653 continue; 654 655 Object realValue = prop.getRealValue(); 656 if (realValue == FormDesignValue.IGNORED_VALUE) 657 continue; 659 realValue = FormUtils.cloneObject(realValue, props[i].getPropertyContext().getFormModel()); 660 writeMethod.invoke(targetBean, new Object [] { realValue }); 661 } 662 catch (CloneNotSupportedException ex) { } 664 catch (Exception ex) { 665 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 666 } 667 } 668 } 669 670 672 public static boolean isContainer(Class beanClass) { 673 679 int containerStatus = canBeContainer(beanClass); 681 if (containerStatus == -1) { containerStatus = 1; 683 Class cls = beanClass.getSuperclass(); 684 while (cls != null 685 && !cls.equals(java.awt.Container .class)) 686 { 687 String beanClassName = cls.getName(); 688 int i; 689 for (i=0; i < forbiddenContainers.length; i++) 690 if (beanClassName.equals(forbiddenContainers[i])) 691 break; 693 if (i < forbiddenContainers.length) { 694 containerStatus = 0; 695 break; 696 } 697 698 cls = cls.getSuperclass(); 699 } 700 } 701 702 return containerStatus == 1; 703 } 710 711 716 public static int canBeContainer(Class beanClass) { 717 if (beanClass == null 718 || !java.awt.Container .class.isAssignableFrom(beanClass)) 719 return 0; 720 721 String beanClassName = beanClass.getName(); 722 for (int i=0; i < forbiddenContainers.length; i++) 723 if (beanClassName.equals(forbiddenContainers[i])) 724 return 0; 726 Object isContainerValue = null; 727 try { 728 BeanDescriptor desc = Utilities.getBeanInfo(beanClass) 729 .getBeanDescriptor(); 730 if (desc != null) 731 isContainerValue = desc.getValue("isContainer"); } 733 catch (Exception ex) { ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 735 } 736 catch (Error ex) { ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 738 } 739 740 if (isContainerValue instanceof Boolean ) 741 return ((Boolean )isContainerValue).booleanValue() ? 1 : 0; 742 return -1; } 744 745 766 768 774 static Object [] getPropertiesCategoryClsf(Class beanClass, 775 BeanDescriptor beanDescriptor) 776 { 777 ArrayList reClsf = null; 778 780 if (javax.swing.JComponent .class.isAssignableFrom(beanClass)) { 782 reClsf = new ArrayList(8); 783 Object isContainerValue = beanDescriptor.getValue("isContainer"); if (isContainerValue == null || Boolean.TRUE.equals(isContainerValue)) { 785 reClsf.add("font"); reClsf.add(PROP_NORMAL); 787 } 788 else { 789 reClsf.add("border"); reClsf.add(PROP_NORMAL); } 792 } 793 794 return collectPropertiesClsf(beanClass, propertyCategories, reClsf); 795 } 796 797 802 static Object getPropertyCategory(PropertyDescriptor pd, 803 Object [] propsClsf) 804 { 805 Object cat = findPropertyClsf(pd.getName(), propsClsf); 806 if (cat != null) 807 return cat; 808 809 if (pd.isHidden()) 810 return PROP_HIDDEN; 811 if (pd.isExpert()) 812 return PROP_EXPERT; 813 if (pd.isPreferred() || Boolean.TRUE.equals(pd.getValue("preferred"))) return PROP_PREFERRED; 815 return PROP_NORMAL; 816 } 817 818 823 static Object [] getPropertiesAccessClsf(Class beanClass) { 824 return collectPropertiesClsf(beanClass, propertiesAccess, null); 825 } 826 827 830 static int getPropertyAccess(PropertyDescriptor pd, 831 Object [] propsClsf) 832 { 833 Object access = findPropertyClsf(pd.getName(), propsClsf); 834 return access == null ? 0 : ((Integer )access).intValue(); 835 } 836 837 static Object [] getPropertiesParentChildDepsClsf(Class beanClass) { 838 return collectPropertiesClsf(beanClass, propertyContainerDeps, null); 839 } 840 841 static String getPropertyParentChildDependency(PropertyDescriptor pd, 842 Object [] propClsf) 843 { 844 return (String ) findPropertyClsf(pd.getName(), propClsf); 845 } 846 847 private static Object [] collectPropertiesClsf(Class beanClass, 848 Object [][] table, 849 java.util.List list) 850 { 851 Set superClasses = superClasses(beanClass); 853 854 for (int i=0; i < table.length; i++) { 855 Object [] clsf = table[i]; 856 String refClass = (String )clsf[0]; 857 Object subclasses = clsf[1]; 858 859 if (refClass.equals(beanClass.getName()) 860 || 861 (subclasses == CLASS_AND_SUBCLASSES 862 && superClasses.contains(refClass)) 863 || 864 (subclasses == CLASS_AND_SWING_SUBCLASSES 865 && superClasses.contains(refClass) 866 && beanClass.getName().startsWith("javax.swing."))) { if (list == null) 868 list = new ArrayList(8); 869 for (int j=2; j < clsf.length; j++) 870 list.add(clsf[j]); 871 } 872 } 873 874 if (list != null) { 875 Object [] array = new Object [list.size()]; 876 list.toArray(array); 877 return array; 878 } 879 return null; 880 } 881 882 private static Object findPropertyClsf(String name, Object [] clsf) { 883 if (clsf != null) { 884 int i = clsf.length; 885 while (i > 0) { 886 if (clsf[i-2].equals(name)) 887 return clsf[i-1]; 888 i -= 2; 889 } 890 } 891 return null; 892 } 893 894 static boolean isMarkedParentDependentProperty(Node.Property prop) { 895 return Boolean.TRUE.equals(prop.getValue(PROP_REQUIRES_PARENT)); 896 } 897 898 static boolean isMarkedChildrenDependentProperty(Node.Property prop) { 899 return Boolean.TRUE.equals(prop.getValue(PROP_REQUIRES_CHILDREN)); 900 } 901 902 904 public static String getMethodName(MethodDescriptor desc) { 905 return getMethodName(desc.getName(), desc.getMethod().getParameterTypes()); 906 } 907 908 public static String getMethodName(String name, Class [] params) { 909 StringBuffer sb = new StringBuffer (name); 910 if ((params == null) ||(params.length == 0)) { 911 sb.append("()"); } else { 913 for (int i = 0; i < params.length; i++) { 914 if (i == 0) sb.append("("); else sb.append(", "); sb.append(Utilities.getShortClassName(params[i])); 917 } 918 sb.append(")"); } 920 921 return sb.toString(); 922 } 923 924 static void sortProperties(Node.Property[] properties) { 925 Arrays.sort(properties, new Comparator() { 926 public int compare(Object o1, Object o2) { 927 String n1 =((Node.Property)o1).getDisplayName(); 928 String n2 =((Node.Property)o2).getDisplayName(); 929 return n1.compareTo(n2); 930 } 931 }); 932 } 933 934 static void reorderProperties(Class beanClass, RADProperty[] properties) { 935 sortProperties(properties); 936 Object [] order = collectPropertiesOrder(beanClass, propertyOrder); 937 for (int i=0; i<order.length/2; i++) { 938 updatePropertiesOrder(properties, (String )order[2*i], (String )order[2*i+1]); 939 } 940 } 941 942 private static void updatePropertiesOrder(RADProperty[] properties, 943 String firstProp, String secondProp) { 944 int firstIndex = findPropertyIndex(properties, firstProp); 945 int secondIndex = findPropertyIndex(properties, secondProp); 946 if ((firstIndex != -1) && (secondIndex != -1) && (firstIndex > secondIndex)) { 947 RADProperty first = properties[firstIndex]; 949 for (int i=firstIndex; i>secondIndex; i--) { 950 properties[i] = properties[i-1]; 951 } 952 properties[secondIndex] = first; 953 } 954 } 955 956 private static int findPropertyIndex(RADProperty[] properties, String property) { 957 int index = -1; 958 for (int i=0; i<properties.length; i++) { 959 if (property.equals(properties[i].getName())) { 960 index = i; 961 break; 962 } 963 } 964 return index; 965 } 966 967 private static Object [] collectPropertiesOrder(Class beanClass, Object [][] table) { 968 Set superClasses = superClasses(beanClass); 970 971 java.util.List list = new LinkedList(); 972 for (int i=0; i < table.length; i++) { 973 Object [] order = table[i]; 974 String refClass = (String )order[0]; 975 976 if (superClasses.contains(refClass)) { 977 for (int j=1; j<order.length; j++) { 978 list.add(order[j]); 979 } 980 } 981 } 982 return list.toArray(); 983 } 984 985 987 994 public static Class loadClass(String name, FileObject formFile) 995 throws ClassNotFoundException 996 { 997 return ClassPathUtils.loadClass(name, formFile); 998 } 999 1000 public static Class loadClass(String name, FormModel form) 1001 throws ClassNotFoundException 1002 { 1003 FormDataObject dobj = FormEditor.getFormDataObject(form); 1004 return dobj != null ? loadClass(name, dobj.getFormFile()) : 1005 loadSystemClass(name); } 1007 1008 1011 public static Class loadSystemClass(String name) throws ClassNotFoundException { 1012 ClassLoader loader = (ClassLoader ) 1013 Lookup.getDefault().lookup(ClassLoader .class); 1014 if (loader == null) 1015 throw new ClassNotFoundException (); 1016 1017 return Class.forName(name, true, loader); 1018 } 1019 1020 1022 private static class OIS extends ObjectInputStream { 1023 private ClassLoader classLoader; 1024 private FormModel formModel; 1025 1026 public OIS(InputStream is, ClassLoader loader, FormModel formModel) throws IOException { 1027 super(is); 1028 this.formModel = formModel; 1029 classLoader = loader; 1030 } 1031 1032 protected Class resolveClass(ObjectStreamClass streamCls) 1033 throws IOException, ClassNotFoundException 1034 { 1035 String name = streamCls.getName(); 1036 return loadClass(name); 1037 } 1038 1039 private Class loadClass(String name) throws ClassNotFoundException { 1040 if (classLoader != null) { 1041 try { 1042 return Class.forName(name, true, classLoader); 1043 } catch (ClassNotFoundException ex) {} 1044 } 1045 return FormUtils.loadClass(name, formModel); 1046 } 1047 1048 } 1049 1050 public static List getSelectedLayoutComponents(Node[] nodes) { 1051 if ((nodes == null) || (nodes.length < 1)) 1052 return null; 1053 1054 List components = new ArrayList(); 1055 for (int i=0; i<nodes.length; i++) { 1056 RADComponentCookie radCookie = 1057 (RADComponentCookie) nodes[i].getCookie(RADComponentCookie.class); 1058 if (radCookie != null) { 1059 RADComponent metacomp = radCookie.getRADComponent(); 1060 if ((metacomp instanceof RADVisualComponent)) { 1061 RADVisualComponent visComp = (RADVisualComponent)metacomp; 1062 RADVisualContainer visCont = visComp.getParentContainer(); 1063 if ((visCont != null) && javax.swing.JScrollPane .class.isAssignableFrom(visCont.getBeanInstance().getClass())) { 1064 visComp = visCont; 1065 visCont = visCont.getParentContainer(); 1066 } 1067 1068 if (isInTopDesignComponent(visComp) && (visCont!= null) && (visCont.getLayoutSupport() == null)) { 1069 components.add(visComp); 1070 } else { 1071 return null; 1072 } 1073 } else { 1074 return null; 1075 } 1076 } 1077 } 1078 return components; 1079 } 1080 1081 public static boolean isInTopDesignComponent(RADComponent comp) { 1082 FormDesigner designer = FormEditor.getFormDesigner(comp.getFormModel()); 1083 RADVisualComponent topDesigned = designer.getTopDesignComponent(); 1084 while ((comp != null) && (comp != topDesigned)) { 1085 comp = comp.getParentComponent(); 1086 } 1087 return (comp == topDesigned); 1088 } 1089 1090 private static Set superClasses(Class beanClass) { 1091 Set superClasses = new HashSet(); 1092 Class [] infaces = beanClass.getInterfaces(); 1093 for (int i=0; i<infaces.length; i++) { 1094 superClasses.add(infaces[i].getName()); 1095 } 1096 Class superClass = beanClass; 1097 do { 1098 superClasses.add(superClass.getName()); 1099 } while ((superClass = superClass.getSuperclass()) != null); 1100 return superClasses; 1101 } 1102 1107 public static java.beans.BeanInfo getBeanInfo(Class clazz) throws java.beans.IntrospectionException { 1108 try { 1109 return Utilities.getBeanInfo(clazz); } catch (Error ex1) { try { 1112 return Introspector.getBeanInfo(clazz, java.beans.Introspector.IGNORE_IMMEDIATE_BEANINFO); 1113 } catch (Error ex2) { 1114 return Introspector.getBeanInfo(clazz, java.beans.Introspector.IGNORE_ALL_BEANINFO); 1115 } 1116 } 1117 } 1118} 1119 | Popular Tags |