Xj3D 2.0 VRML/X3D Code API

org.web3d.x3d.dom.swing
Class DOMTreeModel

java.lang.Object
  extended by org.web3d.x3d.dom.swing.DOMTreeModel
All Implemented Interfaces:
javax.swing.tree.TreeModel, org.w3c.dom.events.EventListener

public class DOMTreeModel
extends java.lang.Object
implements javax.swing.tree.TreeModel, org.w3c.dom.events.EventListener

A complex implementation of the TreeModel that is used for dealing with DOM tree in combination with the cell renderer and DOMTreeNode classes.

This implementation is for generic DOM representations. It does not have any X3D specific handling. That is left to derived classes.

The model is specifically tailored to display DOM objects. The model is configured so that it should always ask if it allows children as some DOM nodes cannot have children. If the Document allows events then this will automatically add listeners and build the correct tree as the underlying DOM changes.

Each of the objects represented by this model will be the DOMTreeNode class. Building of these objects is lazy for memory consumption purposes. We do not build the child DOMTreeNode object until we have to.

The tree will also show attribute information as nodes in the tree. In order to maintain consistency, we will need to make sure that the indexes are always in the same order. To do this, we always put the attributes first and follow with child nodes.

To keep itself consistent, we only register a listener at the root of the document. This is for node add, remove and attribute modified. Because events bubble for these types, there is no need to register an event listener on every single node instance. We just listen at the document root for the events that bubble up to us.

Version:
$Revision: 1.3 $
Author:
Justin Couch

Constructor Summary
DOMTreeModel(org.w3c.dom.Node root)
          Create a new tree model that represents the given document root.
 
Method Summary
 void addTreeModelListener(javax.swing.event.TreeModelListener l)
          Add a tree listener to this model.
 java.lang.Object getChild(java.lang.Object parent, int index)
          Get the child of the given parent at that index.
 int getChildCount(java.lang.Object parent)
          Get the number of children the given parent contains.
 int getIndexOfChild(java.lang.Object parent, java.lang.Object child)
          Get the index of the given child in the parent node.
 java.lang.Object getRoot()
          Get the object that represents the root of this tree model.
 void handleEvent(org.w3c.dom.events.Event evt)
          Handle an incoming event.
 boolean isLeaf(java.lang.Object child)
          Check to see if the given node is a leaf node.
 void removeTreeModelListener(javax.swing.event.TreeModelListener l)
          Remove the tree listener from this model.
 void valueForPathChanged(javax.swing.tree.TreePath path, java.lang.Object value)
          Notification that the UI has changed the value to the destination object to the new value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DOMTreeModel

public DOMTreeModel(org.w3c.dom.Node root)
Create a new tree model that represents the given document root.

Parameters:
root - The DOM node representing the root of the tree
Method Detail

getChild

public java.lang.Object getChild(java.lang.Object parent,
                                 int index)
Get the child of the given parent at that index.

Specified by:
getChild in interface javax.swing.tree.TreeModel
Parameters:
parent - The parent node to ask
index - The position to get the child for
Returns:
The TreeNode object at that position

getChildCount

public int getChildCount(java.lang.Object parent)
Get the number of children the given parent contains. The number is both child elements and attributes as this tree will show both.

Specified by:
getChildCount in interface javax.swing.tree.TreeModel
Parameters:
parent - The parent to quiz for the number of children
Returns:
The number of children of that parent

getIndexOfChild

public int getIndexOfChild(java.lang.Object parent,
                           java.lang.Object child)
Get the index of the given child in the parent node.

Specified by:
getIndexOfChild in interface javax.swing.tree.TreeModel
Parameters:
parent - The parent node to check for
child - The child to find the index of
Returns:
The position of the child in the parent

getRoot

public java.lang.Object getRoot()
Get the object that represents the root of this tree model.

Specified by:
getRoot in interface javax.swing.tree.TreeModel
Returns:
The root document object

isLeaf

public boolean isLeaf(java.lang.Object child)
Check to see if the given node is a leaf node. Leaf nodes are determined if the DOM object does not support children.

Specified by:
isLeaf in interface javax.swing.tree.TreeModel
Parameters:
child - The child node to check
Returns:
True if the DOM node is a leaf

valueForPathChanged

public void valueForPathChanged(javax.swing.tree.TreePath path,
                                java.lang.Object value)
Notification that the UI has changed the value to the destination object to the new value. In this case, the value is always a string and only useful if the destination object is an Attribute. Adding and removing of whole subsections of the tree is not supported here.

Specified by:
valueForPathChanged in interface javax.swing.tree.TreeModel
Parameters:
path - The path to the object that changed
value - The new value for the Node

addTreeModelListener

public void addTreeModelListener(javax.swing.event.TreeModelListener l)
Add a tree listener to this model. Only one instance of the listener can be added at a time. A second call to add the same instance will silently ignore the request.

Specified by:
addTreeModelListener in interface javax.swing.tree.TreeModel
Parameters:
l - The listener to be added

removeTreeModelListener

public void removeTreeModelListener(javax.swing.event.TreeModelListener l)
Remove the tree listener from this model. If the instance is not known about the request is silently ignored.

Specified by:
removeTreeModelListener in interface javax.swing.tree.TreeModel
Parameters:
l - The listener to be removed

handleEvent

public void handleEvent(org.w3c.dom.events.Event evt)
Handle an incoming event. This will be for node insert/remove or attribute modified. Will cause the appropriate tree model event to be fired.

Specified by:
handleEvent in interface org.w3c.dom.events.EventListener
Parameters:
evt - The event to be processed

Xj3D 2.0 VRML/X3D Code API

Copyright © 2001 - 2006 Web3D Consortium