org.zaval.lw.tree
Class LwTree

java.lang.Object
  |
  +--org.zaval.util.ValidationObject
        |
        +--org.zaval.lw.LwCanvas
              |
              +--org.zaval.lw.tree.LwTree
All Implemented Interfaces:
Drawable, java.util.EventListener, Layoutable, LwComponent, LwFocusListener, LwKeyListener, LwMouseListener, LwViewProvider, ScrollObj, TreeListener, Validationable

public class LwTree
extends LwCanvas
implements TreeListener, LwViewProvider, LwMouseListener, LwFocusListener, LwKeyListener, ScrollObj

This is tree view component. The component renders the specified TreeModel and organizes navigation through the tree. The sample below illustrates the component usage:

   ...
   Item root = new Item("root");
   TreeModel data = new Tree(root);
   data.add(data.getRoot(), new Item("First Child root"));
   data.add(data.getRoot(), new Item("Second Child root"));
   LwTree tree = new LwTree(data);
   ...
 

The class provides ability to customize the tree node rendering by defining the LwViewProvider provider. It allows to specify the view for the tree item. The default tree item view is LwTextRender.

Use addSelectionListener and removeSelectionListener methods to handle selection events. The events are performed by the component whenever the tree node has been selected, deselected.

Use addActionListener and removeActionListener methods to be notified whenever the node has been expanded or collapsed. The action event contains the tree expanded or collapsed item that can be got by getData method of the action event object.

It is possible to disable node selection by the enableSelection method.

It is possible to customize views for some elements of the tree by setView The default views of the elements are read as static objects by following keys:

The tree nodes height and width can be extended by setGaps method that defines horizontal and vertical gaps between the nodes.

It is possible disables tree view line rendering by showLines method.


Field Summary
static int CLOSED_VIEW
          Defines closed view element type.
static int LEAST_VIEW
          Defines list view element type.
static int OPENED_VIEW
          Defines opened view element type.
static int TOGGLE_OFF_VIEW
          Defines toggle off view element type.
static int TOGGLE_ON_VIEW
          Defines toggle on view element type.
 
Fields inherited from class org.zaval.lw.LwCanvas
bits, height, insets, parent, psHeight, psWidth, skins, width, x, y
 
Fields inherited from class org.zaval.util.ValidationObject
isValidFlag
 
Constructor Summary
LwTree(TreeModel d)
          Constructs a tree view component with the specified tree model.
LwTree(TreeModel d, boolean b)
          Constructs a tree view component with the specified tree model and tree node state.
 
Method Summary
 void addActionListener(LwActionListener l)
          Adds the specified action listener to receive action events from this tree.
 void addSelectionListener(LwActionListener l)
          Adds the specified selection listener to be notified whenever the tree item has been selected, deselected.
