home *** CD-ROM | disk | FTP | other *** search
- package netscape.palomar.util;
-
- import java.util.Enumeration;
- import java.util.Vector;
-
- public class TreeImpl implements Tree, Cloneable {
- protected Tree _parent;
- private Vector _children = new Vector();
-
- public Tree getParent() {
- return this._parent;
- }
-
- public void setParent(Tree o) throws CascadedException {
- this._parent = o;
- }
-
- public void addChildQuiet(Tree oChild) throws CascadedException {
- if (oChild.getParent() != null) {
- CascadedException pex = new CascadedException(37);
- pex.addToken("child", oChild.toString());
- throw pex;
- } else {
- this._children.addElement(oChild);
- oChild.setParent(this);
- }
- }
-
- public void insertChildQuiet(Tree oChild, int index) throws CascadedException {
- if (oChild.getParent() != null) {
- CascadedException pex = new CascadedException(37);
- pex.addToken("child", oChild.toString());
- throw pex;
- } else {
- this._children.insertElementAt(oChild, index);
- oChild.setParent(this);
- }
- }
-
- public void removeChildQuiet(Tree oChild) throws CascadedException {
- if (oChild.getParent() != this) {
- CascadedException pex = new CascadedException(39);
- pex.addToken("child", oChild.toString());
- throw pex;
- } else {
- this._children.removeElement(oChild);
- oChild.setParent((Tree)null);
- }
- }
-
- public int numChildren() {
- return this._children.size();
- }
-
- public Tree childAt(int index) {
- return (Tree)this._children.elementAt(index);
- }
-
- public int indexOf(Tree child) throws CascadedException {
- return this._children.indexOf(child);
- }
-
- public Enumeration getChildren() {
- return this._children.elements();
- }
-
- public Tree cloneTree() throws CascadedException {
- try {
- Tree newTree = (Tree)super.clone();
- newTree.beenCloned();
- return newTree;
- } catch (Exception ex) {
- CascadedException c = new CascadedException(40, ex);
- c.addToken("parent", this.toString());
- throw c;
- }
- }
-
- public void beenCloned() throws CascadedException {
- try {
- this._parent = null;
- if (this._children != null) {
- Vector oldChildren = this._children;
- this._children = new Vector();
- int last = oldChildren.size();
-
- for(int i = 0; i < last; ++i) {
- Tree oChild = (Tree)oldChildren.elementAt(i);
- this.addChildQuiet(oChild.cloneTree());
- }
-
- }
- } catch (Exception ex) {
- CascadedException c = new CascadedException(41, ex);
- c.addToken("parent", this.toString());
- throw c;
- }
- }
-
- public Tree findCommonAncestor(Tree otherElement) throws CascadedException {
- for(Tree grandParent = this; grandParent != null; grandParent = grandParent.getParent()) {
- for(Tree search2 = otherElement; search2 != null; search2 = search2.getParent()) {
- if (grandParent == search2) {
- return grandParent;
- }
- }
- }
-
- return null;
- }
-
- public void flush() throws CascadedException {
- Vector myChildren = this._children;
- this._children = null;
- this._parent = null;
- int last = myChildren.size();
-
- for(int i = 0; i < last; ++i) {
- ((TreeImpl)myChildren.elementAt(i)).flush();
- }
-
- }
- }
-