org.w3c.tools.widgets
Class TreeBrowser

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Canvas
          extended by org.w3c.tools.widgets.TreeBrowser
All Implemented Interfaces:
java.awt.event.AdjustmentListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible
Direct Known Subclasses:
FrameBrowser, ServerBrowser

public class TreeBrowser
extends java.awt.Canvas
implements java.awt.event.AdjustmentListener

The TreeBrowser class. This class is a generic framework to browser any hierachical structure.

Genericity is obtained through the use of 'handlers': the TreeBrowser itself does not perform any action in response to user events, but simply forward them as notifications to handlers. Each item inserted may have its own handler, but handlers may also (this is the most common case) be shared between handlers.

Any item added in the Tree is displayed with an icon and a label. When a handler receive a notification on a node, it may change this node, to modify or update its appearance.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.awt.Canvas
java.awt.Canvas.AccessibleAWTCanvas
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
protected  int fontHeight
           
protected  java.util.Vector items
           
static int MULTIPLE
          The policy that enables a multiple selection of nodes.
static int SCROLLBARS_ALWAYS
          Specifies that the horizontal/vertical scrollbars should always be shown regardless of the respective sizes of the TreeBrowser.
static int SCROLLBARS_ASNEEDED
          Specifies that horizontal/vertical scrollbars should be shown only when the size of the nodes exceeds the size of the TreeBrowser in the horizontal/vertical dimension.
protected  java.util.Vector selection
           
protected  int selectionPolicy
           
static int SINGLE
          This policy that lets just one node selected at the same time.
protected  int topItem
           
protected  int visibleItemCount
           
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
protected TreeBrowser()
           
  TreeBrowser(java.lang.Object root, java.lang.String label, NodeHandler handler, java.awt.Image icon)
          Builds a new browser instance
 
Method Summary
 void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt)
          Updates graphical appearance in response to a scroll.
 void collapse(TreeNode item)
          Contracts the representation of the specified node.
 TreeNode getNode(java.lang.Object obj)
          Gets the node associated to the specified object, or null if any.
 TreeNode getParent(TreeNode child)
          Returns the parent node of the specified node.
 java.awt.Dimension getPreferredSize()
           
 int getSelectionPolicy()
          Gets the selection policy.
protected  void initialize(java.lang.Object item, java.lang.String label, NodeHandler handler, java.awt.Image icon)
           
 void insert(TreeNode parent, java.lang.Object item, NodeHandler handler, java.lang.String label, java.awt.Image icon)
          Inserts new node.
protected  TreeNode itemAt(int y)
          this should be private.
 void paint(java.awt.Graphics g)
          repaints the View.
 void remove(TreeNode node)
          Removes the specified node.
 void removeBranch(TreeNode node)
          Removes the specified node and its children.
 void select(TreeNode node)
          Selects the specified node.
 java.util.Enumeration selection()
          Returns an Enumeraiton of selected items.
 void setHorizontalScrollbar(java.awt.Scrollbar a)
          Sets 'a' as horizontal Scrollbar.
 void setScrollbarDisplayPolicy(int scrollbarDisplayPolicy)
          Sets the scrollbars display policy to the specified policy.
 void setSelectionBackgroudColor(java.awt.Color color)
          Sets the background color of a selected node to the specified color.
 void setSelectionFontColor(java.awt.Color color)
          Sets the color of a selected node to the specified color.
 void setSelectionPolicy(int policy)
          Sets the selection policy.
 void setVerticalScrollbar(java.awt.Scrollbar a)
          Sets 'a' as vertical Scrollbar.
 void unselect(TreeNode node)
          Unselects the specified node.
 void unselectAll()
          Unselects all selected items.
 void update(java.awt.Graphics pg)
           
 
Methods inherited from class java.awt.Canvas
addNotify, createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SCROLLBARS_ALWAYS

public static final int SCROLLBARS_ALWAYS
Specifies that the horizontal/vertical scrollbars should always be shown regardless of the respective sizes of the TreeBrowser.

See Also:
Constant Field Values

SCROLLBARS_ASNEEDED

public static final int SCROLLBARS_ASNEEDED
Specifies that horizontal/vertical scrollbars should be shown only when the size of the nodes exceeds the size of the TreeBrowser in the horizontal/vertical dimension.

See Also:
Constant Field Values

SINGLE

public static final int SINGLE
This policy that lets just one node selected at the same time.

See Also:
Constant Field Values

MULTIPLE

public static final int MULTIPLE
The policy that enables a multiple selection of nodes.

See Also:
Constant Field Values

items

protected java.util.Vector items

