|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| BeanContextTreeModel.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* EJTools, the Enterprise Java Tools
|
|
| 3 |
*
|
|
| 4 |
* Distributable under LGPL license.
|
|
| 5 |
* See terms of license at www.gnu.org.
|
|
| 6 |
*/
|
|
| 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 |
/**
|
|
| 26 |
* Description of the Class
|
|
| 27 |
*
|
|
| 28 |
* @author Laurent Etiemble
|
|
| 29 |
* @version $Revision: 1.2 $
|
|
| 30 |
*/
|
|
| 31 |
public class BeanContextTreeModel implements SearchableTreeModel, BeanContextMembershipListener |
|
| 32 |
{
|
|
| 33 |
/** Description of the Field */
|
|
| 34 |
protected Collection contexts = new Vector(); |
|
| 35 |
/** Description of the Field */
|
|
| 36 |
protected ObjectIndexer indexer = new DefaultObjectIndexer(); |
|
| 37 |
/** Description of the Field */
|
|
| 38 |
protected Collection listeners = new Vector(); |
|
| 39 |
/** Root of the tree. */
|
|
| 40 |
protected BeanContext root;
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
/**
|
|
| 44 |
* Constructor for the BeanContextTreeModel object
|
|
| 45 |
*
|
|
| 46 |
* @param root Description of the Parameter
|
|
| 47 |
*/
|
|
| 48 | 0 |
public BeanContextTreeModel(BeanContext root)
|
| 49 |
{
|
|
| 50 | 0 |
this.root = root;
|
| 51 | 0 |
this.root.addBeanContextMembershipListener(this); |
| 52 |
} |
|
| 53 |
|
|
| 54 |
|
|
| 55 |
/**
|
|
| 56 |
* Adds a feature to the TreeModelListener attribute of the
|
|
| 57 |
* BeanContextTreeModel object
|
|
| 58 |
*
|
|
| 59 |
* @param tml The feature to be added to the TreeModelListener attribute
|
|
| 60 |
*/
|
|
| 61 | 0 |
public void addTreeModelListener(TreeModelListener tml) |
| 62 |
{
|
|
| 63 | 0 |
this.listeners.add(tml);
|
| 64 |
} |
|
| 65 |
|
|
| 66 |
|
|
| 67 |
/**
|
|
| 68 |
* Description of the Method
|
|
| 69 |
*
|
|
| 70 |
* @param bcme Description of the Parameter
|
|
| 71 |
*/
|
|
| 72 | 0 |
public void childrenAdded(BeanContextMembershipEvent bcme) |
| 73 |
{
|
|
| 74 | 0 |
for (Iterator it = bcme.iterator(); it.hasNext(); )
|
| 75 |
{
|
|
| 76 | 0 |
Object o = it.next(); |
| 77 | 0 |
if (o instanceof BeanContext) |
| 78 |
{
|
|
| 79 | 0 |
BeanContext context = (BeanContext) o; |
| 80 | 0 |
if (!contexts.contains(context))
|
| 81 |
{
|
|
| 82 | 0 |
contexts.add(context); |
| 83 | 0 |
context.addBeanContextMembershipListener(this);
|
| 84 |
} |
|
| 85 |
} |
|
| 86 | 0 |
this.indexer.put(o, computePath(bcme.getBeanContext(), o));
|
| 87 |
} |
|
| 88 | 0 |
this.fireTreeStructureChanged();
|
| 89 |
} |
|
| 90 |
|
|
| 91 |
|
|
| 92 |
/**
|
|
| 93 |
* Description of the Method
|
|
| 94 |
*
|
|
| 95 |
* @param bcme Description of the Parameter
|
|
| 96 |
*/
|
|
| 97 | 0 |
public void childrenRemoved(BeanContextMembershipEvent bcme) |
| 98 |
{
|
|
| 99 | 0 |
for (Iterator it = bcme.iterator(); it.hasNext(); )
|
| 100 |
{
|
|
| 101 | 0 |
Object o = it.next(); |
| 102 | 0 |
if (o instanceof BeanContext) |
| 103 |
{
|
|
| 104 | 0 |
BeanContext context = (BeanContext) o; |
| 105 | 0 |
if (contexts.contains(context))
|
| 106 |
{
|
|
| 107 | 0 |
context.removeBeanContextMembershipListener(this);
|
| 108 | 0 |
contexts.remove(context); |
| 109 |
} |
|
| 110 |
} |
|
| 111 | 0 |
this.indexer.remove(o);
|
| 112 |
} |
|
| 113 | 0 |
this.fireTreeStructureChanged();
|
| 114 |
} |
|
| 115 |
|
|
| 116 |
|
|
| 117 |
/**
|
|
| 118 |
* Description of the Method
|
|
| 119 |
*
|
|
| 120 |
* @param o Description of the Parameter
|
|
| 121 |
* @return Description of the Return Value
|
|
| 122 |
*/
|
|
| 123 | 0 |
public Object find(Object o)
|
| 124 |
{
|
|
| 125 | 0 |
return this.indexer.get(o); |
| 126 |
} |
|
| 127 |
|
|
| 128 |
|
|
| 129 |
/**
|
|
| 130 |
* Gets the child attribute of the BeanContextTreeModel object
|
|
| 131 |
*
|
|
| 132 |
* @param parent Description of the Parameter
|
|
| 133 |
* @param index Description of the Parameter
|
|
| 134 |
* @return The child value
|
|
| 135 |
*/
|
|
| 136 | 0 |
public Object getChild(Object parent, int index) |
| 137 |
{
|
|
| 138 | 0 |
Collection c = (Collection) parent; |
| 139 | 0 |
int i = 0;
|
| 140 | 0 |
Object result = null;
|
| 141 | 0 |
for (Iterator it = c.iterator(); it.hasNext(); i++)
|
| 142 |
{
|
|
| 143 | 0 |
result = it.next(); |
| 144 | 0 |
if (i == index)
|
| 145 |
{
|
|
| 146 | 0 |
break;
|
| 147 |
} |
|
| 148 | 0 |
result = null;
|
| 149 |
} |
|
| 150 | 0 |
return result;
|
| 151 |
} |
|
| 152 |
|
|
| 153 |
|
|
| 154 |
/**
|
|
| 155 |
* Gets the childCount attribute of the BeanContextTreeModel object
|
|
| 156 |
*
|
|
| 157 |
* @param parent Description of the Parameter
|
|
| 158 |
* @return The childCount value
|
|
| 159 |
*/
|
|
| 160 | 0 |
public int getChildCount(Object parent) |
| 161 |
{
|
|
| 162 | 0 |
Collection c = (Collection) parent; |
| 163 | 0 |
return c.size();
|
| 164 |
} |
|
| 165 |
|
|
| 166 |
|
|
| 167 |
/**
|
|
| 168 |
* Gets the indexOfChild attribute of the BeanContextTreeModel object
|
|
| 169 |
*
|
|
| 170 |
* @param parent Description of the Parameter
|
|
| 171 |
* @param child Description of the Parameter
|
|
| 172 |
* @return The indexOfChild value
|
|
| 173 |
*/
|
|
| 174 | 0 |
public int getIndexOfChild(Object parent, Object child) |
| 175 |
{
|
|
| 176 | 0 |
Collection c = (Collection) parent; |
| 177 | 0 |
int i = 0;
|
| 178 | 0 |
int index = -1;
|
| 179 | 0 |
for (Iterator it = c.iterator(); it.hasNext(); i++)
|
| 180 |
{
|
|
| 181 | 0 |
if (it.next().equals(child))
|
| 182 |
{
|
|
| 183 | 0 |
index = i; |
| 184 | 0 |
break;
|
| 185 |
} |
|
| 186 |
} |
|
| 187 | 0 |
return index;
|
| 188 |
} |
|
| 189 |
|
|
| 190 |
|
|
| 191 |
/**
|
|
| 192 |
* Gets the root attribute of the BeanContextTreeModel object
|
|
| 193 |
*
|
|
| 194 |
* @return The root value
|
|
| 195 |
*/
|
|
| 196 | 0 |
public Object getRoot()
|
| 197 |
{
|
|
| 198 | 0 |
return this.root; |
| 199 |
} |
|
| 200 |
|
|
| 201 |
|
|
| 202 |
/**
|
|
| 203 |
* Gets the leaf attribute of the BeanContextTreeModel object
|
|
| 204 |
*
|
|
| 205 |
* @param node Description of the Parameter
|
|
| 206 |
* @return The leaf value
|
|
| 207 |
*/
|
|
| 208 | 0 |
public boolean isLeaf(Object node) |
| 209 |
{
|
|
| 210 | 0 |
Collection c = (Collection) node; |
| 211 | 0 |
return (c.size() == 0);
|
| 212 |
} |
|
| 213 |
|
|
| 214 |
|
|
| 215 |
/**
|
|
| 216 |
* Description of the Method
|
|
| 217 |
*
|
|
| 218 |
* @param tml Description of the Parameter
|
|
| 219 |
*/
|
|
| 220 | 0 |
public void removeTreeModelListener(TreeModelListener tml) |
| 221 |
{
|
|
| 222 | 0 |
this.listeners.remove(tml);
|
| 223 |
} |
|
| 224 |
|
|
| 225 |
|
|
| 226 |
/**
|
|
| 227 |
* Sets the indexer attribute of the BeanContextTreeModel object
|
|
| 228 |
*
|
|
| 229 |
* @param indexer The new indexer value
|
|
| 230 |
*/
|
|
| 231 | 0 |
public void setIndexer(ObjectIndexer indexer) |
| 232 |
{
|
|
| 233 | 0 |
this.indexer = indexer;
|
| 234 |
} |
|
| 235 |
|
|
| 236 |
|
|
| 237 |
/**
|
|
| 238 |
* Description of the Method
|
|
| 239 |
*
|
|
| 240 |
* @param path Description of the Parameter
|
|
| 241 |
* @param newValue Description of the Parameter
|
|
| 242 |
*/
|
|
| 243 | 0 |
public void valueForPathChanged(TreePath path, Object newValue) |
| 244 |
{
|
|
| 245 | 0 |
throw new UnsupportedOperationException("Not yet implemented"); |
| 246 |
} |
|
| 247 |
|
|
| 248 |
|
|
| 249 |
/**
|
|
| 250 |
* Description of the Method
|
|
| 251 |
*
|
|
| 252 |
* @param parent Description of the Parameter
|
|
| 253 |
* @param o Description of the Parameter
|
|
| 254 |
* @return Description of the Return Value
|
|
| 255 |
*/
|
|
| 256 | 0 |
protected Object[] computePath(BeanContext parent, Object o)
|
| 257 |
{
|
|
| 258 | 0 |
ArrayList paths = new ArrayList();
|
| 259 | 0 |
BeanContext current = parent; |
| 260 | 0 |
paths.add(parent); |
| 261 | 0 |
paths.add(o); |
| 262 | 0 |
while (this.root.equals(current.getBeanContext())) |
| 263 |
{
|
|
| 264 | 0 |
current = current.getBeanContext(); |
| 265 | 0 |
paths.add(0, current); |
| 266 |
} |
|
| 267 | 0 |
return paths.toArray();
|
| 268 |
} |
|
| 269 |
|
|
| 270 |
|
|
| 271 |
/** Description of the Method */
|
|
| 272 | 0 |
protected void fireTreeNodesChanged() |
| 273 |
{
|
|
| 274 | 0 |
TreeModelEvent event = new TreeModelEvent(this, new TreePath(this.root)); |
| 275 | 0 |
Object[] copy = this.listeners.toArray();
|
| 276 | 0 |
for (int i = 0; i < copy.length; i++) |
| 277 |
{
|
|
| 278 | 0 |
TreeModelListener listener = (TreeModelListener) copy[i]; |
| 279 | 0 |
listener.treeNodesChanged(event); |
| 280 |
} |
|
| 281 |
} |
|
| 282 |
|
|
| 283 |
|
|
| 284 |
/** Description of the Method */
|
|
| 285 | 0 |
protected void fireTreeNodesInserted() |
| 286 |
{
|
|
| 287 | 0 |
TreeModelEvent event = new TreeModelEvent(this, new TreePath(this.root)); |
| 288 | 0 |
Object[] copy = this.listeners.toArray();
|
| 289 | 0 |
for (int i = 0; i < copy.length; i++) |
| 290 |
{
|
|
| 291 | 0 |
TreeModelListener listener = (TreeModelListener) copy[i]; |
| 292 | 0 |
listener.treeNodesInserted(event); |
| 293 |
} |
|
| 294 |
} |
|
| 295 |
|
|
| 296 |
|
|
| 297 |
/** Description of the Method */
|
|
| 298 | 0 |
protected void fireTreeNodesRemoved() |
| 299 |
{
|
|
| 300 | 0 |
TreeModelEvent event = new TreeModelEvent(this, new TreePath(this.root)); |
| 301 | 0 |
Object[] copy = this.listeners.toArray();
|
| 302 | 0 |
for (int i = 0; i < copy.length; i++) |
| 303 |
{
|
|
| 304 | 0 |
TreeModelListener listener = (TreeModelListener) copy[i]; |
| 305 | 0 |
listener.treeNodesRemoved(event); |
| 306 |
} |
|
| 307 |
} |
|
| 308 |
|
|
| 309 |
|
|
| 310 |
/** Description of the Method */
|
|
| 311 | 0 |
protected void fireTreeStructureChanged() |
| 312 |
{
|
|
| 313 | 0 |
TreeModelEvent event = new TreeModelEvent(this, new TreePath(this.root)); |
| 314 | 0 |
Object[] copy = this.listeners.toArray();
|
| 315 | 0 |
for (int i = 0; i < copy.length; i++) |
| 316 |
{
|
|
| 317 | 0 |
TreeModelListener listener = (TreeModelListener) copy[i]; |
| 318 | 0 |
listener.treeStructureChanged(event); |
| 319 |
} |
|
| 320 |
} |
|
| 321 |
} |
|
| 322 |
|
|
||||||||||