adavid.awt
Class TNode

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--adavid.awt.TNode

public class TNode
extends java.awt.Container
implements java.awt.event.MouseListener, java.awt.event.KeyListener

TNode : Tree Node I create my own tree component because Java copies the nodes without telling me when, and it copies the content of the nodes as well, so when my other components refer to these nodes, well, they are not valid because they have been replaced by new ones! Imagine the time I've lost to understand where the problem came from, and then to implement my own tree!!! This is my second version of tree: it is 100% swing free, only pure java awt, which is much much faster than swing. A node listens to its own events to expand/collapse or send an event to a listener ; to use the serialization the user has to be sure that the content object, the TListener objects and the TNodeRenderer objects are serializable as well!!!!!

See Also:
Serialized Form

Field Summary
static java.awt.Color DEFAULT_COLOR
           
static java.awt.Color DEFAULT_SELECTED_COLOR
          default color settings: the background when selected and the default color for the renderer
static int step
          step used to enlarge the array of leaves/children when adding a node useful when dealing with big trees to reduce memory allocation
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Constructor Summary
TNode(java.lang.Object object)
          contructor : take the content ; if it is a component, it will display itself, otherwise a label will be created with its toString() the DefaultTNodeRenderer is used
TNode(java.lang.Object object, java.awt.Component item)
          another constructor
TNode(java.lang.Object object, TNodeRenderer renderer)
          constructor : same as previously with a customized renderer
TNode(java.lang.Object object, TNodeRenderer renderer, java.awt.Component item)
          real constructor : takes the content (object), the renderer and a component to display the content ; the component should be able to update itself when its background changes.
 
Method Summary
 void add(java.awt.PopupMenu pmenu)
          add/set the popup menu
 void addNode(TNode node)
          add a new node to this tree ; bad things may happen if one adds twice the same node
 void addTListener(TListener listener)
          add a TNode listener
 void collapse()
          collapse this node
 void delete()
          delete this node ; works only if this is not the root ; delegate the call to its parent
 void expand()
          expand this node ; and all the path to this node to make it visible
 void expandAll()
          expand all children and invalidate
 java.awt.Color getColor()
          returns the color used for the renderer ; the Graphics object has a g.setColor(..) just before the call to the renderer
 java.awt.Color getColorSelected()
          returns the color used for background selection
 java.awt.Component getComponent()
          returns the component of the TNode
 java.lang.Object getContent()
          returns the content of the node ; the content is immutable
 boolean getOpenOnDoubleClick()
           
 TNode getParentNode()
          returns the parent node
 java.awt.Dimension getPreferredSize()
          returns the preferred size of this node ; this does the computation of the size when not valid and does the layout as well
 TNode getRootNode()
          returns the root node of this node
 java.lang.String getStringLabel()
          return the string of the ?label
 java.lang.Object[] getSubContents()
          returns the content of all sub nodes
 TNode getSubNode(int index)
          returns one sub node null if invalid
 TNode[] getSubNodes()
          returns all the sub nodes
 void invalidateNode()
          invalidates itself and revalidates itself ; should be called after some addNode in order to update the layout
