org.apache.ecs.examples
Class HtmlTree

java.lang.Object
  |
  +--org.apache.ecs.examples.HtmlTree
All Implemented Interfaces:
java.io.Serializable

public class HtmlTree
extends java.lang.Object
implements java.io.Serializable

This JavaBean returns the hierarchical structure described in a javax.swing.tree.DefaultMutableTreeNode as valid XHTML. This class is a very simple counterpart of the javax.swing.JTree with the exception that the external Controller is integrated into this View. If you want your tree elements (nodes and leafs) to be marked with an anchor, you'll have to make sure that the name of your node contains such an anchor.

A specific node in a tree can be identified by a path, described as colon separated integer values (e.g. "0:1"). Unlike Swing's JTree component, this JavaBean expands only the requested node, leaving all its parent nodes collapsed. An expanded node is one displays its children. A collapsed node is one which hides them.

You can use this class in a JavaServerPage like this:

...template text...

<%-- Get the bean from the request scope or create a new one if none exists --%>
<jsp:useBean id="tree" scope="request" class="org.apache.ecs.examples.HtmlTree">

<%-- Read the path from the request --%>
<jsp:setProperty name="tree" property="path" param="path" />

<%-- Set UI properties --%>
<jsp:setProperty name="tree" property="rootVisible" value="true" />
<jsp:setProperty name="tree" property="indentation" value="2" />
<jsp:setProperty name="tree" property="openIcon" value="open.gif" />
<jsp:setProperty name="tree" property="closedIcon" value="closed.gif" />
<jsp:setProperty name="tree" property="leafIcon" value="leaf.gif" />

<%-- Workaround for Netscape and Opera browsers --%>
<jsp:setProperty name="tree" property="action" value="tree.jsp" />

</jsp:useBean>

...template text...

<%-- Get the XHTML output from the bean --%>
<jsp:getProperty name="tree" property="html" />

Although this class is just an example of using parts of the Element Construction Set, you can use it quite well in real web applications.

Version:
1.1, 2001/12/30, added ability to use a DefaultTreeModel as the data model, added some useful methods including one for basic HTML encoding, improved javadoc comments and fixed minor bugs
Author:
Christian Brensing
See Also:
Serialized Form

Field Summary
protected  java.lang.String action
          Context path of a web resource for the workaround of a strange behaviour of Netscape and Opera browsers.
protected  java.lang.String closedIcon
          Icon for displaying closed nodes.
protected  javax.swing.tree.DefaultMutableTreeNode displayNode
          Stores the requested node to be displayed.
protected  int indentation
          Number of blank spaces around each hierarchical level will be indented in relation to its parent node.
protected  java.lang.String leafIcon
          Icon for displaying leafs.
protected  java.lang.String openIcon
          Icon for displaying open nodes.
protected  java.lang.String parameterName
          Parameter name used to build the query string for the anchor that acts as the Controller for opening and closing nodes.
protected  java.lang.String path
          Path to the requested node to be displayed, described as colon separated integer values.
private  int[] pathArray
          Array that stores the index of each node read from the path property.
protected  javax.swing.tree.DefaultMutableTreeNode root
          The node that defines the tree displayed by this object.
protected  boolean rootVisible
          True if the root node is displayed, false if its children are the highest visible nodes.
 
Constructor Summary
HtmlTree()
          Returns a HtmlTree with a sample model.
HtmlTree(javax.swing.tree.DefaultMutableTreeNode root)
          Returns a HtmlTree with the specified DefaultMutableTreeNode as its root.
HtmlTree(javax.swing.tree.DefaultTreeModel model)
          Returns a HtmlTree with the specified DefaultTreeModel as its model.
 
Method Summary
static java.lang.String encodeToHtml(java.lang.String in)
          Returns the specified string encoded into a format suitable for HTML.
protected static javax.swing.tree.DefaultMutableTreeNode getDefaultTreeModel()
          Creates and returns a sample tree model.
 java.lang.String getHtml()
          Returns the hierarchical structure described in the specified root node as valid XHTML.
 java.lang.String getImg(java.lang.String icon)
          Constructs a valid XHTML <img>-tag from the specified icon.
 int getIndentation()
          Returns the number of blank spaces around each hierarchical level is indented in relation to its parent node.
 javax.swing.tree.DefaultTreeModel getModel()
          Returns the model that defines the tree displayed by this object.
 java.lang.String getNodeName()
          Returns the name of the currently expanded node.
 java.lang.String getParameterName()
          Returns the name of the parameter used to build the query string for the anchor that acts as the Controller for opening and closing nodes.
 java.lang.String getPath()
          Returns the path to the requested node.
 java.lang.String getPath(int level)
          Returns a path constructed from the path array for the specified level.
