1 19 20 package org.netbeans.modules.settings.convertors; 21 22 import java.awt.Graphics ; 23 import java.awt.GraphicsEnvironment ; 24 import java.awt.Image ; 25 import java.awt.Transparency ; 26 import java.awt.image.BufferedImage ; 27 import java.awt.image.ColorModel ; 28 import java.beans.BeanDescriptor ; 29 import java.beans.BeanInfo ; 30 import java.beans.EventSetDescriptor ; 31 import java.beans.IntrospectionException ; 32 import java.beans.PropertyChangeEvent ; 33 import java.beans.PropertyChangeListener ; 34 import java.beans.PropertyEditor ; 35 import java.beans.beancontext.BeanContext ; 36 import java.beans.beancontext.BeanContextProxy ; 37 import java.beans.beancontext.BeanContextMembershipEvent ; 38 import java.beans.beancontext.BeanContextMembershipListener ; 39 import java.beans.beancontext.BeanContextSupport ; 40 import java.lang.reflect.InvocationTargetException ; 41 import java.lang.reflect.Method ; 42 import java.io.IOException ; 43 import java.lang.ref.Reference ; 44 import java.lang.ref.WeakReference ; 45 import java.lang.reflect.Modifier ; 46 import java.util.Arrays ; 47 import java.util.List ; 48 import java.util.ArrayList ; 49 import java.util.Collection ; 50 import java.util.Collections ; 51 import java.util.Enumeration ; 52 import java.util.logging.Level ; 53 import java.util.logging.Logger ; 54 import javax.swing.Action ; 55 import javax.swing.Icon ; 56 import javax.swing.ImageIcon ; 57 import org.openide.actions.ToolsAction; 58 import org.openide.cookies.InstanceCookie; 59 import org.openide.filesystems.FileStateInvalidException; 60 import org.openide.filesystems.FileSystem; 61 import org.openide.loaders.DataNode; 62 import org.openide.loaders.DataObject; 63 import org.openide.loaders.InstanceDataObject; 64 import org.openide.nodes.BeanChildren; 65 import org.openide.nodes.BeanNode; 66 import org.openide.nodes.Children; 67 import org.openide.nodes.Node; 68 import org.openide.nodes.Node.Property; 69 import org.openide.nodes.Sheet; 70 import org.openide.util.Exceptions; 71 import org.openide.util.NbBundle; 72 import org.openide.util.SharedClassObject; 73 import org.openide.util.Utilities; 74 import org.openide.util.WeakListeners; 75 import org.openide.util.actions.SystemAction; 76 77 80 public final class SerialDataNode extends DataNode { 81 82 private PropL propertyChangeListener = null; 83 86 private boolean noBeanInfo = false; 87 private final SerialDataConvertor convertor; 88 private WeakReference <Object > settingInstance = new WeakReference <Object >(null); 89 private boolean isNameChanged = false; 90 93 private final Boolean isAfterNodeConstruction; 94 95 static final String ATTR_DISPLAY_NAME = "SerialDataNode_DisplayName"; 97 98 public SerialDataNode(DataObject dobj) { 99 this(null, dobj, Boolean.FALSE.equals(dobj. 100 getPrimaryFile().getAttribute("beaninfo"))); } 102 103 104 public SerialDataNode(SerialDataConvertor conv) { 105 this (conv, conv.getDataObject(), Boolean.FALSE.equals(conv.getDataObject(). 106 getPrimaryFile().getAttribute("beaninfo"))); } 108 109 112 private SerialDataNode(SerialDataConvertor conv, DataObject dobj, boolean noBeanInfo) { 113 super (dobj, getChildren(dobj, noBeanInfo)); 114 115 this.convertor = conv; 116 this.noBeanInfo = noBeanInfo; 117 isAfterNodeConstruction = Boolean.TRUE; 118 } 119 120 private static Children getChildren(DataObject dobj, boolean noBeanInfo) { 121 if (noBeanInfo) { 122 return Children.LEAF; 123 } 124 InstanceCookie inst = (InstanceCookie)dobj.getCookie(InstanceCookie.class); 125 if (inst == null) return Children.LEAF; 126 try { 127 Class clazz = inst.instanceClass(); 128 if (BeanContext .class.isAssignableFrom(clazz) || 129 BeanContextProxy .class.isAssignableFrom(clazz)) { 130 return new InstanceChildren (); 131 } else { 132 return Children.LEAF; 133 } 134 } catch (Exception ex) { 135 return Children.LEAF; 136 } 137 } 138 139 142 private InstanceDataObject i () { 143 return (InstanceDataObject)getDataObject (); 144 } 145 146 private InstanceCookie.Of ic () { 147 return (InstanceCookie.Of) getDataObject().getCookie(InstanceCookie.Of.class); 148 } 149 150 private SerialDataConvertor getConvertor() { 151 return convertor; 152 } 153 154 155 private Object getSettingInstance() { 156 return settingInstance.get(); 157 } 158 159 private void setSettingsInstance(Object obj) { 160 if (obj == settingInstance.get()) return; 161 isNameChanged = false; 162 settingInstance = new WeakReference <Object >(obj); 163 } 164 165 169 public Image getIcon (int type) { 170 if (noBeanInfo) return super.getIcon(type); 171 Image img = null; 172 try { 173 DataObject dobj = getDataObject(); 174 img = dobj.getPrimaryFile().getFileSystem().getStatus(). 175 annotateIcon (img, type, dobj.files ()); 176 } catch (FileStateInvalidException e) { 177 } 179 180 if (img == null) img = initIcon(type); 181 if (img == null) img = super.getIcon(type); 182 return img; 183 } 184 185 191 public Image getOpenedIcon (int type) { 192 return getIcon (type); 193 } 194 195 public String getHtmlDisplayName() { 196 return null; 197 } 198 199 205 private void resolvePropertyChange() { 206 if (propertyChangeListener != null && 207 propertyChangeListener.getChangeAndReset()) return; 208 209 if (notifyResolvePropertyChange && propertyChangeListener != null ) { 210 notifyResolvePropertyChange = false; 211 XMLSettingsSupport.err.warning( 212 "Warning: no PropertyChangeEvent fired from settings stored in " + getDataObject()); 214 } 215 SerialDataConvertor c = getConvertor(); 216 if (c != null) { 217 c.handleUnfiredChange(); 218 } 219 } 220 221 private boolean notifyResolvePropertyChange = true; 222 223 224 226 private void initPList (Object bean, BeanInfo bInfo, BeanNode.Descriptor descr) { 227 EventSetDescriptor [] descs = bInfo.getEventSetDescriptors(); 228 try { 229 Method setter = null; 230 for (int i = 0; descs != null && i < descs.length; i++) { 231 setter = descs[i].getAddListenerMethod(); 232 if (setter != null && setter.getName().equals("addPropertyChangeListener")) { propertyChangeListener = new PropL(createSupportedPropertyNames(descr)); 234 setter.invoke(bean, new Object [] {WeakListeners.propertyChange(propertyChangeListener, bean)}); 235 setSettingsInstance(bean); 236 } 237 } 238 } catch (Exception ex) { 239 } 241 } 242 243 private Collection <String > createSupportedPropertyNames(BeanNode.Descriptor descr) { 244 ArrayList <String > supportedPropertyNames = new ArrayList <String >(); 245 if (descr.property != null) { 246 for (int i = 0; i < descr.property.length; i++) { 247 Node.Property property = descr.property[i]; 248 supportedPropertyNames.add(property.getName()); 249 } 250 } 251 252 if (descr.expert != null) { 253 for (int i = 0; i < descr.expert.length; i++) { 254 Property property = descr.property[i]; 255 supportedPropertyNames.add(property.getName()); 256 } 257 } 258 259 return supportedPropertyNames; 260 } 261 262 private Image initIcon (int type) { 263 Image beanInfoIcon = null; 264 try { 265 InstanceCookie ic = ic(); 266 if (ic == null) return null; 267 Class <?> clazz = ic.instanceClass(); 268 273 String className = clazz.getName (); 274 BeanInfo bi; 275 if ( 276 className.equals ("javax.swing.JSeparator") || className.equals ("javax.swing.JToolBar$Separator") ) { 279 Class clazzTmp = Class.forName ("javax.swing.JSeparator"); bi = Utilities.getBeanInfo (clazzTmp); 281 } else { 282 bi = Utilities.getBeanInfo (clazz); 283 } 284 285 if (bi != null) { 286 beanInfoIcon = bi.getIcon (type); 287 if (beanInfoIcon != null) { 288 beanInfoIcon = toBufferedImage(beanInfoIcon, true); 289 } 290 } 291 if (SystemAction.class.isAssignableFrom (clazz)) { 293 SystemAction action = SystemAction.get (clazz.asSubclass(SystemAction.class)); 294 if (beanInfoIcon == null) { 295 Icon icon = action.getIcon (); 296 if (icon != null) { 297 beanInfoIcon = Utilities.icon2Image(icon); 298 } 299 } 300 } 301 } catch (Exception e) { 302 Logger.getLogger(SerialDataNode.class.getName()).log(Level.WARNING, null, e); 304 } 305 306 return beanInfoIcon; 307 } 308 309 311 private String getNameForBean() { 312 try { 313 InstanceCookie ic = ic(); 314 if (ic == null) { 315 return NbBundle.getMessage(SerialDataNode.class, 317 "LBL_BrokenSettings"); } 319 Class <?> clazz = ic.instanceClass(); 320 Method nameGetter; 321 Class [] param = new Class [0]; 322 try { 323 nameGetter = clazz.getMethod ("getName", param); if (nameGetter.getReturnType () != String .class) throw new NoSuchMethodException (); 325 } catch (NoSuchMethodException e) { 326 try { 327 nameGetter = clazz.getMethod ("getDisplayName", param); if (nameGetter.getReturnType () != String .class) throw new NoSuchMethodException (); 329 } catch (NoSuchMethodException ee) { 330 return null; 331 } 332 } 333 Object bean = ic.instanceCreate(); 334 setSettingsInstance(bean); 335 return (String ) nameGetter.invoke (bean); 336 } catch (Exception ex) { 337 return null; 339 } 340 } 341 342 343 private Method getDeclaredSetter() { 344 Method nameSetter = null; 345 try { 346 InstanceCookie ic = ic(); 347 if (ic == null) return null; 348 Class <?> clazz = ic.instanceClass(); 349 Class [] param = new Class [] {String .class}; 350 try { 352 nameSetter = clazz.getMethod ("setName", param); } catch (NoSuchMethodException e) { 354 nameSetter = clazz.getMethod ("setDisplayName", param); } 356 if (!Modifier.isPublic(nameSetter.getModifiers())) { 357 nameSetter = null; 358 } 359 } catch (Exception ex) { 360 } 362 return nameSetter; 363 } 364 365 public void setName(String name) { 366 String old = getName(); 367 if (old != null && old.equals(name)) return; 368 InstanceCookie ic = ic(); 369 if (ic == null) { 370 return; 371 } 372 373 Method nameSetter = getDeclaredSetter(); 374 if (nameSetter != null) { 375 try { 376 Object bean = ic.instanceCreate(); 377 setSettingsInstance(bean); 378 nameSetter.invoke(bean, new Object [] {name}); 379 isNameChanged = true; 380 getDataObject().getPrimaryFile().setAttribute(ATTR_DISPLAY_NAME, name); 381 resolvePropertyChange(); 382 return; 383 } catch (IOException ex) { 384 Exceptions.printStackTrace(ex); 385 } catch (ClassNotFoundException ex) { 386 Exceptions.printStackTrace(ex); 387 } catch (IllegalAccessException ex) { 388 Exceptions.attachLocalizedMessage(ex, getDataObject().toString()); 389 Exceptions.printStackTrace(ex); 390 } catch (IllegalArgumentException ex) { 391 Exceptions.attachLocalizedMessage(ex, getDataObject().toString()); 392 Exceptions.printStackTrace(ex); 393 } catch (InvocationTargetException ex) { 394 Exceptions.attachLocalizedMessage(ex, getDataObject().toString()); 395 Exceptions.printStackTrace(ex); 396 } 397 } 398 } 399 400 public String getName() { 401 if (isAfterNodeConstruction == null) return super.getName(); 406 return getDisplayName(); 407 } 408 409 413 public String getDisplayName () { 414 String name; 415 Object setting = getSettingInstance(); 416 if (setting != null && isNameChanged) { 417 name = getNameForBean(); 419 if (name != null) { 420 return name; 421 } 422 } 423 424 425 name = (String ) getDataObject().getPrimaryFile(). 426 getAttribute(SerialDataNode.ATTR_DISPLAY_NAME); 427 if (name == null) { 428 try { 429 String def = "\b"; FileSystem.Status fsStatus = getDataObject().getPrimaryFile(). 431 getFileSystem().getStatus(); 432 name = fsStatus.annotateName(def, getDataObject().files()); 433 if (name.indexOf(def) < 0) { 434 return name; 435 } else { 436 name = getNameForBean(); 437 if (name != null) { 438 name = fsStatus.annotateName (name, getDataObject().files()); 439 } else { 440 name = fsStatus.annotateName (getDataObject().getName(), 441 getDataObject().files()); 442 } 443 } 444 } catch (FileStateInvalidException e) { 445 } 447 } 448 return name; 449 } 450 451 protected Sheet createSheet () { 452 Sheet retVal = new Sheet(); 453 InstanceCookie ic = ic(); 454 455 if (ic != null) { 456 457 try { 458 Class instanceClass = ic.instanceClass (); 459 Object bean = ic.instanceCreate (); 460 BeanInfo beanInfo = Utilities.getBeanInfo (instanceClass); 461 462 BeanNode.Descriptor descr = BeanNode.computeProperties (bean, beanInfo); 463 BeanDescriptor bd = beanInfo.getBeanDescriptor(); 464 465 initPList(bean, beanInfo, descr); 466 467 retVal.put (createPropertiesSet(descr, bd)); 468 469 if (descr.expert != null && descr.expert.length != 0) { 470 retVal.put (createExpertSet(descr, bd)); 471 } 472 473 } catch (ClassNotFoundException ex) { 474 Logger.getLogger(SerialDataNode.class.getName()).log(Level.WARNING, null, ex); 475 } catch (IOException ex) { 476 Logger.getLogger(SerialDataNode.class.getName()).log(Level.WARNING, null, ex); 477 } catch (IntrospectionException ex) { 478 Logger.getLogger(SerialDataNode.class.getName()).log(Level.WARNING, null, ex); 479 } 480 } 481 482 return retVal; 483 } 484 485 486 private Sheet.Set createExpertSet(BeanNode.Descriptor descr, BeanDescriptor bd) { 487 Sheet.Set p = Sheet.createExpertSet(); 488 convertProps (p, descr.expert, this); 489 if (bd != null) { 490 Object helpID = bd.getValue("expertHelpID"); if (helpID != null && helpID instanceof String ) { 492 p.setValue("helpID", helpID); } 494 } 495 return p; 496 } 497 498 private Sheet.Set createPropertiesSet(BeanNode.Descriptor descr, BeanDescriptor bd) { 499 Sheet.Set props; 500 props = Sheet.createPropertiesSet(); 501 if (descr.property != null) { 502 convertProps (props, descr.property, this); 503 } 504 if (bd != null) { 505 Object helpID = bd.getValue("propertiesHelpID"); if (helpID != null && helpID instanceof String ) { 508 props.setValue("helpID", helpID); } 510 } 511 return props; 512 } 513 514 515 520 private static final void convertProps ( 521 Sheet.Set set, Node.Property<?>[] arr, SerialDataNode ido 522 ) { 523 for (int i = 0; i < arr.length; i++) { 524 if (arr[i] instanceof Node.IndexedProperty) { 525 set.put (new I ((Node.IndexedProperty)arr[i], ido)); 526 } else { 527 set.put (new P (arr[i], ido)); 528 } 529 } 530 } 531 532 535 private static final Image toBufferedImage(Image img, boolean load) { 536 if (load) { 538 new ImageIcon (img); 539 } 540 541 BufferedImage rep = createBufferedImage(); 542 Graphics g = rep.createGraphics(); 543 g.drawImage(img, 0, 0, null); 544 g.dispose(); 545 img.flush(); 546 return rep; 547 } 548 549 550 private static final BufferedImage createBufferedImage() { 551 ColorModel model = GraphicsEnvironment.getLocalGraphicsEnvironment(). 552 getDefaultScreenDevice().getDefaultConfiguration().getColorModel(Transparency.BITMASK); 553 BufferedImage buffImage = new BufferedImage (model, 554 model.createCompatibleWritableRaster(16, 16), model.isAlphaPremultiplied(), null); 555 return buffImage; 556 } 557 558 561 public boolean canRename() { 562 return getDeclaredSetter() != null; 563 } 564 565 568 public boolean canDestroy() { 569 try { 570 InstanceCookie ic = ic(); 571 if (ic == null) return true; 572 Class clazz = ic.instanceClass(); 573 return (!SharedClassObject.class.isAssignableFrom(clazz)); 574 } catch (Exception ex) { 575 return true; 576 } 577 } 578 579 public boolean canCut() { 580 try { 581 InstanceCookie ic = ic(); 582 if (ic == null) return false; 583 Class clazz = ic.instanceClass(); 584 return (!SharedClassObject.class.isAssignableFrom(clazz)); 585 } catch (Exception ex) { 586 return false; 587 } 588 } 589 590 public boolean canCopy() { 591 try { 592 InstanceCookie ic = ic(); 593 if (ic == null) return false; 594 Class clazz = ic.instanceClass(); 595 return (!SharedClassObject.class.isAssignableFrom(clazz)); 596 } catch (Exception ex) { 597 return false; 598 } 599 } 600 601 602 public String getShortDescription() { 603 if (noBeanInfo) return super.getShortDescription(); 604 605 try { 606 InstanceCookie ic = ic(); 607 if (ic == null) { 608 return getDataObject().getPrimaryFile().toString(); 610 } 611 612 Class clazz = ic.instanceClass(); 613 BeanDescriptor bd = Utilities.getBeanInfo(clazz).getBeanDescriptor(); 614 String desc = bd.getShortDescription(); 615 return (desc.equals(bd.getDisplayName()))? getDisplayName(): desc; 616 } catch (Exception ex) { 617 return super.getShortDescription(); 618 } 619 } 620 621 622 public Action getPreferredAction() { 623 return null; 624 } 625 626 630 633 private static final class P extends Node.Property { 634 635 private Node.Property del; 636 637 private SerialDataNode t; 638 639 public P (Node.Property del, SerialDataNode t) { 640 super (del.getValueType ()); 641 this.del = del; 642 this.t = t; 643 } 644 645 public void setName(java.lang.String str) { 646 del.setName(str); 647 } 648 649 public void restoreDefaultValue() throws IllegalAccessException , InvocationTargetException { 650 del.restoreDefaultValue(); 651 } 652 653 public void setValue(String str, Object obj) { 654 del.setValue(str, obj); 655 } 656 657 public boolean supportsDefaultValue() { 658 return del.supportsDefaultValue(); 659 } 660 661 public boolean canRead() { 662 return del.canRead (); 663 } 664 665 public PropertyEditor getPropertyEditor() { 666 return del.getPropertyEditor(); 667 } 668 669 public boolean isHidden() { 670 return del.isHidden(); 671 } 672 673 public Object getValue() throws IllegalAccessException , InvocationTargetException { 674 return del.getValue (); 675 } 676 677 public void setExpert(boolean param) { 678 del.setExpert(param); 679 } 680 681 683 public void setValue(Object val) throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 684 del.setValue (val); 685 t.resolvePropertyChange(); 686 } 687 688 public void setShortDescription(String str) { 689 del.setShortDescription(str); 690 } 691 692 public boolean isExpert() { 693 return del.isExpert(); 694 } 695 696 public boolean canWrite() { 697 return del.canWrite (); 698 } 699 700 public Class getValueType() { 701 return del.getValueType(); 702 } 703 704 public String getDisplayName() { 705 return del.getDisplayName(); 706 } 707 708 public Enumeration <String > attributeNames() { 709 return del.attributeNames(); 710 } 711 712 public String getShortDescription() { 713 return del.getShortDescription(); 714 } 715 716 public String getName() { 717 return del.getName(); 718 } 719 720 public void setHidden(boolean param) { 721 del.setHidden(param); 722 } 723 724 public void setDisplayName(String str) { 725 del.setDisplayName(str); 726 } 727 728 public boolean isPreferred() { 729 return del.isPreferred(); 730 } 731 732 public Object getValue(String str) { 733 return del.getValue(str); 734 } 735 736 public void setPreferred(boolean param) { 737 del.setPreferred(param); 738 } 739 740 } 742 745 private static final class I extends Node.IndexedProperty { 746 747 private Node.IndexedProperty del; 748 749 private SerialDataNode t; 750 751 public I (Node.IndexedProperty del, SerialDataNode t) { 752 super (del.getValueType (), del.getElementType ()); 753 this.del = del; 754 this.t = t; 755 } 756 757 public void setName(java.lang.String str) { 758 del.setName(str); 759 } 760 761 public void restoreDefaultValue() throws IllegalAccessException , InvocationTargetException { 762 del.restoreDefaultValue(); 763 } 764 765 public void setValue(String str, Object obj) { 766 del.setValue(str, obj); 767 } 768 769 public boolean supportsDefaultValue() { 770 return del.supportsDefaultValue(); 771 } 772 773 public boolean canRead() { 774 return del.canRead (); 775 } 776 777 public PropertyEditor getPropertyEditor() { 778 return del.getPropertyEditor(); 779 } 780 781 public boolean isHidden() { 782 return del.isHidden(); 783 } 784 785 public Object getValue() throws IllegalAccessException , InvocationTargetException { 786 return del.getValue (); 787 } 788 789 public void setExpert(boolean param) { 790 del.setExpert(param); 791 } 792 793 795 public void setValue(Object val) throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 796 del.setValue (val); 797 t.resolvePropertyChange(); 798 } 799 800 public void setShortDescription(String str) { 801 del.setShortDescription(str); 802 } 803 804 public boolean isExpert() { 805 return del.isExpert(); 806 } 807 808 public boolean canWrite() { 809 return del.canWrite (); 810 } 811 812 public Class getValueType() { 813 return del.getValueType(); 814 } 815 816 public String getDisplayName() { 817 return del.getDisplayName(); 818 } 819 820 public Enumeration <String > attributeNames() { 821 return del.attributeNames(); 822 } 823 824 public String getShortDescription() { 825 return del.getShortDescription(); 826 } 827 828 public String getName() { 829 return del.getName(); 830 } 831 832 public void setHidden(boolean param) { 833 del.setHidden(param); 834 } 835 836 public void setDisplayName(String str) { 837 del.setDisplayName(str); 838 } 839 840 public boolean isPreferred() { 841 return del.isPreferred(); 842 } 843 844 public Object getValue(String str) { 845 return del.getValue(str); 846 } 847 848 public void setPreferred(boolean param) { 849 del.setPreferred(param); 850 } 851 852 public boolean canIndexedRead () { 853 return del.canIndexedRead (); 854 } 855 856 public Class getElementType () { 857 return del.getElementType (); 858 } 859 860 public Object getIndexedValue (int index) throws 861 IllegalAccessException , IllegalArgumentException , InvocationTargetException { 862 return del.getIndexedValue (index); 863 } 864 865 public boolean canIndexedWrite () { 866 return del.canIndexedWrite (); 867 } 868 869 public void setIndexedValue (int indx, Object val) throws 870 IllegalAccessException , IllegalArgumentException , InvocationTargetException { 871 del.setIndexedValue (indx, val); 872 t.resolvePropertyChange(); 873 } 874 875 public PropertyEditor getIndexedPropertyEditor () { 876 return del.getIndexedPropertyEditor (); 877 } 878 } 880 881 private final static class InstanceChildren extends Children.Keys { 882 SerialDataNode task; 883 DataObject dobj; 884 Object bean; 885 ContextL contextL = null; 886 887 public InstanceChildren() { 888 } 889 890 protected void addNotify () { 891 super.addNotify(); 892 893 task = (SerialDataNode) getNode(); 894 dobj = task.getDataObject(); 895 contextL = new ContextL (this); 897 init(); 898 } 899 900 protected void removeNotify () { 901 if (contextL != null && bean != null) 902 ((BeanContext ) bean).removeBeanContextMembershipListener (contextL); 903 contextL = null; 904 905 setKeys(Collections.emptySet()); 906 } 907 908 private void init() { 909 try { 910 InstanceCookie ic = (InstanceCookie) dobj.getCookie(InstanceCookie.class); 911 if (ic == null) { 912 bean = null; 913 return; 914 } 915 Class clazz = ic.instanceClass(); 916 if (BeanContext .class.isAssignableFrom(clazz)) { 917 bean = ic.instanceCreate(); 918 } else if (BeanContextProxy .class.isAssignableFrom(clazz)) { 919 bean = ((BeanContextProxy ) ic.instanceCreate()).getBeanContextProxy(); 920 } else { 921 bean = null; 922 } 923 } catch (Exception ex) { 924 bean = null; 925 Exceptions.printStackTrace(ex); 926 } 927 if (bean != null) { 928 ((BeanContext ) bean).addBeanContextMembershipListener (contextL); 930 } 931 updateKeys(); 932 } 933 934 private void updateKeys() { 935 if (bean == null) { 936 setKeys(Collections.emptySet()); 937 } else { 938 setKeys(((BeanContext ) bean).toArray()); 939 } 940 } 941 942 947 protected Node[] createNodes(Object key) { 948 Object ctx = bean; 949 if (bean == null) return new Node[0]; 950 951 try { 952 if (key instanceof BeanContextSupport ) { 953 BeanContextSupport bcs = (BeanContextSupport )key; 954 955 if (((BeanContext ) ctx).contains (bcs.getBeanContextPeer())) { 956 return new Node[0]; 961 } 962 } 963 964 return new Node[] { new BeanContextNode (key, task) }; 965 } catch (IntrospectionException ex) { 966 return new Node[0]; 968 } 969 } 970 971 973 private static final class ContextL implements BeanContextMembershipListener { 974 975 private final Reference <InstanceChildren> ref; 976 977 978 ContextL (InstanceChildren bc) { 979 ref = new WeakReference <InstanceChildren> (bc); 980 } 981 982 986 public void childrenAdded (BeanContextMembershipEvent bcme) { 987 InstanceChildren bc = ref.get (); 988 if (bc != null) { 989 bc.updateKeys(); 990 } 991 } 992 993 997 public void childrenRemoved (BeanContextMembershipEvent bcme) { 998 InstanceChildren bc = ref.get (); 999 if (bc != null) { 1000 bc.updateKeys (); 1001 } 1002 } 1003 } 1004 } 1005 1006 1008 private static class BeanFactoryImpl implements BeanChildren.Factory { 1009 SerialDataNode task; 1010 public BeanFactoryImpl(SerialDataNode task) { 1011 this.task = task; 1012 } 1013 1014 1015 public Node createNode (Object bean) throws IntrospectionException { 1016 return new BeanContextNode (bean, task); 1017 } 1018 } 1019 1020 private static class BeanContextNode extends BeanNode<Object > { 1021 public BeanContextNode(Object bean, SerialDataNode task) throws IntrospectionException { 1022 super(bean, getChildren(bean, task)); 1023 changeSheet(getSheet(), task); 1024 } 1025 1026 private void changeSheet(Sheet orig, SerialDataNode task) { 1027 Sheet.Set props = orig.get (Sheet.PROPERTIES); 1028 if (props != null) { 1029 convertProps (props, props.getProperties(), task); 1030 } 1031 1032 props = orig.get(Sheet.EXPERT); 1033 if (props != null) { 1034 convertProps (props, props.getProperties(), task); 1035 } 1036 } 1037 private static Children getChildren (Object bean, SerialDataNode task) { 1038 if (bean instanceof BeanContext ) 1039 return new BeanChildren ((BeanContext )bean, new BeanFactoryImpl(task)); 1040 if (bean instanceof BeanContextProxy ) { 1041 java.beans.beancontext.BeanContextChild bch = ((BeanContextProxy )bean).getBeanContextProxy(); 1042 if (bch instanceof BeanContext ) 1043 return new BeanChildren ((BeanContext )bch, new BeanFactoryImpl(task)); 1044 } 1045 return Children.LEAF; 1046 } 1047 1048 public boolean canDestroy() { 1050 return false; 1051 } 1052 1053 public Action [] getActions(boolean context) { 1054 return removeActions(super.getActions(context), new Action [] {SystemAction.get(ToolsAction.class)}); 1055 } 1056 1057 private static Action [] removeActions(Action [] allActions, Action [] toDeleteActions) { 1058 Action [] retVal = allActions; 1059 List <Action > actions = Arrays.asList(allActions); 1060 for (int i = 0; i < toDeleteActions.length; i++) { 1061 Action a = toDeleteActions[i]; 1062 if(actions.contains(a)) { 1063 actions = new ArrayList <Action >(actions); actions.remove(a); 1065 retVal = actions.toArray(new Action [0]); 1066 } 1067 } 1068 return retVal; 1069 } 1070 } 1071 1072 1075 private final class PropL extends Object implements PropertyChangeListener { 1076 private Collection <String > supportedPropertyNames; 1077 PropL(Collection <String > supportedPropertyNames) { 1078 this.supportedPropertyNames = supportedPropertyNames; 1079 } 1080 1081 private boolean isChanged = false; 1082 public void propertyChange(PropertyChangeEvent e) { 1083 isChanged = true; 1084 String name = e.getPropertyName(); 1085 1086 if (isSupportedName(name)) { 1087 firePropertyChange (name, e.getOldValue (), e.getNewValue ()); 1088 1089 if (name.equals("name")) { SerialDataNode.this.isNameChanged = true; 1091 SerialDataNode.this.fireDisplayNameChange(null, null); 1093 } else if (name.equals("displayName")) { SerialDataNode.this.isNameChanged = true; 1095 } 1096 } 1097 } 1098 1099 private boolean isSupportedName(String name) { 1100 return name != null && supportedPropertyNames.contains(name); 1101 } 1102 1103 public boolean getChangeAndReset() { 1104 boolean wasChanged = isChanged; 1105 isChanged = false; 1106 return wasChanged; 1107 } 1108 } 1109 1110} 1111 | Popular Tags |