home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / TreeNode.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  5.0 KB  |  239 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Image;
  4.  
  5. //    02/10/97    RKM    Added data object
  6. //    02/27/97    RKM    Merged in accessors for getParent, getChild, & getSibling
  7.  
  8. /**
  9.  * This is a single node in the TreeView panel. It can have both text and/or an
  10.  * image.
  11.  * @see TreeView
  12.  */
  13. public class TreeNode
  14. {
  15.     TreeNode    sibling;
  16.     TreeNode    child;
  17.     TreeNode    parent;
  18.     String        text;
  19.     Image        collapsedImage;
  20.     Image        expandedImage;
  21.     int            numberOfChildren;
  22.     Object        dataObject;
  23.     
  24.     int            depth = -1;
  25.     boolean    isExpanded = false;
  26.     
  27.     //constructors
  28.  
  29.     /**
  30.      * Constructs a TreeNode with a given text label.
  31.      * @param text the text to display for this node
  32.      */
  33.     public TreeNode(String text)
  34.     {
  35.         this(text, null, null);
  36.     }
  37.     
  38.     /**
  39.      * Constructs a TreeNode with a given text label, and collapsed and
  40.      * expanded images.
  41.      * @param text the text to display for this node
  42.      * @param collapsedImage the image to use when this node is collapsed, hiding
  43.      * all of its child nodes
  44.      * @param expandedImage the image to use when this node is expanded, showing
  45.      * all of its child nodes
  46.      */
  47.     public TreeNode(String text, Image collapsedImage, Image expandedImage)
  48.     {
  49.         this.text                = text;
  50.         this.sibling            = null;
  51.         this.child                = null;
  52.         this.collapsedImage     = collapsedImage;
  53.         this.expandedImage      = expandedImage;
  54.         this.numberOfChildren    = 0;
  55.         this.dataObject            = null;
  56.     }
  57.  
  58.     /**
  59.      * Notes the current depth of this node.
  60.      * @param depth
  61.      * @see #getDepth
  62.      */
  63.     void setDepth(int depth)
  64.     {
  65.         this.depth = depth;
  66.     }
  67.     
  68.     /**
  69.      * Gets the depth of this node as previously noted.
  70.      * @return the depth of this node
  71.      * @see #setDepth
  72.      */
  73.     public int getDepth()
  74.     {
  75.         return depth;
  76.     }
  77.     
  78.     /**
  79.      * Gets the expanded state of this node. A node is expanded
  80.      * if its child nodes are visible.
  81.      * @return true if the node is expanded, false if collapsed
  82.      */
  83.     public boolean isExpanded()
  84.     {
  85.         return isExpanded;
  86.     }
  87.     
  88.     /**
  89.      * Gets whether this node is expandable. A node is expandable
  90.      * if it has one or more child nodes.
  91.      * @return true if the node is expandable, false if not
  92.      */
  93.     public boolean isExpandable()
  94.     {
  95.         return (child!=null);
  96.     }
  97.  
  98.     /**
  99.      * Notes that this node is expanded, if it is expandable.
  100.      */
  101.     public void expand()
  102.     {
  103.         if (isExpandable())
  104.         {
  105.             isExpanded=true;
  106.         }
  107.     }
  108.     
  109.     /**
  110.      * Notes that this node is not expanded.
  111.      */
  112.     public void collapse()
  113.     {
  114.         isExpanded = false;
  115.     }
  116.     
  117.     /**
  118.      * Toggles the node state between collapsed and expanded, if the node
  119.      * is expandable.
  120.      */
  121.     public void toggle()
  122.     {
  123.         if (isExpanded)
  124.         {
  125.             collapse();
  126.         }
  127.         else if (isExpandable())
  128.         {
  129.             expand();
  130.         }
  131.     }
  132.  
  133.     /**
  134.      * Gets the proper node image for its current state, expanded or collapsed.
  135.      * @return the current Image for node in its current state
  136.      */
  137.     public Image getImage()
  138.     {
  139.         return ((isExpanded && (expandedImage != null))
  140.                 ? expandedImage
  141.                 : collapsedImage);
  142.     }
  143.     
  144.     /**
  145.      * Sets the Image to use for this node when it is expanded.
  146.      * @param image the image to use when this node is expanded
  147.      * @see #setCollapsedImage
  148.      * @see #getImage
  149.      */
  150.     public void setExpandedImage(Image image)
  151.     {
  152.         expandedImage = image;
  153.     }
  154.     
  155.     /**
  156.      * Sets the Image to use for this node when it is not expanded.
  157.      * @param image the image to use when this node is collapsed
  158.      * @see #setExpandedImage
  159.      * @see #getImage
  160.      */
  161.     public void setCollapsedImage(Image image)
  162.     {
  163.         collapsedImage = image;
  164.     }
  165.     
  166.     /**
  167.      * Gets the current text label for this node.
  168.      * @return the current text label for this node
  169.      * @see #setText
  170.      */
  171.     public String getText()
  172.     {
  173.         return text;
  174.     }
  175.     
  176.     /**
  177.      * Sets a new text label for this node.
  178.      * @param s the new text label for this node
  179.      * @see #getText
  180.      */
  181.     public void setText(String s)
  182.     {
  183.         text = new String(s);
  184.     }
  185.     
  186.     /**
  187.      * Gets the object associated with this node.
  188.      * @return the object associated with this node
  189.      * @see #setDataObject
  190.      */
  191.     public Object getDataObject()
  192.     {
  193.         return dataObject;
  194.     }
  195.     
  196.     /**
  197.      * Sets an object to associate with this node.
  198.      * @param theObject an object to associate with this node
  199.      * @see #getDataObject
  200.      */
  201.     public void setDataObject(Object theObject)
  202.     {
  203.         dataObject = theObject;
  204.     }
  205.     
  206.     /**
  207.      * Gets the parent of this node.
  208.      * @return this node's parent node
  209.      * @see #getChild
  210.      * @see #getSibling
  211.      */
  212.     public TreeNode getParent()
  213.     {
  214.         return parent;
  215.     }
  216.  
  217.     /**
  218.      * Gets the child of this node.
  219.      * @return this node's child node
  220.      * @see #getParent
  221.      * @see #getSibling
  222.      */
  223.     public TreeNode getChild()
  224.     {
  225.         return child;
  226.     }
  227.  
  228.     /**
  229.      * Gets the sibling of this node.
  230.      * @return this node's sibling node
  231.      * @see #getChild
  232.      * @see #getParent
  233.      */
  234.     public TreeNode getSibling()
  235.     {
  236.         return sibling;
  237.     }
  238. }
  239.