Class DefaultTreeTableModel
- java.lang.Object
-
- org.jdesktop.swingx.treetable.AbstractTreeTableModel
-
- org.jdesktop.swingx.treetable.DefaultTreeTableModel
-
- All Implemented Interfaces:
javax.swing.tree.TreeModel
,TreeTableModel
public class DefaultTreeTableModel extends AbstractTreeTableModel
DefaultTreeTableModel
is a concrete implementation ofAbstractTreeTableModel
and is provided purely as a convenience for use withTreeTableNode
s. Applications that useJXTreeTable
withoutTreeTableNode
s are expected to provide their own implementation of aTreeTableModel
.The
DefaultTreeTableModel
is designed to be used withTreeTableNode
s. Specifically, users should extendAbstractMutableTreeTableNode
to provide custom implementations for data display.Users who do not provide a list of column identifiers must provide a root that contains at least one column. Without specified identifiers the model will attempt to calculate the columns required for display by querying the root node. Normally, the root node can be little more than a shell (in displays that hide it), but without identifiers, the model relies on the root node metadata for display.
- Author:
- Ramesh Gupta, Karl Schaefer
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<?>
columnIdentifiers
TheList
of column identifiers.-
Fields inherited from class org.jdesktop.swingx.treetable.AbstractTreeTableModel
modelSupport, root
-
-
Constructor Summary
Constructors Constructor Description DefaultTreeTableModel()
Creates a newDefaultTreeTableModel
with anull
root.DefaultTreeTableModel(TreeTableNode root)
Creates a newDefaultTreeTableModel
with the specifiedroot
.DefaultTreeTableModel(TreeTableNode root, java.util.List<?> columnNames)
Creates a newDefaultTreeTableModel
with the specifiedroot
and column names.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Object
getChild(java.lang.Object parent, int index)
int
getChildCount(java.lang.Object parent)
int
getColumnCount()
Returns the number of columns in the model.java.lang.String
getColumnName(int column)
Returns the name of the column atcolumnIndex
.int
getIndexOfChild(java.lang.Object parent, java.lang.Object child)
TreeTableNode[]
getPathToRoot(TreeTableNode aNode)
Gets the path from the root to the specified node.TreeTableNode
getRoot()
Returns the root of the tree.java.lang.Object
getValueAt(java.lang.Object node, int column)
Gets the value for thenode
atcolumn
.void
insertNodeInto(MutableTreeTableNode newChild, MutableTreeTableNode parent, int index)
Invoked this to insert newChild at location index in parents children.boolean
isCellEditable(java.lang.Object node, int column)
Returns true if the cell for thenode
atcolumnIndex
is editable.boolean
isLeaf(java.lang.Object node)
Returnstrue
ifnode
is a leaf.void
removeNodeFromParent(MutableTreeTableNode node)
Message this to remove node from its parent.void
setColumnIdentifiers(java.util.List<?> columnIdentifiers)
Replaces the column identifiers in the model.void
setRoot(TreeTableNode root)
Sets the root for this table model.void
setUserObject(TreeTableNode node, java.lang.Object userObject)
Sets the user object for a node.void
setValueAt(java.lang.Object value, java.lang.Object node, int column)
Sets the value for thenode
atcolumnIndex
tovalue
.void
valueForPathChanged(javax.swing.tree.TreePath path, java.lang.Object newValue)
Called when value for the item identified by path has been changed.-
Methods inherited from class org.jdesktop.swingx.treetable.AbstractTreeTableModel
addTreeModelListener, getColumnClass, getHierarchicalColumn, getTreeModelListeners, removeTreeModelListener
-
-
-
-
Constructor Detail
-
DefaultTreeTableModel
public DefaultTreeTableModel()
Creates a newDefaultTreeTableModel
with anull
root.
-
DefaultTreeTableModel
public DefaultTreeTableModel(TreeTableNode root)
Creates a newDefaultTreeTableModel
with the specifiedroot
.- Parameters:
root
- the root node of the tree
-
DefaultTreeTableModel
public DefaultTreeTableModel(TreeTableNode root, java.util.List<?> columnNames)
Creates a newDefaultTreeTableModel
with the specifiedroot
and column names.- Parameters:
root
- the root node of the treecolumnNames
- the names of the columns used by this model- See Also:
setColumnIdentifiers(List)
-
-
Method Detail
-
setColumnIdentifiers
public void setColumnIdentifiers(java.util.List<?> columnIdentifiers)
Replaces the column identifiers in the model. If the number ofnewIdentifier
s is greater than the current number of columns, new columns are added to the end of each row in the model. If the number ofnewIdentifier
s is less than the current number of columns, all the extra columns at the end of a row are discarded.- Parameters:
columnIdentifiers
- vector of column identifiers. Ifnull
, set the model to zero columns
-
getRoot
public TreeTableNode getRoot()
Returns the root of the tree. Returnsnull
only if the tree has no nodes.- Specified by:
getRoot
in interfacejavax.swing.tree.TreeModel
- Overrides:
getRoot
in classAbstractTreeTableModel
- Returns:
- the root of the tree
- Throws:
java.lang.ClassCastException
- ifroot
is not aTreeTableNode
. Even though subclasses have direct access toroot
, they should avoid accessing it directly.- See Also:
AbstractTreeTableModel.root
,setRoot(TreeTableNode)
-
getValueAt
public java.lang.Object getValueAt(java.lang.Object node, int column)
Gets the value for thenode
atcolumn
.- Parameters:
node
- the node whose value is to be queriedcolumn
- the column whose value is to be queried- Returns:
- the value Object at the specified cell
- Throws:
java.lang.IllegalArgumentException
- ifnode
is not an instance ofTreeTableNode
or is not managed by this model, orcolumn
is not a valid column index- See Also:
TreeTableModel.setValueAt(java.lang.Object, java.lang.Object, int)
,TableModel.getValueAt(int, int)
-
setValueAt
public void setValueAt(java.lang.Object value, java.lang.Object node, int column)
Sets the value for thenode
atcolumnIndex
tovalue
.- Specified by:
setValueAt
in interfaceTreeTableModel
- Overrides:
setValueAt
in classAbstractTreeTableModel
- Parameters:
value
- the new valuenode
- the node whose value is to be changedcolumn
- the column whose value is to be changed- See Also:
TreeTableModel.getValueAt(java.lang.Object, int)
,AbstractTreeTableModel.isCellEditable(java.lang.Object, int)
,TableModel.setValueAt(Object, int, int)
-
getColumnCount
public int getColumnCount()
Returns the number of columns in the model. AJXTreeTable
uses this method to determine how many columns it should create and display by default.- Returns:
- the number of columns in the model
- See Also:
TableModel.getColumnCount()
-
getColumnName
public java.lang.String getColumnName(int column)
Returns the name of the column atcolumnIndex
. This is used to initialize the table's column header name. Note: this name does not need to be unique; two columns in a table can have the same name.- Specified by:
getColumnName
in interfaceTreeTableModel
- Overrides:
getColumnName
in classAbstractTreeTableModel
- Parameters:
column
- the index of the column- Returns:
- the name of the column
- See Also:
TableModel.getColumnName(int)
-
getChild
public java.lang.Object getChild(java.lang.Object parent, int index)
-
getChildCount
public int getChildCount(java.lang.Object parent)
-
getIndexOfChild
public int getIndexOfChild(java.lang.Object parent, java.lang.Object child)
-
isCellEditable
public boolean isCellEditable(java.lang.Object node, int column)
Returns true if the cell for thenode
atcolumnIndex
is editable. Otherwise,setValueAt
on the cell will not change the value of that cell. Thenode
must be managed by this model. Unamanaged nodes should throw anIllegalArgumentException
.- Specified by:
isCellEditable
in interfaceTreeTableModel
- Overrides:
isCellEditable
in classAbstractTreeTableModel
- Parameters:
node
- the node whose value to be queriedcolumn
- the column whose value to be queried- Returns:
- true if the cell is editable
- See Also:
TreeTableModel.setValueAt(java.lang.Object, java.lang.Object, int)
,TableModel.isCellEditable(int, int)
-
isLeaf
public boolean isLeaf(java.lang.Object node)
Returnstrue
ifnode
is a leaf.- Specified by:
isLeaf
in interfacejavax.swing.tree.TreeModel
- Overrides:
isLeaf
in classAbstractTreeTableModel
- Parameters:
node
- a node in the tree, obtained from this data source- Returns:
- true if
node
is a leaf
-
getPathToRoot
public TreeTableNode[] getPathToRoot(TreeTableNode aNode)
Gets the path from the root to the specified node.- Parameters:
aNode
- the node to query- Returns:
- an array of
TreeTableNode
s, wherearr[0].equals(getRoot())
andarr[arr.length - 1].equals(aNode)
, or an empty array if the node is not found. - Throws:
java.lang.NullPointerException
- ifaNode
isnull
-
setRoot
public void setRoot(TreeTableNode root)
Sets the root for this table model. If no column identifiers have been specified, this will rebuild the identifier list, usingroot
as an examplar of the table.- Parameters:
root
- the node to set as root
-
insertNodeInto
public void insertNodeInto(MutableTreeTableNode newChild, MutableTreeTableNode parent, int index)
Invoked this to insert newChild at location index in parents children. This will then message nodesWereInserted to create the appropriate event. This is the preferred way to add children as it will create the appropriate event.
-
removeNodeFromParent
public void removeNodeFromParent(MutableTreeTableNode node)
Message this to remove node from its parent. This will message nodesWereRemoved to create the appropriate event. This is the preferred way to remove a node as it handles the event creation for you.
-
valueForPathChanged
public void valueForPathChanged(javax.swing.tree.TreePath path, java.lang.Object newValue)
Called when value for the item identified by path has been changed. If newValue signifies a truly new value the model should post atreeNodesChanged
event.This changes the object backing the
TreeTableNode
described by the path. This change does not alter a nodes children in any way. If you need to change structure of the node, use one of the provided mutator methods.- Specified by:
valueForPathChanged
in interfacejavax.swing.tree.TreeModel
- Overrides:
valueForPathChanged
in classAbstractTreeTableModel
- Parameters:
path
- path to the node that has changednewValue
- the new value- Throws:
java.lang.NullPointerException
- ifpath
isnull
java.lang.IllegalArgumentException
- ifpath
is not a path managed by this modeljava.lang.ClassCastException
- ifpath.getLastPathComponent()
is not aTreeTableNode
-
setUserObject
public void setUserObject(TreeTableNode node, java.lang.Object userObject)
Sets the user object for a node. Client code must use this method, so that the model can notify listeners that a change has occurred.This method is a convenient cover for
valueForPathChanged(TreePath, Object)
.- Parameters:
node
- the node to modifyuserObject
- the new user object to set- Throws:
java.lang.NullPointerException
- ifnode
isnull
java.lang.IllegalArgumentException
- ifnode
is not a node managed by this model
-
-