org.openbp.swing.components.treetable
Class SimpleTreeTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by org.openbp.swing.components.treetable.SimpleTreeTableModel
All Implemented Interfaces:
java.io.Serializable, javax.swing.table.TableModel, javax.swing.tree.TreeModel, TreeTableModel
Direct Known Subclasses:
DefaultTreeTableModel

public class SimpleTreeTableModel
extends javax.swing.table.AbstractTableModel
implements TreeTableModel, java.io.Serializable

The SimpleTreeTableModel can be thought of as an abstract model, although all required methods have been implemented. When wanting to write an own TreeTableModel, it is suggested to extend this model and override only those methods as required.

Author:
Erich Lauterbach
See Also:
Serialized Form

Field Summary
protected  java.lang.String[] columnHeader
          The header information for the columns
protected  TreeTableNode root
          The root element used by the tree.
protected  JTreeTable treeTable
          The JTreeTable using this model.
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
SimpleTreeTableModel(TreeTableNode rootNode)
          Default Constructor Note: When using this constructor the tree table reference to this model will have to be explicity set.
SimpleTreeTableModel(TreeTableNode rootNode, JTreeTable treeTable)
          Default preferred constructor.
 
Method Summary
 void addTreeModelListener(javax.swing.event.TreeModelListener l)
          Adds a listener for the TreeModelEvent posted after the tree changes.
protected  void delayedFireTableDataChanged(int tableEventType)
          Invokes fireTableDataChanged after all the pending events have been processed.
 void fireNodeChanged(javax.swing.tree.TreeNode node)
          Invoke this method if you've totally changed the children of node and its childrens children...
 void fireNodeInserted(javax.swing.tree.TreeNode parent)
          Invoke this method if you've inserted a new node into the model.
 void fireNodeRemoved(javax.swing.tree.TreeNode parent)
          Invoke this method if you've removed a node from the model.
 void fireNodeStructureChanged(javax.swing.tree.TreeNode node)
          Invoke this method if you've totally changed the children of node and its childrens children...
 void fireTableChangesCompleted(int tableEventType)
          Notifies all tree table listeners that all changes have been completed.
protected  void fireTreeNodesChanged(java.lang.Object source, java.lang.Object[] path, int[] childIndices, java.lang.Object[] children)
          Notifies all listeners that have registered interest for notification on this event type.
protected  void fireTreeNodesInserted(java.lang.Object source, java.lang.Object[] path, int[] childIndices, java.lang.Object[] children)
          Notifies all listeners that have registered interest for notification on this event type.
protected  void fireTreeNodesRemoved(java.lang.Object source, java.lang.Object[] path, int[] childIndices, java.lang.Object[] children)
          Notifies all listeners that have registered interest for notification on this event type.
protected  void fireTreeStructureChanged(java.lang.Object source, java.lang.Object[] path, int[] childIndices, java.lang.Object[] children)
          Notifies all listeners that have registered interest for notification on this event type.
 java.lang.Object getChild(java.lang.Object parent, int index)
          Returns the child of parent at index index in the parent's child array.
 int getChildCount(java.lang.Object parent)
          Returns the number of children DefaultTreeTableNodes the receiver contains.
 java.lang.Class getColumnClass(int column)
          Returns the class type for column number column.
 int getColumnCount()
          Gets the number of columns contained to be used in the JTreeTable
 java.lang.String getColumnName(int columnIndex)
          Gets the column header for the specified column index.
 int getIndexOfChild(java.lang.Object parent, java.lang.Object child)
          Returns the index of child in parent.
 java.util.EventListener[] getListeners(java.lang.Class listenerType)
          Returns an array of all the objects currently registered as FooListeners upon this model.
 javax.swing.tree.TreeNode[] getPathToRoot(javax.swing.tree.TreeNode aNode)
          Builds the parents of node up to and including the root node, where the original node is the last element in the returned array.
