1 19 20 package org.openide.loaders; 21 22 23 import java.awt.Image ; 24 import java.beans.*; 25 import java.beans.beancontext.*; 26 import java.io.IOException ; 27 import java.lang.reflect.*; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 import javax.swing.*; 31 import org.openide.cookies.InstanceCookie; 32 import org.openide.filesystems.*; 33 import org.openide.nodes.*; 34 import org.openide.util.*; 35 36 40 final class InstanceNode extends DataNode implements Runnable { 41 42 43 private static final String INSTANCE_ICON_BASE = 44 "org/openide/loaders/instanceObject.gif"; 46 47 private static final String XML_EXT = "settings"; 49 50 private PropL propertyChangeListener = null; 51 private PropertyChangeListener dobjListener; 52 private boolean isSheetCreated = false; 53 56 private boolean noBeanInfo = false; 57 58 59 public InstanceNode (InstanceDataObject obj) { 60 this (obj, Boolean.FALSE.equals (obj.getPrimaryFile ().getAttribute ("beaninfo"))); } 62 63 66 private InstanceNode (InstanceDataObject obj, boolean noBeanInfo) { 67 super (obj, getChildren(obj, noBeanInfo)); 68 69 initIconBase(); 70 71 this.noBeanInfo = noBeanInfo; 72 73 if (!noBeanInfo && !getDataObject().getPrimaryFile().hasExt(XML_EXT)) { 74 SwingUtilities.invokeLater (this); 75 } 76 77 dobjListener = new PropertyChangeListener() { 79 public void propertyChange(PropertyChangeEvent evt) { 80 if (evt.getPropertyName().equals(DataObject.PROP_COOKIE)) { 81 if (propertyChangeListener != null) { 82 propertyChangeListener.destroy(); 83 propertyChangeListener = null; 84 } 85 if (InstanceNode.this.noBeanInfo || ic() == null) { 86 initIconBase(); 87 } else { 88 fireIconChange(); 89 } 90 fireNameChange(null, null); 91 fireDisplayNameChange(null, null); 92 fireShortDescriptionChange(null, null); 93 if (isSheetCreated) setSheet(createSheet()); 94 } 95 } 96 }; 97 obj.addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(dobjListener, obj)); 98 } 99 100 101 private void initIconBase() { 102 InstanceCookie.Of ic = ic(); 103 String iconBase = INSTANCE_ICON_BASE; 104 if (ic == null) { iconBase = "org/openide/loaders/instanceBroken.gif"; } 107 setIconBaseWithExtension(iconBase); 108 } 109 110 private static Children getChildren(DataObject dobj, boolean noBeanInfo) { 111 if (noBeanInfo) { 112 return Children.LEAF; 113 } 114 InstanceCookie inst = (InstanceCookie)dobj.getCookie(InstanceCookie.class); 115 if (inst == null) return Children.LEAF; 116 try { 117 Class clazz = inst.instanceClass(); 118 if (BeanContext.class.isAssignableFrom(clazz) || 119 BeanContextProxy.class.isAssignableFrom(clazz)) { 120 return new InstanceChildren ((InstanceDataObject) dobj); 121 } else { 122 return Children.LEAF; 123 } 124 } catch (Exception ex) { 125 return Children.LEAF; 126 } 127 } 128 129 132 private InstanceDataObject i () { 133 return (InstanceDataObject)getDataObject (); 134 } 135 136 private InstanceCookie.Of ic () { 137 return (InstanceCookie.Of) getDataObject().getCookie(InstanceCookie.Of.class); 138 } 139 140 144 public Image getIcon (int type) { 145 if (noBeanInfo) return super.getIcon(type); 146 Image img = null; 147 try { 148 DataObject dobj = getDataObject(); 149 img = dobj.getPrimaryFile().getFileSystem().getStatus(). 150 annotateIcon (img, type, dobj.files ()); 151 } catch (FileStateInvalidException e) { 152 } 154 155 if (img == null) img = initIcon(type); 156 if (img == null) img = super.getIcon(type); 157 return img; 158 } 159 160 166 public Image getOpenedIcon (int type) { 167 return getIcon (type); 168 } 169 170 171 private void initPList () { 172 try { 173 InstanceCookie ic = ic(); 174 if (ic == null) return; 175 BeanInfo info = Utilities.getBeanInfo(ic.instanceClass()); 176 java.beans.EventSetDescriptor [] descs = info.getEventSetDescriptors(); 177 Method setter = null; 178 for (int i = 0; descs != null && i < descs.length; i++) { 179 setter = descs[i].getAddListenerMethod(); 180 if (setter != null && setter.getName().equals("addPropertyChangeListener")) { Object bean = ic.instanceCreate(); 182 propertyChangeListener = new PropL(); 183 setter.invoke(bean, new Object [] {org.openide.util.WeakListeners.propertyChange(propertyChangeListener, bean)}); 184 } 185 } 186 } catch (Exception ex) { 187 } catch (LinkageError ex) { 188 } 191 } 192 193 private Image initIcon (int type) { 194 Image beanInfoIcon = null; 195 try { 196 InstanceCookie ic = ic(); 197 if (ic == null) return null; 198 Class clazz = ic.instanceClass(); 199 204 String className = clazz.getName (); 205 BeanInfo bi; 206 if ( 207 className.equals ("javax.swing.JSeparator") || className.equals ("javax.swing.JToolBar$Separator") ) { 210 Class clazzTmp = Class.forName ("javax.swing.JSeparator"); bi = Utilities.getBeanInfo (clazzTmp); 212 } else { 213 bi = Utilities.getBeanInfo (clazz); 214 } 215 216 if (bi != null) { 217 beanInfoIcon = bi.getIcon (type); 218 if (beanInfoIcon != null) { 219 beanInfoIcon = toBufferedImage(beanInfoIcon, true); 220 } 221 } 222 if (beanInfoIcon == null && Action.class.isAssignableFrom(clazz)) { 224 Action action = (Action)ic.instanceCreate(); 225 Icon icon = (Icon)action.getValue(Action.SMALL_ICON); 226 if (icon != null) { 227 beanInfoIcon = Utilities.icon2Image(icon); 228 } else { 229 beanInfoIcon = Utilities.loadImage("org/openide/loaders/empty.gif", true); } 231 } 232 } catch (Exception e) { 233 Logger.getLogger(InstanceNode.class.getName()).log(Level.WARNING, null, e); 235 } catch (LinkageError e) { 236 Logger.getLogger(InstanceNode.class.getName()).log(Level.WARNING, null, e); 239 } 240 241 return beanInfoIcon; 242 } 243 244 246 public void run() { 247 try { 248 InstanceCookie ic = ic(); 249 if (ic == null) return; 250 Class clazz = ic.instanceClass(); 251 252 String className = clazz.getName (); 253 if (className.equals ("javax.swing.JSeparator") || className.equals ("javax.swing.JToolBar$Separator")) { 256 setDisplayName (NbBundle.getMessage (InstanceDataObject.class, 257 "LBL_separator_instance")); return; 259 } 260 if (Action.class.isAssignableFrom(clazz)) { 262 Action action = (Action)ic.instanceCreate(); 263 String name = (String )action.getValue(Action.NAME); 265 266 if (name == null) { 269 DataObject.LOG.warning( 270 "Please attach following information to the issue " + "<http://www.netbeans.org/issues/show_bug.cgi?id=31227>: " + "action " + className + " does not implement SystemAction.getName() or Action.getValue(NAME) properly. It returns null!"); setDisplayName(className); 274 return; 275 } 276 277 int amper = name.indexOf ((char) '&'); 278 if (amper != -1) 279 name = name.substring (0, amper) + name.substring (amper + 1); 280 if (name.endsWith ("...")) name = name.substring (0, name.length () - 3); 282 name = name.trim (); 283 setDisplayName (name); 284 return; 285 } 286 } catch (Exception e) { 287 Logger.getLogger(InstanceNode.class.getName()).log(Level.WARNING, null, e); 288 setDisplayName(getDataObject().getName()); 289 return; 290 } 291 } 292 293 295 private String getNameForBean() { 296 try { 297 InstanceCookie ic = ic(); 298 if (ic == null) { 299 return NbBundle.getMessage(InstanceNode.class, 301 "LBL_BrokenSettings"); } 303 Class clazz = ic.instanceClass(); 304 Method nameGetter; 305 Class [] param = new Class [0]; 306 try { 307 nameGetter = clazz.getMethod ("getName", param); if (nameGetter.getReturnType () != String .class) throw new NoSuchMethodException (); 309 } catch (NoSuchMethodException e) { 310 try { 311 nameGetter = clazz.getMethod ("getDisplayName", param); if (nameGetter.getReturnType () != String .class) throw new NoSuchMethodException (); 313 } catch (NoSuchMethodException ee) { 314 return null; 315 } 316 } 317 Object bean = ic.instanceCreate(); 318 return (String ) nameGetter.invoke (bean); 319 } catch (Exception ex) { 320 return null; 321 } 322 } 323 324 325 private Method getDeclaredSetter() { 326 Method nameSetter = null; 327 try { 328 InstanceCookie ic = ic(); 329 if (ic == null) return null; 330 Class clazz = ic.instanceClass(); 331 Class [] param = new Class [] {String .class}; 332 try { 334 nameSetter = clazz.getMethod ("setName", param); } catch (NoSuchMethodException e) { 336 nameSetter = clazz.getMethod ("setDisplayName", param); } 338 } catch (Exception ex) { 339 } 340 return nameSetter; 341 } 342 343 public void setName(String name) { 344 if (!getDataObject().getPrimaryFile().hasExt(XML_EXT)) { 345 super.setName(name); 346 return ; 347 } 348 String old = getNameImpl(); 349 if (old != null && old.equals(name)) return; 350 InstanceCookie ic = ic(); 351 if (ic == null) { 352 super.setName(name); 353 return; 354 } 355 356 Method nameSetter = getDeclaredSetter(); 357 if (nameSetter != null) { 358 try { 359 Object bean = ic.instanceCreate(); 360 nameSetter.invoke(bean, new Object [] {name}); 361 i().scheduleSave(); 362 } catch (Exception ex) { 363 ex.printStackTrace(); 364 } 365 } 366 super.setName(name); 367 } 368 369 373 public String getDisplayName () { 374 String name = (String ) getDataObject().getPrimaryFile(). 375 getAttribute(InstanceDataObject.EA_NAME); 376 if (name == null) { 377 try { 378 String def = "\b"; FileSystem.Status fsStatus = getDataObject().getPrimaryFile(). 380 getFileSystem().getStatus(); 381 name = fsStatus.annotateName(def, getDataObject().files()); 382 if (name.indexOf(def) < 0) { 383 return name; 384 } else { 385 name = getNameForBean(); 386 if (name != null) { 387 name = fsStatus.annotateName (name, getDataObject().files()); 388 } else { 389 name = super.getDisplayName(); 390 } 391 } 392 } catch (FileStateInvalidException e) { 393 } 395 } 396 return name; 397 } 398 399 400 private String getNameImpl() { 401 String name; 402 name = getNameForBean(); 403 if (name == null) name = getName(); 404 405 return name; 406 } 407 408 protected Sheet createSheet () { 409 Sheet orig; 410 411 if (getDataObject ().getPrimaryFile ().hasExt ("ser") || getDataObject ().getPrimaryFile ().hasExt (XML_EXT)) { 413 orig = new Sheet(); 414 changeSheet (orig); 415 } else { 416 orig = super.createSheet (); 418 Sheet.Set props = orig.get (Sheet.PROPERTIES); 419 final InstanceCookie ic = ic(); 420 if (ic == null) { 421 props.put (new PropertySupport.ReadOnly<String > ( 422 "className", String .class, NbBundle.getMessage (InstanceDataObject.class, "PROP_instance_class"), NbBundle.getMessage (InstanceDataObject.class, "HINT_instance_class") ) { 426 public String getValue () { 427 return ic.instanceName (); 428 } 429 }); 430 } 431 } 432 433 isSheetCreated = true; 434 return orig; 435 } 436 437 438 private void changeSheet (Sheet orig) { 439 Sheet.Set props = orig.get (Sheet.PROPERTIES); 440 441 try { 442 InstanceCookie ic = ic(); 443 if (ic == null) return; 444 BeanInfo beanInfo = Utilities.getBeanInfo (ic.instanceClass ()); 446 BeanNode.Descriptor descr = BeanNode.computeProperties (ic.instanceCreate (), beanInfo); 447 initPList(); 448 449 props = Sheet.createPropertiesSet(); 450 if (descr.property != null) { 451 convertProps (props, descr.property, i ()); 452 } 453 orig.put (props); 454 455 if (descr.expert != null && descr.expert.length != 0) { 456 Sheet.Set p = Sheet.createExpertSet(); 457 convertProps (p, descr.expert, i ()); 458 orig.put (p); 459 } 460 } catch (ClassNotFoundException ex) { 461 Logger.getLogger(InstanceNode.class.getName()).log(Level.WARNING, null, ex); 462 } catch (IOException ex) { 463 Logger.getLogger(InstanceNode.class.getName()).log(Level.WARNING, null, ex); 464 } catch (IntrospectionException ex) { 465 Logger.getLogger(InstanceNode.class.getName()).log(Level.WARNING, null, ex); 466 } catch (LinkageError ex) { 467 Logger.getLogger(InstanceNode.class.getName()).log(Level.WARNING, null, ex); 469 } 470 } 471 472 473 478 private static final void convertProps ( 479 Sheet.Set set, Node.Property[] arr, InstanceDataObject ido 480 ) { 481 for (int i = 0; i < arr.length; i++) { 482 if (arr[i] instanceof Node.IndexedProperty) { 483 set.put (new I ((Node.IndexedProperty)arr[i], ido)); 484 } else { 485 set.put (new P (arr[i], ido)); 486 } 487 } 488 } 489 490 493 private static final java.awt.Image toBufferedImage(Image img, boolean load) { 494 if (load) { 496 new javax.swing.ImageIcon (img); 497 } 498 499 java.awt.image.BufferedImage rep = createBufferedImage(); 500 java.awt.Graphics g = rep.createGraphics(); 501 g.drawImage(img, 0, 0, null); 502 g.dispose(); 503 img.flush(); 504 return rep; 505 } 506 507 508 private static final java.awt.image.BufferedImage createBufferedImage() { 509 java.awt.image.ColorModel model = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(). 510 getDefaultScreenDevice().getDefaultConfiguration().getColorModel(java.awt.Transparency.BITMASK); 511 java.awt.image.BufferedImage buffImage = new java.awt.image.BufferedImage (model, 512 model.createCompatibleWritableRaster(16, 16), model.isAlphaPremultiplied(), null); 513 return buffImage; 514 } 515 516 519 public boolean canRename() { 520 if (!getDataObject().getPrimaryFile().hasExt(XML_EXT)) return super.canRename(); 521 return getDeclaredSetter() != null; 522 } 523 524 527 public boolean canDestroy() { 528 if (!getDataObject().getPrimaryFile().hasExt(XML_EXT)) return super.canDestroy(); 529 try { 530 InstanceCookie ic = ic(); 531 if (ic == null) return true; 532 Class clazz = ic.instanceClass(); 533 return (!SharedClassObject.class.isAssignableFrom(clazz)); 534 } catch (Exception ex) { 535 return false; 536 } 537 } 538 539 public boolean canCut() { 540 if (!getDataObject().getPrimaryFile().hasExt(XML_EXT)) return super.canCut(); 541 try { 542 InstanceCookie ic = ic(); 543 if (ic == null) return false; 544 Class clazz = ic.instanceClass(); 545 return (!SharedClassObject.class.isAssignableFrom(clazz)); 546 } catch (Exception ex) { 547 return false; 548 } 549 } 550 551 public boolean canCopy() { 552 if (!getDataObject().getPrimaryFile().hasExt(XML_EXT)) return super.canCopy(); 553 try { 554 InstanceCookie ic = ic(); 555 if (ic == null) return false; 556 Class clazz = ic.instanceClass(); 557 return (!SharedClassObject.class.isAssignableFrom(clazz)); 560 } catch (Exception ex) { 561 return false; 562 } 563 } 564 565 566 public String getShortDescription() { 567 if (noBeanInfo) return super.getShortDescription(); 568 569 try { 570 InstanceCookie ic = ic(); 571 if (ic == null) { 572 return getDataObject().getPrimaryFile().toString(); 574 } 575 576 Class clazz = ic.instanceClass(); 577 java.beans.BeanDescriptor bd = Utilities.getBeanInfo(clazz).getBeanDescriptor(); 578 String desc = bd.getShortDescription(); 579 return (desc.equals(bd.getName()))? getDisplayName(): desc; 580 } catch (Exception ex) { 581 return super.getShortDescription(); 582 } catch (LinkageError ex) { 583 return super.getShortDescription(); 585 } 586 } 587 588 589 public Action getPreferredAction() { 590 return null; 591 } 592 593 597 600 private static final class P<T> extends Node.Property<T> { 601 602 private Node.Property<T> del; 603 604 private InstanceDataObject t; 605 606 public P (Node.Property<T> del, InstanceDataObject t) { 607 super (del.getValueType ()); 608 this.del = del; 609 this.t = t; 610 } 611 612 public void setName(java.lang.String str) { 613 del.setName(str); 614 } 615 616 public void restoreDefaultValue() throws IllegalAccessException , InvocationTargetException { 617 del.restoreDefaultValue(); 618 } 619 620 public void setValue(java.lang.String str, java.lang.Object obj) { 621 del.setValue(str, obj); 622 } 623 624 public boolean supportsDefaultValue() { 625 return del.supportsDefaultValue(); 626 } 627 628 public boolean canRead() { 629 return del.canRead (); 630 } 631 632 public PropertyEditor getPropertyEditor() { 633 return del.getPropertyEditor(); 634 } 635 636 public boolean isHidden() { 637 return del.isHidden(); 638 } 639 640 public T getValue() throws IllegalAccessException , InvocationTargetException { 641 return del.getValue (); 642 } 643 644 public void setExpert(boolean param) { 645 del.setExpert(param); 646 } 647 648 650 public void setValue(T val) throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 651 del.setValue (val); 652 t.scheduleSave(); 653 } 654 655 public void setShortDescription(java.lang.String str) { 656 del.setShortDescription(str); 657 } 658 659 public boolean isExpert() { 660 return del.isExpert(); 661 } 662 663 public boolean canWrite() { 664 return del.canWrite (); 665 } 666 667 public Class <T> getValueType() { 668 return del.getValueType(); 669 } 670 671 public java.lang.String getDisplayName() { 672 return del.getDisplayName(); 673 } 674 675 public java.util.Enumeration <String > attributeNames() { 676 return del.attributeNames(); 677 } 678 679 public java.lang.String getShortDescription() { 680 return del.getShortDescription(); 681 } 682 683 public java.lang.String getName() { 684 return del.getName(); 685 } 686 687 public void setHidden(boolean param) { 688 del.setHidden(param); 689 } 690 691 public void setDisplayName(java.lang.String str) { 692 del.setDisplayName(str); 693 } 694 695 public boolean isPreferred() { 696 return del.isPreferred(); 697 } 698 699 public java.lang.Object getValue(java.lang.String str) { 700 return del.getValue(str); 701 } 702 703 public void setPreferred(boolean param) { 704 del.setPreferred(param); 705 } 706 707 } 709 712 private static final class I<T> extends Node.IndexedProperty<T, InstanceDataObject> { 713 714 private Node.IndexedProperty<T, InstanceDataObject> del; 715 716 private InstanceDataObject t; 717 718 public I (Node.IndexedProperty<T, InstanceDataObject> del, InstanceDataObject t) { 719 super (del.getValueType (), del.getElementType ()); 720 this.del = del; 721 this.t = t; 722 } 723 724 public void setName(java.lang.String str) { 725 del.setName(str); 726 } 727 728 public void restoreDefaultValue() throws IllegalAccessException , InvocationTargetException { 729 del.restoreDefaultValue(); 730 } 731 732 public void setValue(java.lang.String str, java.lang.Object obj) { 733 del.setValue(str, obj); 734 } 735 736 public boolean supportsDefaultValue() { 737 return del.supportsDefaultValue(); 738 } 739 740 public boolean canRead() { 741 return del.canRead (); 742 } 743 744 public PropertyEditor getPropertyEditor() { 745 return del.getPropertyEditor(); 746 } 747 748 public boolean isHidden() { 749 return del.isHidden(); 750 } 751 752 public T getValue() throws IllegalAccessException , InvocationTargetException { 753 return del.getValue (); 754 } 755 756 public void setExpert(boolean param) { 757 del.setExpert(param); 758 } 759 760 762 public void setValue(T val) throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 763 del.setValue (val); 764 t.scheduleSave(); 765 } 766 767 public void setShortDescription(java.lang.String str) { 768 del.setShortDescription(str); 769 } 770 771 public boolean isExpert() { 772 return del.isExpert(); 773 } 774 775 public boolean canWrite() { 776 return del.canWrite (); 777 } 778 779 public Class <T> getValueType() { 780 return del.getValueType(); 781 } 782 783 public java.lang.String getDisplayName() { 784 return del.getDisplayName(); 785 } 786 787 public java.util.Enumeration <String > attributeNames() { 788 return del.attributeNames(); 789 } 790 791 public java.lang.String getShortDescription() { 792 return del.getShortDescription(); 793 } 794 795 public java.lang.String getName() { 796 return del.getName(); 797 } 798 799 public void setHidden(boolean param) { 800 del.setHidden(param); 801 } 802 803 public void setDisplayName(java.lang.String str) { 804 del.setDisplayName(str); 805 } 806 807 public boolean isPreferred() { 808 return del.isPreferred(); 809 } 810 811 public java.lang.Object getValue(java.lang.String str) { 812 return del.getValue(str); 813 } 814 815 public void setPreferred(boolean param) { 816 del.setPreferred(param); 817 } 818 819 public boolean canIndexedRead () { 820 return del.canIndexedRead (); 821 } 822 823 public Class <InstanceDataObject> getElementType () { 824 return del.getElementType (); 825 } 826 827 public InstanceDataObject getIndexedValue (int index) throws 828 IllegalAccessException , IllegalArgumentException , InvocationTargetException { 829 return del.getIndexedValue (index); 830 } 831 832 public boolean canIndexedWrite () { 833 return del.canIndexedWrite (); 834 } 835 836 public void setIndexedValue (int indx, InstanceDataObject val) throws 837 IllegalAccessException , IllegalArgumentException , InvocationTargetException { 838 del.setIndexedValue (indx, val); 839 t.scheduleSave(); 840 } 841 842 public PropertyEditor getIndexedPropertyEditor () { 843 return del.getIndexedPropertyEditor (); 844 } 845 } 847 848 private final static class InstanceChildren extends Children.Keys implements PropertyChangeListener { 849 java.lang.ref.WeakReference <PropertyChangeListener> dobjListener; 850 InstanceDataObject dobj; 851 Object bean; 852 ContextL contextL = null; 853 854 public InstanceChildren(InstanceDataObject dobj) { 855 this.dobj = dobj; 856 } 857 858 protected void addNotify () { 859 super.addNotify(); 860 861 PropertyChangeListener p = org.openide.util.WeakListeners.propertyChange(this, dobj); 862 dobjListener = new java.lang.ref.WeakReference <PropertyChangeListener>(p); 863 dobj.addPropertyChangeListener(p); 864 contextL = new ContextL (this); 866 propertyChange(null); 867 } 868 869 protected void removeNotify () { 870 if (contextL != null && bean != null) 871 ((BeanContext) bean).removeBeanContextMembershipListener (contextL); 872 contextL = null; 873 874 PropertyChangeListener p = dobjListener.get(); 875 if (p != null) { 876 dobj.removePropertyChangeListener(p); 877 dobjListener.clear(); 878 } 879 880 setKeys (java.util.Collections.emptySet()); 881 } 882 883 public void propertyChange(PropertyChangeEvent evt) { 884 if (evt != null && !evt.getPropertyName().equals(InstanceDataObject.PROP_COOKIE)) return; 885 886 if (contextL != null && bean != null) 887 ((BeanContext) bean).removeBeanContextMembershipListener (contextL); 888 889 try { 890 InstanceCookie ic = dobj.getCookie(InstanceCookie.class); 891 if (ic == null) { 892 bean = null; 893 return; 894 } 895 Class <?> clazz = ic.instanceClass(); 896 if (BeanContext.class.isAssignableFrom(clazz)) { 897 bean = ic.instanceCreate(); 898 } else if (BeanContextProxy.class.isAssignableFrom(clazz)) { 899 bean = ((BeanContextProxy) dobj.instanceCreate()).getBeanContextProxy(); 900 } else { 901 bean = null; 902 } 903 } catch (Exception ex) { 904 bean = null; 905 Exceptions.printStackTrace(ex); 906 } 907 if (bean != null) { 908 ((BeanContext) bean).addBeanContextMembershipListener (contextL); 910 } 911 updateKeys(); 912 } 913 914 private void updateKeys() { 915 if (bean == null) { 916 setKeys(java.util.Collections.emptySet()); 917 } else { 918 setKeys(((BeanContext) bean).toArray()); 919 } 920 } 921 922 927 protected Node[] createNodes(Object key) { 928 Object ctx = bean; 929 if (bean == null) return new Node[0]; 930 931 try { 932 if (key instanceof java.beans.beancontext.BeanContextSupport ) { 933 java.beans.beancontext.BeanContextSupport bcs = (java.beans.beancontext.BeanContextSupport )key; 934 935 if (((BeanContext) ctx).contains (bcs.getBeanContextPeer())) { 936 return new Node[0]; 941 } 942 } 943 944 return new Node[] { new BeanContextNode<Object > (key, dobj) }; 945 } catch (IntrospectionException ex) { 946 return new Node[0]; 948 } 949 } 950 951 953 private static final class ContextL implements BeanContextMembershipListener { 954 955 private java.lang.ref.WeakReference <InstanceChildren> ref; 956 957 958 ContextL (InstanceChildren bc) { 959 ref = new java.lang.ref.WeakReference <InstanceChildren> (bc); 960 } 961 962 966 public void childrenAdded (BeanContextMembershipEvent bcme) { 967 InstanceChildren bc = ref.get (); 968 if (bc != null) { 969 bc.updateKeys(); 970 } 971 } 972 973 977 public void childrenRemoved (BeanContextMembershipEvent bcme) { 978 InstanceChildren bc = ref.get (); 979 if (bc != null) { 980 bc.updateKeys (); 981 } 982 } 983 } 984 } 985 986 988 private static class BeanFactoryImpl implements BeanChildren.Factory { 989 InstanceDataObject task; 990 public BeanFactoryImpl(InstanceDataObject task) { 991 this.task = task; 992 } 993 994 995 public Node createNode (Object bean) throws IntrospectionException { 996 return new BeanContextNode<Object > (bean, task); 997 } 998 } 999 1000 private static class BeanContextNode<T> extends BeanNode<T> { 1001 public BeanContextNode(T bean, InstanceDataObject task) throws IntrospectionException { 1002 super(bean, getChildren(bean, task)); 1003 changeSheet(getSheet(), task); 1004 } 1005 1006 private void changeSheet(Sheet orig, InstanceDataObject task) { 1007 Sheet.Set props = orig.get (Sheet.PROPERTIES); 1008 if (props != null) { 1009 convertProps (props, props.getProperties(), task); 1010 } 1011 1012 props = orig.get(Sheet.EXPERT); 1013 if (props != null) { 1014 convertProps (props, props.getProperties(), task); 1015 } 1016 } 1017 private static Children getChildren (Object bean, InstanceDataObject task) { 1018 if (bean instanceof BeanContext) 1019 return new BeanChildren ((BeanContext)bean, new BeanFactoryImpl(task)); 1020 if (bean instanceof BeanContextProxy) { 1021 java.beans.beancontext.BeanContextChild bch = ((BeanContextProxy)bean).getBeanContextProxy(); 1022 if (bch instanceof BeanContext) 1023 return new BeanChildren ((BeanContext)bch, new BeanFactoryImpl(task)); 1024 } 1025 return Children.LEAF; 1026 } 1027 1028 public boolean canDestroy() { 1030 return false; 1031 } 1032 1033 } 1034 1035 1038 private final class PropL extends Object implements PropertyChangeListener { 1039 private boolean doNotListen = false; 1040 PropL() {} 1041 1042 public void propertyChange(PropertyChangeEvent e) { 1043 if (doNotListen) return; 1044 firePropertyChange (e.getPropertyName (), e.getOldValue (), e.getNewValue ()); 1045 } 1046 1047 public void destroy() { 1048 doNotListen = true; 1049 } 1050 } 1051 1052} 1053 | Popular Tags |