1 19 20 package org.netbeans.modules.form; 21 22 import java.awt.*; 23 import javax.swing.*; 24 import java.util.*; 25 26 import org.openide.ErrorManager; 27 28 import org.netbeans.modules.form.fakepeer.FakePeerSupport; 29 import org.netbeans.modules.form.layoutsupport.*; 30 import org.netbeans.modules.form.layoutdesign.support.SwingLayoutBuilder; 32 33 45 46 public class VisualReplicator { 48 private RADVisualComponent topMetaComponent; 50 51 private Map idToClone = new HashMap(); 52 private Map cloneToId = new HashMap(); 53 54 private Map layoutBuilders = new HashMap(); 55 56 private int designRestrictions; 57 58 static final int ATTACH_FAKE_PEERS = 1; 60 static final int DISABLE_FOCUSING = 2; 61 62 private Class requiredTopClass; 64 private Class [] forbiddenClasses; 65 66 68 public VisualReplicator() { 69 } 70 71 public VisualReplicator(Class requiredClass, 72 Class [] forbiddenClasses, 73 int designRestrictions) 74 { 75 setRequiredTopVisualClass(requiredClass); 76 setForbiddenTopVisualClasses(forbiddenClasses); 77 setDesignRestrictions(designRestrictions); 78 } 80 81 84 public Object getClonedComponent(RADComponent metacomponent) { 85 return idToClone.get(metacomponent.getId()); 86 } 87 88 public Object getClonedComponent(String id) { 89 return idToClone.get(id); 90 } 91 92 public String getClonedComponentId(Object component) { 93 return (String ) cloneToId.get(component); 94 } 95 96 98 private FormModel getFormModel() { 99 return getTopMetaComponent().getFormModel(); 100 } 101 102 SwingLayoutBuilder getLayoutBuilder(String containerId) { 103 SwingLayoutBuilder builder = (SwingLayoutBuilder) layoutBuilders.get(containerId); 104 if (builder == null) { 105 RADVisualContainer metacont = (RADVisualContainer) 106 getFormModel().getMetaComponent(containerId); 107 Container cont = (Container) getClonedComponent(containerId); 108 Container contDelegate = metacont.getContainerDelegate(cont); 109 110 builder = new SwingLayoutBuilder(getFormModel().getLayoutModel(), 111 contDelegate, containerId, 112 getDesignRestrictions() != 0); 113 layoutBuilders.put(containerId, builder); 114 } 115 return builder; 116 } 117 118 121 125 public RADVisualComponent getTopMetaComponent() { 126 return topMetaComponent; 127 } 128 129 public void setTopMetaComponent(RADVisualComponent metacomponent) { 130 topMetaComponent = metacomponent; 132 idToClone.clear(); 133 cloneToId.clear(); 134 layoutBuilders.clear(); 135 } 136 137 public int getDesignRestrictions() { 138 return designRestrictions; 139 } 140 141 public void setDesignRestrictions(int restrictions) { 142 designRestrictions = restrictions; 143 } 144 145 public Class getRequiredTopVisualClass() { 146 return requiredTopClass; 147 } 148 149 public void setRequiredTopVisualClass(Class requiredClass) { 150 requiredTopClass = requiredClass; 151 } 152 153 public Class [] getForbiddenTopVisualClasses() { 154 return forbiddenClasses; 155 } 156 157 public void setForbiddenTopVisualClasses(Class [] forbiddenClasses) { 158 this.forbiddenClasses = forbiddenClasses; 159 } 160 161 164 public Object createClone() { 165 return createClone(getTopMetaComponent()); 166 } 167 168 public Object createClone(RADComponent metacomp) { 169 if (metacomp == null) 170 return null; 171 172 Object clone; 173 ArrayList relativeProperties = new ArrayList(); 174 175 try { 176 clone = cloneComponent(metacomp, relativeProperties); 178 179 if (!relativeProperties.isEmpty()) 181 copyRelativeProperties(relativeProperties); 182 } 183 catch (Exception ex) { 184 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 185 clone = null; 186 } 187 188 return clone; 189 } 190 191 public void reorderComponents(ComponentContainer metacont) { 192 if (metacont instanceof RADVisualContainer) { 193 updateContainerLayout((RADVisualContainer) metacont); 194 } 195 else if (metacont instanceof RADMenuComponent) { 196 Container cont = (Container) getClonedComponent((RADComponent)metacont); 197 if (cont == null) return; 199 200 cont.removeAll(); 201 202 RADComponent[] metacomps = ((RADMenuComponent)metacont).getSubBeans(); 203 for (int i = 0; i < metacomps.length; i++) { 204 addToMenu(cont, getClonedComponent(metacomps[i])); 205 } 206 } 207 } 208 209 public void updateContainerLayout(RADVisualContainer metacont) { 210 Container cont = (Container) getClonedComponent(metacont); 211 if (cont == null) return; Container contDelegate = metacont.getContainerDelegate(cont); 214 LayoutSupportManager laysup = metacont.getLayoutSupport(); 215 SwingLayoutBuilder layoutBuilder; 216 217 if (laysup != null) { layoutBuilder = null; 220 layoutBuilders.remove(metacont.getId()); 221 laysup.clearContainer(cont, contDelegate); 222 } 223 else { layoutBuilder = getLayoutBuilder(metacont.getId()); 225 layoutBuilder.clearContainer(); 226 } 227 228 RADVisualComponent[] metacomps = metacont.getSubComponents(); 230 Component[] comps = new Component[metacomps.length]; 231 String [] compIds = new String [metacomps.length]; 232 233 for (int i = 0; i < metacomps.length; i++) { 234 RADVisualComponent metacomp = metacomps[i]; 235 236 Component comp = (Component) getClonedComponent(metacomp); 237 if (comp == null) 238 comp = (Component) createClone(metacomp); 239 else { 240 if (comp.getParent() != null) 241 comp.getParent().remove(comp); 242 } 243 244 Boolean visible = null; 246 FormProperty visibilityProp = metacomp.getBeanProperty("visible"); if (visibilityProp != null && visibilityProp.isChanged()) { 248 Object value; 249 try { 250 value = visibilityProp.getRealValue(); 251 } catch (Exception ex) { value = null; 253 } 254 if (value instanceof Boolean ) 255 visible = (Boolean ) value; 256 } 257 if (visible == null) { 258 Component defaultComp = (Component) BeanSupport.getDefaultInstance(comp.getClass()); 259 if (defaultComp != null) 260 visible = defaultComp.isVisible() ? Boolean.TRUE : Boolean.FALSE; 261 } 262 if (visible != null) { 263 comp.setVisible(visible.booleanValue()); 264 } 265 266 FakePeerSupport.attachFakePeer(comp); 268 if (comp instanceof Container) 269 FakePeerSupport.attachFakePeerRecursively((Container)comp); 270 271 comps[i] = comp; 272 compIds[i] = metacomp.getId(); 273 } 274 275 if (laysup != null) { laysup.setLayoutToContainer(cont, contDelegate); 278 if (comps.length > 0) 279 laysup.addComponentsToContainer(cont, contDelegate, comps, 0); 280 laysup.arrangeContainer(cont, contDelegate); 281 } 282 else { contDelegate.removeAll(); 285 for (int i=0; i<comps.length; i++) { 286 FakePeerSupport.attachFakePeer(comps[i]); 287 if (comps[i] instanceof Container) 288 FakePeerSupport.attachFakePeerRecursively((Container)comps[i]); 289 } 290 291 setupContainerLayout(layoutBuilder, comps, compIds); 292 } 293 } 294 295 public void updateAddedComponents(ComponentContainer metacont) { 296 Container container = null; 297 if (metacont instanceof RADComponent) { 298 Object contClone = getClonedComponent((RADComponent)metacont); 299 if (contClone instanceof Container) { 300 if (metacont instanceof RADVisualContainer) { 301 RADVisualContainer visualMetaCont = (RADVisualContainer)metacont; 302 if (visualMetaCont.getLayoutSupport() == null) { 303 updateContainerLayout(visualMetaCont); 305 } 307 container = visualMetaCont.getContainerDelegate((Container)contClone); 308 } 309 else container = (Container)contClone; 310 } 311 } 312 313 RADComponent[] subComps = metacont.getSubBeans(); 314 for (int i=0; i < subComps.length; i++) { 315 Object compClone = getClonedComponent(subComps[i]); 316 if (compClone == null) 317 addComponent(subComps[i]); 318 else if (compClone instanceof Component) { 319 Container cloneCont = ((Component)compClone).getParent(); 320 if (cloneCont != container && cloneToId.get(cloneCont) != null) 321 return; } } 324 } 325 326 public void addComponent(RADComponent metacomp) { 329 if (metacomp == null) 330 return; 331 if (getClonedComponent(metacomp) != null) 332 return; 333 334 if (metacomp instanceof RADVisualComponent) { 335 Object clone = createClone(metacomp); 336 if (!(clone instanceof Component)) 337 return; 338 339 RADVisualContainer metacont = (RADVisualContainer) 340 metacomp.getParentComponent(); 341 LayoutSupportManager laysup = metacont.getLayoutSupport(); 342 343 if (laysup != null) { Container cont = (Container) getClonedComponent(metacont); 345 if (cont == null) return; 346 Container contDelegate = metacont.getContainerDelegate(cont); 347 laysup.addComponentsToContainer( 348 cont, 349 contDelegate, 350 new Component[] { (Component) clone }, 351 ((RADVisualComponent)metacomp).getComponentIndex()); 352 laysup.arrangeContainer(cont, contDelegate); 353 } 354 } 360 else if (metacomp instanceof RADMenuItemComponent) { 361 Object clone = createClone(metacomp); 362 363 RADComponent menuCont = metacomp.getParentComponent(); 364 if (menuCont == null) 365 return; 367 Object cont = getClonedComponent(menuCont); 368 if (menuCont instanceof RADVisualContainer) 369 setContainerMenu((Container)cont, clone); 370 else 371 addToMenu(cont, clone); 372 } 373 } 374 375 public void removeComponent(RADComponent metacomp, 376 ComponentContainer metacont) 377 { 378 if (metacomp == null) 379 return; 380 381 Object clone = getClonedComponent(metacomp); 382 if (clone == null) 383 return; 384 385 if (clone instanceof JMenuBar) { Container menuParent = ((Component)clone).getParent(); 388 Container cont = menuParent; 389 while (cont != null && !(cont instanceof JRootPane)) 390 cont = cont.getParent(); 391 392 if (cont != null) 393 ((JRootPane)cont).setJMenuBar(null); 394 else if (menuParent != null) 395 menuParent.remove((Component)clone); 396 else return; 397 } 398 else if (clone instanceof Component) { Component comp = (Component) clone; 400 RADVisualContainer parentCont = 402 metacont instanceof RADVisualContainer ? 403 (RADVisualContainer) metacont : null; 404 Container cont = parentCont != null ? 405 (Container) getClonedComponent(parentCont) : null; 406 407 if (cont == null) { 408 if (comp.getParent() != null) 411 comp.getParent().remove(comp); 412 } 413 else { Container contDelegate = parentCont.getContainerDelegate(cont); 415 LayoutSupportManager laysup = parentCont.getLayoutSupport(); 416 if (laysup != null) { if (!laysup.removeComponentFromContainer( 418 cont, contDelegate, comp)) 419 { laysup.clearContainer(cont, contDelegate); 422 423 RADVisualComponent[] metacomps = parentCont.getSubComponents(); 424 if (metacomps.length > 0) { 425 Component[] comps = new Component[metacomps.length]; 427 for (int i=0; i < metacomps.length; i++) { 428 comp = (Component) getClonedComponent(metacomps[i]); 429 FakePeerSupport.attachFakePeer(comp); 432 if (comp instanceof Container) 433 FakePeerSupport.attachFakePeerRecursively( 434 (Container)comp); 435 comps[i] = comp; 436 } 437 laysup.addComponentsToContainer(cont, contDelegate, comps, 0); 438 } 439 } 440 } 441 else { contDelegate.removeAll(); 444 RADVisualComponent[] metacomps = parentCont.getSubComponents(); 445 for (int i=0; i<metacomps.length; i++) { 446 Component component = (Component)getClonedComponent(metacomps[i]); 447 FakePeerSupport.attachFakePeer(component); 448 if (component instanceof Container) 449 FakePeerSupport.attachFakePeerRecursively((Container)component); 450 } 451 452 getLayoutBuilder(parentCont.getId()).removeComponentsFromContainer( 453 new Component[] { comp }, 454 new String [] { metacomp.getId() } ); 455 } 456 } 457 } 458 else if (clone instanceof MenuComponent) { MenuComponent menuComp = (MenuComponent) clone; 460 MenuContainer menuCont = menuComp.getParent(); 461 if (menuCont != null) 462 menuCont.remove(menuComp); 463 else return; 464 } 465 466 removeMapping(metacomp); 467 } 468 469 public void updateComponentProperty(RADProperty property) { 470 if (property == null) 471 return; 472 473 RADComponent metacomp = property.getRADComponent(); 474 475 Object targetComp = getClonedComponent(metacomp); 477 if (targetComp == null) 478 return; 479 480 if (targetComp instanceof java.awt.Scrollbar ) { 484 removeComponent(metacomp, null); 486 addComponent(metacomp); 487 return; 488 } 489 490 if (targetComp instanceof JComponent 493 && (getDesignRestrictions() & ATTACH_FAKE_PEERS) != 0 494 && (("doubleBuffered".equals(property.getName()) && hasAwtParent(metacomp)) 496 || "debugGraphicsOptions".equals(property.getName()))) return; 498 499 if ("text".equals(property.getName()) && (targetComp instanceof AbstractButton 502 || targetComp instanceof JLabel) 503 && JavaCodeGenerator.isUsingMnemonics(property.getRADComponent())) 504 { 505 try { 506 String str = (String ) property.getRealValue(); 507 if (targetComp instanceof JLabel) 508 org.openide.awt.Mnemonics.setLocalizedText( 509 (JLabel)targetComp, str); 510 else 511 org.openide.awt.Mnemonics.setLocalizedText( 512 (AbstractButton)targetComp, str); 513 return; 514 } 515 catch (Exception ex) {} } 517 519 java.lang.reflect.Method writeMethod = 520 property.getPropertyDescriptor().getWriteMethod(); 521 if (writeMethod == null) 522 return; 523 524 if (!writeMethod.getDeclaringClass().isAssignableFrom( 525 targetComp.getClass())) 526 { try { 528 writeMethod = targetComp.getClass().getMethod( 529 writeMethod.getName(), 530 writeMethod.getParameterTypes()); 531 } 532 catch (Exception ex) { return; 534 } 535 } 536 537 try { 538 Object value = property.getValue(); 539 540 if (value instanceof RADComponent.ComponentReference) { 541 value = ((RADComponent.ComponentReference)value).getComponent(); 542 } 543 544 if (value instanceof RADConnectionPropertyEditor.RADConnectionDesignValue 545 && (((RADConnectionPropertyEditor.RADConnectionDesignValue)value).type 546 == RADConnectionPropertyEditor.RADConnectionDesignValue.TYPE_BEAN)) { 547 value = ((RADConnectionPropertyEditor.RADConnectionDesignValue)value).getRADComponent(); 548 } 549 550 if (value instanceof RADComponent) { 551 Object propertyComp = 553 getClonedComponent((RADComponent)value); 554 if (propertyComp == null) propertyComp = createClone((RADComponent)value); 556 557 value = propertyComp; 558 } 559 else { 560 value = property.getRealValue(); 561 562 if (value == FormDesignValue.IGNORED_VALUE) 563 return; 565 value = FormUtils.cloneObject(value, property.getPropertyContext().getFormModel()); 566 } 567 568 writeMethod.invoke(targetComp, new Object [] { value }); 569 570 if (targetComp instanceof Component) 571 ((Component)targetComp).invalidate(); 572 } 573 catch (Exception ex) { 574 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 575 } 576 } 577 578 581 private Object cloneComponent(RADComponent metacomp, 583 java.util.List relativeProperties) 584 throws Exception 585 { 586 Object clone; Object compClone = null; 589 if (needsConversion(metacomp)) { clone = cloneComponentWithConversion( 591 metacomp, 592 metacomp == getTopMetaComponent() ? requiredTopClass : null, 593 relativeProperties); 594 595 if (clone instanceof RootPaneContainer 596 && !(metacomp.getBeanInstance() instanceof RootPaneContainer)) 597 { compClone = ((RootPaneContainer)clone).getContentPane().getComponent(0); 599 } 600 } 601 else if ("java.awt.ScrollPane".equals(metacomp.getBeanClass().getName()) && ((getDesignRestrictions() & ATTACH_FAKE_PEERS) != 0) 603 && (System.getProperty("java.version").startsWith("1.4"))) { clone = new java.awt.ScrollPane () { 607 public void addNotify() { 608 if (getComponentCount()>0) { 609 Component comp = getComponent(0); 610 remove(0); 611 super.addNotify(); 612 FakePeerSupport.attachFakePeer(comp); 613 add(comp); 614 } 615 } 616 }; 617 } 618 else { clone = metacomp.cloneBeanInstance(relativeProperties); 620 } 621 622 if (clone == null) 623 return null; 624 625 if (compClone == null) 626 compClone = clone; 627 628 idToClone.put(metacomp.getId(), compClone); 629 cloneToId.put(compClone, metacomp.getId()); 630 631 if (compClone instanceof java.beans.DesignMode ) 632 ((java.beans.DesignMode )compClone).setDesignTime( 633 getDesignRestrictions() != 0); 634 635 if (metacomp instanceof RADVisualContainer) { 636 RADVisualContainer metacont = (RADVisualContainer) metacomp; 637 final Container cont = (Container) compClone; 638 final Container contDelegate = metacont.getContainerDelegate(cont); 639 640 if (metacont.getContainerMenu() != null) { 642 Object menu = cloneComponent(metacont.getContainerMenu(), 643 relativeProperties); 644 setContainerMenu(cont, menu); 645 } 646 647 RADVisualComponent[] metacomps = metacont.getSubComponents(); 649 final Component[] comps = new Component[metacomps.length]; 650 String [] compIds = new String [metacomps.length]; 651 for (int i=0; i < metacomps.length; i++) { 652 comps[i] = (Component) cloneComponent(metacomps[i], 653 relativeProperties); 654 compIds[i] = metacomps[i].getId(); 655 } 656 657 final LayoutSupportManager laysup = metacont.getLayoutSupport(); 659 if (laysup != null) { laysup.setLayoutToContainer(cont, contDelegate); 661 if (comps.length > 0) { if (!(cont instanceof JToolBar)) 663 laysup.addComponentsToContainer(cont, contDelegate, comps, 0); 664 else { SwingUtilities.invokeLater(new Runnable () { 666 public void run() { 667 laysup.addComponentsToContainer(cont, contDelegate, 668 comps, 0); 669 } 670 }); 671 } 672 } 673 laysup.arrangeContainer(cont, contDelegate); 674 } 675 else { setupContainerLayout(getLayoutBuilder(metacont.getId()), comps, compIds); 677 } 678 } 679 else if (metacomp instanceof RADMenuComponent) { 680 RADComponent[] metacomps = ((RADMenuComponent)metacomp).getSubBeans(); 681 for (int i = 0; i < metacomps.length; i++) { 682 Object menuItem = cloneComponent( 683 (RADMenuItemComponent)metacomps[i], relativeProperties); 684 addToMenu(compClone, menuItem); 685 } 686 } 687 688 if (clone instanceof Component) { 689 int restrictions = getDesignRestrictions(); 690 if ((restrictions & ATTACH_FAKE_PEERS) != 0) { 691 FakePeerSupport.attachFakePeer((Component)clone); 692 if (clone instanceof Container) 693 FakePeerSupport.attachFakePeerRecursively((Container)clone); 694 695 if (clone instanceof JComponent) { 696 if (hasAwtParent(metacomp)) 698 setDoubleBufferedRecursively((JComponent)clone, false); 699 ((JComponent)clone).setDebugGraphicsOptions( 701 DebugGraphics.NONE_OPTION); 702 } 703 } 704 705 if ((restrictions & DISABLE_FOCUSING) != 0) { 706 disableFocusing((Component)clone); 707 708 if (clone instanceof JInternalFrame) 710 ((JInternalFrame)clone).getGlassPane().setVisible(false); 711 } 712 } 713 714 if ((clone instanceof AbstractButton || clone instanceof JLabel) 716 && JavaCodeGenerator.isUsingMnemonics(metacomp)) 717 { 718 FormProperty prop = metacomp.getBeanProperty("text"); if (prop != null && prop.isChanged()) { 720 try { 721 String str = (String ) prop.getRealValue(); 722 if (clone instanceof JLabel) 723 org.openide.awt.Mnemonics.setLocalizedText( 724 (JLabel)clone, str); 725 else 726 org.openide.awt.Mnemonics.setLocalizedText( 727 (AbstractButton)clone, str); 728 } 729 catch (Exception ex) {} } 731 } 732 734 return clone; 735 } 736 737 private void setupContainerLayout(SwingLayoutBuilder layoutBuilder, Component[] comps, String [] compIds) { 738 Throwable th = null; 739 try { 740 layoutBuilder.setupContainerLayout(comps, compIds); 741 } catch (Exception ex) { 742 th = ex; 743 } catch (Error err) { 744 th = err; 745 } 746 if (th != null) { 747 ErrorManager.getDefault().notify(th); 748 getFormModel().forceUndoOfCompoundEdit(); 749 } 750 } 751 752 private boolean needsConversion(RADComponent metacomp) { 753 Class beanClass = metacomp.getBeanClass(); 754 755 if (forbiddenClasses != null) { 756 for (int i=0; i < forbiddenClasses.length; i++) { 757 if (forbiddenClasses[i].isAssignableFrom(beanClass)) 758 return true; 759 } 760 } 761 762 return metacomp == getTopMetaComponent() 763 && requiredTopClass != null 764 && !requiredTopClass.isAssignableFrom(beanClass); 765 } 766 767 private static void disableFocusing(Component comp) { 768 comp.setFocusable(false); 769 if (comp instanceof Container) { 770 Container cont = (Container) comp; 771 for (int i=0, n=cont.getComponentCount(); i < n; i++) 772 disableFocusing(cont.getComponent(i)); 773 } 774 } 775 776 private static void setContainerMenu(Container cont, 777 Object menu) { 778 if (cont instanceof RootPaneContainer) { 779 if (menu instanceof JMenuBar) 780 ((RootPaneContainer)cont).getRootPane() 781 .setJMenuBar((JMenuBar)menu); 782 } 783 else if (cont instanceof JRootPane) { 784 if (menu instanceof JMenuBar) 785 ((JRootPane)cont).setJMenuBar((JMenuBar)menu); 786 } 787 else if (cont instanceof Frame) { 788 if (menu instanceof MenuBar) 789 ((Frame)cont).setMenuBar((MenuBar)menu); 790 } 791 } 792 793 private static void addToMenu(Object menu, Object menuItem) { 794 if (menu instanceof JMenuBar) { 795 ((JMenuBar)menu).add((JMenu)menuItem); 796 } 797 else if (menu instanceof JMenu) { 798 if (menuItem instanceof JMenuItem) 799 ((JMenu)menu).add((JMenuItem)menuItem); 800 else 801 ((JMenu)menu).addSeparator(); 802 } 803 else if (menu instanceof MenuBar) { 804 ((MenuBar)menu).add((Menu)menuItem); 805 } 806 else if (menu instanceof Menu) { 807 if (menuItem instanceof MenuItem) 808 ((Menu)menu).add((MenuItem)menuItem); 809 else 810 ((Menu)menu).addSeparator(); 811 } 812 } 813 814 private static Component cloneComponentWithConversion( 817 RADComponent metacomp, 818 Class requiredClass, 819 java.util.List relativeProperties) 820 throws Exception { 821 Class beanClass = metacomp.getBeanClass(); 822 823 if (requiredClass == null) { 824 if (RootPaneContainer.class.isAssignableFrom(beanClass) 826 || Frame.class.isAssignableFrom(beanClass)) 827 requiredClass = JRootPane.class; 828 else if (Window.class.isAssignableFrom(beanClass) 829 || Panel.class.isAssignableFrom(beanClass)) 830 requiredClass = Panel.class; 831 else if (Component.class.isAssignableFrom(beanClass)) 832 requiredClass = JPanel.class; 833 else if (MenuComponent.class.isAssignableFrom(beanClass)) 834 requiredClass = convertMenuClassToSwing(beanClass); 835 else 836 requiredClass = Object .class; 837 } 838 839 Component component = 840 (Component) CreationFactory.createDefaultInstance(requiredClass); 841 842 if (component instanceof RootPaneContainer) { 843 if (!RootPaneContainer.class.isAssignableFrom(beanClass) 844 && !Window.class.isAssignableFrom(beanClass) && !java.applet.Applet .class.isAssignableFrom(beanClass)) 846 { Component clone = (Component) 849 metacomp.cloneBeanInstance(relativeProperties); 850 ((RootPaneContainer)component).getContentPane().add(clone); 851 return component; 852 } 853 } 854 else if (component instanceof JRootPane) { 855 Class contentClass = 857 RootPaneContainer.class.isAssignableFrom(beanClass) ? 858 JPanel.class : Panel.class; 859 Container contentCont = 860 (Container) CreationFactory.createDefaultInstance(contentClass); 861 862 ((JRootPane)component).setContentPane(contentCont); 864 } 865 else if (MenuItem.class.isAssignableFrom(beanClass) 866 && JMenuItem.class.isAssignableFrom(requiredClass)) { 867 ((JMenuItem)component).setText( 868 ((MenuItem)metacomp.getBeanInstance()).getLabel()); 869 870 ((JMenuItem)component).setFont( 871 ((MenuItem)metacomp.getBeanInstance()).getFont()); 872 } 873 874 FormUtils.copyPropertiesToBean(metacomp.getKnownBeanProperties(), 876 component, 877 relativeProperties); 878 return component; 879 } 880 881 private static Class convertMenuClassToSwing(Class menuClass) { 883 if (MenuBar.class.isAssignableFrom(menuClass)) 884 return JMenuBar.class; 885 if (PopupMenu.class.isAssignableFrom(menuClass)) 886 return JPopupMenu.class; 887 if (Menu.class.isAssignableFrom(menuClass)) 888 return JMenu.class; 889 if (CheckboxMenuItem.class.isAssignableFrom(menuClass)) 890 return JCheckBoxMenuItem.class; 891 if (MenuItem.class.isAssignableFrom(menuClass)) 892 return JMenuItem.class; 893 894 return menuClass; 895 } 896 897 private static boolean hasAwtParent(RADComponent metacomp) { 898 RADComponent parent = metacomp.getParentComponent(); 899 while (parent != null) { 900 Class beanClass = parent.getBeanClass(); 901 if (Component.class.isAssignableFrom(beanClass) 902 && !JComponent.class.isAssignableFrom(beanClass) 903 && !RootPaneContainer.class.isAssignableFrom(beanClass)) 904 { return true; 906 } 907 908 parent = parent.getParentComponent(); 909 } 910 return false; 911 } 912 913 private static void setDoubleBufferedRecursively(JComponent component, 914 boolean value) 915 { 916 component.setDoubleBuffered(value); 917 Component[] subcomps = component.getComponents(); 918 for (int i=0; i < subcomps.length; i++) 919 if (subcomps[i] instanceof JComponent) 920 setDoubleBufferedRecursively((JComponent)subcomps[i], value); 921 } 922 923 925 private void copyRelativeProperties(java.util.List relativeProperties) { 927 for (int i=0; i < relativeProperties.size(); i++) { 928 RADProperty property = (RADProperty) relativeProperties.get(i); 929 try { 930 Object value = property.getValue(); 931 if (value instanceof RADComponent.ComponentReference) 932 value = 933 ((RADComponent.ComponentReference)value).getComponent(); 934 935 if (value instanceof RADConnectionPropertyEditor.RADConnectionDesignValue) { 936 RADConnectionPropertyEditor.RADConnectionDesignValue connection = 937 (RADConnectionPropertyEditor.RADConnectionDesignValue)value; 938 assert connection.type == RADConnectionPropertyEditor.RADConnectionDesignValue.TYPE_BEAN; 939 value = connection.getRADComponent(); 940 } 941 942 if (value instanceof RADComponent) { 943 Object propertyComp = 945 getClonedComponent((RADComponent)value); 946 if (propertyComp == null) propertyComp = cloneComponent((RADComponent)value, 948 relativeProperties); 949 950 Object targetComp = 952 getClonedComponent(property.getRADComponent()); 953 954 java.lang.reflect.Method writeMethod = 955 property.getPropertyDescriptor().getWriteMethod(); 956 if (writeMethod != null) { 957 writeMethod.invoke(targetComp, 958 new Object [] { propertyComp }); 959 } 960 else if (propertyComp instanceof ButtonGroup 961 && targetComp instanceof AbstractButton) 962 { ((ButtonGroup)propertyComp).remove((AbstractButton)targetComp); 964 ((ButtonGroup)propertyComp).add((AbstractButton)targetComp); 965 } 966 } 967 } 968 catch (Exception ex) {} } 970 } 971 972 private void removeMapping(RADComponent metacomp) { 973 Object comp = idToClone.remove(metacomp.getId()); 974 if (comp != null) 975 cloneToId.remove(comp); 976 977 if (metacomp instanceof ComponentContainer) { 978 layoutBuilders.remove(metacomp.getId()); 979 RADComponent[] subcomps = ((ComponentContainer)metacomp).getSubBeans(); 980 for (int i=0; i < subcomps.length; i++) 981 removeMapping(subcomps[i]); 982 } 983 } 984 } 985 | Popular Tags |