protected  java.awt.Dimension calcPreferredSize()
          Gets the preferred size of this component.
 boolean canHaveFocus()
          Specifies if the component can have focus.
 void enableSelection(boolean b)
          Enables the tree node selecting.
 void focusGained(LwFocusEvent e)
          Invoked when the light weight component gained focus.
 void focusLost(LwFocusEvent e)
          Invoked when the light weight component lost focus.
 ItemMetrics getItemMetrics(Item i)
          Gets the item metric for the specified item.
 java.awt.Color getLinesColor()
          Gets the color that is used to paint tree lines.
 TreeModel getModel()
          Gets the tree model that is rendered with the tree view component.
 java.awt.Point getOrigin()
          Returns an origin of the component.
 Item getSelectedItem()
          Gets the item that is selected at the moment.
 java.awt.Color getSelectionColor(boolean isHasFocus)
          Gets the selection marker color for the specified component focus state.
 java.awt.Point getSOLocation()
          Gets the scroll object location.
 java.awt.Dimension getSOSize()
          Gets the scroll object size.
 LwView getView(Drawable d, java.lang.Object obj)
          Gets the view for the specified object of the specified component.
 LwViewProvider getViewProvider()
          Gets the view provider.
 void invalidate()
          Invalidates this component.
 boolean isLinesShown()
          Tests if the tree line rendering is enabled.
 boolean isOpen(Item i)
          Checks if the tree item is expanded.
 boolean isSelectionEnabled()
          Tests if the tree items selection is enabled.
 void itemInserted(TreeEvent e)
          Invoked when the item has been inserted as a memeber of the source tree.
 void itemModified(TreeEvent e)
          Invoked when the item of the source tree has been modified.
 void itemRemoved(TreeEvent e)
          Invoked when the item has been removed from the source tree.
 void keyPressed(LwKeyEvent e)
          Invoked when a key has been pressed.
 void keyReleased(LwKeyEvent e)
          Invoked when a key has been released.
 void keyTyped(LwKeyEvent e)
          Invoked when a key has been typed.
 void mouseClicked(LwMouseEvent e)
          Invoked when the mouse button has been clicked on a light weight component.
 void mouseEntered(LwMouseEvent e)
          Invoked when the mouse enters a light weight component.
 void mouseExited(LwMouseEvent e)
          Invoked when the mouse exits a light weight component.
 void mousePressed(LwMouseEvent e)
          Invoked when the mouse button has been pressed on a light weight component.
 void mouseReleased(LwMouseEvent e)
          Invoked when the mouse button has been released on a light weight component.
 boolean moveContent()
          Tests if the scroll component performs scrolling by changing its location or moving view.
 void paint(java.awt.Graphics g)
          Paints this component.
protected  void recalc()
          The method is overrided to calculate metrical characteristics of the tree view component.
 void removeActionListener(LwActionListener l)
          Removes the specified action listener.
 void removeSelectionListener(LwActionListener l)
          Removes the specified selection listener.
 void select(Item item)
          Selects the specified node of the tree.
 void setGaps(int gx, int gy)
          Sets the specified vertical and horizontal gaps.
 void setLinesColor(java.awt.Color c)
          Sets the specified color to paint tree lines.
 void setModel(TreeModel d)
          Sets the specified tree model to be rendered with the component.
 void setScrollMan(ScrollMan m)
          Sets the specified scroll manager for the scroll object.
 void setSelectionColor(java.awt.Color c, boolean isHasFocus)
          Sets the selection marker color for the given tree view component state.
 void setSOLocation(int x, int y)
          Sets the specified scroll object location.
 void setView(int id, LwView v)
          Sets the specified view for the specified tree view element.
 void setViewProvider(LwViewProvider p)
          Sets the specified view provider.
 void showLines(boolean b)
          Enables the tree lines rendering.
 void toggle(Item item)
          Switches a toggle state for the specified tree item.
 
Methods inherited from class org.zaval.lw.LwCanvas
getBackground, getBounds, getHeight, getInsets, getLocation, getLwComponentAt, getLwParent, getPreferredSize, getSize, getViewMan, getVisiblePart, getWidth, getX, getY, hasFocus, isEnabled, isOpaque, isVisible, repaint, repaint, requestFocus, setBackground, setEnabled, setInsets, setLocation, setLwParent, setOpaque, setPSSize, setSize, setViewMan, setVisible, toString, update, validate, viewManChanged, vrp
 
Methods inherited from class org.zaval.util.ValidationObject
isValid
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.zaval.util.Validationable
isValid
 

Field Detail

LEAST_VIEW

public static final int LEAST_VIEW
Defines list view element type.

OPENED_VIEW

public static final int OPENED_VIEW
Defines opened view element type.

CLOSED_VIEW

public static final int CLOSED_VIEW
Defines closed view element type.

TOGGLE_OFF_VIEW

public static final int TOGGLE_OFF_VIEW
Defines toggle off view element type.

TOGGLE_ON_VIEW

public static final int TOGGLE_ON_VIEW
Defines toggle on view element type.
Constructor Detail

LwTree

public LwTree(TreeModel d)
Constructs a tree view component with the specified tree model.
Parameters:
d - the specified tree model.

LwTree

