1 19 package org.openide.nodes; 20 21 22 import java.beans.IntrospectionException ; 23 import java.beans.beancontext.*; 24 25 import java.lang.ref.*; 26 27 import java.util.*; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 31 32 40 public class BeanChildren extends Children.Keys { 41 42 private static final Factory DEFAULT_FACTORY = new BeanFactory(); 43 44 51 private static final java.util.Map <Node,Reference[]> nodes2Beans = 52 new WeakHashMap<Node,Reference[]>(); 54 55 private BeanContext bean; 56 57 58 private Factory factory; 59 60 61 private ContextL contextL; 62 63 66 public BeanChildren(BeanContext bean) { 67 this(bean, DEFAULT_FACTORY); 68 } 69 70 74 public BeanChildren(BeanContext bean, Factory factory) { 75 this.bean = bean; 76 this.factory = factory; 77 } 78 79 81 final void updateKeys() { 82 setKeys(bean.toArray()); 83 } 84 85 90 protected Node[] createNodes(Object subbean) { 91 try { 92 if (subbean instanceof BeanContextSupport) { 93 BeanContextSupport bcs = (BeanContextSupport) subbean; 94 95 if (bean.contains(bcs.getBeanContextPeer()) && (bcs != bcs.getBeanContextPeer())) { 96 return new Node[0]; 101 } 102 } 103 104 Node n = factory.createNode(subbean); 105 106 synchronized (nodes2Beans) { 108 nodes2Beans.put(n, new Reference[] { new WeakReference<BeanContext>(bean), new WeakReference<Object >(subbean) }); 109 } 110 111 n.addNodeListener(contextL); 112 113 return new Node[] { n }; 114 } catch (IntrospectionException ex) { 115 Logger.getLogger(BeanChildren.class.getName()).log(Level.WARNING, null, ex); 116 117 return new Node[0]; 118 } 119 } 120 121 123 protected void addNotify() { 124 contextL = new ContextL(this); 126 bean.addBeanContextMembershipListener(contextL); 127 128 updateKeys(); 129 } 130 131 133 protected void removeNotify() { 134 if (contextL != null) { 135 bean.removeBeanContextMembershipListener(contextL); 136 } 137 138 contextL = null; 139 140 setKeys(java.util.Collections.EMPTY_SET); 141 } 142 143 147 public static interface Factory { 148 153 public Node createNode(Object bean) throws IntrospectionException ; 154 } 155 156 158 private static class BeanFactory extends Object implements Factory { 159 BeanFactory() { 160 } 161 162 163 public Node createNode(Object bean) throws IntrospectionException { 164 return new BeanNode(bean); 165 } 166 } 167 168 170 private static final class ContextL extends NodeAdapter implements BeanContextMembershipListener { 171 172 private WeakReference<BeanChildren> ref; 173 174 ContextL() { 175 } 176 177 178 ContextL(BeanChildren bc) { 179 ref = new WeakReference<BeanChildren>(bc); 180 } 181 182 186 public void childrenAdded(BeanContextMembershipEvent bcme) { 187 BeanChildren bc = ref.get(); 188 189 if (bc != null) { 190 bc.updateKeys(); 191 } 192 } 193 194 198 public void childrenRemoved(BeanContextMembershipEvent bcme) { 199 BeanChildren bc = ref.get(); 200 201 if (bc != null) { 202 bc.updateKeys(); 203 } 204 } 205 206 public void nodeDestroyed(NodeEvent ev) { 207 Node n = ev.getNode(); 208 Reference[] refs; 209 210 synchronized (nodes2Beans) { 211 refs = nodes2Beans.get(n); 212 } 213 214 if (refs != null) { 215 BeanContext bean = (BeanContext) refs[0].get(); 216 217 if (bean != null) { 218 Object subbean = refs[1].get(); 219 220 if (subbean != null) { 221 try { 224 bean.remove(subbean); 225 } catch (RuntimeException re) { 226 Logger.getLogger(BeanChildren.class.getName()).log(Level.WARNING, null, re); 231 } 232 } 233 } 234 } 235 } 236 } 237 } 238 | Popular Tags |