metasim 01/01/11 14:30:12
Modified: src/antidote/org/apache/tools/ant/gui/acs
ACSTreeNodeElement.java ElementTreeModel.java
Log:
Started rewrite of tree model to remove ACS dependency on TreeNode.
Revision Changes Path
1.5 +2 -120
jakarta-ant/src/antidote/org/apache/tools/ant/gui/acs/ACSTreeNodeElement.java
Index: ACSTreeNodeElement.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/antidote/org/apache/tools/ant/gui/acs/ACSTreeNodeElement.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ACSTreeNodeElement.java 2001/01/11 19:24:32 1.4
+++ ACSTreeNodeElement.java 2001/01/11 22:30:02 1.5
@@ -63,127 +63,9 @@
/**
* Abstract base class for all ACSElement classes that are also tree node.
*
- * @version $Revision: 1.4 $
+ * @version $Revision: 1.5 $
* @author Simeon Fitch
*/
-public abstract class ACSTreeNodeElement extends ACSElement
- implements TreeNode {
+public abstract class ACSTreeNodeElement extends ACSElement {
- /** Cache of TreeNode only children. */
- private List _treeNodeCache = null;
-
- /**
- * Get the cache of TreeNode only children.
- *
- * @return List of TreeNodes that are children.
- */
- private List getCache() {
- if(_treeNodeCache == null) {
- _treeNodeCache = new ArrayList();
-
- // XXX this crazy casting is to get around an
- // inconsistency between jikes and javac whereby
- // the call without this cast when compiled with
- // jikes causes an IllegalAccessException
- // because the implementation of getLength() and
- // item() are actually in a package only class
- // in the Sun implementation classes.
- int len = ((NodeList)this).getLength();
-
- for(int i = 0; i < len; i++) {
- Object n = ((NodeList)this).item(i);
-
- if(n instanceof TreeNode) {
- _treeNodeCache.add(n);
- }
- }
- }
-
- return _treeNodeCache;
- }
- /**
- * Returns the child <code>TreeNode</code> at index
- * <code>childIndex</code>.
- */
- public TreeNode getChildAt(int childIndex) {
- List nodes = getCache();
- return (TreeNode) nodes.get(childIndex);
- }
-
- /**
- * Returns the number of children <code>TreeNode</code>s the receiver
- * contains.
- */
- public int getChildCount() {
- List nodes = getCache();
- return nodes.size();
- }
-
- /**
- * Returns the parent <code>TreeNode</code> of the receiver.
- */
- public TreeNode getParent() {
- // XXX this barfs becase a different "getParent()" is in Node
- // interface. Need to fix...
- return (TreeNode) getParent();
- }
-
- /**
- * Returns the index of <code>node</code> in the receivers children.
- * If the receiver does not contain <code>node</code>, -1 will be
- * returned.
- */
- public int getIndex(TreeNode node) {
- List nodes = getCache();
- return nodes.indexOf(node);
- }
-
- /**
- * Returns true if the receiver allows children.
- */
- public boolean getAllowsChildren() {
- return true;
- }
-
- /**
- * Returns true if the receiver is a leaf.
- */
- public boolean isLeaf() {
- List nodes = getCache();
- return nodes.size() <= 0;
- }
-
- /**
- * Returns the children of the reciever as an Enumeration.
- */
- public Enumeration children() {
- return new NodeEnum();
- }
-
- /** Internal iterator for the child nodes. */
- private class NodeEnum implements Enumeration {
- /** Current child index. */
- private int _index = 0;
-
- /**
- * Determine if there are more elements to visit.
- *
- * @return True if nextElement() can be called, false otherwise.
- */
- public boolean hasMoreElements() {
- List nodes = getCache();
- return _index < nodes.size();
- }
-
- /**
- * Get the next element. hasMoreElements() must currently return
true.
- *
- * @return Next element
- */
- public Object nextElement() {
- List nodes = getCache();
- return nodes.get(_index++);
- }
-
- }
}
1.3 +154 -5
jakarta-ant/src/antidote/org/apache/tools/ant/gui/acs/ElementTreeModel.java
Index: ElementTreeModel.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/antidote/org/apache/tools/ant/gui/acs/ElementTreeModel.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ElementTreeModel.java 2001/01/03 14:18:17 1.2
+++ ElementTreeModel.java 2001/01/11 22:30:05 1.3
@@ -54,18 +54,167 @@
package org.apache.tools.ant.gui.acs;
-import javax.swing.tree.DefaultTreeModel;
+import javax.swing.tree.*;
+import javax.swing.event.TreeModelListener;
+import javax.swing.event.TreeModelEvent;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
import org.apache.tools.ant.gui.acs.ACSProjectElement;
+import java.util.*;
-
/**
* Provides a tree model view of the Project class. XXX This
* is a major hack right now that needs to be cleaned up.
*
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
* @author Simeon H.K. Fitch */
-public class ElementTreeModel extends DefaultTreeModel {
+public class ElementTreeModel implements TreeModel {
+ /** Root of the tree. */
+ private ACSProjectElement _root = null;
+ /** List of listeners. */
+ private List _listeners = new ArrayList();
+
public ElementTreeModel(ACSProjectElement root) {
- super(root);
+ _root = root;
+ }
+
+ /**
+ * Returns the root of the tree. Returns null only if the tree has
+ * no nodes.
+ *
+ * @return the root of the tree
+ */
+ public Object getRoot() {
+ return _root;
+ }
+
+ /**
+ * Gets the set of children that this tree model is interested in.
+ *
+ * @param parent Parent to extract children from.
+ */
+ private List getChildren(Node parent) {
+ NodeList children = parent.getChildNodes();
+ int length = children.getLength();
+
+ List retval = new ArrayList(length);
+ for(int i = 0; i < length; i++) {
+ // XXX This is where we will eventually add dynamic filtering
+ // capabilities.
+ Node n = children.item(i);
+ if(n instanceof ACSTreeNodeElement) {
+ retval.add(n);
+ }
+ }
+
+ return retval;
+ }
+
+ /**
+ * Returns the child of <I>parent</I> at index <I>index</I> in the
parent's
+ * child array. <I>parent</I> must be a node previously obtained from
+ * this data source. This should not return null if <i>index</i>
+ * is a valid index for <i>parent</i> (that is <i>index</i> >= 0 &&
+ * <i>index</i> < getChildCount(<i>parent</i>)).
+ *
+ * @param parent a node in the tree, obtained from this data source
+ * @return the child of <I>parent</I> at index <I>index</I>
+ */
+ public Object getChild(Object parent, int index) {
+ if(parent instanceof Node) {
+ Node n = (Node) parent;
+ return getChildren(n).get(index);
+ }
+ else {
+ return null;
+ }
+ }
+
+
+ /**
+ * Returns the number of children of <I>parent</I>. Returns 0 if the
node
+ * is a leaf or if it has no children. <I>parent</I> must be a node
+ * previously obtained from this data source.
+ *
+ * @param parent a node in the tree, obtained from this data source
+ * @return the number of children of the node <I>parent</I>
+ */
+ public int getChildCount(Object parent) {
+ if(parent instanceof Node) {
+ Node n = (Node) parent;
+ return getChildren(n).size();
+ }
+ else {
+ return 0;
+ }
}
+
+ /**
+ * Returns true if <I>node</I> is a leaf. It is possible for this method
+ * to return false even if <I>node</I> has no children. A directory in a
+ * filesystem, for example, may contain no files; the node representing
+ * the directory is not a leaf, but it also has no children.
+ *
+ * @param node a node in the tree, obtained from this data source
+ * @return true if <I>node</I> is a leaf
+ */
+ public boolean isLeaf(Object node) {
+ if(node instanceof Node) {
+ Node n = (Node) node;
+ return getChildren(n).size() == 0;
+ }
+ else {
+ return true;
+ }
+
+ }
+
+ /**
+ * Returns the index of child in parent.
+ */
+ public int getIndexOfChild(Object parent, Object child) {
+ if(parent instanceof Node && child instanceof Node) {
+ Node n = (Node) parent;
+ List children = getChildren(n);
+ int count = children.size();
+ for(int i = 0; i < count; i++) {
+ if(children.get(i) == child) return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * Messaged when the user has altered the value for the item identified
+ * by <I>path</I> to <I>newValue</I>. If <I>newValue</I> signifies
+ * a truly new value the model should post a treeNodesChanged
+ * event.
+ *
+ * @param path path to the node that the user has altered.
+ * @param newValue the new value from the TreeCellEditor.
+ */
+ public void valueForPathChanged(TreePath path, Object newValue) {
+ }
+
+
+ /**
+ * Adds a listener for the TreeModelEvent posted after the tree changes.
+ *
+ * @see #removeTreeModelListener
+ * @param l the listener to add
+ */
+ public void addTreeModelListener(TreeModelListener l) {
+ _listeners.add(l);
+ }
+
+ /**
+ * Removes a listener previously added with
<B>addTreeModelListener()</B>.
+ *
+ * @see #addTreeModelListener
+ * @param l the listener to remove
+ */
+ public void removeTreeModelListener(TreeModelListener l) {
+ _listeners.remove(l);
+ }
+
}