public LwTree(TreeModel d,
              boolean b)
Constructs a tree view component with the specified tree model and tree node state. The state defines if all nodes that have children should be opened or closed.
Parameters:
d - the specified tree model.
b - the specified tree node state.
Method Detail

canHaveFocus

public boolean canHaveFocus()
Description copied from interface: LwComponent
Specifies if the component can have focus.
Overrides:
canHaveFocus in class LwCanvas
Following copied from interface: org.zaval.lw.LwComponent
Returns:
true if the component can have the focus.

enableSelection

public void enableSelection(boolean b)
Enables the tree node selecting.
Parameters:
b - use true to enable the tree node selecting; false otherwise.

isSelectionEnabled

public boolean isSelectionEnabled()
Tests if the tree items selection is enabled.
Returns:
true if the tree items selection is enabled; false otherwise.

showLines

public void showLines(boolean b)
Enables the tree lines rendering.
Parameters:
b - use true to enable the tree line rendering; false otherwise.

isLinesShown

public boolean isLinesShown()
Tests if the tree line rendering is enabled.
Returns:
true if the line rendering is enabled; false otherwise.

getLinesColor

public java.awt.Color getLinesColor()
Gets the color that is used to paint tree lines.
Parameters:
a - color.

setLinesColor

public void setLinesColor(java.awt.Color c)
Sets the specified color to paint tree lines.
Parameters:
c - the specified color.

setGaps

public void setGaps(int gx,
                    int gy)
Sets the specified vertical and horizontal gaps. The method doesn't touch a gap in case if it is less zero, so it is possible to use the method to set only vertical or only horizontal gap.
Parameters:
gx - the specified horizontal gap.
gy - the specified vertical gap.

getModel

public TreeModel getModel()
Gets the tree model that is rendered with the tree view component.
Returns:
a tree model.

setViewProvider

public void setViewProvider(LwViewProvider p)
Sets the specified view provider. The provider is used to define a view that will be used to paint the node. It is possible to use null value as the input argument, in this case the LwTextRender view is used (the method sets itself as the view provider) to paint the node.
Parameters:
p - the specified view provider.

getViewProvider

public LwViewProvider getViewProvider()
Gets the view provider.
Returns:
a view provider.

setView

public void setView(int id,
                    LwView v)
Sets the specified view for the specified tree view element. Use one of the following constants as the view element id:
Parameters:
id - the specified element id.
v - the specified view.

setModel

public void setModel(TreeModel d)
Sets the specified tree model to be rendered with the component.
Parameters:
d - the specified tree model.

paint

public void paint(java.awt.Graphics g)
Paints this component.
Overrides:
paint in class LwCanvas
Parameters:
g - the graphics context to be used for painting.

recalc

protected void recalc()
The method is overrided to calculate metrical characteristics of the tree view component.
Overrides:
recalc in class LwCanvas

addSelectionListener

public void addSelectionListener(LwActionListener l)
Adds the specified selection listener to be notified whenever the tree item has been selected, deselected.
Parameters:
l - the specified selection listener.

removeSelectionListener

public void removeSelectionListener(LwActionListener l)
Removes the specified selection listener.
Parameters:
l - the specified selection listener.

getView

public LwView getView(Drawable d,
                      java.lang.Object obj)
Description copied from interface: LwViewProvider
Gets the view for the specified object of the specified component.
Specified by:
getView in interface LwViewProvider
Following copied from interface: org.zaval.lw.LwViewProvider
Parameters:
d - the specified component.
obj - the specified object.
Returns:
a view.

focusGained

public void focusGained(LwFocusEvent e)
Description copied from interface: LwFocusListener
Invoked when the light weight component gained focus.
Specified by:
focusGained in interface LwFocusListener
Following copied from interface: org.zaval.lw.event.LwFocusListener
Parameters:
e - the specified focus event.

focusLost

public void focusLost(LwFocusEvent e)
Description copied from interface: LwFocusListener
Invoked when the light weight component lost focus.
Specified by:
focusLost in interface LwFocusListener
Following copied from interface: org.zaval.lw.event.LwFocusListener
Parameters:
e - the specified focus event.

