home *** CD-ROM | disk | FTP | other *** search
/ Datatid 2000 #1 / Datatid-2000-01.iso / Internet / JAVA_NAVIGATOR / JAVANAVIGATOR.EXE / %MAINDIR% / files / JClass / JAVATree.jar / TreeView.class (.txt) < prev   
Encoding:
Java Class File  |  1999-06-08  |  22.0 KB  |  1,198 lines

  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.Event;
  7. import java.awt.Font;
  8. import java.awt.FontMetrics;
  9. import java.awt.Graphics;
  10. import java.awt.Image;
  11. import java.awt.LayoutManager;
  12. import java.awt.Panel;
  13. import java.awt.Polygon;
  14. import java.awt.Rectangle;
  15. import java.awt.Scrollbar;
  16. import java.util.StringTokenizer;
  17. import java.util.Vector;
  18.  
  19. public class TreeView extends Panel {
  20.    public static final int LEFT = 0;
  21.    public static final int RIGHT = 1;
  22.    // $FF: renamed from: UP int
  23.    public static final int field_0 = 2;
  24.    public static final int DOWN = 3;
  25.    public static final int CHILD = 0;
  26.    public static final int NEXT = 1;
  27.    public static final int LAST = 2;
  28.    private TreeNode rootNode;
  29.    private TreeNode mouseOverNode;
  30.    private TreeNode selectedNode;
  31.    private TreeNode topVisibleNode;
  32.    Scrollbar sbV;
  33.    int sbVPosition;
  34.    int sbVWidth;
  35.    long sbVTimer;
  36.    private int count;
  37.    private int viewCount;
  38.    private Color bgHighlightColor;
  39.    private Color fgHighlightColor;
  40.    private Color fgMouseOverColor;
  41.    private Color cLineColor;
  42.    private Color cFolderCColor;
  43.    private Color cFolderOColor;
  44.    private Color cLeafColor;
  45.    private Color cToolTipFC;
  46.    private Color cToolTipBC;
  47.    private Color cMouseOverFrame;
  48.    private int viewHeight;
  49.    private int viewWidth;
  50.    private int viewWidest;
  51.    int cellSize;
  52.    int clickSize;
  53.    int imageInset;
  54.    int textInset;
  55.    int textBaseLine;
  56.    // $FF: renamed from: fm java.awt.FontMetrics
  57.    private FontMetrics field_1;
  58.    long timeMouseDown;
  59.    int doubleClickResolution;
  60.    protected Image im1;
  61.    protected Image imgBG;
  62.    // $FF: renamed from: g1 java.awt.Graphics
  63.    protected Graphics field_2;
  64.    private String[] treeStructure;
  65.    int[] xPts;
  66.    int[] yPts;
  67.    int iStep;
  68.    String sStyle;
  69.    String sMouseOver;
  70.    Font fFont;
  71.    String dblClick;
  72.    String sToolTip;
  73.    TreeNode newNode;
  74.    TreeNode oldNode;
  75.    TreeNode oldSNode;
  76.    String flgBorder;
  77.    boolean mouseOver;
  78.    boolean flgToolTip;
  79.    boolean flgMouseOverRect;
  80.    private Polygon poly;
  81.    // $FF: renamed from: e java.util.Vector
  82.    private Vector field_3;
  83.    // $FF: renamed from: v java.util.Vector
  84.    private Vector field_4;
  85.  
  86.    void TreeView_MouseExit(Event event) {
  87.       if (this.oldSNode != null) {
  88.          if (this.flgToolTip) {
  89.             this.flgToolTip = false;
  90.             this.redraw();
  91.          }
  92.  
  93.          this.mouseOver = false;
  94.          this.drawNodeText(this.oldSNode, (this.field_4.indexOf(this.oldSNode) - this.sbVPosition) * this.cellSize, true);
  95.          ((Component)this).repaint();
  96.          this.oldNode = null;
  97.       }
  98.  
  99.    }
  100.  
  101.    public TreeView() {
  102.       this.sbVWidth = 16;
  103.       this.sbVTimer = -1L;
  104.       this.bgHighlightColor = Color.blue;
  105.       this.fgHighlightColor = Color.white;
  106.       this.fgMouseOverColor = Color.red;
  107.       this.cLineColor = Color.black;
  108.       this.cFolderCColor = Color.black;
  109.       this.cFolderOColor = Color.black;
  110.       this.cLeafColor = Color.black;
  111.       this.cToolTipFC = Color.black;
  112.       this.cToolTipBC = Color.yellow;
  113.       this.cMouseOverFrame = Color.red;
  114.       this.viewHeight = 300;
  115.       this.viewWidth = 300;
  116.       this.cellSize = 16;
  117.       this.clickSize = 8;
  118.       this.imageInset = 3;
  119.       this.textInset = 6;
  120.       this.textBaseLine = 3;
  121.       this.doubleClickResolution = 333;
  122.       this.iStep = 3;
  123.       this.sStyle = "AXAA";
  124.       this.sMouseOver = "none";
  125.       this.fFont = new Font("Dialog", 0, 12);
  126.       this.dblClick = "no";
  127.       this.sToolTip = "yes";
  128.       this.mouseOver = false;
  129.       this.flgToolTip = false;
  130.       this.flgMouseOverRect = false;
  131.       super.setLayout(new BorderLayout());
  132.       ((Container)this).add("East", this.sbV = new Scrollbar(1));
  133.    }
  134.  
  135.    public TreeView(TreeNode head) {
  136.       this();
  137.       this.selectedNode = this.rootNode = head;
  138.       this.count = 1;
  139.    }
  140.  
  141.    public void setBackground(Color c) {
  142.       super.setBackground(c);
  143.       ((Component)this).invalidate();
  144.    }
  145.  
  146.    public void setStyle(String s) {
  147.       this.sStyle = s;
  148.       ((Component)this).invalidate();
  149.    }
  150.  
  151.    public void setMouseOverStyle(String s) {
  152.       this.sMouseOver = s;
  153.       ((Component)this).invalidate();
  154.    }
  155.  
  156.    public void setNode(String s) {
  157.       this.recount();
  158.  
  159.       for(int i = 0; i < this.count; ++i) {
  160.          TreeNode tn = (TreeNode)this.field_3.elementAt(i);
  161.          if (tn.text != null && s.equals(tn.text)) {
  162.             this.selectedNode = tn;
  163.             if (tn.depth > 0) {
  164.                TreeNode tn1 = tn.parent;
  165.                tn1.expand();
  166.  
  167.                while(tn1.parent != null) {
  168.                   tn1 = tn1.parent;
  169.                   tn1.expand();
  170.                }
  171.  
  172.                tn.parent.expand();
  173.                this.resetVector();
  174.                this.selectedNode = tn;
  175.             }
  176.  
  177.             if (this.viewable(tn)) {
  178.                this.sbVPosition = this.field_4.indexOf(tn);
  179.                this.sbV.setValue(this.sbVPosition);
  180.             }
  181.  
  182.             this.redraw();
  183.          }
  184.       }
  185.  
  186.    }
  187.  
  188.    public void setForeground(Color c) {
  189.       super.setForeground(c);
  190.       ((Component)this).invalidate();
  191.    }
  192.  
  193.    public void setToolTipForeColor(Color c) {
  194.       this.cToolTipFC = c;
  195.       ((Component)this).invalidate();
  196.    }
  197.  
  198.    public void setToolTipBackColor(Color c) {
  199.       this.cToolTipBC = c;
  200.       ((Component)this).invalidate();
  201.    }
  202.  
  203.    public void setFgHilite(Color c) {
  204.       this.fgHighlightColor = c;
  205.       ((Component)this).invalidate();
  206.    }
  207.  
  208.    public void setMouseOverFrameColor(Color c) {
  209.       this.cMouseOverFrame = c;
  210.       ((Component)this).invalidate();
  211.    }
  212.  
  213.    public void setBgHilite(Color c) {
  214.       this.bgHighlightColor = c;
  215.       ((Component)this).invalidate();
  216.    }
  217.  
  218.    public void setMOHilite(Color c) {
  219.       this.fgMouseOverColor = c;
  220.       ((Component)this).invalidate();
  221.    }
  222.  
  223.    public void setLineColor(Color c) {
  224.       this.cLineColor = c;
  225.       ((Component)this).invalidate();
  226.    }
  227.  
  228.    public void setLineStep(int i) {
  229.       this.iStep = i;
  230.       ((Component)this).invalidate();
  231.    }
  232.  
  233.    public void setFolderOColor(Color c) {
  234.       this.cFolderOColor = c;
  235.       ((Component)this).invalidate();
  236.    }
  237.  
  238.    public void setFolderCColor(Color c) {
  239.       this.cFolderCColor = c;
  240.       ((Component)this).invalidate();
  241.    }
  242.  
  243.    public void setLeafColor(Color c) {
  244.       this.cLeafColor = c;
  245.       ((Component)this).invalidate();
  246.    }
  247.  
  248.    public void setBorder(String s) {
  249.       this.flgBorder = s;
  250.       ((Component)this).invalidate();
  251.    }
  252.  
  253.    public void setFont(Font f) {
  254.       if (f != null) {
  255.          this.fFont = f;
  256.       }
  257.  
  258.       ((Component)this).invalidate();
  259.    }
  260.  
  261.    public void setDblClick(String s) {
  262.       this.dblClick = s;
  263.       ((Component)this).invalidate();
  264.    }
  265.  
  266.    public void setToolTip(String s) {
  267.       this.sToolTip = s;
  268.       ((Component)this).invalidate();
  269.    }
  270.  
  271.    public void setBGImage(Image img) {
  272.       this.imgBG = img;
  273.       ((Component)this).invalidate();
  274.    }
  275.  
  276.    public void insert(TreeNode newNode, TreeNode relativeNode, int position, boolean expand) {
  277.       if (newNode != null && relativeNode != null) {
  278.          if (this.exists(relativeNode)) {
  279.             switch (position) {
  280.                case 0:
  281.                   this.addChild(newNode, relativeNode, expand);
  282.                   return;
  283.                case 1:
  284.                   this.addSibling(newNode, relativeNode, expand);
  285.                   return;
  286.                case 2:
  287.                   this.addSibling(newNode, relativeNode, expand);
  288.                   return;
  289.                default:
  290.             }
  291.          }
  292.       }
  293.    }
  294.  
  295.    public TreeNode getRootNode() {
  296.       return this.rootNode;
  297.    }
  298.  
  299.    public TreeNode getMouseOverNode() {
  300.       return this.mouseOverNode;
  301.    }
  302.  
  303.    public int getCount() {
  304.       return this.count;
  305.    }
  306.  
  307.    public int getViewCount() {
  308.       return this.viewCount;
  309.    }
  310.  
  311.    boolean viewable(TreeNode node) {
  312.       for(int i = 0; i < this.viewCount; ++i) {
  313.          if (node == this.field_4.elementAt(i)) {
  314.             return true;
  315.          }
  316.       }
  317.  
  318.       return false;
  319.    }
  320.  
  321.    boolean viewable(String s) {
  322.       if (s == null) {
  323.          return false;
  324.       } else {
  325.          for(int i = 0; i < this.viewCount; ++i) {
  326.             TreeNode tn = (TreeNode)this.field_4.elementAt(i);
  327.             if (tn.text != null && s.equals(tn.text)) {
  328.                return true;
  329.             }
  330.          }
  331.  
  332.          return false;
  333.       }
  334.    }
  335.  
  336.    public boolean exists(TreeNode node) {
  337.       this.recount();
  338.  
  339.       for(int i = 0; i < this.count; ++i) {
  340.          if (node == this.field_3.elementAt(i)) {
  341.             return true;
  342.          }
  343.       }
  344.  
  345.       return false;
  346.    }
  347.  
  348.    public boolean exists(String s) {
  349.       this.recount();
  350.       if (s == null) {
  351.          return false;
  352.       } else {
  353.          for(int i = 0; i < this.count; ++i) {
  354.             TreeNode tn = (TreeNode)this.field_3.elementAt(i);
  355.             if (tn.text != null && s.equals(tn.text)) {
  356.                return true;
  357.             }
  358.          }
  359.  
  360.          return false;
  361.       }
  362.    }
  363.  
  364.    public void append(TreeNode newNode, boolean expand) {
  365.       if (this.rootNode == null) {
  366.          this.rootNode = newNode;
  367.          this.selectedNode = this.rootNode;
  368.          this.count = 1;
  369.       } else {
  370.          this.addSibling(newNode, this.rootNode, expand);
  371.       }
  372.    }
  373.  
  374.    void addChild(TreeNode newNode, TreeNode relativeNode, boolean expand) {
  375.       if (relativeNode.child == null) {
  376.          relativeNode.child = newNode;
  377.          newNode.parent = relativeNode;
  378.          if (expand) {
  379.             relativeNode.expand();
  380.          }
  381.  
  382.          ++this.count;
  383.       } else {
  384.          this.addSibling(newNode, relativeNode.child, expand);
  385.       }
  386.  
  387.       ++relativeNode.numberOfChildren;
  388.    }
  389.  
  390.    void addSibling(TreeNode newNode, TreeNode siblingNode, boolean expand) {
  391.       TreeNode tempNode;
  392.       for(tempNode = siblingNode; tempNode.sibling != null; tempNode = tempNode.sibling) {
  393.       }
  394.  
  395.       tempNode.sibling = newNode;
  396.       newNode.parent = tempNode.parent;
  397.       if (expand) {
  398.          newNode.expand();
  399.       }
  400.  
  401.       ++this.count;
  402.    }
  403.  
  404.    public TreeNode remove(String s) {
  405.       this.recount();
  406.  
  407.       for(int i = 0; i < this.count; ++i) {
  408.          TreeNode tn = (TreeNode)this.field_3.elementAt(i);
  409.          if (tn.text != null && s.equals(tn.text)) {
  410.             this.remove(tn);
  411.             return tn;
  412.          }
  413.       }
  414.  
  415.       return null;
  416.    }
  417.  
  418.    public void removeSelected() {
  419.       if (this.selectedNode != null) {
  420.          this.remove(this.selectedNode);
  421.       }
  422.  
  423.    }
  424.  
  425.    public void remove(TreeNode node) {
  426.       if (this.exists(node)) {
  427.          if (node == this.selectedNode) {
  428.             int index = this.field_4.indexOf(this.selectedNode);
  429.             if (index == -1) {
  430.                index = this.field_3.indexOf(this.selectedNode);
  431.             }
  432.  
  433.             if (index > this.viewCount - 1) {
  434.                index = this.viewCount - 1;
  435.             }
  436.  
  437.             if (index > 0) {
  438.                this.changeSelection((TreeNode)this.field_4.elementAt(index - 1));
  439.             } else if (this.viewCount > 0) {
  440.                this.changeSelection((TreeNode)this.field_4.elementAt(1));
  441.             }
  442.          }
  443.  
  444.          if (node.parent != null) {
  445.             if (node.parent.child == node) {
  446.                if (node.sibling != null) {
  447.                   node.parent.child = node.sibling;
  448.                } else {
  449.                   node.parent.child = null;
  450.                   node.parent.collapse();
  451.                }
  452.             } else {
  453.                TreeNode tn;
  454.                for(tn = node.parent.child; tn.sibling != node; tn = tn.sibling) {
  455.                }
  456.  
  457.                if (node.sibling != null) {
  458.                   tn.sibling = node.sibling;
  459.                } else {
  460.                   tn.sibling = null;
  461.                }
  462.             }
  463.          } else if (node == this.rootNode) {
  464.             if (node.sibling == null) {
  465.                this.rootNode = null;
  466.             } else {
  467.                this.rootNode = node.sibling;
  468.             }
  469.          } else {
  470.             TreeNode tn;
  471.             for(tn = this.rootNode; tn.sibling != node; tn = tn.sibling) {
  472.             }
  473.  
  474.             if (node.sibling != null) {
  475.                tn.sibling = node.sibling;
  476.             } else {
  477.                tn.sibling = null;
  478.             }
  479.          }
  480.  
  481.          this.recount();
  482.       }
  483.    }
  484.  
  485.    private void recount() {
  486.       this.count = 0;
  487.       this.field_3 = new Vector();
  488.       if (this.rootNode != null) {
  489.          this.rootNode.depth = 0;
  490.          this.traverse(this.rootNode);
  491.       }
  492.  
  493.    }
  494.  
  495.    protected void drawToolTip(Graphics g2, int x, int y, String s, FontMetrics fm, Color bc, Color fc) {
  496.       if (this.sToolTip.toLowerCase().equals("yes")) {
  497.          Dimension d = ((Component)this).size();
  498.          if (x + fm.stringWidth(s) > d.width - this.sbVWidth && y > 0) {
  499.             String sWord = "";
  500.             String tmpS = "";
  501.             String tmpS1 = "";
  502.             String tmpS2 = "";
  503.             String tmpS3 = "";
  504.             int pos = 0;
  505.             x = 0;
  506.             pos = s.indexOf(32, pos);
  507.             if (pos == -1) {
  508.                for(int i = 0; i < s.length(); ++i) {
  509.                   if (x + fm.stringWidth(tmpS1) < d.width - (this.sbVWidth + 10)) {
  510.                      tmpS1 = tmpS1 + s.charAt(i);
  511.                   } else {
  512.                      tmpS2 = tmpS2 + s.charAt(i);
  513.                   }
  514.                }
  515.             } else {
  516.                StringTokenizer parser = new StringTokenizer(s.trim(), " ");
  517.  
  518.                while(parser.hasMoreTokens()) {
  519.                   try {
  520.                      sWord = parser.nextToken();
  521.                      if (tmpS.equals("")) {
  522.                         tmpS = sWord;
  523.                      } else {
  524.                         tmpS = tmpS + " " + sWord;
  525.                      }
  526.  
  527.                      if (x + fm.stringWidth(tmpS) > d.width - (this.sbVWidth + 10)) {
  528.                         if (!tmpS1.trim().equals("")) {
  529.                            tmpS2 = tmpS3;
  530.                            break;
  531.                         }
  532.  
  533.                         tmpS1 = tmpS3;
  534.                         tmpS = "";
  535.                         tmpS3 = "";
  536.                      }
  537.  
  538.                      if (tmpS3.equals("")) {
  539.                         tmpS3 = sWord;
  540.                      } else {
  541.                         tmpS3 = tmpS3 + " " + sWord;
  542.                      }
  543.                   } catch (Exception var16) {
  544.                   }
  545.                }
  546.  
  547.                if (!tmpS3.equals("")) {
  548.                   if (tmpS1.trim().equals("")) {
  549.                      tmpS1 = tmpS3;
  550.                   } else {
  551.                      tmpS2 = tmpS3;
  552.                   }
  553.                }
  554.             }
  555.  
  556.             if (y + fm.getHeight() > d.height) {
  557.                y = d.height - fm.getHeight() - 2;
  558.             }
  559.  
  560.             g2.setColor(bc);
  561.             if (tmpS2.equals("")) {
  562.                g2.fillRect(x, y, d.width - 1 - x - this.sbVWidth, fm.getHeight() + 2);
  563.                g2.setColor(fc);
  564.                g2.drawRect(x, y, d.width - 1 - x - this.sbVWidth, fm.getHeight() + 2);
  565.                this.field_2.drawString(tmpS1, x + 5, y + this.cellSize - this.textBaseLine);
  566.             } else {
  567.                g2.fillRect(x, y, d.width - 1 - x - this.sbVWidth, fm.getHeight() * 2 + 2);
  568.                g2.setColor(fc);
  569.                g2.drawRect(x, y, d.width - 1 - x - this.sbVWidth, fm.getHeight() * 2 + 2);
  570.                this.field_2.drawString(tmpS1, x + 5, y + this.cellSize - this.textBaseLine);
  571.                this.field_2.drawString(tmpS2, x + 5, y + fm.getHeight() + this.cellSize - this.textBaseLine);
  572.             }
  573.  
  574.             this.flgToolTip = true;
  575.          }
  576.  
  577.       }
  578.    }
  579.  
  580.    private void traverse(TreeNode node) {
  581.       ++this.count;
  582.       this.field_3.addElement(node);
  583.       if (node.child != null) {
  584.          node.child.depth = node.depth + 1;
  585.          this.traverse(node.child);
  586.       }
  587.  
  588.       if (node.sibling != null) {
  589.          node.sibling.depth = node.depth;
  590.          this.traverse(node.sibling);
  591.       }
  592.  
  593.    }
  594.  
  595.    private void resetVector() {
  596.       this.field_4 = new Vector(this.count);
  597.       this.viewWidest = 30;
  598.       if (this.count < 1) {
  599.          this.viewCount = 0;
  600.       } else {
  601.          this.rootNode.depth = 0;
  602.          this.vectorize(this.rootNode);
  603.          this.viewCount = this.field_4.size();
  604.       }
  605.    }
  606.  
  607.    private void vectorize(TreeNode node) {
  608.       if (node != null) {
  609.          this.field_4.addElement(node);
  610.          if (node.isExpanded()) {
  611.             node.child.depth = node.depth + 1;
  612.             this.vectorize(node.child);
  613.          }
  614.  
  615.          if (node.sibling != null) {
  616.             node.sibling.depth = node.depth;
  617.             this.vectorize(node.sibling);
  618.          }
  619.  
  620.       }
  621.    }
  622.  
  623.    public boolean handleEvent(Event event) {
  624.       if (event.target == this.sbV) {
  625.          if (event.arg == null) {
  626.             return false;
  627.          }
  628.  
  629.          if (this.sbVPosition != (Integer)event.arg) {
  630.             this.sbVPosition = (Integer)event.arg;
  631.             if (this.sbVPosition < 0) {
  632.                this.sbVPosition = 0;
  633.             } else {
  634.                this.redraw();
  635.             }
  636.          }
  637.       }
  638.  
  639.       if (event.target == this && event.id == 505) {
  640.          this.TreeView_MouseExit(event);
  641.          return true;
  642.       } else {
  643.          return super.handleEvent(event);
  644.       }
  645.    }
  646.  
  647.    public boolean mouseMove(Event event, int x, int y) {
  648.       try {
  649.          int index = y / this.cellSize + this.sbVPosition;
  650.          if (index > this.viewCount - 1) {
  651.             if (this.oldSNode != null) {
  652.                this.mouseOver = false;
  653.                this.drawNodeText(this.oldSNode, (this.field_4.indexOf(this.oldSNode) - this.sbVPosition) * this.cellSize, true);
  654.                ((Component)this).repaint();
  655.             }
  656.  
  657.             this.oldNode = null;
  658.             this.mouseOverNode = null;
  659.             this.flgToolTip = false;
  660.             this.redraw();
  661.          }
  662.  
  663.          this.newNode = (TreeNode)this.field_4.elementAt(index);
  664.          if (this.newNode != this.oldNode) {
  665.             if (this.flgToolTip) {
  666.                this.flgToolTip = false;
  667.                this.redraw();
  668.             }
  669.  
  670.             if (this.flgMouseOverRect) {
  671.                this.flgMouseOverRect = false;
  672.                this.redraw();
  673.             }
  674.  
  675.             if (this.oldSNode != null) {
  676.                this.mouseOver = false;
  677.                this.drawNodeText(this.oldSNode, (this.field_4.indexOf(this.oldSNode) - this.sbVPosition) * this.cellSize, true);
  678.             }
  679.  
  680.             this.mouseOver = true;
  681.             this.drawNodeText(this.newNode, (this.field_4.indexOf(this.newNode) - this.sbVPosition) * this.cellSize, true);
  682.             ((Component)this).repaint();
  683.             this.oldNode = this.newNode;
  684.          }
  685.  
  686.          ((Component)this).repaint();
  687.       } catch (Exception var5) {
  688.       }
  689.  
  690.       return super.mouseMove(event, x, y);
  691.    }
  692.  
  693.    public boolean mouseDown(Event event, int x, int y) {
  694.       int index = y / this.cellSize + this.sbVPosition;
  695.       if (index > this.viewCount - 1) {
  696.          this.mouseOverNode = null;
  697.          return false;
  698.       } else {
  699.          TreeNode oldNode = this.selectedNode;
  700.          TreeNode newNode = (TreeNode)this.field_4.elementAt(index);
  701.          int newDepth = newNode.getDepth();
  702.          Rectangle toggleBox = new Rectangle(this.cellSize * newDepth + this.cellSize / 4, (index - this.sbVPosition) * this.cellSize + this.clickSize / 2, this.clickSize, this.clickSize);
  703.          if (toggleBox.inside(x, y)) {
  704.             newNode.toggle();
  705.             this.redraw();
  706.          } else {
  707.             this.changeSelection(newNode);
  708.             if (this.dblClick.toLowerCase().equals("yes")) {
  709.                if (newNode == oldNode && event.when - this.timeMouseDown < (long)this.doubleClickResolution) {
  710.                   newNode.toggle();
  711.                   this.redraw();
  712.                   this.sendActionEvent(event);
  713.                   return false;
  714.                }
  715.  
  716.                this.timeMouseDown = event.when;
  717.                this.redraw();
  718.             } else {
  719.                if (newNode != oldNode || event.when - this.timeMouseDown >= (long)this.doubleClickResolution) {
  720.                   newNode.toggle();
  721.                   this.redraw();
  722.                   this.sendActionEvent(event);
  723.                   return false;
  724.                }
  725.  
  726.                this.timeMouseDown = event.when;
  727.             }
  728.          }
  729.  
  730.          return true;
  731.       }
  732.    }
  733.  
  734.    public boolean keyDown(Event event, int key) {
  735.       int index = this.field_4.indexOf(this.selectedNode);
  736.       switch (key) {
  737.          case 10:
  738.             TreeNode newNode = (TreeNode)this.field_4.elementAt(index);
  739.             this.changeSelection(newNode);
  740.             newNode.toggle();
  741.             this.redraw();
  742.             event.id = 501;
  743.             event.arg = new String(this.selectedNode.getText());
  744.             return false;
  745.          case 1006:
  746.             if (this.selectedNode.isExpanded()) {
  747.                this.selectedNode.toggle();
  748.                this.redraw();
  749.                break;
  750.             }
  751.          case 1004:
  752.             if (index > 0) {
  753.                --index;
  754.                this.changeSelection((TreeNode)this.field_4.elementAt(index));
  755.                this.redraw();
  756.                ((Component)this).requestFocus();
  757.             }
  758.             break;
  759.          case 1007:
  760.             if (this.selectedNode.isExpandable() && !this.selectedNode.isExpanded()) {
  761.                this.selectedNode.toggle();
  762.                this.redraw();
  763.                break;
  764.             } else if (!this.selectedNode.isExpandable()) {
  765.                break;
  766.             }
  767.          case 1005:
  768.             if (index < this.viewCount - 1) {
  769.                ++index;
  770.                this.changeSelection((TreeNode)this.field_4.elementAt(index));
  771.                this.redraw();
  772.                ((Component)this).requestFocus();
  773.             }
  774.       }
  775.  
  776.       return false;
  777.    }
  778.  
  779.    private void sendActionEvent(Event event) {
  780.       int id = event.id;
  781.       Object arg = event.arg;
  782.       event.id = 1001;
  783.       event.arg = new String(this.selectedNode.getText());
  784.       ((Component)this).postEvent(event);
  785.       event.id = id;
  786.       event.arg = arg;
  787.    }
  788.  
  789.    public TreeNode getSelectedNode() {
  790.       return this.selectedNode;
  791.    }
  792.  
  793.    public String getSelectedText() {
  794.       return this.selectedNode == null ? null : this.selectedNode.getText();
  795.    }
  796.  
  797.    private void changeSelection(TreeNode node) {
  798.       if (node != this.selectedNode) {
  799.          TreeNode oldNode = this.selectedNode;
  800.          this.selectedNode = node;
  801.          this.drawNodeText(oldNode, (this.field_4.indexOf(oldNode) - this.sbVPosition) * this.cellSize, true);
  802.          this.drawNodeText(node, (this.field_4.indexOf(node) - this.sbVPosition) * this.cellSize, true);
  803.          int index = this.field_4.indexOf(this.selectedNode);
  804.          if (index < this.sbVPosition) {
  805.             --this.sbVPosition;
  806.             this.sbV.setValue(this.sbVPosition);
  807.             this.redraw();
  808.          } else if (index >= this.sbVPosition + (this.viewHeight - this.cellSize / 2) / this.cellSize) {
  809.             ++this.sbVPosition;
  810.             this.sbV.setValue(this.sbVPosition);
  811.             this.redraw();
  812.          } else {
  813.             ((Component)this).repaint();
  814.          }
  815.       }
  816.    }
  817.  
  818.    public void update(Graphics g) {
  819.       this.paint(g);
  820.    }
  821.  
  822.    public void paint(Graphics g) {
  823.       Dimension d = ((Component)this).size();
  824.       if (d.width == this.viewWidth && d.height == this.viewHeight) {
  825.          if (Beans.isDesignTime()) {
  826.             this.resetVector();
  827.             ((Container)this).layout();
  828.             this.drawTree();
  829.          }
  830.       } else {
  831.          this.redraw();
  832.       }
  833.  
  834.       g.drawImage(this.im1, 0, 0, this);
  835.    }
  836.  
  837.    public void redraw() {
  838.       this.resetVector();
  839.       if (this.viewCount > this.viewHeight / this.cellSize) {
  840.          System.out.println("viewCount: " + this.viewCount + " | viewHeight/cellSize: " + this.viewHeight / this.cellSize);
  841.          this.sbV.setValues(this.sbVPosition, this.viewHeight / this.cellSize, 0, this.viewCount - 2);
  842.          this.sbV.setPageIncrement(1);
  843.          this.sbVWidth = 16;
  844.          ((Component)this).getParent().paintAll(((Component)this).getParent().getGraphics());
  845.          this.sbV.show();
  846.          ((Container)this).layout();
  847.       } else {
  848.          this.sbV.hide();
  849.          this.sbVWidth = 0;
  850.          this.sbVPosition = 0;
  851.          ((Container)this).layout();
  852.       }
  853.  
  854.       this.drawTree();
  855.       ((Component)this).repaint();
  856.    }
  857.  
  858.    public void drawTree() {
  859.       Dimension d = ((Component)this).size();
  860.       if (d.width != this.viewWidth || d.height != this.viewHeight || this.field_2 == null) {
  861.          this.im1 = ((Component)this).createImage(d.width, d.height);
  862.          if (this.field_2 != null) {
  863.             this.field_2.dispose();
  864.          }
  865.  
  866.          this.field_2 = this.im1.getGraphics();
  867.          this.viewWidth = d.width;
  868.          this.viewHeight = d.height;
  869.       }
  870.  
  871.       this.field_2.setFont(this.fFont);
  872.       this.setFont(this.fFont);
  873.       this.field_1 = ((Component)this).getFontMetrics(this.fFont);
  874.       this.field_2.setColor(((Component)this).getBackground());
  875.       if (this.imgBG != null) {
  876.          for(int x = 0; x < d.width; x += this.imgBG.getWidth(this)) {
  877.             for(int i = 0; i < d.height; i += this.imgBG.getHeight(this)) {
  878.                this.field_2.drawImage(this.imgBG, x, i, this.imgBG.getWidth(this), this.imgBG.getHeight(this), Color.black, this);
  879.             }
  880.          }
  881.  
  882.          ((Component)this).repaint();
  883.       } else {
  884.          this.field_2.fillRect(0, 0, this.viewWidth, this.viewHeight);
  885.       }
  886.  
  887.       int lastOne = this.sbVPosition + this.viewHeight / this.cellSize;
  888.       if (lastOne > this.viewCount) {
  889.          lastOne = this.viewCount;
  890.       }
  891.  
  892.       for(int i = this.sbVPosition; i < lastOne; ++i) {
  893.          TreeNode node = (TreeNode)this.field_4.elementAt(i);
  894.          int x = this.cellSize * (node.depth + 1);
  895.          int y = (i - this.sbVPosition) * this.cellSize;
  896.          if (this.sStyle.charAt(2) != 'X') {
  897.             this.field_2.setColor(((Component)this).getForeground());
  898.             if (node.sibling != null) {
  899.                int k = this.field_4.indexOf(node.sibling) - i;
  900.                if (k > lastOne) {
  901.                   k = lastOne;
  902.                }
  903.  
  904.                this.drawDotLine(x - this.cellSize / 2, y + this.cellSize / 2, x - this.cellSize / 2, y + this.cellSize / 2 + k * this.cellSize);
  905.             }
  906.  
  907.             for(int m = 0; m < i; ++m) {
  908.                TreeNode sib = (TreeNode)this.field_4.elementAt(m);
  909.                if (sib.sibling == node && m < this.sbVPosition) {
  910.                   this.drawDotLine(x - this.cellSize / 2, 0, x - this.cellSize / 2, y + this.cellSize / 2);
  911.                }
  912.             }
  913.  
  914.             if (node.isExpanded()) {
  915.                this.drawDotLine(x + this.cellSize / 2, y + this.cellSize, x + this.cellSize / 2, y + this.cellSize + this.cellSize / 2);
  916.             }
  917.  
  918.             this.field_2.setColor(((Component)this).getForeground());
  919.             this.drawDotLine(x - this.cellSize / 2, y + this.cellSize / 2, x + this.cellSize / 2, y + this.cellSize / 2);
  920.          }
  921.  
  922.          if (this.sStyle.charAt(0) == 'A') {
  923.             if (node.isExpandable()) {
  924.                this.field_2.setColor(((Component)this).getBackground());
  925.                this.field_2.fillRect(this.cellSize * node.depth + this.cellSize / 4, y + this.clickSize / 2, this.clickSize, this.clickSize);
  926.                this.field_2.setColor(this.cLineColor);
  927.                this.field_2.drawRect(this.cellSize * node.depth + this.cellSize / 4, y + this.clickSize / 2, this.clickSize, this.clickSize);
  928.                this.field_2.drawLine(this.cellSize * node.depth + this.cellSize / 4 + 2, y + this.cellSize / 2, this.cellSize * node.depth + this.cellSize / 4 + this.clickSize - 2, y + this.cellSize / 2);
  929.                if (!node.isExpanded()) {
  930.                   this.field_2.drawLine(this.cellSize * node.depth + this.cellSize / 2, y + this.clickSize / 2 + 2, this.cellSize * node.depth + this.cellSize / 2, y + this.clickSize / 2 + this.clickSize - 2);
  931.                }
  932.             }
  933.          } else if (node.isExpandable()) {
  934.             this.xPts = new int[4];
  935.             this.yPts = new int[4];
  936.             if (!node.isExpanded()) {
  937.                if (this.sStyle.charAt(0) == 'E' || this.sStyle.charAt(0) == 'F') {
  938.                   this.field_2.setColor(this.cFolderCColor);
  939.                   this.xPts[0] = this.cellSize * node.depth + this.cellSize / 4 + 1;
  940.                   this.xPts[1] = this.cellSize * node.depth + this.cellSize / 4 + this.clickSize;
  941.                   this.xPts[2] = this.cellSize * node.depth + this.cellSize / 4 + 1;
  942.                   this.xPts[3] = this.cellSize * node.depth + this.cellSize / 4 + 1;
  943.                   this.yPts[0] = y + 1;
  944.                   this.yPts[1] = y + this.cellSize / 2;
  945.                   this.yPts[2] = y + this.cellSize - 1;
  946.                   this.yPts[3] = y + 1;
  947.                   this.poly = new Polygon(this.xPts, this.yPts, this.xPts.length);
  948.                   this.field_2.fillPolygon(this.poly);
  949.                }
  950.  
  951.                if (this.sStyle.charAt(0) == 'B' || this.sStyle.charAt(0) == 'C' || this.sStyle.charAt(0) == 'D') {
  952.                   this.field_2.setColor(this.cFolderCColor);
  953.                   this.field_2.fillRect(this.cellSize * node.depth + this.cellSize / 4, y + this.clickSize / 2, this.clickSize + 2, this.clickSize + 2);
  954.                }
  955.             } else {
  956.                if (this.sStyle.charAt(0) == 'C' || this.sStyle.charAt(0) == 'E') {
  957.                   this.field_2.setColor(this.cFolderOColor);
  958.                   this.xPts[0] = this.cellSize * node.depth + this.cellSize / 4 - 2;
  959.                   this.xPts[1] = this.cellSize * node.depth + this.cellSize / 4 - 2 + this.clickSize + 2;
  960.                   this.xPts[2] = this.cellSize * node.depth + this.cellSize / 4 - 2 + (this.clickSize + 2) / 2;
  961.                   this.xPts[3] = this.cellSize * node.depth + this.cellSize / 4 - 2;
  962.                   this.yPts[0] = y + 3;
  963.                   this.yPts[1] = y + 3;
  964.                   this.yPts[2] = y + this.cellSize - 3;
  965.                   this.yPts[3] = y + 3;
  966.                   this.poly = new Polygon(this.xPts, this.yPts, this.xPts.length);
  967.                   this.field_2.fillPolygon(this.poly);
  968.                }
  969.  
  970.                if (this.sStyle.charAt(0) == 'D' || this.sStyle.charAt(0) == 'F') {
  971.                   this.field_2.setColor(this.cFolderOColor);
  972.                   this.xPts[0] = this.cellSize * node.depth + this.cellSize / 4 - 2 + (this.clickSize + 2) / 2;
  973.                   this.xPts[1] = this.cellSize * node.depth + this.cellSize / 4 - 2 + this.clickSize + 2;
  974.                   this.xPts[2] = this.cellSize * node.depth + this.cellSize / 4 - 2;
  975.                   this.xPts[3] = this.cellSize * node.depth + this.cellSize / 4 - 2 + (this.clickSize + 2) / 2;
  976.                   this.yPts[0] = y + 3;
  977.                   this.yPts[1] = y + this.cellSize - 3;
  978.                   this.yPts[2] = y + this.cellSize - 3;
  979.                   this.yPts[3] = y + 3;
  980.                   this.poly = new Polygon(this.xPts, this.yPts, this.xPts.length);
  981.                   this.field_2.fillPolygon(this.poly);
  982.                }
  983.  
  984.                if (this.sStyle.charAt(0) == 'B') {
  985.                   this.field_2.setColor(this.cFolderOColor);
  986.                   this.xPts[0] = this.cellSize * node.depth + this.cellSize / 4 + 1;
  987.                   this.xPts[1] = this.cellSize * node.depth + this.cellSize / 4 + this.clickSize;
  988.                   this.xPts[2] = this.cellSize * node.depth + this.cellSize / 4 + 1;
  989.                   this.xPts[3] = this.cellSize * node.depth + this.cellSize / 4 + 1;
  990.                   this.yPts[0] = y + 1;
  991.                   this.yPts[1] = y + this.cellSize / 2;
  992.                   this.yPts[2] = y + this.cellSize - 1;
  993.                   this.yPts[3] = y + 1;
  994.                   this.poly = new Polygon(this.xPts, this.yPts, this.xPts.length);
  995.                   this.field_2.fillPolygon(this.poly);
  996.                }
  997.             }
  998.          } else if (this.sStyle.charAt(1) == 'A') {
  999.             this.field_2.setColor(this.cLeafColor);
  1000.             this.field_2.fillRect(this.cellSize * node.depth + this.cellSize / 4, y + this.clickSize / 2, this.clickSize, this.clickSize);
  1001.          }
  1002.  
  1003.          Image nodeImage = node.getImage();
  1004.          if (this.sStyle.charAt(3) == 'A') {
  1005.             if (this.sStyle.charAt(0) != 'X' && this.sStyle.charAt(2) == 'X') {
  1006.                --x;
  1007.             }
  1008.  
  1009.             if (this.sStyle.charAt(2) == 'X' && this.sStyle.charAt(0) == 'X') {
  1010.                x -= 10;
  1011.             }
  1012.  
  1013.             if (nodeImage != null) {
  1014.                this.field_2.drawImage(nodeImage, x + this.imageInset, y, this);
  1015.             }
  1016.          }
  1017.  
  1018.          if (node.text != null) {
  1019.             this.drawNodeText(node, y, node == this.selectedNode);
  1020.          }
  1021.       }
  1022.  
  1023.       if (this.flgBorder.toLowerCase().equals("yes")) {
  1024.          this.field_2.setColor(((Component)this).getForeground());
  1025.          this.field_2.drawRect(0, 0, this.viewWidth - this.sbVWidth - 1, this.viewHeight - 1);
  1026.       }
  1027.  
  1028.    }
  1029.  
  1030.    private void drawNodeText(TreeNode node, int yPosition, boolean eraseBackground) {
  1031.       int depth = node.depth;
  1032.       int textOffset = (depth + 1) * this.cellSize + this.cellSize + this.textInset;
  1033.       if (this.sStyle.charAt(3) != 'A') {
  1034.          textOffset -= 15;
  1035.          if (this.sStyle.charAt(2) == 'X') {
  1036.             textOffset -= 5;
  1037.          }
  1038.       } else if (this.sStyle.charAt(0) == 'X' && this.sStyle.charAt(2) == 'X') {
  1039.          textOffset -= 5;
  1040.       }
  1041.  
  1042.       Color fg;
  1043.       if (node == this.selectedNode) {
  1044.          fg = this.fgHighlightColor;
  1045.          Color bg = this.bgHighlightColor;
  1046.          this.mouseOverNode = node;
  1047.          if (eraseBackground) {
  1048.             this.field_2.setColor(bg);
  1049.             this.field_2.fillRect(textOffset - 1, yPosition + 1, this.field_1.stringWidth(node.text) + 4, this.cellSize - 1);
  1050.          }
  1051.       } else {
  1052.          fg = ((Component)this).getForeground();
  1053.          Color bg = ((Component)this).getBackground();
  1054.          if (this.mouseOver) {
  1055.             fg = this.fgMouseOverColor;
  1056.             this.mouseOverNode = node;
  1057.             this.oldSNode = node;
  1058.          }
  1059.       }
  1060.  
  1061.       this.field_2.setColor(fg);
  1062.       this.field_2.drawString(node.text, textOffset, yPosition + this.cellSize - this.textBaseLine);
  1063.       if (this.mouseOver) {
  1064.          if (this.sMouseOver.toLowerCase().equals("rect")) {
  1065.             this.field_2.setColor(this.cMouseOverFrame);
  1066.             this.field_2.drawRect(textOffset - 1, yPosition + 1, this.field_1.stringWidth(node.text) + 1, this.cellSize - 1);
  1067.             this.flgMouseOverRect = true;
  1068.          }
  1069.  
  1070.          if (this.sMouseOver.toLowerCase().equals("line")) {
  1071.             this.field_2.setColor(this.cMouseOverFrame);
  1072.             this.field_2.drawRect(textOffset - 1, yPosition + 1 + this.field_1.getHeight(), this.field_1.stringWidth(node.text) + 1, 1);
  1073.             this.flgMouseOverRect = true;
  1074.          }
  1075.  
  1076.          this.drawToolTip(this.field_2, textOffset - 1, yPosition + 1, node.text, this.field_1, this.cToolTipBC, this.cToolTipFC);
  1077.          this.mouseOver = false;
  1078.       }
  1079.  
  1080.    }
  1081.  
  1082.    private void drawDotLine(int x0, int y0, int x1, int y1) {
  1083.       this.field_2.setColor(this.cLineColor);
  1084.       int iStep1;
  1085.       if (this.sStyle.charAt(2) == 'A') {
  1086.          iStep1 = this.iStep;
  1087.       } else {
  1088.          iStep1 = 1;
  1089.       }
  1090.  
  1091.       if (y0 == y1) {
  1092.          for(int i = x0; i < x1; i += iStep1) {
  1093.             this.field_2.drawLine(i, y0, i, y1);
  1094.          }
  1095.  
  1096.       } else {
  1097.          for(int i = y0; i < y1; i += iStep1) {
  1098.             this.field_2.drawLine(x0, i, x1, i);
  1099.          }
  1100.  
  1101.       }
  1102.    }
  1103.  
  1104.    public void setTreeStructure(String[] s) {
  1105.       this.rootNode = this.selectedNode = this.topVisibleNode = null;
  1106.       this.treeStructure = s;
  1107.  
  1108.       try {
  1109.          this.parseTreeStructure();
  1110.       } catch (InvalidTreeNodeException e) {
  1111.          System.out.println(e);
  1112.       }
  1113.    }
  1114.  
  1115.    public String[] getTreeStructure() {
  1116.       return this.treeStructure;
  1117.    }
  1118.  
  1119.    private void parseTreeStructure() throws InvalidTreeNodeException {
  1120.       String[] tempStructure = null;
  1121.       String entry = null;
  1122.       tempStructure = this.treeStructure;
  1123.       entry = tempStructure[0];
  1124.       if (this.findLastPreSpace(entry) > -1) {
  1125.          throw new InvalidTreeNodeException();
  1126.       } else {
  1127.          TreeNode node = new TreeNode(entry.trim());
  1128.          node.setDepth(0);
  1129.          this.append(node, false);
  1130.  
  1131.          for(int i = 1; i < tempStructure.length; ++i) {
  1132.             entry = tempStructure[i];
  1133.             int indentLevel = this.findLastPreSpace(entry);
  1134.             if (indentLevel == -1) {
  1135.                throw new InvalidTreeNodeException();
  1136.             }
  1137.  
  1138.             TreeNode currentNode = this.rootNode;
  1139.  
  1140.             for(int j = 0; j < indentLevel; ++j) {
  1141.                int numberOfChildren = currentNode.numberOfChildren;
  1142.                TreeNode tempNode = null;
  1143.                if (numberOfChildren > 0) {
  1144.                   for(tempNode = currentNode.child; tempNode.sibling != null; tempNode = tempNode.sibling) {
  1145.                   }
  1146.                }
  1147.  
  1148.                if (tempNode == null) {
  1149.                   break;
  1150.                }
  1151.  
  1152.                currentNode = tempNode;
  1153.             }
  1154.  
  1155.             int diff = indentLevel - currentNode.getDepth();
  1156.             if (diff > 1) {
  1157.                throw new InvalidTreeNodeException();
  1158.             }
  1159.  
  1160.             node = new TreeNode(entry.trim());
  1161.             node.setDepth(indentLevel);
  1162.             if (diff == 1) {
  1163.                this.insert(node, currentNode, 0, false);
  1164.             } else {
  1165.                this.insert(node, currentNode, 1, false);
  1166.             }
  1167.          }
  1168.  
  1169.       }
  1170.    }
  1171.  
  1172.    private int findLastPreSpace(String s) {
  1173.       int length = s.length();
  1174.       if (s.charAt(0) != ' ') {
  1175.          return -1;
  1176.       } else {
  1177.          for(int i = 1; i < length; ++i) {
  1178.             if (s.charAt(i) != ' ') {
  1179.                return i;
  1180.             }
  1181.          }
  1182.  
  1183.          return -1;
  1184.       }
  1185.    }
  1186.  
  1187.    public synchronized Dimension preferredSize() {
  1188.       return new Dimension(175, 125);
  1189.    }
  1190.  
  1191.    public synchronized Dimension minimumSize() {
  1192.       return new Dimension(50, 50);
  1193.    }
  1194.  
  1195.    public void setLayout(LayoutManager lm) {
  1196.    }
  1197. }
  1198.