selection

protected java.util.Vector selection

topItem

protected int topItem

visibleItemCount

protected int visibleItemCount

selectionPolicy

protected int selectionPolicy

fontHeight

protected int fontHeight
Constructor Detail

TreeBrowser

public TreeBrowser(java.lang.Object root,
                   java.lang.String label,
                   NodeHandler handler,
                   java.awt.Image icon)
Builds a new browser instance

Parameters:
root - the root node for this hierarchy
label - the label that should be displayed for this item
handler - the handler for this node
icon - the icon that must be displayed for this item

TreeBrowser

protected TreeBrowser()
Method Detail

initialize

protected void initialize(java.lang.Object item,
                          java.lang.String label,
                          NodeHandler handler,
                          java.awt.Image icon)

getPreferredSize

public java.awt.Dimension getPreferredSize()
Overrides:
getPreferredSize in class java.awt.Component

setSelectionFontColor

public void setSelectionFontColor(java.awt.Color color)
Sets the color of a selected node to the specified color.

Parameters:
color - the color used to paint a selected node

setSelectionBackgroudColor

public void setSelectionBackgroudColor(java.awt.Color color)
Sets the background color of a selected node to the specified color.

Parameters:
color - the color used to paint the background of a selected node

setScrollbarDisplayPolicy

public void setScrollbarDisplayPolicy(int scrollbarDisplayPolicy)
Sets the scrollbars display policy to the specified policy. The default is SCROLLBARS_ALWAYS

Parameters:
scrollbarDisplayPolicy - SCROLLBARS_NEVER | SCROLLBARS_ASNEEDED | SCROLLBARS_ALWAYS

paint

public void paint(java.awt.Graphics g)
repaints the View.

Overrides:
paint in class java.awt.Canvas

itemAt

protected TreeNode itemAt(int y)
this should be private. having it protected is a present for dummy VM that doesn't know that an inner class can access private method of its parent class


update

public void update(java.awt.Graphics pg)
Overrides:
update in class java.awt.Canvas

insert

public void insert(TreeNode parent,
                   java.lang.Object item,
                   NodeHandler handler,
                   java.lang.String label,
                   java.awt.Image icon)
Inserts new node.

Parameters:
parent - the parent node.

remove

public void remove(TreeNode node)
Removes the specified node. This simply removes a node, without modifying its children if any. USE WITH CAUTION.

Parameters:
node - the node to remove

removeBranch

public void removeBranch(TreeNode node)
Removes the specified node and its children. NOTE: if two threads are doing adds and removes, this can lead to IndexOutOfBound exception. You will probably have to use locks to get rid of that problem

Parameters:
node - the node to remove

collapse

public void collapse(TreeNode item)
Contracts the representation of the specified node. removes all the children nodes of 'item'. It is caller's responsibility to call repaint() afterwards.

Parameters:
item - the node to contracts

setSelectionPolicy

public void setSelectionPolicy(int policy)
Sets the selection policy.

Parameters:
policy: - SINGLE or MULTIPLE

getSelectionPolicy

public int getSelectionPolicy()
Gets the selection policy.


select

public void select(TreeNode node)
Selects the specified node. Selects the given node. If selectionPolicy is SINGLE any previously selected node is unselected first. It is caller's responsibility to call repaint()

Parameters:
node - the node to select

unselect

public void unselect(TreeNode node)
Unselects the specified node. It is caller's responsibility to call repaint()

Parameters:
node - the node to unselect

unselectAll

public void unselectAll()
Unselects all selected items.


selection

public java.util.Enumeration selection()
Returns an Enumeraiton of selected items.


setVerticalScrollbar

public void setVerticalScrollbar(java.awt.Scrollbar a)
Sets 'a' as vertical Scrollbar. The Browser becomes an AdjustmentListener of this scrollbar.


setHorizontalScrollbar

public void setHorizontalScrollbar(java.awt.Scrollbar a)
Sets 'a' as horizontal Scrollbar. The Browser becomes an AdjustmentListener of this scrollbar.


adjustmentValueChanged

public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt)
Updates graphical appearance in response to a scroll.

Specified by:
adjustmentValueChanged in interface java.awt.event.AdjustmentListener

getParent

public TreeNode getParent(TreeNode child)
Returns the parent node of the specified node. If 'child' is a valid node belonging to the Tree and has a parent node, returns its parent. Returns null otherwise.

Parameters:
child - the child node you want to get its parent

getNode

public TreeNode getNode(java.lang.Object obj)
Gets the node associated to the specified object, or null if any.

Parameters:
obj - the object related to a node