1 /* DefaultTreeModel.java --
2 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.tree;
41 import java.io.IOException;
42 import java.io.ObjectInputStream;
43 import java.io.ObjectOutputStream;
44 import java.io.Serializable;
45 import java.util.EventListener;
47 import javax.swing.event.EventListenerList;
48 import javax.swing.event.TreeModelEvent;
49 import javax.swing.event.TreeModelListener;
53 * @author Andrew Selkirk
55 public class DefaultTreeModel
56 implements Serializable, TreeModel
58 static final long serialVersionUID = -2621068368932566998L;
63 protected TreeNode root = null;
68 protected EventListenerList listenerList = new EventListenerList();
73 protected boolean asksAllowsChildren;
76 * Constructor DefaultTreeModel
79 public DefaultTreeModel(TreeNode root)
85 * Constructor DefaultTreeModel
89 public DefaultTreeModel(TreeNode root, boolean asksAllowsChildren)
92 this.asksAllowsChildren = asksAllowsChildren;
98 * @exception IOException TODO
100 private void writeObject(ObjectOutputStream value0) throws IOException
108 * @exception IOException TODO
109 * @exception ClassNotFoundException TODO
111 private void readObject(ObjectInputStream value0)
112 throws IOException, ClassNotFoundException
121 public boolean asksAllowsChildren()
123 return asksAllowsChildren;
127 * setAsksAllowsChildren
130 public void setAsksAllowsChildren(boolean value)
132 asksAllowsChildren = value; // TODO
139 public void setRoot(TreeNode root)
144 throw new IllegalArgumentException("null root");
156 public Object getRoot()
167 public int getIndexOfChild(Object parent, Object child)
178 public Object getChild(Object node, int idx)
180 if (node instanceof TreeNode)
181 return ((TreeNode)node).getChildAt(idx);
191 public int getChildCount(Object node)
193 if (node instanceof TreeNode)
194 return ((TreeNode)node).getChildCount();
204 public boolean isLeaf(Object node)
206 if (node instanceof TreeNode)
207 return ((TreeNode)node).isLeaf();
224 public void reload(TreeNode value0)
230 * valueForPathChanged
234 public void valueForPathChanged(TreePath value0, Object value1)
245 public void insertNodeInto(MutableTreeNode value0, MutableTreeNode value1,
252 * removeNodeFromParent
255 public void removeNodeFromParent(MutableTreeNode value0)
264 public void nodeChanged(TreeNode value0)
274 public void nodesWereInserted(TreeNode value0, int[] value1)
285 public void nodesWereRemoved(TreeNode value0, int[] value1, Object[] value2)
295 public void nodesChanged(TreeNode value0, int[] value1)
301 * nodeStructureChanged
304 public void nodeStructureChanged(TreeNode value0)
314 public TreeNode[] getPathToRoot(TreeNode value0)
325 protected TreeNode[] getPathToRoot(TreeNode value0, int value1)
331 * Registers a listere to the model.
333 * @param listener the listener to add
335 public void addTreeModelListener(TreeModelListener listener)
337 listenerList.add(TreeModelListener.class, listener);
341 * Removes a listener from the model.
343 * @param listener the listener to remove
345 public void removeTreeModelListener(TreeModelListener listener)
347 listenerList.remove(TreeModelListener.class, listener);
351 * Returns all registered <code>TreeModelListener</code> listeners.
353 * @return an array of listeners.
357 public TreeModelListener[] getTreeModelListeners()
359 return (TreeModelListener[]) listenerList.getListeners(TreeModelListener.class);
363 * fireTreeNodesChanged
365 * @param source the node being changed
366 * @param path the path to the root node
367 * @param childIndices the indices of the changed elements
368 * @param children the changed elements
370 protected void fireTreeNodesChanged(Object source, Object[] path,
371 int[] childIndices, Object[] children)
373 TreeModelEvent event =
374 new TreeModelEvent(source, path, childIndices, children);
375 TreeModelListener[] listeners = getTreeModelListeners();
377 for (int i = listeners.length - 1; i >= 0; --i)
378 listeners[i].treeNodesChanged(event);
382 * fireTreeNodesInserted
384 * @param source the node where new nodes got inserted
385 * @param path the path to the root node
386 * @param childIndices the indices of the new elements
387 * @param children the new elements
389 protected void fireTreeNodesInserted(Object source, Object[] path,
390 int[] childIndices, Object[] children)
392 TreeModelEvent event =
393 new TreeModelEvent(source, path, childIndices, children);
394 TreeModelListener[] listeners = getTreeModelListeners();
396 for (int i = listeners.length - 1; i >= 0; --i)
397 listeners[i].treeNodesInserted(event);
401 * fireTreeNodesRemoved
403 * @param source the node where nodes got removed-
404 * @param path the path to the root node
405 * @param childIndices the indices of the removed elements
406 * @param children the removed elements
408 protected void fireTreeNodesRemoved(Object source, Object[] path,
409 int[] childIndices, Object[] children)
411 TreeModelEvent event =
412 new TreeModelEvent(source, path, childIndices, children);
413 TreeModelListener[] listeners = getTreeModelListeners();
415 for (int i = listeners.length - 1; i >= 0; --i)
416 listeners[i].treeNodesRemoved(event);
420 * fireTreeStructureChanged
422 * @param source the node where the model has changed
423 * @param path the path to the root node
424 * @param childIndices the indices of the affected elements
425 * @param children the affected elements
427 protected void fireTreeStructureChanged(Object source, Object[] path,
428 int[] childIndices, Object[] children)
430 TreeModelEvent event =
431 new TreeModelEvent(source, path, childIndices, children);
432 TreeModelListener[] listeners = getTreeModelListeners();
434 for (int i = listeners.length - 1; i >= 0; --i)
435 listeners[i].treeStructureChanged(event);
439 * Returns the registered listeners of a given type.
441 * @param listenerType the listener type to return
443 * @return an array of listeners
447 public EventListener[] getListeners(Class listenerType)
449 return listenerList.getListeners(listenerType);