Class DebugTreeNodePanel

All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable

public class DebugTreeNodePanel
extends javax.swing.JPanel

This JPanel derived class can be used to display a DebugTreeNode graph and to save a shot of the graph as a PNG image.

Important notice: the data structure displayed can't have nodes with back references to parent nodes. Otherwise it will result into an infinite recursion loop!

This service can be a powerfull debug tool allowing to take/save a graphical view of tree data strcutures built with objects implementing the DebugTreeNode interface.

The class recognizes a set of control codes merged into the text returned by DebugTreeNode.getDebugNodeText() to allow some simple presentation styles, changing font attribute (underline, bold), font color and background color of the node. This can help to highlight with the use of color/font attributes some objects in the tree.

The capability to save the graph as PNG image allows debugging on those contexts where SWING can't be used or where an interactive interface is not available, as well as for a kind of graphical logging feature.

The class can show the tree in two different styles: the conventional root at the top style and the indentation style (using Knuth's terminology). By default the graph style is the conventional one. The style can be set with setGraphStyle(int). Following images show same expression A || B && C && D parsed into a tree of DebugTreeNode objects and displayed by DebugTreeNodePanel with conventional (left) and indentation (right) style:


The text returned by DebugTreeNode.getDebugNodeText() is drawn into a box for the node as node description. It can contain newline \n characters to have the text drawn on different lines. The text can also include control codes to change font and color attributes. At the beginning of each line of text the attributes are reset to normal font, black color. The background color codes apply to the whole node and only the first encountered background color code is used, others are ignored. The control codes are escaped by the % character. Valid control codes are listed in the following table:

 %% = the % character
 %u = underline
 %s = strikeout
 %r = red text
 %b = blue text
 %g = green text
 %d = dark grey text
 %w = white text
 %m = magenta text
 %y = light yellow background
 %Y = yellow background
 %c = light cyan background
 %e = light green background
 %k = red background
 %l = blue background
 %0 = reset to normal font / black
Unknown control character are interpreted as the character itself.

Following image shows an example of what can be achieved using control codes:

See Also:
DebugTreeNode, Serialized Form

Field Summary
static int EXPLORER
          Constant for tree drawing style indentation.
static int TREE
          Constant for tree drawing style conventional root at the top.
Fields inherited from class javax.swing.JComponent
Fields inherited from class java.awt.Component
Fields inherited from interface java.awt.image.ImageObserver
Constructor Summary
          Creates a new DebugTreeNodePanel object.
DebugTreeNodePanel(DebugTreeNode root)
          Creates a new DebugTreeNodePanel object.
Method Summary
 void drawIntoFile(java.lang.String name)
          Saves the tree as a PNG image whose pathname is passed as argument.
 int getGraphStyle()
          Return the current tree graph style.
protected  void paintComponent(java.awt.Graphics g)
          Paint the component contents.
 void setGraphStyle(int graphStyle)
          Set the current tree graph style.
 void setRoot(DebugTreeNode root)
          Sets the root node of the tree to be draw.
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Field Detail


public static final int TREE
Constant for tree drawing style conventional root at the top.

See Also:
Constant Field Values


public static final int EXPLORER
Constant for tree drawing style indentation.

See Also:
Constant Field Values
Constructor Detail


public DebugTreeNodePanel()
Creates a new DebugTreeNodePanel object.


public DebugTreeNodePanel(DebugTreeNode root)
Creates a new DebugTreeNodePanel object.

root - the tree to be displayed into the panel.
Method Detail


public void setRoot(DebugTreeNode root)
Sets the root node of the tree to be draw.

root - the tree to be displayed into the panel.


public void drawIntoFile(java.lang.String name)
                  throws java.io.IOException
Saves the tree as a PNG image whose pathname is passed as argument.

name - pathname of the PNG image file.
IOException - an I/O exception can occur while writing the image file.


protected void paintComponent(java.awt.Graphics g)
Paint the component contents. Invokes one of the tree paint methods based on the current style.

paintComponent in class javax.swing.JComponent
g - the Graphics to draw onto.


public void setGraphStyle(int graphStyle)
Set the current tree graph style.

graphStyle - new tree graph style, can be TREE or EXPLORER.


public int getGraphStyle()
Return the current tree graph style.

current tree drawing style.