vipratech.beans.standardbeans
Class TreeView

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--java.awt.Panel
                    |
                    +--vipratech.beans.standardbeans.TreeView

public class TreeView
extends java.awt.Panel
implements java.awt.ItemSelectable

Creates an "outline view" of text headings and, optionally, images. The headings are arranged in a hierarchical fashion, and can be expanded to show their sub-headings or collapsed, hiding their sub-headings.

A TreeView is typically used to display information that is organized in a hierarchical fashion like an index or table of contents such as seen in Microsoft Windows Explorer. The example code below demonstrates how to set up a simple collapsable tree strucutre.

import java.awt.*;
import java.applet.*;
import vipratech.beans.*;
import vipratech.beans.TreeNode;

public class TreeExample extends Applet
{
public void init()
{
//{{INIT_CONTROLS
setLayout(null);
setSize(555,487);
treeView1 = new vipratech.beans.TreeView();
{
java.lang.String[] tempString = new java.lang.String[5];
tempString[0] = new java.lang.String("Root");
tempString[1] = new java.lang.String(" Folder1");
tempString[2] = new java.lang.String(" Folder 2");
tempString[3] = new java.lang.String(" Folder 3");
tempString[4] = new java.lang.String("Level 1");
treeView1.setTreeStructure(tempString);
}
treeView1.setBounds(84,84,120,48);
add(treeView1);
label1 = new java.awt.Label("text");
label1.setBounds(252,84,234,50);
add(label1);
//}}

//{{REGISTER_LISTENERS
SymItem lSymItem = new SymItem();
treeView1.addItemListener(lSymItem);
//}}
}

//{{DECLARE_CONTROLS
vipratech.beans.TreeView treeView1;
java.awt.Label label1;
//}}

class SymItem implements java.awt.event.ItemListener
{
public void itemStateChanged(java.awt.event.ItemEvent event)
{
Object object = event.getSource();
if (object == treeView1)
treeView1_itemStateChanged(event);
}
}

void treeView1_itemStateChanged(java.awt.event.ItemEvent event)
{
// to do: code goes here.

Object o = event.getItem();
TreeNode tn = (TreeNode)o;
String s = tn.getText();

if(o==null) s = "null";

//{{CONNECTION
// Set the text for Label...
label1.setText(s);
//}}
}
}

A TreeNode object is used for each heading.

This bean was developed from bean orgininally available in Symantec Visual Cafe version 2.

See Also:
TreeNode, Serialized Form

Field Summary
static int CHILD
          Constant indicating that the new node is to be a child of the existing node.
static int LAST
          Constant indicating that the new node is to be the last sibling of the existing node.
static int NEXT
          Constant indicating that the new node is to be the next sibling of the existing node.
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Constructor Summary
TreeView()
          Constructs an empty TreeView.
TreeView(TreeNode head)
          Constructs a TreeView with the given node.
 