mouseClicked

public void mouseClicked(LwMouseEvent e)
Description copied from interface: LwMouseListener
Invoked when the mouse button has been clicked on a light weight component.
Specified by:
mouseClicked in interface LwMouseListener
Following copied from interface: org.zaval.lw.event.LwMouseListener
Parameters:
e - the specified mouse event.

mouseEntered

public void mouseEntered(LwMouseEvent e)
Description copied from interface: LwMouseListener
Invoked when the mouse enters a light weight component.
Specified by:
mouseEntered in interface LwMouseListener
Following copied from interface: org.zaval.lw.event.LwMouseListener
Parameters:
e - the specified mouse event.

mouseExited

public void mouseExited(LwMouseEvent e)
Description copied from interface: LwMouseListener
Invoked when the mouse exits a light weight component.
Specified by:
mouseExited in interface LwMouseListener
Following copied from interface: org.zaval.lw.event.LwMouseListener
Parameters:
e - the specified mouse event.

mouseReleased

public void mouseReleased(LwMouseEvent e)
Description copied from interface: LwMouseListener
Invoked when the mouse button has been released on a light weight component.
Specified by:
mouseReleased in interface LwMouseListener
Following copied from interface: org.zaval.lw.event.LwMouseListener
Parameters:
e - the specified mouse event.

keyReleased

public void keyReleased(LwKeyEvent e)
Description copied from interface: LwKeyListener
Invoked when a key has been released.
Specified by:
keyReleased in interface LwKeyListener
Following copied from interface: org.zaval.lw.event.LwKeyListener
Parameters:
e - the specified key event.

keyTyped

public void keyTyped(LwKeyEvent e)
Description copied from interface: LwKeyListener
Invoked when a key has been typed.
Specified by:
keyTyped in interface LwKeyListener
Following copied from interface: org.zaval.lw.event.LwKeyListener
Parameters:
e - the specified key event.

keyPressed

public void keyPressed(LwKeyEvent e)
Description copied from interface: LwKeyListener
Invoked when a key has been pressed.
Specified by:
keyPressed in interface LwKeyListener
Following copied from interface: org.zaval.lw.event.LwKeyListener
Parameters:
e - the specified key event.

mousePressed

public void mousePressed(LwMouseEvent e)
Description copied from interface: LwMouseListener
Invoked when the mouse button has been pressed on a light weight component.
Specified by:
mousePressed in interface LwMouseListener
Following copied from interface: org.zaval.lw.event.LwMouseListener
Parameters:
e - the specified mouse event.

setSelectionColor

public void setSelectionColor(java.awt.Color c,
                              boolean isHasFocus)
Sets the selection marker color for the given tree view component state.
Parameters:
c - the selection marker color. Use null if the marker should not be rendered.
isHasFocus - the given tree view component focus state. Use true to identify the selection color when the component has focus, otherwise use false

getSelectionColor

public java.awt.Color getSelectionColor(boolean isHasFocus)
Gets the selection marker color for the specified component focus state.
Parameters:
isHasFocus - the given tree view component focus state. Use true to identify the selection color when the component has focus, otherwise use false
Returns:
a selection marker color.

toggle

public void toggle(Item item)
Switches a toggle state for the specified tree item. If the toggle state is "on" than the method switches it to "off" and the "off" state will be switched to "on" state.
Parameters:
item - the specified tree item.

select

public void select(Item item)
Selects the specified node of the tree.
Parameters:
item - the specified node.

getSelectedItem

public Item getSelectedItem()
Gets the item that is selected at the moment.
Returns:
a selected item.

addActionListener

public void addActionListener(LwActionListener l)
Adds the specified action listener to receive action events from this tree. The event is generated whenever the tree node has been expanded or collapsed. The node can be got by getData method of the action event.
Parameters:
l - the specified action listener.

removeActionListener