static int[] getPathResolved(java.lang.String path)
          Returns an array that stores the index of each node read from the specified path.
 javax.swing.tree.DefaultMutableTreeNode getRequestedNode()
          Expands the tree by following the specified path and returns the requested node to be displayed.
 javax.swing.tree.DefaultMutableTreeNode getRoot()
          Returns the node that defines the tree displayed by this object.
 int getRowCount()
          Returns the number of rows that are currently being displayed.
 boolean isRootVisible()
          Returns true if the root node of the tree is displayed.
 void setAction(java.lang.String action)
          Workaround of a strange behaviour of Netscape and Opera browsers.
 void setClosedIcon(java.lang.String closedIcon)
          Sets the icon for displaying closed nodes.
 void setIndentation(int indentation)
          Sets the number of blank spaces around each hierarchical level will be indented in relation to its parent node.
 void setLeafIcon(java.lang.String leafIcon)
          Sets the icon for displaying leafs.
 void setModel(javax.swing.tree.DefaultTreeModel model)
          Sets the tree model that will provide the data.
 void setOpenIcon(java.lang.String openIcon)
          Sets the icon for displaying open nodes.
 void setParameterName(java.lang.String parameterName)
          Sets the name of the parameter used to build the query string for the anchor that acts as the Controller for opening and closing nodes.
 void setPath(java.lang.String path)
          Sets the path - described as colon separated integer values - to the requested node to be displayed.
 void setRoot(javax.swing.tree.DefaultMutableTreeNode root)
          Sets the root node that will provide the data.
 void setRootVisible(boolean rootVisible)
          Determines whether or not the root node is visible.
 java.lang.String toString()
          Overrides toString() to print something meaningful.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

indentation

protected int indentation
Number of blank spaces around each hierarchical level will be indented in relation to its parent node. Default is 2.

path

protected java.lang.String path
Path to the requested node to be displayed, described as colon separated integer values. Default is an empty String.

parameterName

protected java.lang.String parameterName
Parameter name used to build the query string for the anchor that acts as the Controller for opening and closing nodes. Default is "path".

pathArray

private int[] pathArray
Array that stores the index of each node read from the path property.

leafIcon

protected java.lang.String leafIcon
Icon for displaying leafs. Default is an empty String.

openIcon

protected java.lang.String openIcon
Icon for displaying open nodes. Default is an empty String.

closedIcon

protected java.lang.String closedIcon
Icon for displaying closed nodes. Default is an empty String.

action

protected java.lang.String action
Context path of a web resource for the workaround of a strange behaviour of Netscape and Opera browsers. Default is an empty String.

rootVisible

protected boolean rootVisible
True if the root node is displayed, false if its children are the highest visible nodes. Default is true.

root

protected javax.swing.tree.DefaultMutableTreeNode root
The node that defines the tree displayed by this object. Default is a sample model.

displayNode

protected javax.swing.tree.DefaultMutableTreeNode displayNode
Stores the requested node to be displayed. Default is the root node.
Constructor Detail

HtmlTree

public HtmlTree()
Returns a HtmlTree with a sample model.

HtmlTree

public HtmlTree(javax.swing.tree.DefaultMutableTreeNode root)
Returns a HtmlTree with the specified DefaultMutableTreeNode as its root. If the DefaultMutableTreeNode is null, a sample model is set automatically.
Parameters:
root - a DefaultMutableTreeNode object

HtmlTree

public HtmlTree(javax.swing.tree.DefaultTreeModel model)
Returns a HtmlTree with the specified DefaultTreeModel as its model. If the specified DefaultTreeModel is null, a sample model is set automatically.
Parameters:
model - a DefaultTreeModel object
Method Detail

setRoot

public void setRoot(javax.swing.tree.DefaultMutableTreeNode root)
Sets the root node that will provide the data. If the specified DefaultMutableTreeNode is null, a sample model is set automatically.
Parameters:
root - a DefaultMutableTreeNode object

getRoot

public javax.swing.tree.DefaultMutableTreeNode getRoot()
Returns the node that defines the tree displayed by this object.
Returns:
the root node that provides the data

setModel

public void setModel(javax.swing.tree.DefaultTreeModel model)
Sets the tree model that will provide the data. If the specified DefaultTreeModel is null, a sample model is set automatically.
Parameters:
model - a DefaultTreeModel object

getModel

public javax.swing.tree.DefaultTreeModel getModel()
Returns the model that defines the tree displayed by this object.
Returns:
the model that provides the data

setPath

public void setPath(java.lang.String path)
Sets the path - described as colon separated integer values - to the requested node to be displayed.
Parameters:
path - the path to the requested node to be displayed

getPath

public java.lang.String getPath()
Returns the path to the requested node.
Returns:
the path to the requested node

getPath

public java.lang.String getPath(int level)
Returns a path constructed from the path array for the specified level.
Parameters:
level - the distance from the node to the root node.
Returns:
the path for the specified level

