home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / ui / tree / TreeNode.java < prev    next >
Text File  |  1997-01-02  |  8KB  |  349 lines

  1. /* file      : TreeNode.java
  2.  * purpose   : represents a node in a Tree
  3.  *             may have any number of children
  4.  *             children are ordered (by time of creation)
  5.  * author    : Thomas Koch
  6.  * copyright : 1995 , GMD
  7.  *            (Forschungszentrum Informationstechnik GmbH, FIT.CSCW,
  8.  *             GMD Schloss Birlinghoven, 53754 Sankt Augustin, Germany).
  9.  *
  10.  *
  11.  *              Extended to include image labels and highlighting.
  12.  *  Author  :   Cameron Gillies
  13.  */
  14.  
  15. package ui.tree;
  16.  
  17. import java.lang.*;
  18. import java.util.*;
  19. import java.net.URL;
  20. import java.awt.*;
  21. import java.awt.image.*;
  22. import java.net.MalformedURLException;
  23. import java.io.*;
  24. import ui.tree.*;
  25.  
  26. public class TreeNode extends Canvas implements ImageObserver
  27. {
  28.   private Vector children = null;  // Vector of TreeNode
  29.   protected String info = null;    // encoded node position in tree
  30.   protected boolean is_selected,is_open;
  31.   protected Image   oIcon = null;
  32.   protected Image   cIcon = null;
  33.   protected Image   showImage = null;
  34.   protected URL     url = null;
  35.   protected int imgHeight = 0;
  36.   protected int imgWidth = 0;
  37.   protected Color   drawColor;
  38.   protected Color   highlightColor;
  39.   public int x,y;      // x,y will be set by TreeView
  40.   public int wid,hei;     // wid,hei should be set in subclass
  41.  
  42.  
  43.     public TreeNode()
  44.     {
  45.         this("","","");
  46.     }
  47.  
  48.     public TreeNode(String label, String cPath)
  49.     {
  50.         this(label,cPath,cPath);
  51.     }
  52.  
  53.     public TreeNode(String label, String cPath, String oPath)
  54.     {
  55.         is_selected = false;
  56.         is_open = true;
  57.         if((cPath!="") || (cPath != null))
  58.             cIcon = setUpImages(cPath,cIcon);
  59.         if((oPath!="") || (oPath != null))
  60.             oIcon = setUpImages(oPath,oIcon);
  61.  
  62.         if(is_open)
  63.             prepareImage(oIcon, this);
  64.         else
  65.             prepareImage(cIcon, this);
  66.  
  67.         info = new String(label); // root
  68.         children = new Vector(3);
  69.         // paranoia setting :
  70.         x = 0 ;
  71.         y = 0 ;
  72.         wid = 40;
  73.         hei = 20;
  74.     }
  75.  
  76.     private URL stringToURL(String path)
  77.     {
  78.         String  urlPrefix = "file:";
  79. //        File f = new File(path);
  80. //           if (f.exists())
  81. //         {
  82.             try
  83.             {
  84.                 //System.out.println("URL = " + urlPrefix + path);
  85.                 return new URL(urlPrefix + path);
  86.             }
  87.             catch(MalformedURLException ex)
  88.             {
  89.                 System.out.println("Malformed URL");
  90.                 return null;
  91.             }
  92. //        }
  93. //        else
  94. //            return null;
  95.     }
  96.  
  97.     /*
  98.     **  Loads in the required image and prepares it to be shown
  99.     */
  100.     protected Image setUpImages(String fname,Image img)
  101.     {
  102.         url = stringToURL(fname);
  103.         if (url == null)
  104.         {
  105.             System.out.println("Unable to open file - wrong path");
  106.             return img;
  107.         }
  108.         else
  109.         {
  110.             //System.out.println("Url = " + url);
  111.             img = Toolkit.getDefaultToolkit().getImage(url);
  112.         }
  113.         return img;
  114.     }
  115.  
  116.     // override this to show your own data presented by node
  117.     public void draw(Graphics g)
  118.     {
  119.         FontMetrics fm = g.getFontMetrics();
  120.         String str = new String("");
  121.         if ((!is_open) && (!isLeaf()))
  122.         {
  123.             showImage = cIcon;
  124.             str += "[+] ";
  125.         }
  126.         else
  127.             showImage = oIcon;
  128.         str +=info;
  129.         imgWidth = 0;
  130.         imgHeight = 0;
  131.         if (showImage!=null)
  132.         {
  133.             imgWidth = showImage.getWidth(this);
  134.             imgHeight = showImage.getHeight(this);
  135.             g.drawImage(showImage, x , y , this);
  136.         }
  137.         // set the highlighted area
  138.         wid = imgWidth+fm.stringWidth(str)+2;
  139.         hei = fm.getHeight()+3;
  140.         if (is_selected)
  141.         {
  142.             drawColor = Color.white;
  143.             highlightColor = Color.blue;
  144.         }
  145.         else
  146.         {
  147.             drawColor = Color.black;
  148.             highlightColor = Color.white;
  149.         }
  150.         g.setColor(highlightColor);
  151.         g.fillRect(x+imgWidth+2,y,fm.stringWidth(str)+2,fm.getHeight()+2);
  152.         g.setColor(drawColor);
  153.         g.drawString(str,x+imgWidth+3,y+hei-5);
  154.     }
  155.  
  156.     public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
  157.     {
  158.         super.imageUpdate(img,flags,x,y,w,h);
  159.         if((flags & ALLBITS)==0)
  160.             repaint(100);
  161.         return super.imageUpdate(img, flags, x, y, w, h);
  162.     }
  163.  
  164.     public synchronized void addNotify()
  165.     {
  166.         super.addNotify();
  167.         repaint();
  168.     }
  169.  
  170.   public boolean inside(int xc,int yc) {
  171.     if ((xc<x) || (xc>x+wid))
  172.       return false;
  173.     if ((yc<y) || (yc>y+hei))
  174.       return false;
  175.     return true;
  176.   }
  177.  
  178.  
  179.   public String toString() {
  180.     return info;
  181.   }
  182.  
  183.     public void setString(String str)
  184.     {
  185.         info = str;
  186.     }
  187.  
  188.   public int numberChilds() {
  189.     return children.size();
  190.   }
  191.  
  192.  
  193.   public TreeNode getChild(int i) {
  194.     return (TreeNode) children.elementAt(i);
  195.   }
  196.  
  197.  
  198.   public boolean isLeaf() {
  199.     return (children.isEmpty());
  200.   }
  201.  
  202.  
  203.   public boolean isSelected() {
  204.     return is_selected;
  205.   }
  206.  
  207.  
  208.   public boolean isOpen() {
  209.     return is_open;
  210.   }
  211.  
  212.  
  213.   public void select() {
  214.     is_selected=true;
  215.   }
  216.  
  217.  
  218.   public void unSelect() {
  219.     is_selected=false;
  220.   }
  221.  
  222.  
  223.   public void open() {
  224.     is_open = true;
  225.   }
  226.  
  227.  
  228.   public void close() {
  229.     is_open = false;
  230.   }
  231.  
  232.  
  233.   public void addChild(TreeNode n) {
  234.     int s = children.size();
  235.     children.addElement(n);
  236.   }
  237.  
  238.  
  239.   public void changeInfo(int atpos,int toval) {
  240.     String sub1 = info.substring(0,atpos);
  241.     String val = String.valueOf(toval);
  242.     String sub2 = null;
  243.     if ((atpos+1)<info.length())
  244.       sub2 = val.concat(info.substring(atpos+1,info.length()));
  245.     else sub2 = val;
  246.     info = sub1.concat(sub2);
  247.     // System.out.println("changed info string to "+info);
  248.     for (int i=0;i<numberChilds();i++) {
  249.       TreeNode child = getChild(i);
  250.       child.changeInfo(atpos,toval);
  251.     }
  252.   }
  253.  
  254.  
  255.   public void removeSubtree() {
  256.     for (int i=0;i<numberChilds();i++) {
  257.       TreeNode child = getChild(i);
  258.       if (!child.isLeaf())
  259.     child.removeSubtree();
  260.     }
  261.     children.removeAllElements();
  262.   }
  263.  
  264.  
  265.   public void removeSubtree(TreeTool t) {
  266.     // System.out.println("TreeNode::removeSubtree(TreeTool) - this is node "+this);
  267.     for (int i=0;i<numberChilds();i++) {
  268.       TreeNode child = getChild(i);
  269.       if (!child.isLeaf())
  270.     child.removeSubtree(t);
  271.     }
  272.     TreeNode tokill = null;
  273.     do {
  274.     try {
  275.      tokill = (TreeNode) children.lastElement();
  276.     } catch(Exception e) { tokill=null; } // no more children
  277.     if(tokill!=null) {
  278.           children.removeElement(tokill);
  279.       t.notifyRemove(tokill);
  280.     }
  281.      } while(tokill!=null);
  282.   }
  283.  
  284.  
  285.   public boolean removeChild(TreeNode n) { // removes complete subtree !
  286.     // System.out.println("TreeNode::removeChild("+n+") - this is node "+this);
  287.     TreeNode update = null;
  288.     int index = children.indexOf(n);
  289.     if (index<0) {
  290.       System.out.println("TreeNode::removeChild() Error - not a child");
  291.       return false;
  292.     }
  293.     n.removeSubtree();
  294.     children.removeElementAt(index);
  295.  
  296.     // run the garbage collector !
  297.     System.gc();
  298.  
  299.     int pos = info.length();
  300.     while (index<numberChilds()) {
  301.       update = getChild(index);
  302.       update.changeInfo(pos,index);
  303.       index++;
  304.     }
  305.     return true;
  306.   }
  307.  
  308.  
  309.   public boolean removeChild(TreeNode n,TreeTool t) { // removes complete subtree !
  310.     // System.out.println("TreeNode::removeChild("+n+",TreeTool) - this is node "+this);
  311.     TreeNode update = null;
  312.     int index = children.indexOf(n);
  313.     if (index<0) {
  314.       System.out.println("TreeNode::removeChild() Error - not a child");
  315.       return false;
  316.     }
  317.     n.removeSubtree(t);
  318.     children.removeElementAt(index);
  319.     t.notifyRemove(n);
  320.     n = null;
  321.  
  322.     // run the garbage collector !
  323.     System.gc();
  324.  
  325.     int pos = info.length();
  326.     while (index<numberChilds()) {
  327.       update = getChild(index);
  328.       update.changeInfo(pos,index);
  329.       index++;
  330.     }
  331.     return true;
  332.   }
  333.  
  334.  
  335.   public boolean isChild(TreeNode n) {
  336.     return children.contains(n);
  337.   }
  338.  
  339.  
  340.   public boolean equals(Object obj) {
  341.     TreeNode node = (TreeNode)obj;
  342.     if(node == this)
  343.         return true;
  344.     else
  345.         return false;
  346.   }
  347.  
  348. }
  349.