Class DefaultTreeTableModel

java.lang.Object
org.jdesktop.swingx.treetable.AbstractTreeTableModel
org.jdesktop.swingx.treetable.DefaultTreeTableModel
All Implemented Interfaces:
TreeModel, TreeTableModel

public class DefaultTreeTableModel extends AbstractTreeTableModel
DefaultTreeTableModel is a concrete implementation of AbstractTreeTableModel and is provided purely as a convenience for use with TreeTableNodes. Applications that use JXTreeTable without TreeTableNodes are expected to provide their own implementation of a TreeTableModel.

The DefaultTreeTableModel is designed to be used with TreeTableNodes. Specifically, users should extend AbstractMutableTreeTableNode 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 Details

    • columnIdentifiers

      protected List<?> columnIdentifiers
      The List of column identifiers.
  • Constructor Details

    • DefaultTreeTableModel

      public DefaultTreeTableModel()
      Creates a new DefaultTreeTableModel with a null root.
    • DefaultTreeTableModel

      public DefaultTreeTableModel(TreeTableNode root)
      Creates a new DefaultTreeTableModel with the specified root.
      Parameters:
      root - the root node of the tree
    • DefaultTreeTableModel

      public DefaultTreeTableModel(TreeTableNode root, List<?> columnNames)
      Creates a new DefaultTreeTableModel with the specified root and column names.
      Parameters:
      root - the root node of the tree
      columnNames - the names of the columns used by this model
      See Also:
  • Method Details

    • setColumnIdentifiers

      public void setColumnIdentifiers(List<?> columnIdentifiers)
      Replaces the column identifiers in the model. If the number of newIdentifiers is greater than the current number of columns, new columns are added to the end of each row in the model. If the number of newIdentifiers 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. If null, set the model to zero columns
    • getRoot

      public TreeTableNode getRoot()
      Returns the root of the tree. Returns null only if the tree has no nodes.
      Specified by:
      getRoot in interface TreeModel
      Overrides:
      getRoot in class AbstractTreeTableModel
      Returns:
      the root of the tree
      Throws:
      ClassCastException - if root is not a TreeTableNode. Even though subclasses have direct access to root, they should avoid accessing it directly.
      See Also:
    • getValueAt

      public Object getValueAt(Object node, int column)
      Gets the value for the node at column. Delegates to TreeTableNode.getValueAt(int)
      Parameters:
      node - the node whose value is to be queried
      column - the column whose value is to be queried
      Returns:
      the value Object at the specified cell
      Throws:
      IllegalArgumentException - if node is not an instance of TreeTableNode or is not managed by this model, or column is not a valid column index
      See Also:
    • setValueAt

      public void setValueAt(Object value, Object node, int column)
      Sets the value for the node at columnIndex to value.

      is no-op; provided for convenience for uneditable models

      Specified by:
      setValueAt in interface TreeTableModel
      Overrides:
      setValueAt in class AbstractTreeTableModel
      Parameters:
      value - the new value
      node - the node whose value is to be changed
      column - the column whose value is to be changed
      See Also:
    • getColumnCount

      public int getColumnCount()
      Returns the number of columns in the model. A JXTreeTable 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:
    • getColumnName

      public String getColumnName(int column)
      Returns the name of the column at columnIndex. 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 interface TreeTableModel
      Overrides:
      getColumnName in class AbstractTreeTableModel
      Parameters:
      column - the index of the column
      Returns:
      the name of the column
      See Also:
    • getChild

      public Object getChild(Object parent, int index)
    • getChildCount

      public int getChildCount(Object parent)
    • getIndexOfChild

      public int getIndexOfChild(Object parent, Object child)
    • isCellEditable

      public boolean isCellEditable(Object node, int column)
      Returns true if the cell for the node at columnIndex is editable. Otherwise, setValueAt on the cell will not change the value of that cell. The node must be managed by this model. Unamanaged nodes should throw an IllegalArgumentException.
      Specified by:
      isCellEditable in interface TreeTableModel
      Overrides:
      isCellEditable in class AbstractTreeTableModel
      Parameters:
      node - the node whose value to be queried
      column - the column whose value to be queried
      Returns:
      true if the cell is editable
      See Also:
    • isLeaf

      public boolean isLeaf(Object node)
      Returns true if node is a leaf.
      Specified by:
      isLeaf in interface TreeModel
      Overrides:
      isLeaf in class AbstractTreeTableModel
      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 TreeTableNodes, where arr[0].equals(getRoot()) and arr[arr.length - 1].equals(aNode), or an empty array if the node is not found.
      Throws:
      NullPointerException - if aNode is null
    • 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, using root 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.
      Parameters:
      newChild - MutableTreeTableNode
      parent - MutableTreeTableNode
      index - TODO maven-javadoc-plugin 3.3.2 needs a doc here
    • 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.
      Parameters:
      node - MutableTreeTableNode
    • valueForPathChanged

      public void valueForPathChanged(TreePath path, 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 a treeNodesChanged 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 interface TreeModel
      Overrides:
      valueForPathChanged in class AbstractTreeTableModel
      Parameters:
      path - path to the node that has changed
      newValue - the new value
      Throws:
      NullPointerException - if path is null
      IllegalArgumentException - if path is not a path managed by this model
      ClassCastException - if path.getLastPathComponent() is not a TreeTableNode
    • setUserObject

      public void setUserObject(TreeTableNode node, 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 modify
      userObject - the new user object to set
      Throws:
      NullPointerException - if node is null
      IllegalArgumentException - if node is not a node managed by this model