home *** CD-ROM | disk | FTP | other *** search
/ Popular Software (Premium Edition) / mycd.iso / INTERNET / NETSCAP4.06 / CP32E406.EXE / netcast.z / ncjava10.jar / netscape / palomar / util / TreeImpl.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  3.5 KB  |  124 lines

  1. package netscape.palomar.util;
  2.  
  3. import java.util.Enumeration;
  4. import java.util.Vector;
  5.  
  6. public class TreeImpl implements Tree, Cloneable {
  7.    protected Tree _parent;
  8.    private Vector _children = new Vector();
  9.  
  10.    public Tree getParent() {
  11.       return this._parent;
  12.    }
  13.  
  14.    public void setParent(Tree o) throws CascadedException {
  15.       this._parent = o;
  16.    }
  17.  
  18.    public void addChildQuiet(Tree oChild) throws CascadedException {
  19.       if (oChild.getParent() != null) {
  20.          CascadedException pex = new CascadedException(37);
  21.          pex.addToken("child", oChild.toString());
  22.          throw pex;
  23.       } else {
  24.          this._children.addElement(oChild);
  25.          oChild.setParent(this);
  26.       }
  27.    }
  28.  
  29.    public void insertChildQuiet(Tree oChild, int index) throws CascadedException {
  30.       if (oChild.getParent() != null) {
  31.          CascadedException pex = new CascadedException(37);
  32.          pex.addToken("child", oChild.toString());
  33.          throw pex;
  34.       } else {
  35.          this._children.insertElementAt(oChild, index);
  36.          oChild.setParent(this);
  37.       }
  38.    }
  39.  
  40.    public void removeChildQuiet(Tree oChild) throws CascadedException {
  41.       if (oChild.getParent() != this) {
  42.          CascadedException pex = new CascadedException(39);
  43.          pex.addToken("child", oChild.toString());
  44.          throw pex;
  45.       } else {
  46.          this._children.removeElement(oChild);
  47.          oChild.setParent((Tree)null);
  48.       }
  49.    }
  50.  
  51.    public int numChildren() {
  52.       return this._children.size();
  53.    }
  54.  
  55.    public Tree childAt(int index) {
  56.       return (Tree)this._children.elementAt(index);
  57.    }
  58.  
  59.    public int indexOf(Tree child) throws CascadedException {
  60.       return this._children.indexOf(child);
  61.    }
  62.  
  63.    public Enumeration getChildren() {
  64.       return this._children.elements();
  65.    }
  66.  
  67.    public Tree cloneTree() throws CascadedException {
  68.       try {
  69.          Tree newTree = (Tree)super.clone();
  70.          newTree.beenCloned();
  71.          return newTree;
  72.       } catch (Exception ex) {
  73.          CascadedException c = new CascadedException(40, ex);
  74.          c.addToken("parent", this.toString());
  75.          throw c;
  76.       }
  77.    }
  78.  
  79.    public void beenCloned() throws CascadedException {
  80.       try {
  81.          this._parent = null;
  82.          if (this._children != null) {
  83.             Vector oldChildren = this._children;
  84.             this._children = new Vector();
  85.             int last = oldChildren.size();
  86.  
  87.             for(int i = 0; i < last; ++i) {
  88.                Tree oChild = (Tree)oldChildren.elementAt(i);
  89.                this.addChildQuiet(oChild.cloneTree());
  90.             }
  91.  
  92.          }
  93.       } catch (Exception ex) {
  94.          CascadedException c = new CascadedException(41, ex);
  95.          c.addToken("parent", this.toString());
  96.          throw c;
  97.       }
  98.    }
  99.  
  100.    public Tree findCommonAncestor(Tree otherElement) throws CascadedException {
  101.       for(Tree grandParent = this; grandParent != null; grandParent = grandParent.getParent()) {
  102.          for(Tree search2 = otherElement; search2 != null; search2 = search2.getParent()) {
  103.             if (grandParent == search2) {
  104.                return grandParent;
  105.             }
  106.          }
  107.       }
  108.  
  109.       return null;
  110.    }
  111.  
  112.    public void flush() throws CascadedException {
  113.       Vector myChildren = this._children;
  114.       this._children = null;
  115.       this._parent = null;
  116.       int last = myChildren.size();
  117.  
  118.       for(int i = 0; i < last; ++i) {
  119.          ((TreeImpl)myChildren.elementAt(i)).flush();
  120.       }
  121.  
  122.    }
  123. }
  124.