protected  javax.swing.tree.TreeNode[] getPathToRoot(javax.swing.tree.TreeNode aNode, int depth)
          Builds the parents of node up to and including the root node, where the original node is the last element in the returned array.
 java.lang.Object getRoot()
          Returns the root of the tree.
 int getRowCount()
          Returns the number of rows contained in the table.
 javax.swing.event.TreeModelListener[] getTreeModelListeners()
          Returns an array of all the tree model listeners registered on this model.
 java.lang.Object getValueAt(int rowIndex, int columnIndex)
          Gets the object value for a specified cell for the receiver node.
 boolean isCellEditable(int row, int column)
          Returns if a cell at a specified row and column is editable.
 boolean isCellSelectable(int row, int column)
          Used by the tree table to determine if a specified cell is selectable.
 boolean isLeaf(java.lang.Object node)
          Returns true if the receiver is a leaf.
 void removeTreeModelListener(javax.swing.event.TreeModelListener l)
          Removes a listener previously added with addTreeModelListener.
 void setColumnHeader(java.lang.String[] columnHeader)
          Set the column headers.
 void setRoot(TreeTableNode root)
          Sets the root.
 void setTreeTable(JTreeTable treeTable)
          If the constructor was used in which the JTreeTable was not set, this method should be called directly after instantiating the model to set the JTreeTable.
 void setValueAt(java.lang.Object aValue, java.lang.Object node, int column)
          Sets the value for node node, at column number column.
 void valueForPathChanged(javax.swing.tree.TreePath path, java.lang.Object newValue)
          Messaged when the user has altered the value for the item identified by path to newValue.
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getTableModelListeners, removeTableModelListener, setValueAt
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.table.TableModel
addTableModelListener, removeTableModelListener, setValueAt
 

Field Detail

root

protected TreeTableNode root
The root element used by the tree.


treeTable

protected JTreeTable treeTable
The JTreeTable using this model.


columnHeader

protected java.lang.String[] columnHeader
The header information for the columns

Constructor Detail

SimpleTreeTableModel

public SimpleTreeTableModel(TreeTableNode rootNode)
Default Constructor Note: When using this constructor the tree table reference to this model will have to be explicity set. Rather use the other constructor.

Parameters:
rootNode - The root node for this model

SimpleTreeTableModel

public SimpleTreeTableModel(TreeTableNode rootNode,
                            JTreeTable treeTable)
Default preferred constructor.

Parameters:
rootNode - The root element of the tree
treeTable - The treeTable using this model
Method Detail

setTreeTable

public void setTreeTable(JTreeTable treeTable)
If the constructor was used in which the JTreeTable was not set, this method should be called directly after instantiating the model to set the JTreeTable.

Specified by:
setTreeTable in interface TreeTableModel
Parameters:
treeTable - The JTreeTable using this model

setColumnHeader

public void setColumnHeader(java.lang.String[] columnHeader)
Set the column headers.

Parameters:
columnHeader - A string array

getRowCount

public int getRowCount()
Returns the number of rows contained in the table.

Specified by:
getRowCount in interface javax.swing.table.TableModel
Returns:
The number of rows in the table

getColumnClass

public java.lang.Class getColumnClass(int column)
Returns the class type for column number column. By default for column 0 a TreeTableModel class is returned, and the rest are Object classes.

Specified by:
getColumnClass in interface javax.swing.table.TableModel
Overrides:
getColumnClass in class javax.swing.table.AbstractTableModel
Parameters:
column - The column of interest
Returns:
The class for a specified column

isCellEditable

public boolean isCellEditable(int row,
                              int column)
Returns if a cell at a specified row and column is editable. By default true is returned for all cells.

Specified by:
isCellEditable in interface javax.swing.table.TableModel
Overrides:
isCellEditable in class javax.swing.table.AbstractTableModel
Parameters:
row - The cell's row
column - The cell's column
Returns:
True if the specified cell is editable
False if the cell is NOT editable.

setValueAt

public void setValueAt(java.lang.Object aValue,
                       java.lang.Object node,
                       int column)
Sets the value for node node, at column number column. This method is not implemented an will throw an IllegalStateException, stating that the method needs to overridden. Reasons for this method not to be absract is that in some special cases this method might not be required.

Parameters:
aValue - The object to be set
node - The node to which the object is to be set
column - The column index where the object is to set

getValueAt

public java.lang.Object getValueAt(int rowIndex,
                                   int columnIndex)
Gets the object value for a specified cell for the receiver node.

