001 /*
002 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing.tree;
026
027 import javax.swing.event.*;
028
029 /**
030 * The model used by <code>JTree</code>.
031 * <p>
032 * <code>JTree</code> and its related classes make extensive use of
033 * <code>TreePath</code>s for indentifying nodes in the <code>TreeModel</code>.
034 * If a <code>TreeModel</code> returns the same object, as compared by
035 * <code>equals</code>, at two different indices under the same parent
036 * than the resulting <code>TreePath</code> objects will be considered equal
037 * as well. Some implementations may assume that if two
038 * <code>TreePath</code>s are equal, they identify the same node. If this
039 * condition is not met, painting problems and other oddities may result.
040 * In other words, if <code>getChild</code> for a given parent returns
041 * the same Object (as determined by <code>equals</code>) problems may
042 * result, and it is recommended you avoid doing this.
043 * <p>
044 * Similarly <code>JTree</code> and its related classes place
045 * <code>TreePath</code>s in <code>Map</code>s. As such if
046 * a node is requested twice, the return values must be equal
047 * (using the <code>equals</code> method) and have the same
048 * <code>hashCode</code>.
049 * <p>
050 * For further information on tree models,
051 * including an example of a custom implementation,
052 * see <a
053 href="http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html">How to Use Trees</a>
054 * in <em>The Java Tutorial.</em>
055 *
056 * @see TreePath
057 *
058 * @version 1.32 05/05/07
059 * @author Rob Davis
060 * @author Ray Ryan
061 */
062 public interface TreeModel {
063
064 /**
065 * Returns the root of the tree. Returns <code>null</code>
066 * only if the tree has no nodes.
067 *
068 * @return the root of the tree
069 */
070 public Object getRoot();
071
072 /**
073 * Returns the child of <code>parent</code> at index <code>index</code>
074 * in the parent's
075 * child array. <code>parent</code> must be a node previously obtained
076 * from this data source. This should not return <code>null</code>
077 * if <code>index</code>
078 * is a valid index for <code>parent</code> (that is <code>index >= 0 &&
079 * index < getChildCount(parent</code>)).
080 *
081 * @param parent a node in the tree, obtained from this data source
082 * @return the child of <code>parent</code> at index <code>index</code>
083 */
084 public Object getChild(Object parent, int index);
085
086 /**
087 * Returns the number of children of <code>parent</code>.
088 * Returns 0 if the node
089 * is a leaf or if it has no children. <code>parent</code> must be a node
090 * previously obtained from this data source.
091 *
092 * @param parent a node in the tree, obtained from this data source
093 * @return the number of children of the node <code>parent</code>
094 */
095 public int getChildCount(Object parent);
096
097 /**
098 * Returns <code>true</code> if <code>node</code> is a leaf.
099 * It is possible for this method to return <code>false</code>
100 * even if <code>node</code> has no children.
101 * A directory in a filesystem, for example,
102 * may contain no files; the node representing
103 * the directory is not a leaf, but it also has no children.
104 *
105 * @param node a node in the tree, obtained from this data source
106 * @return true if <code>node</code> is a leaf
107 */
108 public boolean isLeaf(Object node);
109
110 /**
111 * Messaged when the user has altered the value for the item identified
112 * by <code>path</code> to <code>newValue</code>.
113 * If <code>newValue</code> signifies a truly new value
114 * the model should post a <code>treeNodesChanged</code> event.
115 *
116 * @param path path to the node that the user has altered
117 * @param newValue the new value from the TreeCellEditor
118 */
119 public void valueForPathChanged(TreePath path, Object newValue);
120
121 /**
122 * Returns the index of child in parent. If either <code>parent</code>
123 * or <code>child</code> is <code>null</code>, returns -1.
124 * If either <code>parent</code> or <code>child</code> don't
125 * belong to this tree model, returns -1.
126 *
127 * @param parent a node in the tree, obtained from this data source
128 * @param child the node we are interested in
129 * @return the index of the child in the parent, or -1 if either
130 * <code>child</code> or <code>parent</code> are <code>null</code>
131 * or don't belong to this tree model
132 */
133 public int getIndexOfChild(Object parent, Object child);
134
135 //
136 // Change Events
137 //
138
139 /**
140 * Adds a listener for the <code>TreeModelEvent</code>
141 * posted after the tree changes.
142 *
143 * @param l the listener to add
144 * @see #removeTreeModelListener
145 */
146 void addTreeModelListener(TreeModelListener l);
147
148 /**
149 * Removes a listener previously added with
150 * <code>addTreeModelListener</code>.
151 *
152 * @see #addTreeModelListener
153 * @param l the listener to remove
154 */
155 void removeTreeModelListener(TreeModelListener l);
156
157 }
|