public void removeActionListener(LwActionListener l)
Removes the specified action listener.
Parameters:
l - the specified action listener.

itemInserted

public void itemInserted(TreeEvent e)
Description copied from interface: TreeListener
Invoked when the item has been inserted as a memeber of the source tree.
Specified by:
itemInserted in interface TreeListener
Following copied from interface: org.zaval.data.event.TreeListener
Parameters:
e - the specified event.

itemRemoved

public void itemRemoved(TreeEvent e)
Description copied from interface: TreeListener
Invoked when the item has been removed from the source tree.
Specified by:
itemRemoved in interface TreeListener
Following copied from interface: org.zaval.data.event.TreeListener
Parameters:
e - the specified event.

itemModified

public void itemModified(TreeEvent e)
Description copied from interface: TreeListener
Invoked when the item of the source tree has been modified.
Specified by:
itemModified in interface TreeListener
Following copied from interface: org.zaval.data.event.TreeListener
Parameters:
e - the specified event.

invalidate

public void invalidate()
Description copied from class: LwCanvas
Invalidates this component. The parent will be invalidated with this component too.
Overrides:
invalidate in class LwCanvas

getOrigin

public java.awt.Point getOrigin()
Description copied from class: LwCanvas
Returns an origin of the component. The origin defines an offset of the component view relatively the component point of origin. The origin can be used to scroll the component view. The default origin value is null, in this case the origin is (0, 0).
Overrides:
getOrigin in class LwCanvas
Following copied from class: org.zaval.lw.LwCanvas
Returns:
an origin of the component.

getSOLocation

public java.awt.Point getSOLocation()
Description copied from interface: ScrollObj
Gets the scroll object location.
Specified by:
getSOLocation in interface ScrollObj
Following copied from interface: org.zaval.misc.ScrollObj
Returns:
a scroll object location.

setSOLocation

public void setSOLocation(int x,
                          int y)
Description copied from interface: ScrollObj
Sets the specified scroll object location. The method defines a mechanism that will be used to scrool the object.
Specified by:
setSOLocation in interface ScrollObj
Following copied from interface: org.zaval.misc.ScrollObj
Parameters:
x - the specified x coordinate.
y - the specified y coordinate.

getSOSize

public java.awt.Dimension getSOSize()
Description copied from interface: ScrollObj
Gets the scroll object size. The size is a size that the scroll object wants to have.
Specified by:
getSOSize in interface ScrollObj
Following copied from interface: org.zaval.misc.ScrollObj
Returns:
a scroll object size.

setScrollMan

public void setScrollMan(ScrollMan m)
Description copied from interface: ScrollObj
Sets the specified scroll manager for the scroll object. The manager reference should be used with the scroll object to notify when the scroll object has been moved or resized.
Specified by:
setScrollMan in interface ScrollObj
Following copied from interface: org.zaval.misc.ScrollObj
Parameters:
m - the specified scroll manager.

moveContent

public boolean moveContent()
Description copied from interface: ScrollObj
Tests if the scroll component performs scrolling by changing its location or moving view.
Specified by:
moveContent in interface ScrollObj
Following copied from interface: org.zaval.misc.ScrollObj
Returns:
true if the scroll component organizes scrolling by moving its view; otherwise false.

isOpen

public boolean isOpen(Item i)
Checks if the tree item is expanded. The item is expanded if it is expanded and all parent tree items are exapnded too.
Parameters:
i - the specified tree item.
Returns:
true if the item and its parent item is expanded too; false otherwise.

getItemMetrics

public ItemMetrics getItemMetrics(Item i)
Gets the item metric for the specified item.
Parameters:
i - the specified item.
Returns:
an item metric.

calcPreferredSize

protected java.awt.Dimension calcPreferredSize()
Gets the preferred size of this component. The method is overrided with the component to returns the "pure" preferred size basing on the tree structure.
Overrides:
calcPreferredSize in class LwCanvas
Returns:
a dimension object indicating this component preferred size.


Copyright © Zaval Creative Engineering Group, 2000-2005.
http://www.zaval.org