home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &… the Search for Life CD 3 / 0_CD-ROM.iso / install / jre1_3 / lib / rt.jar / javax / swing / tree / DefaultMutableTreeNode.class (.txt) < prev    next >
Encoding:
Java Class File  |  1979-12-31  |  7.5 KB  |  543 lines

  1. package javax.swing.tree;
  2.  
  3. import java.io.IOException;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.io.Serializable;
  7. import java.util.Enumeration;
  8. import java.util.NoSuchElementException;
  9. import java.util.Vector;
  10.  
  11. public class DefaultMutableTreeNode implements Cloneable, MutableTreeNode, Serializable {
  12.    public static final Enumeration EMPTY_ENUMERATION = new 2();
  13.    protected MutableTreeNode parent;
  14.    protected Vector children;
  15.    protected transient Object userObject;
  16.    protected boolean allowsChildren;
  17.  
  18.    public DefaultMutableTreeNode() {
  19.       this((Object)null);
  20.    }
  21.  
  22.    public DefaultMutableTreeNode(Object var1) {
  23.       this(var1, true);
  24.    }
  25.  
  26.    public DefaultMutableTreeNode(Object var1, boolean var2) {
  27.       this.parent = null;
  28.       this.allowsChildren = var2;
  29.       this.userObject = var1;
  30.    }
  31.  
  32.    public void insert(MutableTreeNode var1, int var2) {
  33.       if (!this.allowsChildren) {
  34.          throw new IllegalStateException("node does not allow children");
  35.       } else if (var1 == null) {
  36.          throw new IllegalArgumentException("new child is null");
  37.       } else if (this.isNodeAncestor(var1)) {
  38.          throw new IllegalArgumentException("new child is an ancestor");
  39.       } else {
  40.          MutableTreeNode var3 = (MutableTreeNode)var1.getParent();
  41.          if (var3 != null) {
  42.             var3.remove(var1);
  43.          }
  44.  
  45.          var1.setParent(this);
  46.          if (this.children == null) {
  47.             this.children = new Vector();
  48.          }
  49.  
  50.          this.children.insertElementAt(var1, var2);
  51.       }
  52.    }
  53.  
  54.    public void remove(int var1) {
  55.       MutableTreeNode var2 = (MutableTreeNode)this.getChildAt(var1);
  56.       this.children.removeElementAt(var1);
  57.       var2.setParent((MutableTreeNode)null);
  58.    }
  59.  
  60.    public void setParent(MutableTreeNode var1) {
  61.       this.parent = var1;
  62.    }
  63.  
  64.    public TreeNode getParent() {
  65.       return this.parent;
  66.    }
  67.  
  68.    public TreeNode getChildAt(int var1) {
  69.       if (this.children == null) {
  70.          throw new ArrayIndexOutOfBoundsException("node has no children");
  71.       } else {
  72.          return (TreeNode)this.children.elementAt(var1);
  73.       }
  74.    }
  75.  
  76.    public int getChildCount() {
  77.       return this.children == null ? 0 : this.children.size();
  78.    }
  79.  
  80.    public int getIndex(TreeNode var1) {
  81.       if (var1 == null) {
  82.          throw new IllegalArgumentException("argument is null");
  83.       } else {
  84.          return !this.isNodeChild(var1) ? -1 : this.children.indexOf(var1);
  85.       }
  86.    }
  87.  
  88.    public Enumeration children() {
  89.       return this.children == null ? EMPTY_ENUMERATION : this.children.elements();
  90.    }
  91.  
  92.    public void setAllowsChildren(boolean var1) {
  93.       if (var1 != this.allowsChildren) {
  94.          this.allowsChildren = var1;
  95.          if (!this.allowsChildren) {
  96.             this.removeAllChildren();
  97.          }
  98.       }
  99.  
  100.    }
  101.  
  102.    public boolean getAllowsChildren() {
  103.       return this.allowsChildren;
  104.    }
  105.  
  106.    public void setUserObject(Object var1) {
  107.       this.userObject = var1;
  108.    }
  109.  
  110.    public Object getUserObject() {
  111.       return this.userObject;
  112.    }
  113.  
  114.    public void removeFromParent() {
  115.       MutableTreeNode var1 = (MutableTreeNode)this.getParent();
  116.       if (var1 != null) {
  117.          var1.remove(this);
  118.       }
  119.  
  120.    }
  121.  
  122.    public void remove(MutableTreeNode var1) {
  123.       if (var1 == null) {
  124.          throw new IllegalArgumentException("argument is null");
  125.       } else if (!this.isNodeChild(var1)) {
  126.          throw new IllegalArgumentException("argument is not a child");
  127.       } else {
  128.          this.remove(this.getIndex(var1));
  129.       }
  130.    }
  131.  
  132.    public void removeAllChildren() {
  133.       for(int var1 = this.getChildCount() - 1; var1 >= 0; --var1) {
  134.          this.remove(var1);
  135.       }
  136.  
  137.    }
  138.  
  139.    public void add(MutableTreeNode var1) {
  140.       if (var1 != null && var1.getParent() == this) {
  141.          this.insert(var1, this.getChildCount() - 1);
  142.       } else {
  143.          this.insert(var1, this.getChildCount());
  144.       }
  145.  
  146.    }
  147.  
  148.    public boolean isNodeAncestor(TreeNode var1) {
  149.       if (var1 == null) {
  150.          return false;
  151.       } else {
  152.          Object var2 = this;
  153.  
  154.          while(var2 != var1) {
  155.             if ((var2 = ((TreeNode)var2).getParent()) == null) {
  156.                return false;
  157.             }
  158.          }
  159.  
  160.          return true;
  161.       }
  162.    }
  163.  
  164.    public boolean isNodeDescendant(DefaultMutableTreeNode var1) {
  165.       return var1 == null ? false : var1.isNodeAncestor(this);
  166.    }
  167.  
  168.    public TreeNode getSharedAncestor(DefaultMutableTreeNode var1) {
  169.       if (var1 == this) {
  170.          return this;
  171.       } else if (var1 == null) {
  172.          return null;
  173.       } else {
  174.          int var2 = this.getLevel();
  175.          int var3 = var1.getLevel();
  176.          int var4;
  177.          Object var5;
  178.          Object var6;
  179.          if (var3 > var2) {
  180.             var4 = var3 - var2;
  181.             var5 = var1;
  182.             var6 = this;
  183.          } else {
  184.             var4 = var2 - var3;
  185.             var5 = this;
  186.             var6 = var1;
  187.          }
  188.  
  189.          while(var4 > 0) {
  190.             var5 = ((TreeNode)var5).getParent();
  191.             --var4;
  192.          }
  193.  
  194.          while(var5 != var6) {
  195.             var5 = ((TreeNode)var5).getParent();
  196.             var6 = ((TreeNode)var6).getParent();
  197.             if (var5 == null) {
  198.                if (var5 == null && var6 == null) {
  199.                   return null;
  200.                }
  201.  
  202.                throw new Error("nodes should be null");
  203.             }
  204.          }
  205.  
  206.          return (TreeNode)var5;
  207.       }
  208.    }
  209.  
  210.    public boolean isNodeRelated(DefaultMutableTreeNode var1) {
  211.       return var1 != null && this.getRoot() == var1.getRoot();
  212.    }
  213.  
  214.    public int getDepth() {
  215.       Object var1 = null;
  216.  
  217.       for(Enumeration var2 = this.breadthFirstEnumeration(); var2.hasMoreElements(); var1 = var2.nextElement()) {
  218.       }
  219.  
  220.       if (var1 == null) {
  221.          throw new Error("nodes should be null");
  222.       } else {
  223.          return ((DefaultMutableTreeNode)var1).getLevel() - this.getLevel();
  224.       }
  225.    }
  226.  
  227.    public int getLevel() {
  228.       int var2 = 0;
  229.  
  230.       for(Object var1 = this; (var1 = ((TreeNode)var1).getParent()) != null; ++var2) {
  231.       }
  232.  
  233.       return var2;
  234.    }
  235.  
  236.    public TreeNode[] getPath() {
  237.       return this.getPathToRoot(this, 0);
  238.    }
  239.  
  240.    protected TreeNode[] getPathToRoot(TreeNode var1, int var2) {
  241.       TreeNode[] var3;
  242.       if (var1 == null) {
  243.          if (var2 == 0) {
  244.             return null;
  245.          }
  246.  
  247.          var3 = new TreeNode[var2];
  248.       } else {
  249.          ++var2;
  250.          var3 = this.getPathToRoot(var1.getParent(), var2);
  251.          var3[var3.length - var2] = var1;
  252.       }
  253.  
  254.       return var3;
  255.    }
  256.  
  257.    public Object[] getUserObjectPath() {
  258.       TreeNode[] var1 = this.getPath();
  259.       Object[] var2 = new Object[var1.length];
  260.  
  261.       for(int var3 = 0; var3 < var1.length; ++var3) {
  262.          var2[var3] = ((DefaultMutableTreeNode)var1[var3]).getUserObject();
  263.       }
  264.  
  265.       return var2;
  266.    }
  267.  
  268.    public TreeNode getRoot() {
  269.       Object var1 = this;
  270.  
  271.       Object var2;
  272.       do {
  273.          var2 = var1;
  274.          var1 = ((TreeNode)var1).getParent();
  275.       } while(var1 != null);
  276.  
  277.       return (TreeNode)var2;
  278.    }
  279.  
  280.    public boolean isRoot() {
  281.       return this.getParent() == null;
  282.    }
  283.  
  284.    public DefaultMutableTreeNode getNextNode() {
  285.       if (this.getChildCount() != 0) {
  286.          return (DefaultMutableTreeNode)this.getChildAt(0);
  287.       } else {
  288.          DefaultMutableTreeNode var1 = this.getNextSibling();
  289.          if (var1 != null) {
  290.             return var1;
  291.          } else {
  292.             for(DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent(); var2 != null; var2 = (DefaultMutableTreeNode)var2.getParent()) {
  293.                var1 = var2.getNextSibling();
  294.                if (var1 != null) {
  295.                   return var1;
  296.                }
  297.             }
  298.  
  299.             return null;
  300.          }
  301.       }
  302.    }
  303.  
  304.    public DefaultMutableTreeNode getPreviousNode() {
  305.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  306.       if (var2 == null) {
  307.          return null;
  308.       } else {
  309.          DefaultMutableTreeNode var1 = this.getPreviousSibling();
  310.          if (var1 != null) {
  311.             return var1.getChildCount() == 0 ? var1 : var1.getLastLeaf();
  312.          } else {
  313.             return var2;
  314.          }
  315.       }
  316.    }
  317.  
  318.    public Enumeration preorderEnumeration() {
  319.       return new PreorderEnumeration(this, this);
  320.    }
  321.  
  322.    public Enumeration postorderEnumeration() {
  323.       return new PostorderEnumeration(this, this);
  324.    }
  325.  
  326.    public Enumeration breadthFirstEnumeration() {
  327.       return new BreadthFirstEnumeration(this, this);
  328.    }
  329.  
  330.    public Enumeration depthFirstEnumeration() {
  331.       return this.postorderEnumeration();
  332.    }
  333.  
  334.    public Enumeration pathFromAncestorEnumeration(TreeNode var1) {
  335.       return new PathBetweenNodesEnumeration(this, var1, this);
  336.    }
  337.  
  338.    public boolean isNodeChild(TreeNode var1) {
  339.       boolean var2;
  340.       if (var1 == null) {
  341.          var2 = false;
  342.       } else if (this.getChildCount() == 0) {
  343.          var2 = false;
  344.       } else {
  345.          var2 = var1.getParent() == this;
  346.       }
  347.  
  348.       return var2;
  349.    }
  350.  
  351.    public TreeNode getFirstChild() {
  352.       if (this.getChildCount() == 0) {
  353.          throw new NoSuchElementException("node has no children");
  354.       } else {
  355.          return this.getChildAt(0);
  356.       }
  357.    }
  358.  
  359.    public TreeNode getLastChild() {
  360.       if (this.getChildCount() == 0) {
  361.          throw new NoSuchElementException("node has no children");
  362.       } else {
  363.          return this.getChildAt(this.getChildCount() - 1);
  364.       }
  365.    }
  366.  
  367.    public TreeNode getChildAfter(TreeNode var1) {
  368.       if (var1 == null) {
  369.          throw new IllegalArgumentException("argument is null");
  370.       } else {
  371.          int var2 = this.getIndex(var1);
  372.          if (var2 == -1) {
  373.             throw new IllegalArgumentException("node is not a child");
  374.          } else {
  375.             return var2 < this.getChildCount() - 1 ? this.getChildAt(var2 + 1) : null;
  376.          }
  377.       }
  378.    }
  379.  
  380.    public TreeNode getChildBefore(TreeNode var1) {
  381.       if (var1 == null) {
  382.          throw new IllegalArgumentException("argument is null");
  383.       } else {
  384.          int var2 = this.getIndex(var1);
  385.          if (var2 == -1) {
  386.             throw new IllegalArgumentException("argument is not a child");
  387.          } else {
  388.             return var2 > 0 ? this.getChildAt(var2 - 1) : null;
  389.          }
  390.       }
  391.    }
  392.  
  393.    public boolean isNodeSibling(TreeNode var1) {
  394.       boolean var2;
  395.       if (var1 == null) {
  396.          var2 = false;
  397.       } else if (var1 == this) {
  398.          var2 = true;
  399.       } else {
  400.          TreeNode var3 = this.getParent();
  401.          var2 = var3 != null && var3 == var1.getParent();
  402.          if (var2 && !((DefaultMutableTreeNode)this.getParent()).isNodeChild(var1)) {
  403.             throw new Error("sibling has different parent");
  404.          }
  405.       }
  406.  
  407.       return var2;
  408.    }
  409.  
  410.    public int getSiblingCount() {
  411.       TreeNode var1 = this.getParent();
  412.       return var1 == null ? 1 : var1.getChildCount();
  413.    }
  414.  
  415.    public DefaultMutableTreeNode getNextSibling() {
  416.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  417.       DefaultMutableTreeNode var1;
  418.       if (var2 == null) {
  419.          var1 = null;
  420.       } else {
  421.          var1 = (DefaultMutableTreeNode)var2.getChildAfter(this);
  422.       }
  423.  
  424.       if (var1 != null && !this.isNodeSibling(var1)) {
  425.          throw new Error("child of parent is not a sibling");
  426.       } else {
  427.          return var1;
  428.       }
  429.    }
  430.  
  431.    public DefaultMutableTreeNode getPreviousSibling() {
  432.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  433.       DefaultMutableTreeNode var1;
  434.       if (var2 == null) {
  435.          var1 = null;
  436.       } else {
  437.          var1 = (DefaultMutableTreeNode)var2.getChildBefore(this);
  438.       }
  439.  
  440.       if (var1 != null && !this.isNodeSibling(var1)) {
  441.          throw new Error("child of parent is not a sibling");
  442.       } else {
  443.          return var1;
  444.       }
  445.    }
  446.  
  447.    public boolean isLeaf() {
  448.       return this.getChildCount() == 0;
  449.    }
  450.  
  451.    public DefaultMutableTreeNode getFirstLeaf() {
  452.       DefaultMutableTreeNode var1;
  453.       for(var1 = this; !var1.isLeaf(); var1 = (DefaultMutableTreeNode)var1.getFirstChild()) {
  454.       }
  455.  
  456.       return var1;
  457.    }
  458.  
  459.    public DefaultMutableTreeNode getLastLeaf() {
  460.       DefaultMutableTreeNode var1;
  461.       for(var1 = this; !var1.isLeaf(); var1 = (DefaultMutableTreeNode)var1.getLastChild()) {
  462.       }
  463.  
  464.       return var1;
  465.    }
  466.  
  467.    public DefaultMutableTreeNode getNextLeaf() {
  468.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  469.       if (var2 == null) {
  470.          return null;
  471.       } else {
  472.          DefaultMutableTreeNode var1 = this.getNextSibling();
  473.          return var1 != null ? var1.getFirstLeaf() : var2.getNextLeaf();
  474.       }
  475.    }
  476.  
  477.    public DefaultMutableTreeNode getPreviousLeaf() {
  478.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  479.       if (var2 == null) {
  480.          return null;
  481.       } else {
  482.          DefaultMutableTreeNode var1 = this.getPreviousSibling();
  483.          return var1 != null ? var1.getLastLeaf() : var2.getPreviousLeaf();
  484.       }
  485.    }
  486.  
  487.    public int getLeafCount() {
  488.       int var1 = 0;
  489.       Enumeration var3 = this.breadthFirstEnumeration();
  490.  
  491.       while(var3.hasMoreElements()) {
  492.          TreeNode var2 = (TreeNode)var3.nextElement();
  493.          if (var2.isLeaf()) {
  494.             ++var1;
  495.          }
  496.       }
  497.  
  498.       if (var1 < 1) {
  499.          throw new Error("tree has zero leaves");
  500.       } else {
  501.          return var1;
  502.       }
  503.    }
  504.  
  505.    public String toString() {
  506.       return this.userObject == null ? null : this.userObject.toString();
  507.    }
  508.  
  509.    public Object clone() {
  510.       DefaultMutableTreeNode var1 = null;
  511.  
  512.       try {
  513.          var1 = (DefaultMutableTreeNode)super.clone();
  514.          var1.children = null;
  515.          var1.parent = null;
  516.          return var1;
  517.       } catch (CloneNotSupportedException var3) {
  518.          throw new Error(((Throwable)var3).toString());
  519.       }
  520.    }
  521.  
  522.    private void writeObject(ObjectOutputStream var1) throws IOException {
  523.       var1.defaultWriteObject();
  524.       Object[] var2;
  525.       if (this.userObject != null && this.userObject instanceof Serializable) {
  526.          var2 = new Object[]{"userObject", this.userObject};
  527.       } else {
  528.          var2 = new Object[0];
  529.       }
  530.  
  531.       var1.writeObject(var2);
  532.    }
  533.  
  534.    private void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException {
  535.       var1.defaultReadObject();
  536.       Object[] var2 = var1.readObject();
  537.       if (var2.length > 0 && var2[0].equals("userObject")) {
  538.          this.userObject = var2[1];
  539.       }
  540.  
  541.    }
  542. }
  543.