home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1996 October / PCO_10.ISO / filesbbs / clearweb.arj / CVIEW.CMP / APPLET.ZIP / OutlineNode.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-08-15  |  6.1 KB  |  268 lines

  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.FontMetrics;
  4. import java.awt.Graphics;
  5. import java.awt.Rectangle;
  6. import java.util.Vector;
  7.  
  8. class OutlineNode {
  9.    private Outline Owner;
  10.    protected int NodeIndex;
  11.    protected int NodeDepth;
  12.    protected OutlineNodeData NodeData;
  13.    protected OutlineNode ParentNode;
  14.    protected OutlineNode OlderSiblingNode;
  15.    protected OutlineNode YoungerSiblingNode;
  16.    protected int NodeChildren;
  17.    protected OutlineNode FirstChildNode;
  18.    protected boolean IsExpanded;
  19.    private static int currentDrawIdx;
  20.  
  21.    public OutlineNode(Outline owner) {
  22.       this(owner, (OutlineNodeData)null);
  23.    }
  24.  
  25.    public OutlineNode(Outline owner, OutlineNodeData Data) {
  26.       this.OlderSiblingNode = this.YoungerSiblingNode = this;
  27.       this.NodeDepth = -1;
  28.       this.NodeIndex = -1;
  29.       this.NodeChildren = 0;
  30.       this.FirstChildNode = null;
  31.       this.IsExpanded = true;
  32.       this.NodeData = Data;
  33.       this.Owner = owner;
  34.    }
  35.  
  36.    public void attach(OutlineNode relative, int relation) {
  37.       OutlineNode prev = null;
  38.       if (relative != null) {
  39.          if (relation == 0) {
  40.             this.ParentNode = relative;
  41.             ++this.ParentNode.NodeChildren;
  42.             if (this.ParentNode.FirstChildNode == null) {
  43.                prev = null;
  44.             } else {
  45.                prev = this.ParentNode.FirstChildNode.OlderSiblingNode;
  46.             }
  47.  
  48.             this.ParentNode.FirstChildNode = this;
  49.          } else if (relation == 1) {
  50.             this.ParentNode = relative;
  51.             ++relative.NodeChildren;
  52.             if (relative.FirstChildNode == null) {
  53.                relative.FirstChildNode = this;
  54.                prev = null;
  55.             } else {
  56.                prev = relative.FirstChildNode.OlderSiblingNode;
  57.             }
  58.          } else if (relation == 2) {
  59.             this.ParentNode = relative.ParentNode;
  60.             ++this.ParentNode.NodeChildren;
  61.             prev = relative.OlderSiblingNode;
  62.             if (relative == this.ParentNode.FirstChildNode) {
  63.                this.ParentNode.FirstChildNode = this;
  64.             }
  65.          } else if (relation == 3) {
  66.             this.ParentNode = relative.ParentNode;
  67.             ++this.ParentNode.NodeChildren;
  68.             prev = relative;
  69.          }
  70.  
  71.          if (prev != null) {
  72.             this.OlderSiblingNode = prev;
  73.             this.YoungerSiblingNode = prev.YoungerSiblingNode;
  74.             this.YoungerSiblingNode.OlderSiblingNode = this;
  75.             this.OlderSiblingNode.YoungerSiblingNode = this;
  76.          } else {
  77.             this.OlderSiblingNode = this.YoungerSiblingNode = this;
  78.          }
  79.  
  80.          this.NodeDepth = this.ParentNode.NodeDepth + 1;
  81.          this.IsExpanded = false;
  82.       }
  83.    }
  84.  
  85.    public void detach() {
  86.       --this.ParentNode.NodeChildren;
  87.       if (this.ParentNode.NodeChildren == 0) {
  88.          this.ParentNode.FirstChildNode = null;
  89.       } else if (this.ParentNode.FirstChildNode == this) {
  90.          this.ParentNode.FirstChildNode = this.YoungerSiblingNode;
  91.       }
  92.  
  93.       this.YoungerSiblingNode.OlderSiblingNode = this.OlderSiblingNode;
  94.       this.OlderSiblingNode.YoungerSiblingNode = this.YoungerSiblingNode;
  95.    }
  96.  
  97.    public void delete() {
  98.       while(this.FirstChildNode != null) {
  99.          OutlineNode nd = this.FirstChildNode;
  100.          nd.detach();
  101.          nd.delete();
  102.       }
  103.  
  104.    }
  105.  
  106.    public void expand(boolean doExpand) {
  107.       if (this.NodeChildren > 0 && doExpand) {
  108.          this.IsExpanded = true;
  109.       } else {
  110.          this.IsExpanded = false;
  111.       }
  112.    }
  113.  
  114.    final int reindexChildren(int index) {
  115.       this.NodeIndex = index++;
  116.       if (this.FirstChildNode != null) {
  117.          OutlineNode nd = this.FirstChildNode;
  118.          index = nd.reindexChildren(index);
  119.  
  120.          for(OutlineNode var4 = nd.YoungerSiblingNode; var4 != this.FirstChildNode; var4 = var4.YoungerSiblingNode) {
  121.             index = var4.reindexChildren(index);
  122.          }
  123.       }
  124.  
  125.       return index;
  126.    }
  127.  
  128.    final OutlineNode getDescendantWithIndex(int index) {
  129.       if (this.NodeIndex == index) {
  130.          return this;
  131.       } else if (this.NodeChildren == 1) {
  132.          return this.FirstChildNode.getDescendantWithIndex(index);
  133.       } else {
  134.          OutlineNode nd = this.FirstChildNode;
  135.  
  136.          while(nd.YoungerSiblingNode.NodeIndex <= index && nd.YoungerSiblingNode != this.FirstChildNode) {
  137.             nd = nd.YoungerSiblingNode;
  138.             if (nd == this.FirstChildNode) {
  139.                return null;
  140.             }
  141.          }
  142.  
  143.          return nd.getDescendantWithIndex(index);
  144.       }
  145.    }
  146.  
  147.    public int getIndex() {
  148.       return this.NodeIndex;
  149.    }
  150.  
  151.    public int getDepth() {
  152.       return this.NodeDepth;
  153.    }
  154.  
  155.    public OutlineNodeData getData() {
  156.       return this.NodeData;
  157.    }
  158.  
  159.    public int getParent() {
  160.       return this.ParentNode != null ? this.ParentNode.NodeIndex : -1;
  161.    }
  162.  
  163.    public int getYoungerSibling() {
  164.       return this.YoungerSiblingNode != this.ParentNode.FirstChildNode ? this.YoungerSiblingNode.NodeIndex : -1;
  165.    }
  166.  
  167.    public int getOlderSibling() {
  168.       return this != this.ParentNode.FirstChildNode ? this.OlderSiblingNode.NodeIndex : -1;
  169.    }
  170.  
  171.    public int children() {
  172.       return this.NodeChildren;
  173.    }
  174.  
  175.    public int getFirstChild() {
  176.       return this.FirstChildNode != null ? this.FirstChildNode.NodeIndex : -1;
  177.    }
  178.  
  179.    public boolean expanded() {
  180.       return this.IsExpanded;
  181.    }
  182.  
  183.    void drawNode(Graphics g, int wndDrawIdx, Dimension NodeDim, boolean customColor, Color fore, Color back) {
  184.       Color fg = null;
  185.       Rectangle NodeRect = new Rectangle(0, wndDrawIdx * NodeDim.height, NodeDim.width, NodeDim.height);
  186.       FontMetrics m = g.getFontMetrics(g.getFont());
  187.       int ftHeight = (NodeRect.height - 1 + m.getAscent()) / 2;
  188.       NodeRect.x = (this.NodeDepth + 2) * NodeDim.height;
  189.       NodeRect.width = m.stringWidth(this.NodeData.NodeTitle());
  190.       if (this.NodeData.NodeIcon() != null) {
  191.          g.drawImage(this.NodeData.NodeIcon(), NodeRect.x - NodeRect.height + 1, NodeRect.y, NodeRect.height - 2, NodeRect.height - 2, this.Owner);
  192.       } else {
  193.          NodeRect.x -= NodeDim.height;
  194.       }
  195.  
  196.       if (customColor) {
  197.          fg = g.getColor();
  198.          g.setColor(back);
  199.          g.fillRect(NodeRect.x, NodeRect.y, NodeRect.width, NodeRect.height);
  200.          g.setColor(fore);
  201.       }
  202.  
  203.       g.drawString(this.NodeData.NodeTitle(), NodeRect.x, NodeRect.y + ftHeight);
  204.       if (customColor) {
  205.          g.setColor(fg);
  206.       }
  207.  
  208.       this.drawTree(g, wndDrawIdx, NodeDim);
  209.    }
  210.  
  211.    private void drawTree(Graphics g, int wndDrawIdx, Dimension NodeDim) {
  212.       int dim = NodeDim.height;
  213.       Rectangle TreeRect = new Rectangle(dim, dim);
  214.       TreeRect.x = this.NodeDepth * dim;
  215.       TreeRect.y = wndDrawIdx * dim;
  216.       if (this.NodeChildren > 0) {
  217.          Rectangle btn = new Rectangle(TreeRect.x + dim / 4, TreeRect.y + dim / 4, dim / 2, dim / 2);
  218.          if (this.ParentNode.ParentNode != null || this.ParentNode.FirstChildNode != this) {
  219.             g.drawLine(TreeRect.x + dim / 2, TreeRect.y, TreeRect.x + dim / 2, TreeRect.y + dim / 4);
  220.          }
  221.  
  222.          if (this.ParentNode.FirstChildNode != this.YoungerSiblingNode) {
  223.             g.drawLine(TreeRect.x + dim / 2, TreeRect.y + dim / 4 + dim / 2, TreeRect.x + dim / 2, TreeRect.y + dim);
  224.          }
  225.  
  226.          g.drawRect(btn.x, btn.y, btn.width, btn.height);
  227.          g.drawLine(TreeRect.x + dim / 2 + dim / 4, TreeRect.y + dim / 2, TreeRect.x + dim - 1, TreeRect.y + dim / 2);
  228.          g.drawLine(btn.x + 2, btn.y + btn.height / 2, btn.x + btn.width - 2, btn.y + btn.height / 2);
  229.          if (!this.IsExpanded) {
  230.             g.drawLine(btn.x + btn.width / 2, btn.y + 2, btn.x + btn.width / 2, btn.y + btn.height - 2);
  231.          }
  232.       } else {
  233.          if (this.ParentNode.ParentNode != null || this.ParentNode.FirstChildNode != this) {
  234.             g.drawLine(TreeRect.x + dim / 2, TreeRect.y, TreeRect.x + dim / 2, TreeRect.y + dim / 2);
  235.          }
  236.  
  237.          if (this.ParentNode.FirstChildNode != this.YoungerSiblingNode) {
  238.             g.drawLine(TreeRect.x + dim / 2, TreeRect.y + dim / 2, TreeRect.x + dim / 2, TreeRect.y + dim - 1);
  239.          }
  240.  
  241.          g.drawLine(TreeRect.x + dim / 2, TreeRect.y + dim / 2, TreeRect.x + dim - 1, TreeRect.y + dim / 2);
  242.       }
  243.  
  244.       for(OutlineNode nd = this.ParentNode; nd.ParentNode != null; nd = nd.ParentNode) {
  245.          TreeRect.x -= dim;
  246.          if (nd != nd.ParentNode.FirstChildNode.OlderSiblingNode) {
  247.             g.drawLine(TreeRect.x + dim / 2, TreeRect.y, TreeRect.x + dim / 2, TreeRect.y + dim - 1);
  248.          }
  249.       }
  250.  
  251.    }
  252.  
  253.    void getVisibleDescendants(Vector VisibleList) {
  254.       if (this.NodeChildren != 0) {
  255.          if (this.IsExpanded) {
  256.             OutlineNode nd = this.FirstChildNode;
  257.  
  258.             do {
  259.                VisibleList.addElement(nd);
  260.                nd.getVisibleDescendants(VisibleList);
  261.                nd = nd.YoungerSiblingNode;
  262.             } while(nd != this.FirstChildNode);
  263.  
  264.          }
  265.       }
  266.    }
  267. }
  268.