home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161a.iso / handson / files / copyjava.exe / com / sun / java / swing / tree / DefaultMutableTreeNode.class (.txt) next >
Encoding:
Java Class File  |  1998-02-26  |  9.5 KB  |  542 lines

  1. package com.sun.java.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 1();
  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.    public boolean isNodeAncestor(TreeNode var1) {
  148.       if (var1 == null) {
  149.          return false;
  150.       } else {
  151.          Object var2 = this;
  152.  
  153.          while(var2 != var1) {
  154.             if ((var2 = ((TreeNode)var2).getParent()) == null) {
  155.                return false;
  156.             }
  157.          }
  158.  
  159.          return true;
  160.       }
  161.    }
  162.  
  163.    public boolean isNodeDescendant(DefaultMutableTreeNode var1) {
  164.       return var1 == null ? false : var1.isNodeAncestor(this);
  165.    }
  166.  
  167.    public TreeNode getSharedAncestor(DefaultMutableTreeNode var1) {
  168.       if (var1 == this) {
  169.          return this;
  170.       } else if (var1 == null) {
  171.          return null;
  172.       } else {
  173.          int var2 = this.getLevel();
  174.          int var3 = var1.getLevel();
  175.          int var4;
  176.          Object var5;
  177.          Object var6;
  178.          if (var3 > var2) {
  179.             var4 = var3 - var2;
  180.             var5 = var1;
  181.             var6 = this;
  182.          } else {
  183.             var4 = var2 - var3;
  184.             var5 = this;
  185.             var6 = var1;
  186.          }
  187.  
  188.          while(var4 > 0) {
  189.             var5 = ((TreeNode)var5).getParent();
  190.             --var4;
  191.          }
  192.  
  193.          while(var5 != var6) {
  194.             var5 = ((TreeNode)var5).getParent();
  195.             var6 = ((TreeNode)var6).getParent();
  196.             if (var5 == null) {
  197.                if (var5 == null && var6 == null) {
  198.                   return null;
  199.                }
  200.  
  201.                throw new InternalError("nodes should be null");
  202.             }
  203.          }
  204.  
  205.          return (TreeNode)var5;
  206.       }
  207.    }
  208.  
  209.    public boolean isNodeRelated(DefaultMutableTreeNode var1) {
  210.       return var1 != null && this.getRoot() == var1.getRoot();
  211.    }
  212.  
  213.    public int getDepth() {
  214.       Object var1 = null;
  215.  
  216.       for(Enumeration var2 = this.breadthFirstEnumeration(); var2.hasMoreElements(); var1 = var2.nextElement()) {
  217.       }
  218.  
  219.       if (var1 == null) {
  220.          throw new InternalError("nodes should be null");
  221.       } else {
  222.          return ((DefaultMutableTreeNode)var1).getLevel() - this.getLevel();
  223.       }
  224.    }
  225.  
  226.    public int getLevel() {
  227.       int var2 = 0;
  228.  
  229.       for(Object var1 = this; (var1 = ((TreeNode)var1).getParent()) != null; ++var2) {
  230.       }
  231.  
  232.       return var2;
  233.    }
  234.  
  235.    public TreeNode[] getPath() {
  236.       return this.getPathToRoot(this, 0);
  237.    }
  238.  
  239.    protected TreeNode[] getPathToRoot(TreeNode var1, int var2) {
  240.       TreeNode[] var3;
  241.       if (var1 == null) {
  242.          if (var2 == 0) {
  243.             return null;
  244.          }
  245.  
  246.          var3 = new TreeNode[var2];
  247.       } else {
  248.          ++var2;
  249.          var3 = this.getPathToRoot(var1.getParent(), var2);
  250.          var3[var3.length - var2] = var1;
  251.       }
  252.  
  253.       return var3;
  254.    }
  255.  
  256.    public Object[] getUserObjectPath() {
  257.       TreeNode[] var1 = this.getPath();
  258.       Object[] var2 = new Object[var1.length];
  259.  
  260.       for(int var3 = 0; var3 < var1.length; ++var3) {
  261.          var2[var3] = ((DefaultMutableTreeNode)var1[var3]).getUserObject();
  262.       }
  263.  
  264.       return var2;
  265.    }
  266.  
  267.    public TreeNode getRoot() {
  268.       Object var1 = this;
  269.  
  270.       Object var2;
  271.       do {
  272.          var2 = var1;
  273.          var1 = ((TreeNode)var1).getParent();
  274.       } while(var1 != null);
  275.  
  276.       return (TreeNode)var2;
  277.    }
  278.  
  279.    public boolean isRoot() {
  280.       return this.getParent() == null;
  281.    }
  282.  
  283.    public DefaultMutableTreeNode getNextNode() {
  284.       if (this.getChildCount() != 0) {
  285.          return (DefaultMutableTreeNode)this.getChildAt(0);
  286.       } else {
  287.          DefaultMutableTreeNode var1 = this.getNextSibling();
  288.          if (var1 != null) {
  289.             return var1;
  290.          } else {
  291.             for(DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent(); var2 != null; var2 = (DefaultMutableTreeNode)var2.getParent()) {
  292.                var1 = var2.getNextSibling();
  293.                if (var1 != null) {
  294.                   return var1;
  295.                }
  296.             }
  297.  
  298.             return null;
  299.          }
  300.       }
  301.    }
  302.  
  303.    public DefaultMutableTreeNode getPreviousNode() {
  304.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  305.       if (var2 == null) {
  306.          return null;
  307.       } else {
  308.          DefaultMutableTreeNode var1 = this.getPreviousSibling();
  309.          if (var1 != null) {
  310.             return var1.getChildCount() == 0 ? var1 : var1.getLastLeaf();
  311.          } else {
  312.             return var2;
  313.          }
  314.       }
  315.    }
  316.  
  317.    public Enumeration preorderEnumeration() {
  318.       return new PreorderEnumeration(this, this);
  319.    }
  320.  
  321.    public Enumeration postorderEnumeration() {
  322.       return new PostorderEnumeration(this, this);
  323.    }
  324.  
  325.    public Enumeration breadthFirstEnumeration() {
  326.       return new BreadthFirstEnumeration(this, this);
  327.    }
  328.  
  329.    public Enumeration depthFirstEnumeration() {
  330.       return this.postorderEnumeration();
  331.    }
  332.  
  333.    public Enumeration pathFromAncestorEnumeration(TreeNode var1) {
  334.       return new PathBetweenNodesEnumeration(this, var1, this);
  335.    }
  336.  
  337.    public boolean isNodeChild(TreeNode var1) {
  338.       boolean var2;
  339.       if (var1 == null) {
  340.          var2 = false;
  341.       } else if (this.getChildCount() == 0) {
  342.          var2 = false;
  343.       } else {
  344.          var2 = var1.getParent() == this;
  345.       }
  346.  
  347.       return var2;
  348.    }
  349.  
  350.    public TreeNode getFirstChild() {
  351.       if (this.getChildCount() == 0) {
  352.          throw new NoSuchElementException("node has no children");
  353.       } else {
  354.          return this.getChildAt(0);
  355.       }
  356.    }
  357.  
  358.    public TreeNode getLastChild() {
  359.       if (this.getChildCount() == 0) {
  360.          throw new NoSuchElementException("node has no children");
  361.       } else {
  362.          return this.getChildAt(this.getChildCount() - 1);
  363.       }
  364.    }
  365.  
  366.    public TreeNode getChildAfter(TreeNode var1) {
  367.       if (var1 == null) {
  368.          throw new IllegalArgumentException("argument is null");
  369.       } else {
  370.          int var2 = this.getIndex(var1);
  371.          if (var2 == -1) {
  372.             throw new IllegalArgumentException("node is not a child");
  373.          } else {
  374.             return var2 < this.getChildCount() - 1 ? this.getChildAt(var2 + 1) : null;
  375.          }
  376.       }
  377.    }
  378.  
  379.    public TreeNode getChildBefore(TreeNode var1) {
  380.       if (var1 == null) {
  381.          throw new IllegalArgumentException("argument is null");
  382.       } else {
  383.          int var2 = this.getIndex(var1);
  384.          if (var2 == -1) {
  385.             throw new IllegalArgumentException("argument is not a child");
  386.          } else {
  387.             return var2 > 0 ? this.getChildAt(var2 - 1) : null;
  388.          }
  389.       }
  390.    }
  391.  
  392.    public boolean isNodeSibling(TreeNode var1) {
  393.       boolean var2;
  394.       if (var1 == null) {
  395.          var2 = false;
  396.       } else if (var1 == this) {
  397.          var2 = true;
  398.       } else {
  399.          TreeNode var3 = this.getParent();
  400.          var2 = var3 != null && var3 == var1.getParent();
  401.          if (var2 && !((DefaultMutableTreeNode)this.getParent()).isNodeChild(var1)) {
  402.             throw new InternalError("sibling has different parent");
  403.          }
  404.       }
  405.  
  406.       return var2;
  407.    }
  408.  
  409.    public int getSiblingCount() {
  410.       TreeNode var1 = this.getParent();
  411.       return var1 == null ? 1 : var1.getChildCount();
  412.    }
  413.  
  414.    public DefaultMutableTreeNode getNextSibling() {
  415.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  416.       DefaultMutableTreeNode var1;
  417.       if (var2 == null) {
  418.          var1 = null;
  419.       } else {
  420.          var1 = (DefaultMutableTreeNode)var2.getChildAfter(this);
  421.       }
  422.  
  423.       if (var1 != null && !this.isNodeSibling(var1)) {
  424.          throw new InternalError("child of parent is not a sibling");
  425.       } else {
  426.          return var1;
  427.       }
  428.    }
  429.  
  430.    public DefaultMutableTreeNode getPreviousSibling() {
  431.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  432.       DefaultMutableTreeNode var1;
  433.       if (var2 == null) {
  434.          var1 = null;
  435.       } else {
  436.          var1 = (DefaultMutableTreeNode)var2.getChildBefore(this);
  437.       }
  438.  
  439.       if (var1 != null && !this.isNodeSibling(var1)) {
  440.          throw new InternalError("child of parent is not a sibling");
  441.       } else {
  442.          return var1;
  443.       }
  444.    }
  445.  
  446.    public boolean isLeaf() {
  447.       return this.getChildCount() == 0;
  448.    }
  449.  
  450.    public DefaultMutableTreeNode getFirstLeaf() {
  451.       DefaultMutableTreeNode var1;
  452.       for(var1 = this; !var1.isLeaf(); var1 = (DefaultMutableTreeNode)var1.getFirstChild()) {
  453.       }
  454.  
  455.       return var1;
  456.    }
  457.  
  458.    public DefaultMutableTreeNode getLastLeaf() {
  459.       DefaultMutableTreeNode var1;
  460.       for(var1 = this; !var1.isLeaf(); var1 = (DefaultMutableTreeNode)var1.getLastChild()) {
  461.       }
  462.  
  463.       return var1;
  464.    }
  465.  
  466.    public DefaultMutableTreeNode getNextLeaf() {
  467.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  468.       if (var2 == null) {
  469.          return null;
  470.       } else {
  471.          DefaultMutableTreeNode var1 = this.getNextSibling();
  472.          return var1 != null ? var1.getFirstLeaf() : var2.getNextLeaf();
  473.       }
  474.    }
  475.  
  476.    public DefaultMutableTreeNode getPreviousLeaf() {
  477.       DefaultMutableTreeNode var2 = (DefaultMutableTreeNode)this.getParent();
  478.       if (var2 == null) {
  479.          return null;
  480.       } else {
  481.          DefaultMutableTreeNode var1 = this.getPreviousSibling();
  482.          return var1 != null ? var1.getLastLeaf() : var2.getPreviousLeaf();
  483.       }
  484.    }
  485.  
  486.    public int getLeafCount() {
  487.       int var1 = 0;
  488.       Enumeration var3 = this.breadthFirstEnumeration();
  489.  
  490.       while(var3.hasMoreElements()) {
  491.          TreeNode var2 = (TreeNode)var3.nextElement();
  492.          if (var2.isLeaf()) {
  493.             ++var1;
  494.          }
  495.       }
  496.  
  497.       if (var1 < 1) {
  498.          throw new InternalError("tree has zero leaves");
  499.       } else {
  500.          return var1;
  501.       }
  502.    }
  503.  
  504.    public String toString() {
  505.       return this.userObject == null ? null : this.userObject.toString();
  506.    }
  507.  
  508.    public Object clone() {
  509.       DefaultMutableTreeNode var1 = null;
  510.  
  511.       try {
  512.          var1 = (DefaultMutableTreeNode)super.clone();
  513.          var1.children = null;
  514.          var1.parent = null;
  515.          return var1;
  516.       } catch (CloneNotSupportedException var3) {
  517.          throw new InternalError(((Throwable)var3).toString());
  518.       }
  519.    }
  520.  
  521.    private void writeObject(ObjectOutputStream var1) throws IOException {
  522.       var1.defaultWriteObject();
  523.       Object[] var2;
  524.       if (this.userObject != null && this.userObject instanceof Serializable) {
  525.          var2 = new Object[]{"userObject", this.userObject};
  526.       } else {
  527.          var2 = new Object[0];
  528.       }
  529.  
  530.       var1.writeObject(var2);
  531.    }
  532.  
  533.    private void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException {
  534.       var1.defaultReadObject();
  535.       Object[] var2 = var1.readObject();
  536.       if (var2.length > 0 && var2[0].equals("userObject")) {
  537.          this.userObject = var2[1];
  538.       }
  539.  
  540.    }
  541. }
  542.