static void invalidateNode(TNode node)
          invalidate the node given in arguments if one node is not valid, then all its parents are not valid, so we do not need to propagate when we find one !valid, thus the test in the loop
 boolean isExpanded()
          returns the expansion state
 boolean isMutable()
          returns true if the node is mutable
 void keyPressed(java.awt.event.KeyEvent e)
           
 void keyReleased(java.awt.event.KeyEvent e)
           
 void keyTyped(java.awt.event.KeyEvent e)
          same as before ; we call the nodeClicked method as if it were a click when one presses the space bar ; the return key just toggles the node
 void mouseClicked(java.awt.event.MouseEvent e)
          mouse listener ; delegate the call to the listeners after processing (expand/collapse)
 void mouseEntered(java.awt.event.MouseEvent e)
           
 void mouseExited(java.awt.event.MouseEvent e)
           
 void mousePressed(java.awt.event.MouseEvent e)
           
 void mouseReleased(java.awt.event.MouseEvent e)
          unused methods of listeners I prefer to use this method instead of using the "empty" listener provided by Sun, which we should extend, imagine, you download two classes just for a simple listener, and you have to pass arguments to have access to what you want.
 void paint(java.awt.Graphics g)
          the paint method: delegate the call to the renderer the component is called by the super method
 void remove(java.awt.MenuComponent mc)
          remove the popup menu
 void removeAllNodes()
          remove all nodes
 void removeNode(TNode node)
          removes a node ; local management of memory as in Vector
 void removeTListener(TListener listener)
          remove a TNode listener
 void repaint(long tm, int x, int y, int width, int height)
          this is to clean the component before repainting it
 void select()
          selects this node ; unselect the selected one and selects itself see unselect for the repaint
 void setColor(java.awt.Color color)
          change the color for the renderer
 void setColorSelected(java.awt.Color color)
          change the color for the background selection
 void setFont(java.awt.Font font)
          change the font : resend this to the component which represents the content of this node
 void setMutable(boolean flag)
          sets the mutable flag: mutable is inherited
 void setOpenOnDoubleClick(boolean flag)
          customize the double click->open/close
 void setRenderer(TNodeRenderer renderer)
          change the renderer ; test if it is not null
 void setScrollPane(java.awt.ScrollPane pane)
          set the scroll pane (useful for the root only)
 void toggle()
          toggle between collapse/expand and repaint
 java.lang.String toString()
          string representation of this node
 void unselect()
          unselects itself : repaints itself ; the component which represents this node should be able to repaint itself on its own after the setBackground call
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addNotify, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getInsets, getLayout, getMaximumSize, getMinimumSize, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setCursor, setLayout, update, validate, validateTree
 
Methods inherited from class java.awt.Component
action, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, 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, isFocusTraversable, 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, processComponentEvent, processFocusEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setDropTarget, setEnabled, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_SELECTED_COLOR

public static final java.awt.Color DEFAULT_SELECTED_COLOR
default color settings: the background when selected and the default color for the renderer

DEFAULT_COLOR

public static final java.awt.Color DEFAULT_COLOR

step

public static int step
step used to enlarge the array of leaves/children when adding a node useful when dealing with big trees to reduce memory allocation
Constructor Detail

TNode

public TNode(java.lang.Object object)
contructor : take the content ; if it is a component, it will display itself, otherwise a label will be created with its toString() the DefaultTNodeRenderer is used

TNode

public TNode(java.lang.Object object,
             TNodeRenderer renderer)
constructor : same as previously with a customized renderer

TNode

public TNode(java.lang.Object object,
             java.awt.Component item)
another constructor

TNode

public TNode(java.lang.Object object,
             TNodeRenderer renderer,
             java.awt.Component item)
real constructor : takes the content (object), the renderer and a component to display the content ; the component should be able to update itself when its background changes.
Method Detail

getContent

public java.lang.Object getContent()
returns the content of the node ; the content is immutable

setMutable

public void setMutable(boolean flag)
sets the mutable flag: mutable is inherited

isMutable

public boolean isMutable()
returns true if the node is mutable

getComponent

public java.awt.Component getComponent()
returns the component of the TNode

setOpenOnDoubleClick

public void setOpenOnDoubleClick(boolean flag)
customize the double click->open/close

getOpenOnDoubleClick

public boolean getOpenOnDoubleClick()

add

public void add(java.awt.PopupMenu pmenu)
add/set the popup menu
Overrides:
add in class java.awt.Component

remove

public void remove(java.awt.MenuComponent mc)
remove the popup menu
Overrides:
remove in class java.awt.Component

setFont

public void setFont(java.awt.Font font)
change the font : resend this to the component which represents the content of this node
Overrides:
setFont in class java.awt.Container

