001: package jdepend.swingui;
002:
003: import java.util.*;
004: import javax.swing.tree.*;
005: import javax.swing.event.*;
006:
007: /**
008: * The <code>DependTreeModel</code> class defines the data model being
009: * observed by a <code>DependTree</code> instance.
010: *
011: * @author <b>Mike Clark</b>
012: * @author Clarkware Consulting, Inc.
013: */
014:
015: public class DependTreeModel implements TreeModel {
016:
017: private PackageNode root;
018:
019: private Vector listeners;
020:
021: /**
022: * Constructs a <code>DependTreeModel</code> with the specified root
023: * package node.
024: *
025: * @param root Root package node.
026: */
027: public DependTreeModel(PackageNode root) {
028: this .root = root;
029: listeners = new Vector();
030: }
031:
032: /**
033: * Returns the root of the tree.
034: *
035: * @return The root of the tree, or <code>null</code> if the tree has no
036: * nodes.
037: */
038: public Object getRoot() {
039: return root;
040: }
041:
042: /**
043: * Returns the child of the specified parent at the specified index in the
044: * parent's child collection.
045: * <p>
046: * The specified parent must be a node previously obtained from this data
047: * source.
048: *
049: * @param parent A node in the tree, obtained from this data source.
050: * @param index Index of child in the parent's child collection.
051: * @return Child.
052: */
053: public Object getChild(Object parent, int index) {
054:
055: Object answer = null;
056: ArrayList children;
057:
058: if (parent instanceof PackageNode) {
059: children = ((PackageNode) parent).getChildren();
060:
061: if (children != null) {
062: if (index < children.size()) {
063: answer = children.get(index);
064: }
065: }
066: }
067:
068: return answer;
069: }
070:
071: /**
072: * Returns the number of children for the specified parent.
073: * <p>
074: * The specified parent must be a node previously obtained from this data
075: * source.
076: *
077: * @param parent A node in the tree, obtained from this data source.
078: * @return The number of children of the specified parent, or 0 if the
079: * parent is a leaf node or if it has no children.
080: */
081: public int getChildCount(Object parent) {
082:
083: int answer = 0;
084: ArrayList children;
085:
086: if (parent instanceof PackageNode) {
087: children = ((PackageNode) parent).getChildren();
088:
089: if (children != null) {
090: answer = children.size();
091: }
092: }
093:
094: return answer;
095: }
096:
097: /**
098: * Determines whether the specified tree node is a leaf node.
099: *
100: * @param o A node in the tree, obtained from this data source.
101: * @return <code>true</code> if the node is a leaf; <code>false</code>
102: * otherwise.
103: */
104: public boolean isLeaf(Object o) {
105:
106: boolean answer = true;
107:
108: if (o instanceof PackageNode) {
109: PackageNode node = (PackageNode) o;
110: return node.isLeaf();
111: }
112:
113: return answer;
114: }
115:
116: /**
117: * Callback method triggered when the value for the item specified by
118: * <i>path </i> has changed to <i>newValue </i>.
119: *
120: * @param path Path to the node that has changed.
121: * @param newValue The new value of the node.
122: */
123: public void valueForPathChanged(TreePath path, Object newValue) {
124: // do nothing
125: }
126:
127: /**
128: * Returns the index of the specified child within the specified parent.
129: *
130: * @param parent Parent node.
131: * @param child Child node.
132: * @return Index of child within parent.
133: */
134: public int getIndexOfChild(Object parent, Object child) {
135: int answer = -1;
136: ArrayList children = null;
137:
138: if (parent instanceof PackageNode) {
139: children = ((PackageNode) parent).getChildren();
140:
141: if (children != null) {
142: answer = children.indexOf(child);
143: }
144: }
145:
146: return answer;
147: }
148:
149: /**
150: * Adds a listener for the <code>TreeModelEvent</code> posted after the
151: * tree changes.
152: *
153: * @param l The listener to add.
154: */
155: public void addTreeModelListener(TreeModelListener l) {
156:
157: if ((l != null) && !listeners.contains(l)) {
158: listeners.addElement(l);
159: }
160: }
161:
162: /**
163: * Removes a listener for <code>TreeModelEvent</code>s.
164: *
165: * @param l The listener to remove.
166: */
167: public void removeTreeModelListener(TreeModelListener l) {
168: listeners.removeElement(l);
169: }
170: }
|