Method Summary
 void addActionListener(java.awt.event.ActionListener l)
          Adds the specified action listener to receive action events from this button.
 void addChild(TreeNode newNode, TreeNode relativeNode)
          Adds the specified child node to the specified parent node
 void addItemListener(java.awt.event.ItemListener l)
          Add a listener to recieve item events when the state of an item changes.
 void addNotify()
          Tells this component that it has been added to a container.
 void addSibling(TreeNode newNode, TreeNode siblingNode)
          Adds the specified node as a sibling to the specified node
 void addSibling(TreeNode newNode, TreeNode siblingNode, boolean asLastSibling)
          Adds the specified node as a sibling to the specified node
 void append(TreeNode newNode)
          Adds a new node at root level.
 void clear()
          Clears the tree structure and redraws.
 void drawTree()
          Draws the TreeView into an offscreen image.
 boolean exists(java.lang.String s)
          Determines if the node with the given text is in the TreeView.
 boolean exists(TreeNode node)
          Determines if the given node is in the TreeView.
 int getCount()
          Returns the total number of nodes in the tree.
 java.awt.Dimension getMinimumSize()
          Returns the minimum dimensions to properly display this component.
 java.awt.Dimension getPreferredSize()
          Returns the recommended dimensions to properly display this component.
 TreeNode getRootNode()
          Returns the "root" node.
 TreeNode getSelectedNode()
          Gets the currently selected node.
 java.lang.Object[] getSelectedObjects()
          Returns the selected items or null if no items are selected.
 java.lang.String getSelectedText()
          Gets the text of the currently selected node.
 java.lang.String[] getTreeStructure()
          Gets a string array that reflects the current TreeView's contents.
 int getViewCount()
          Returns the total number of viewable nodes in the tree.
 void insert(TreeNode newNode, TreeNode relativeNode, int position)
          Inserts a new node relative to an existing node in the tree.
 boolean isFocusTraversable()
           
 void paint(java.awt.Graphics g)
          Paints this component using the given graphics context.
 void printTree(TreeNode node)
          Print out the text of each node in the TreeView beginning with the given node.
 void printTree(TreeNode node, boolean isIndented)
          Print out the text of each node in the TreeView beginning with the given node.
 void redraw()
          Lays out the vertical scrollbar as needed, then draws the TreeView into an offscreen image.
 void redraw(java.awt.Graphics g)
          Lays out the vertical scrollbar as needed, then draws the TreeView into an offscreen image.
 TreeNode remove(java.lang.String s)
          Removes the node with the given text from the TreeView.
 void remove(TreeNode node)
          Removes the given node from the TreeView.
 void removeActionListener(java.awt.event.ActionListener l)
          Removes the specified action listener so it no longer receives action events from this button.
 void removeItemListener(java.awt.event.ItemListener l)
          Removes an item listener.
 void removeNotify()
          Tells this component that it is being removed from a container.
 void removeSelected()
          Removes the currently selected node from the TreeView.
 void setLayout(java.awt.LayoutManager lm)
          Takes no action.
 void setTreeStructure(java.lang.String[] s)
          Initializes the TreeView from a string array.
 void triggerRedraw()
          Triggers redrawing the entire image, even if the size of the component has not changed.
 void update(java.awt.Graphics g)
          Handles redrawing of this component on the screen.
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getInsets, getLayout, getMaximumSize, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, remove, remove, removeAll, removeContainerListener, setFont, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, bounds, checkImage, checkImage, contains, contains, createImage, createImage, disable, dispatchEvent, enable, enable, enableInputMethods, getBackground, getBounds, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isDisplayable, isDoubleBuffered, isEnabled, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CHILD

public static final int CHILD
Constant indicating that the new node is to be a child of the existing node.
See Also:
insert(vipratech.beans.standardbeans.TreeNode, vipratech.beans.standardbeans.TreeNode, int)

NEXT

public static final int NEXT
Constant indicating that the new node is to be the next sibling of the existing node.
See Also:
insert(vipratech.beans.standardbeans.TreeNode, vipratech.beans.standardbeans.TreeNode, int)

LAST

public static final int LAST
Constant indicating that the new node is to be the last sibling of the existing node.
See Also:
insert(vipratech.beans.standardbeans.TreeNode, vipratech.beans.standardbeans.TreeNode, int)
Constructor Detail

TreeView

public TreeView()
Constructs an empty TreeView.

TreeView

public TreeView(TreeNode head)
Constructs a TreeView with the given node.
Parameters:
head - the root node of the constructed tree
Method Detail

setTreeStructure

public void setTreeStructure(java.lang.String[] s)
Initializes the TreeView from a string array. There is one string for each node in the array. That string contains the text of the node indented with same number of leading spaces as the depth of that node in the tree.
Parameters:
s - the string array used for initialization. If null, the tree will be cleared.
See Also:
getTreeStructure()

getTreeStructure

public java.lang.String[] getTreeStructure()
Gets a string array that reflects the current TreeView's contents. There is one string for each node in the array. That string contains the text of the node indented with same number of leading spaces as the depth of that node in the tree.
Returns:
the string array that reflects the TreeView's contents
See Also:
setTreeStructure(java.lang.String[])

getSelectedObjects

public java.lang.Object[] getSelectedObjects()
Returns the selected items or null if no items are selected.

This is a standard method of the ItemSelectable interface.

Specified by:
getSelectedObjects in interface java.awt.ItemSelectable

insert

public void insert(TreeNode newNode,
                   TreeNode relativeNode,
                   int position)
Inserts a new node relative to an existing node in the tree.
Parameters:
newNode - the new node to insert into the tree
relativeNode - the existing node used for a position reference
position - where to insert the new node relative to relativeNode. Legal values are CHILD, NEXT and LAST.
See Also:
CHILD, NEXT, LAST, append(vipratech.beans.standardbeans.TreeNode)

clear

public void clear()
Clears the tree structure and redraws.

getRootNode

public TreeNode getRootNode()
Returns the "root" node. The root node is the first top-level node in the tree hierarchy. All other nodes are either children or siblings of that one.
Returns:
the root tree node