setRenderer

public void setRenderer(TNodeRenderer renderer)
change the renderer ; test if it is not null

getColorSelected

public java.awt.Color getColorSelected()
returns the color used for background selection

getColor

public java.awt.Color getColor()
returns the color used for the renderer ; the Graphics object has a g.setColor(..) just before the call to the renderer

setColor

public void setColor(java.awt.Color color)
change the color for the renderer

setColorSelected

public void setColorSelected(java.awt.Color color)
change the color for the background selection

setScrollPane

public void setScrollPane(java.awt.ScrollPane pane)
set the scroll pane (useful for the root only)

delete

public void delete()
delete this node ; works only if this is not the root ; delegate the call to its parent

getParentNode

public TNode getParentNode()
returns the parent node

isExpanded

public boolean isExpanded()
returns the expansion state

getSubNodes

public TNode[] getSubNodes()
returns all the sub nodes

getSubNode

public TNode getSubNode(int index)
returns one sub node null if invalid

getStringLabel

public java.lang.String getStringLabel()
return the string of the ?label

getSubContents

public java.lang.Object[] getSubContents()
returns the content of all sub nodes

addNode

public void addNode(TNode node)
add a new node to this tree ; bad things may happen if one adds twice the same node

invalidateNode

public static void invalidateNode(TNode node)
invalidate the node given in arguments if one node is not valid, then all its parents are not valid, so we do not need to propagate when we find one !valid, thus the test in the loop

invalidateNode

public void invalidateNode()
invalidates itself and revalidates itself ; should be called after some addNode in order to update the layout

removeNode

public void removeNode(TNode node)
removes a node ; local management of memory as in Vector

removeAllNodes

public void removeAllNodes()
remove all nodes

expand

public void expand()
expand this node ; and all the path to this node to make it visible

expandAll

public void expandAll()
expand all children and invalidate

collapse

public void collapse()
collapse this node

toString

public java.lang.String toString()
string representation of this node
Overrides:
toString in class java.awt.Component

getPreferredSize

public java.awt.Dimension getPreferredSize()
returns the preferred size of this node ; this does the computation of the size when not valid and does the layout as well
Overrides:
getPreferredSize in class java.awt.Container

paint

public void paint(java.awt.Graphics g)
the paint method: delegate the call to the renderer the component is called by the super method
Overrides:
paint in class java.awt.Container

repaint

public void repaint(long tm,
                    int x,
                    int y,
                    int width,
                    int height)
this is to clean the component before repainting it
Overrides:
repaint in class java.awt.Component

addTListener

public void addTListener(TListener listener)
add a TNode listener

removeTListener

public void removeTListener(TListener listener)
remove a TNode listener

unselect

public void unselect()
unselects itself : repaints itself ; the component which represents this node should be able to repaint itself on its own after the setBackground call

getRootNode

public TNode getRootNode()
returns the root node of this node

select

public void select()
selects this node ; unselect the selected one and selects itself see unselect for the repaint

toggle

public void toggle()
toggle between collapse/expand and repaint

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
mouse listener ; delegate the call to the listeners after processing (expand/collapse)
Specified by:
mouseClicked in interface java.awt.event.MouseListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Specified by:
mousePressed in interface java.awt.event.MouseListener

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
same as before ; we call the nodeClicked method as if it were a click when one presses the space bar ; the return key just toggles the node
Specified by:
keyTyped in interface java.awt.event.KeyListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
unused methods of listeners I prefer to use this method instead of using the "empty" listener provided by Sun, which we should extend, imagine, you download two classes just for a simple listener, and you have to pass arguments to have access to what you want. This is much simpler.
Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Specified by:
mouseExited in interface java.awt.event.MouseListener

keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
Specified by:
keyPressed in interface java.awt.event.KeyListener

keyReleased

public void keyReleased(java.awt.event.KeyEvent e)
Specified by:
keyReleased in interface java.awt.event.KeyListener