Specified by:
getValueAt in interface javax.swing.table.TableModel
Parameters:
rowIndex - The row index of the specified cell
columnIndex - The column index for the specified cell
Returns:
Object The object for the cell

getColumnCount

public int getColumnCount()
Gets the number of columns contained to be used in the JTreeTable

Specified by:
getColumnCount in interface javax.swing.table.TableModel
Returns:
The number of columns

getColumnName

public java.lang.String getColumnName(int columnIndex)
Gets the column header for the specified column index.

Specified by:
getColumnName in interface javax.swing.table.TableModel
Overrides:
getColumnName in class javax.swing.table.AbstractTableModel
Parameters:
columnIndex - Index of the column
Returns:
The column header

getRoot

public java.lang.Object getRoot()
Returns the root of the tree. Returns null only if the tree has no nodes.

Specified by:
getRoot in interface javax.swing.tree.TreeModel
Returns:
the root of the tree

setRoot

public void setRoot(TreeTableNode root)
Sets the root.

Parameters:
root - The root to set

getChildCount

public int getChildCount(java.lang.Object parent)
Returns the number of children DefaultTreeTableNodes the receiver contains. See javax.swing.tree.TreeModel.getChildCount(Object).

Specified by:
getChildCount in interface javax.swing.tree.TreeModel
Parameters:
parent - The node from which the child count is required
Returns:
The number of children the parent node contains

isLeaf

public boolean isLeaf(java.lang.Object node)
Returns true if the receiver is a leaf. For further information see javax.swing.tree.TreeModel.isLeaf(Object)

Specified by:
isLeaf in interface javax.swing.tree.TreeModel
Parameters:
node - The node to be examined
Returns:
True if the node is a leaf, else false

getChild

public java.lang.Object getChild(java.lang.Object parent,
                                 int index)
Returns the child of parent at index index in the parent's child array. See javax.swing.tree.TreeModel.getChild(Object, int)

Specified by:
getChild in interface javax.swing.tree.TreeModel
Parameters:
parent - The node in the tree, obtained from this data source
index - The index of the child node in the parent array
Returns:
The child of parent at index index

getIndexOfChild

public int getIndexOfChild(java.lang.Object parent,
                           java.lang.Object child)
Returns the index of child in parent. If either the parent or child is null or not of type DefaultTreeTableNode, returns -1.

Specified by:
getIndexOfChild in interface javax.swing.tree.TreeModel
Parameters:
parent - a note in the tree, obtained from this data source
child - the node we are interested in
Returns:
the index of the child in the parent, or -1 if either the parent or the child is null

valueForPathChanged

public void valueForPathChanged(javax.swing.tree.TreePath path,
                                java.lang.Object newValue)
Messaged when the user has altered the value for the item identified by path to newValue. If newValue signifies a truly new value the model should post a treeNodesChanged event.

Specified by:
valueForPathChanged in interface javax.swing.tree.TreeModel
Parameters:
path - path to the node that the user has altered
newValue - the new value from the TreeCellEditor

isCellSelectable

public boolean isCellSelectable(int row,
                                int column)
Used by the tree table to determine if a specified cell is selectable.

Specified by:
isCellSelectable in interface TreeTableModel
Parameters:
row - Row index of the specified cell
column - Column index of the specified cell
Returns:
true if the cell is selectable
false if the cell isn't selectable.

addTreeModelListener

public void addTreeModelListener(javax.swing.event.TreeModelListener l)
Adds a listener for the TreeModelEvent posted after the tree changes.

Specified by:
addTreeModelListener in interface javax.swing.tree.TreeModel
Parameters:
l - the listener to add

removeTreeModelListener

public void removeTreeModelListener(javax.swing.event.TreeModelListener l)
Removes a listener previously added with addTreeModelListener.

Specified by:
removeTreeModelListener in interface javax.swing.tree.TreeModel
Parameters:
l - the listener to remove

getPathToRoot

public javax.swing.tree.TreeNode[] getPathToRoot(javax.swing.tree.TreeNode aNode)
Builds the parents of node up to and including the root node, where the original node is the last element in the returned array. The length of the returned array gives the node's depth in the tree.

Parameters:
aNode - the TreeNode to get the path for
Returns:
an array of TreeNodes giving the path from the root to the specified node

getPathToRoot