getCount

public int getCount()
Returns the total number of nodes in the tree.

getViewCount

public int getViewCount()
Returns the total number of viewable nodes in the tree. A node is viewable if all of its parents are expanded.

exists

public boolean exists(TreeNode node)
Determines if the given node is in the TreeView.
Parameters:
node - the node to check
Returns:
true if the node is in the TreeView, false if it is not
See Also:
exists(java.lang.String)

exists

public boolean exists(java.lang.String s)
Determines if the node with the given text is in the TreeView.
Parameters:
s - the node text to find
Returns:
true if the node is in the TreeView, false if it is not
See Also:
#exists(symantec.itools.awt.TreeNode)

append

public void append(TreeNode newNode)
Adds a new node at root level. If there is no root node, the given node is made the root node. If there is a root node, the given node is made a sibling of the root node.
Parameters:
newNode - the new node to add Does not redraw the component. Allows you to call repeatedly without causing the component to flicker while nodes are manipulated. After all manipulation is done, repaint the tree.
See Also:
insert(vipratech.beans.standardbeans.TreeNode, vipratech.beans.standardbeans.TreeNode, int)

addChild

public void addChild(TreeNode newNode,
                     TreeNode relativeNode)
Adds the specified child node to the specified parent node
Parameters:
newNode - the node to add as a child
relativeNode - the node to add the child to. Does not redraw the component. Allows you to call repeatedly without causing the component to flicker while nodes are manipulated. After all manipulation is done, repaint the tree.

addSibling

public void addSibling(TreeNode newNode,
                       TreeNode siblingNode)
Adds the specified node as a sibling to the specified node
Parameters:
newNode - the node to add as a sibling
siblingNode - a sibling node to the new node. Does not redraw the component. Allows you to call repeatedly without causing the component to flicker while nodes are manipulated. After all manipulation is done, repaint the tree.
See Also:
addSibling(TreeNode newNode, TreeNode siblingNode, boolean asLastSibling)

addSibling

public void addSibling(TreeNode newNode,
                       TreeNode siblingNode,
                       boolean asLastSibling)
Adds the specified node as a sibling to the specified node
Parameters:
newNode - the node to add as a sibling
siblingNode - a sibling node to the new node.
asLastSibling - if true, then add the new node as the last (bottommost) sibling node to the specified sibling node. Does not redraw the component. Allows you to call repeatedly without causing the component to flicker while nodes are manipulated. After all manipulation is done, repaint the tree.
See Also:
addSibling(TreeNode newNode, TreeNode siblingNode)

remove

public TreeNode remove(java.lang.String s)
Removes the node with the given text from the TreeView.
Parameters:
s - the node text to find
Returns:
the TreeNode removed from this TreeView or null if not found Does not redraw the component. Allows you to call repeatedly without causing the component to flicker while nodes are manipulated. After all manipulation is done, repaint the tree.
See Also:
#remove(symantec.itools.awt.TreeNode), removeSelected()

removeSelected

public void removeSelected()
Removes the currently selected node from the TreeView. Does not redraw the component. Allows you to call repeatedly without causing the component to flicker while nodes are manipulated. After all manipulation is done, repaint the tree.
See Also:
#remove(symantec.itools.awt.TreeNode), remove(java.lang.String)

remove

public void remove(TreeNode node)
Removes the given node from the TreeView.
Parameters:
node - the node to remove
Returns:
the TreeNode removed from this TreeView or null if not found Does not redraw the component. Allows you to call repeatedly without causing the component to flicker while nodes are manipulated. After all manipulation is done, repaint the tree.
See Also:
remove(java.lang.String), removeSelected()

printTree

public void printTree(TreeNode node)
Print out the text of each node in the TreeView beginning with the given node. The nodes are printed out one per line with no indenting.
Parameters:
node - the first node to print

printTree

public void printTree(TreeNode node,
                      boolean isIndented)
Print out the text of each node in the TreeView beginning with the given node. The nodes are printed out one per line with no indenting.
Parameters:
node - the first node to print
isIndented, - if true, nodes will have hierarchical indenting, if false, all nodes will pe printed at the same level.

getSelectedNode

public TreeNode getSelectedNode()
Gets the currently selected node.
Returns:
the currently selected node, or null if none selected

getSelectedText

public java.lang.String getSelectedText()
Gets the text of the currently selected node.
Returns:
the text of the currently selected node or null if no node is selected

update

