1 7 package org.ejtools.adwt; 8 9 import java.beans.beancontext.BeanContext ; 10 import java.beans.beancontext.BeanContextMembershipEvent ; 11 import java.beans.beancontext.BeanContextMembershipListener ; 12 import java.util.ArrayList ; 13 import java.util.Collection ; 14 import java.util.Iterator ; 15 import java.util.Vector ; 16 17 import javax.swing.event.TreeModelEvent ; 18 import javax.swing.event.TreeModelListener ; 19 import javax.swing.tree.TreePath ; 20 21 import org.ejtools.adwt.util.DefaultObjectIndexer; 22 import org.ejtools.adwt.util.ObjectIndexer; 23 import org.ejtools.adwt.util.SearchableTreeModel; 24 25 31 public class BeanContextTreeModel implements SearchableTreeModel, BeanContextMembershipListener 32 { 33 34 protected Collection contexts = new Vector (); 35 36 protected ObjectIndexer indexer = new DefaultObjectIndexer(); 37 38 protected Collection listeners = new Vector (); 39 40 protected BeanContext root; 41 42 43 48 public BeanContextTreeModel(BeanContext root) 49 { 50 this.root = root; 51 this.root.addBeanContextMembershipListener(this); 52 } 53 54 55 61 public void addTreeModelListener(TreeModelListener tml) 62 { 63 this.listeners.add(tml); 64 } 65 66 67 72 public void childrenAdded(BeanContextMembershipEvent bcme) 73 { 74 for (Iterator it = bcme.iterator(); it.hasNext(); ) 75 { 76 Object o = it.next(); 77 if (o instanceof BeanContext ) 78 { 79 BeanContext context = (BeanContext ) o; 80 if (!contexts.contains(context)) 81 { 82 contexts.add(context); 83 context.addBeanContextMembershipListener(this); 84 } 85 } 86 this.indexer.put(o, computePath(bcme.getBeanContext(), o)); 87 } 88 this.fireTreeStructureChanged(); 89 } 90 91 92 97 public void childrenRemoved(BeanContextMembershipEvent bcme) 98 { 99 for (Iterator it = bcme.iterator(); it.hasNext(); ) 100 { 101 Object o = it.next(); 102 if (o instanceof BeanContext ) 103 { 104 BeanContext context = (BeanContext ) o; 105 if (contexts.contains(context)) 106 { 107 context.removeBeanContextMembershipListener(this); 108 contexts.remove(context); 109 } 110 } 111 this.indexer.remove(o); 112 } 113 this.fireTreeStructureChanged(); 114 } 115 116 117 123 public Object find(Object o) 124 { 125 return this.indexer.get(o); 126 } 127 128 129 136 public Object getChild(Object parent, int index) 137 { 138 Collection c = (Collection ) parent; 139 int i = 0; 140 Object result = null; 141 for (Iterator it = c.iterator(); it.hasNext(); i++) 142 { 143 result = it.next(); 144 if (i == index) 145 { 146 break; 147 } 148 result = null; 149 } 150 return result; 151 } 152 153 154 160 public int getChildCount(Object parent) 161 { 162 Collection c = (Collection ) parent; 163 return c.size(); 164 } 165 166 167 174 public int getIndexOfChild(Object parent, Object child) 175 { 176 Collection c = (Collection ) parent; 177 int i = 0; 178 int index = -1; 179 for (Iterator it = c.iterator(); it.hasNext(); i++) 180 { 181 if (it.next().equals(child)) 182 { 183 index = i; 184 break; 185 } 186 } 187 return index; 188 } 189 190 191 196 public Object getRoot() 197 { 198 return this.root; 199 } 200 201 202 208 public boolean isLeaf(Object node) 209 { 210 Collection c = (Collection ) node; 211 return (c.size() == 0); 212 } 213 214 215 220 public void removeTreeModelListener(TreeModelListener tml) 221 { 222 this.listeners.remove(tml); 223 } 224 225 226 231 public void setIndexer(ObjectIndexer indexer) 232 { 233 this.indexer = indexer; 234 } 235 236 237 243 public void valueForPathChanged(TreePath path, Object newValue) 244 { 245 throw new UnsupportedOperationException ("Not yet implemented"); 246 } 247 248 249 256 protected Object [] computePath(BeanContext parent, Object o) 257 { 258 ArrayList paths = new ArrayList (); 259 BeanContext current = parent; 260 paths.add(parent); 261 paths.add(o); 262 while (this.root.equals(current.getBeanContext())) 263 { 264 current = current.getBeanContext(); 265 paths.add(0, current); 266 } 267 return paths.toArray(); 268 } 269 270 271 272 protected void fireTreeNodesChanged() 273 { 274 TreeModelEvent event = new TreeModelEvent (this, new TreePath (this.root)); 275 Object [] copy = this.listeners.toArray(); 276 for (int i = 0; i < copy.length; i++) 277 { 278 TreeModelListener listener = (TreeModelListener ) copy[i]; 279 listener.treeNodesChanged(event); 280 } 281 } 282 283 284 285 protected void fireTreeNodesInserted() 286 { 287 TreeModelEvent event = new TreeModelEvent (this, new TreePath (this.root)); 288 Object [] copy = this.listeners.toArray(); 289 for (int i = 0; i < copy.length; i++) 290 { 291 TreeModelListener listener = (TreeModelListener ) copy[i]; 292 listener.treeNodesInserted(event); 293 } 294 } 295 296 297 298 protected void fireTreeNodesRemoved() 299 { 300 TreeModelEvent event = new TreeModelEvent (this, new TreePath (this.root)); 301 Object [] copy = this.listeners.toArray(); 302 for (int i = 0; i < copy.length; i++) 303 { 304 TreeModelListener listener = (TreeModelListener ) copy[i]; 305 listener.treeNodesRemoved(event); 306 } 307 } 308 309 310 311 protected void fireTreeStructureChanged() 312 { 313 TreeModelEvent event = new TreeModelEvent (this, new TreePath (this.root)); 314 Object [] copy = this.listeners.toArray(); 315 for (int i = 0; i < copy.length; i++) 316 { 317 TreeModelListener listener = (TreeModelListener ) copy[i]; 318 listener.treeStructureChanged(event); 319 } 320 } 321 } 322 | Popular Tags |