protected javax.swing.tree.TreeNode[] getPathToRoot(javax.swing.tree.TreeNode aNode,
                                                    int depth)
Builds the parents of node up to and including the root node, where the original node is the last element in the returned array. The length of the returned array gives the node's depth in the tree.

Parameters:
aNode - the TreeNode to get the path for
depth - an int giving the number of steps already taken towards the root (on recursive calls), used to size the returned array
Returns:
an array of TreeNodes giving the path from the root to the specified node

getTreeModelListeners

public javax.swing.event.TreeModelListener[] getTreeModelListeners()
Returns an array of all the tree model listeners registered on this model.

Returns:
all of this model's TreeModelListeners or an empty array if no tree model listeners are currently registered

fireNodeStructureChanged

public void fireNodeStructureChanged(javax.swing.tree.TreeNode node)
Invoke this method if you've totally changed the children of node and its childrens children... This will post a treeStructureChanged event.

Parameters:
node - The node where the structture has changed

fireNodeInserted

public void fireNodeInserted(javax.swing.tree.TreeNode parent)
Invoke this method if you've inserted a new node into the model.

Parameters:
parent - Parent of the node that was inserted

fireNodeRemoved

public void fireNodeRemoved(javax.swing.tree.TreeNode parent)
Invoke this method if you've removed a node from the model.

Parameters:
parent - Parent of the node that was removed

fireNodeChanged

public void fireNodeChanged(javax.swing.tree.TreeNode node)
Invoke this method if you've totally changed the children of node and its childrens children... This will post a treeStructureChanged event.

Parameters:
node - The node where the structure has changed

fireTreeNodesChanged

protected void fireTreeNodesChanged(java.lang.Object source,
                                    java.lang.Object[] path,
                                    int[] childIndices,
                                    java.lang.Object[] children)
Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Parameters:
source - the node being changed
path - the path to the root node
childIndices - the indices of the changed elements
children - the changed elements

fireTreeNodesInserted

protected void fireTreeNodesInserted(java.lang.Object source,
                                     java.lang.Object[] path,
                                     int[] childIndices,
                                     java.lang.Object[] children)
Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Parameters:
source - the node where new elements are being inserted
path - the path to the root node
childIndices - the indices of the new elements
children - the new elements

fireTreeNodesRemoved

protected void fireTreeNodesRemoved(java.lang.Object source,
                                    java.lang.Object[] path,
                                    int[] childIndices,
                                    java.lang.Object[] children)
Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Parameters:
source - the node where elements are being removed
path - the path to the root node
childIndices - the indices of the removed elements
children - the removed elements

fireTreeStructureChanged

protected void fireTreeStructureChanged(java.lang.Object source,
                                        java.lang.Object[] path,
                                        int[] childIndices,
                                        java.lang.Object[] children)
Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Parameters:
source - the node where the tree model has changed
path - the path to the root node
childIndices - the indices of the affected elements
children - the affected elements

fireTableChangesCompleted

public void fireTableChangesCompleted(int tableEventType)
Notifies all tree table listeners that all changes have been completed.

Parameters:
tableEventType - The table event type of TableModelEvent

getListeners

public java.util.EventListener[] getListeners(java.lang.Class listenerType)
Returns an array of all the objects currently registered as FooListeners upon this model. FooListeners are registered using the addFooListener method.

You can specify the listenerType argument with a class literal, such as FooListener.class. For example, you can query a DefaultTreeModel m for its tree model listeners with the following code:

TreeModelListener[] tmls = (TreeModelListener[])(m.getListeners(TreeModelListener.class));
If no such listeners exist, this method returns an empty array.

Overrides:
getListeners in class javax.swing.table.AbstractTableModel
Parameters:
listenerType - the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener
Returns:
an array of all objects registered as FooListeners on this component, or an empty array if no such listeners have been added
Throws:
java.lang.ClassCastException - if listenerType doesn't specify a class or interface that implements java.util.EventListener

delayedFireTableDataChanged

protected void delayedFireTableDataChanged(int tableEventType)
Invokes fireTableDataChanged after all the pending events have been processed. SwingUtilities.invokeLater is used to handle this.

Parameters:
tableEventType - The table event type of TableModelEvent


Copyright © 2011. All Rights Reserved.