public void update(java.awt.Graphics g)
Handles redrawing of this component on the screen. This is a standard Java AWT method which gets called by the Java AWT (repaint()) to handle repainting this component on the screen. The graphics context clipping region is set to the bounding rectangle of this component and its [0,0] coordinate is this component's top-left corner. Typically this method paints the background color to clear the component's drawing space, sets graphics context to be the foreground color, and then calls paint() to draw the component. It is overridden here to reduce flicker by eliminating the uneeded clearing of the background.
Parameters:
g - the graphics context
Overrides:
update in class java.awt.Container
See Also:
Component.repaint(), paint(java.awt.Graphics)

paint

public void paint(java.awt.Graphics g)
Paints this component using the given graphics context. This is a standard Java AWT method which typically gets called by the AWT to handle painting this component. It paints this component using the given graphics context. The graphics context clipping region is set to the bounding rectangle of this component and its [0,0] coordinate is this component's top-left corner.
Parameters:
g - the graphics context used for painting
Overrides:
paint in class java.awt.Container
See Also:
Component.repaint(), update(java.awt.Graphics)

redraw

public void redraw()
Lays out the vertical scrollbar as needed, then draws the TreeView into an offscreen image. This is used for cleaner refresh.

redraw

public void redraw(java.awt.Graphics g)
Lays out the vertical scrollbar as needed, then draws the TreeView into an offscreen image. This is used for cleaner refresh.
Parameters:
g - the graphics object use for drawing

drawTree

public void drawTree()
Draws the TreeView into an offscreen image. This is used for cleaner refresh.

getPreferredSize

public java.awt.Dimension getPreferredSize()
Returns the recommended dimensions to properly display this component. This is a standard Java AWT method which gets called to determine the recommended size of this component.
Overrides:
getPreferredSize in class java.awt.Container
See Also:
getMinimumSize()

getMinimumSize

public java.awt.Dimension getMinimumSize()
Returns the minimum dimensions to properly display this component. This is a standard Java AWT method which gets called to determine the minimum size of this component.
Overrides:
getMinimumSize in class java.awt.Container
See Also:
getPreferredSize()

setLayout

public void setLayout(java.awt.LayoutManager lm)
Takes no action. This is a standard Java AWT method which gets called to specify which layout manager should be used to layout the components in standard containers. Since layout managers CANNOT BE USED with this container the standard setLayout has been OVERRIDDEN for this container and does nothing.
Parameters:
lm - the layout manager to use to layout this container's components (IGNORED)
Overrides:
setLayout in class java.awt.Container
See Also:
Container.getLayout()

isFocusTraversable

public boolean isFocusTraversable()
Overrides:
isFocusTraversable in class java.awt.Component

addNotify

public void addNotify()
Tells this component that it has been added to a container. This is a standard Java AWT method which gets called by the AWT when this component is added to a container. Typically, it is used to create this component's peer. It has been overridden here to hook-up event listeners.
Overrides:
addNotify in class java.awt.Panel
See Also:
removeNotify()

removeNotify

public void removeNotify()
Tells this component that it is being removed from a container. This is a standard Java AWT method which gets called by the AWT when this component is removed from a container. Typically, it is used to destroy the peers of this component and all its subcomponents. It has been overridden here to unhook event listeners.
Overrides:
removeNotify in class java.awt.Container
See Also:
addNotify()

triggerRedraw

public void triggerRedraw()
Triggers redrawing the entire image, even if the size of the component has not changed.

addActionListener

public void addActionListener(java.awt.event.ActionListener l)
Adds the specified action listener to receive action events from this button.
Parameters:
l - the action listener

removeActionListener

public void removeActionListener(java.awt.event.ActionListener l)
Removes the specified action listener so it no longer receives action events from this button.
Parameters:
l - the action listener

addItemListener

public void addItemListener(java.awt.event.ItemListener l)
Add a listener to recieve item events when the state of an item changes.

This is a standard method of the ItemSelectable interface.

Specified by:
addItemListener in interface java.awt.ItemSelectable
Parameters:
l - the listener to recieve events
See Also:
ItemEvent

removeItemListener

public void removeItemListener(java.awt.event.ItemListener l)
Removes an item listener.

This is a standard method of the ItemSelectable interface.

Specified by:
removeItemListener in interface java.awt.ItemSelectable
Parameters:
l - the listener being removed
See Also:
ItemEvent

dathe@chemie.uni-leipzig.de"

http://leipzig.vernetztes-studium.de