getPathResolved

public static int[] getPathResolved(java.lang.String path)
                             throws java.lang.NumberFormatException
Returns an array that stores the index of each node read from the specified path.
Returns:
an array of node indices
Throws:
java.lang.NumberFormatException - if the specified path does not consist of colon separated integer values

setParameterName

public void setParameterName(java.lang.String parameterName)
Sets the name of the parameter used to build the query string for the anchor that acts as the Controller for opening and closing nodes. You only have to set this name if the default name "path" is already used by your web application.
Parameters:
parameterName - the name of the parameter used to build the query string

getParameterName

public java.lang.String getParameterName()
Returns the name of the parameter used to build the query string for the anchor that acts as the Controller for opening and closing nodes.
Returns:
the name of the parameter

setRootVisible

public void setRootVisible(boolean rootVisible)
Determines whether or not the root node is visible.
Parameters:
rootVisible - true if the root node of the tree is to be displayed

isRootVisible

public boolean isRootVisible()
Returns true if the root node of the tree is displayed.
Returns:
true if the root node of the tree is displayed

setIndentation

public void setIndentation(int indentation)
Sets the number of blank spaces around each hierarchical level will be indented in relation to its parent node.
Parameters:
indentation - the number of blank spaces

getIndentation

public int getIndentation()
Returns the number of blank spaces around each hierarchical level is indented in relation to its parent node.
Returns:
the number of blank spaces

getNodeName

public java.lang.String getNodeName()
Returns the name of the currently expanded node.
Returns:
the name of the node

getRowCount

public int getRowCount()
Returns the number of rows that are currently being displayed.
Returns:
the number of rows

setOpenIcon

public void setOpenIcon(java.lang.String openIcon)
Sets the icon for displaying open nodes.
Parameters:
openIcon - the URI of an image file

setClosedIcon

public void setClosedIcon(java.lang.String closedIcon)
Sets the icon for displaying closed nodes.
Parameters:
closedIcon - the URI of an image file

setLeafIcon

public void setLeafIcon(java.lang.String leafIcon)
Sets the icon for displaying leafs.
Parameters:
leafIcon - the URI of an image file

setAction

public void setAction(java.lang.String action)
Workaround of a strange behaviour of Netscape and Opera browsers. In these browsers the relative URL containg only the query string with the path information (e.g. "?path=0:1") are not translated correctly into an absoulte URL. By setting a context path, on which to append the query string, this behaviour should be fixed.
Parameters:
action - the context path of a web resource (e.g. a JSP-File)

getImg

public java.lang.String getImg(java.lang.String icon)
Constructs a valid XHTML <img>-tag from the specified icon. Although this method uses the xml-package to generate a valid tag, the xhtml-package would provide the same functionality.
Parameters:
icon - the URI of an image file
Returns:
a valid XHTML <img>-tag

getRequestedNode

public javax.swing.tree.DefaultMutableTreeNode getRequestedNode()
Expands the tree by following the specified path and returns the requested node to be displayed.
Returns:
the requested node to be displayed

getHtml

public java.lang.String getHtml()
Returns the hierarchical structure described in the specified root node as valid XHTML. For perfomance reasons all parent nodes of the requested node are collapsed. The source code does not contain any formatting tags or attributes. I recommend that you use Cascading Style Sheets instead. For this purpose, the tree elements are marked with the class attribute values "tree", "parent" and "child".

A sample Style Sheet would look like this:

a { color: black; text-decoration: none; }
a:hover { color: black; text-decoration: underline; }
img { margin-right: 5px; vertical-align: middle; border: none; }
table { font: normal 8pt Arial,Helvetica,sans-serif; }
td.parent { font-weight: bold; }

Returns:
the hierarchical structure within a <table>-tag

toString

public java.lang.String toString()
Overrides toString() to print something meaningful. Returns the hierarchical structure described in the specified root node as valid XHTML.
Overrides:
toString in class java.lang.Object
See Also:
getHtml()

getDefaultTreeModel

protected static javax.swing.tree.DefaultMutableTreeNode getDefaultTreeModel()
Creates and returns a sample tree model. Used primarily during the design of the JavaServerPage to show something interesting.
Returns:
a DefaultMutableTreeNode with a sample tree model

encodeToHtml

public static java.lang.String encodeToHtml(java.lang.String in)
Returns the specified string encoded into a format suitable for HTML. All single-quote, double-quote, greater-than, less-than and ampersand characters are replaced with their corresponding HTML Character Entity codes. You can use this method to encode the designated node names before appending this node to your tree model. Please don't encode node names that already include a valid tag, because they are equally converted and thus won't be displayed by the browser.
Parameters:
in - the String to encode
Returns:
the encoded String


Copyright © 1999-2003 Apache Software Foundation. All Rights Reserved.