KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > tree > TreeModel


1 /*
2  * @(#)TreeModel.java 1.23 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.tree;
8
9 import javax.swing.event.*;
10
11 /**
12  * The interface that defines a suitable data model for a <code>JTree</code>.
13  * For further information on tree models,
14  * including an example of a custom implementation,
15  * see <a
16  href="http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html">How to Use Trees</a>
17  * in <em>The Java Tutorial.</em>
18  * <p>
19  * <code>JTree</code> and its related classes make extensive use of
20  * <code>TreePath</code>s for indentifying nodes in the <code>TreeModel</code>.
21  * If a <code>TreeModel</code> returns the same object, as compared by
22  * <code>equals</code>, at two different indices under the same parent
23  * than the resulting <code>TreePath</code> objects will be considered equal
24  * as well. Some implementations may assume that if two
25  * <code>TreePath</code>s are equal, they identify the same node. If this
26  * condition is not met, painting problems and other oddities may result.
27  * In other words, if <code>getChild</code> for a given parent returns
28  * the same Object (as determined by <code>equals</code>) problems may
29  * result, and it is recommended you avoid doing this.
30  *
31  * @see TreePath
32  *
33  * @version 1.23 12/19/03
34  * @author Rob Davis
35  * @author Ray Ryan
36  */

37 public interface TreeModel
38 {
39
40     /**
41      * Returns the root of the tree. Returns <code>null</code>
42      * only if the tree has no nodes.
43      *
44      * @return the root of the tree
45      */

46     public Object JavaDoc getRoot();
47
48
49     /**
50      * Returns the child of <code>parent</code> at index <code>index</code>
51      * in the parent's
52      * child array. <code>parent</code> must be a node previously obtained
53      * from this data source. This should not return <code>null</code>
54      * if <code>index</code>
55      * is a valid index for <code>parent</code> (that is <code>index >= 0 &&
56      * index < getChildCount(parent</code>)).
57      *
58      * @param parent a node in the tree, obtained from this data source
59      * @return the child of <code>parent</code> at index <code>index</code>
60      */

61     public Object JavaDoc getChild(Object JavaDoc parent, int index);
62
63
64     /**
65      * Returns the number of children of <code>parent</code>.
66      * Returns 0 if the node
67      * is a leaf or if it has no children. <code>parent</code> must be a node
68      * previously obtained from this data source.
69      *
70      * @param parent a node in the tree, obtained from this data source
71      * @return the number of children of the node <code>parent</code>
72      */

73     public int getChildCount(Object JavaDoc parent);
74
75
76     /**
77      * Returns <code>true</code> if <code>node</code> is a leaf.
78      * It is possible for this method to return <code>false</code>
79      * even if <code>node</code> has no children.
80      * A directory in a filesystem, for example,
81      * may contain no files; the node representing
82      * the directory is not a leaf, but it also has no children.
83      *
84      * @param node a node in the tree, obtained from this data source
85      * @return true if <code>node</code> is a leaf
86      */

87     public boolean isLeaf(Object JavaDoc node);
88
89     /**
90       * Messaged when the user has altered the value for the item identified
91       * by <code>path</code> to <code>newValue</code>.
92       * If <code>newValue</code> signifies a truly new value
93       * the model should post a <code>treeNodesChanged</code> event.
94       *
95       * @param path path to the node that the user has altered
96       * @param newValue the new value from the TreeCellEditor
97       */

98     public void valueForPathChanged(TreePath JavaDoc path, Object JavaDoc newValue);
99
100     /**
101      * Returns the index of child in parent. If either <code>parent</code>
102      * or <code>child</code> is <code>null</code>, returns -1.
103      * If either <code>parent</code> or <code>child</code> don't
104      * belong to this tree model, returns -1.
105      *
106      * @param parent a note in the tree, obtained from this data source
107      * @param child the node we are interested in
108      * @return the index of the child in the parent, or -1 if either
109      * <code>child</code> or <code>parent</code> are <code>null</code>
110      * or don't belong to this tree model
111      */

112     public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child);
113
114 //
115
// Change Events
116
//
117

118     /**
119      * Adds a listener for the <code>TreeModelEvent</code>
120      * posted after the tree changes.
121      *
122      * @param l the listener to add
123      * @see #removeTreeModelListener
124      */

125     void addTreeModelListener(TreeModelListener l);
126
127     /**
128      * Removes a listener previously added with
129      * <code>addTreeModelListener</code>.
130      *
131      * @see #addTreeModelListener
132      * @param l the listener to remove
133      */

134     void removeTreeModelListener(TreeModelListener l);
135
136 }
137
Popular Tags