TreeNode.java
00001 package edu.virtualschool.jwaa;
00002
00003 import java.util.ArrayList;
00004 import java.util.Iterator;
00005
00006 import org.apache.log4j.Logger;
00007
00024 public abstract class TreeNode
00025 {
00026 private TreeNode parentNode = null;
00027 private final ArrayList childNodeList = new ArrayList();
00028
00029 final static Logger logger = Logger.getLogger(TreeNode.class.getName());
00030
00034 public TreeNode() { }
00039 public final void addChildNode(TreeNode child)
00040 {
00041 if (child != null)
00042 child.setParentNode(this);
00043 }
00044 public final void addChildNodeArray(TreeNode[] c)
00045 {
00046 for (int i = 0; i < c.length; i++)
00047 addChildNode(c[i]);
00048 }
00049 public final void removeChildNode(TreeNode child)
00050 {
00051 if (child != null)
00052 child.setParentNode(null);
00053 }
00054 public final void removeAllChildNodes()
00055 {
00056 for (Iterator iterator = childrenIterator(); iterator.hasNext(); )
00057 {
00058 TreeNode child = (TreeNode)iterator.next();
00059 removeChildNode(child);
00060 }
00061 }
00066 public final TreeNode getParentNode() { return parentNode; }
00070 public final void detachFromParent()
00071 {
00072 setParentNode(null);
00073 }
00079 private final TreeNode setParentNode(TreeNode newParent)
00080 {
00081 TreeNode oldParent = parentNode;
00082 if (parentNode == newParent)
00083 return parentNode;
00084 if (oldParent != null && oldParent != newParent)
00085 {
00086 logger.warn("reparenting "+this+"\nfrom "+oldParent+"\nto "+newParent);
00087 oldParent.childNodeList.remove(this);
00088 }
00089 if (newParent != null)
00090 newParent.childNodeList.add(this);
00091 this.parentNode = newParent;
00092 return oldParent;
00093 }
00098 public final int getDepth()
00099 {
00100 int depth = 0;
00101 for(TreeNode node = this; node != null; node = node.parentNode)
00102 depth++;
00103 return depth;
00104 }
00109 public final ArrayList getRootPath()
00110 {
00111 ArrayList path = new ArrayList();
00112 for (TreeNode node = this; node != null; node = node.parentNode)
00113 path.add(node);
00114 return path;
00115 }
00120 public final TreeNode getRootNode()
00121 {
00122 TreeNode node = null;
00123 for (node = this; node != null; node = node.parentNode)
00124 if (node.parentNode == null)
00125 break;
00126 return node;
00127 }
00132 public final Iterator childrenIterator()
00133 {
00134 return childNodeList.iterator();
00135 }
00140
00141
00142
00143
00149 public final boolean isChildNodeOf(TreeNode thatNode)
00150 {
00151 for (TreeNode thisNode = this; thisNode != null; thisNode = thisNode.parentNode)
00152 if (thisNode == thatNode) return true;
00153 return false;
00154 }
00159 public final boolean isRootNode() { return parentNode == null; }
00166 public final TreeNode nextSiblingNode()
00167 {
00168 if (parentNode == null) return null;
00169 ArrayList siblings = parentNode.childNodeList;
00170 int index = siblings.indexOf(this);
00171 if (index+1 < siblings.size()) return (TreeNode)siblings.get(index+1);
00172 else return null;
00173 }
00180 public final TreeNode prevSiblingNode()
00181 {
00182 if (parentNode == null) return null;
00183 ArrayList siblings = parentNode.childNodeList;
00184 int index = siblings.indexOf(this);
00185 if (index-1 >= 0) return (TreeNode)siblings.get(index-1);
00186 else return parentNode;
00187 }
00192 public final void accept(Visitor visitor)
00193 {
00194 visitor.visit(this);
00195 if (visitor.isDone()) return;
00196 for (Iterator e = childNodeList.iterator(); e.hasNext(); )
00197 {
00198 TreeNode child = (TreeNode)e.next();
00199 child.accept(visitor);
00200 }
00201 }
00202 }