Documentation of 'org.jdesktop.swingx.treetable.TreeTableModelAdapter' Java class
TreeTableModelAdapter
org.jdesktop.swingx.treetable

Class TreeTableModelAdapter

  • All Implemented Interfaces:
    javax.swing.tree.TreeModel, TreeTableModel


    public class TreeTableModelAdapter
    extends java.lang.Object
    implements TreeTableModel
    Adapts a TreeModel to a TreeTableModel. The per-node columnar data is provided by a NodeModel. If the columnar data should be editable, a NodeChangedMediator is required to handle the change notification in the adapted TreeModel.

    Example usage: Assuming a default tree model with build from treeNodes which have Person objects as userObject, developers can implement a PersonNodeModel for the columnar data

    
     public class PersonNodeModel implements NodeModel {
          public Class<?> getColumnClass(int column) {
          if (column == 2) return Date.class;
          return String.class;
      }
     
      public int getColumnCount() {
          return 3;
      }
     
      public String getColumnName(int column) {
          switch(column) {
                  case 0:
                          return "Name";
                  case 1:
                          return "First Name";
                  case 2:
                          return "Birth Date";
                  default:
                          return null;
          }
      }
     
      public int getHierarchicalColumn() {
          return 0;
      }
     
      public Object getValueAt(Object node, int column) {
          Person person = getPerson(node);
          if (person == null) return note;
          switch (column) {
          case 0:
                  return person.getFamilyName();
          case 1:
                  return person.getFirstName();
          case 2:
                  return person.getBirthDate();
          }
          return null;
      }
     
      private Person getPerson(Object node) {
          if (node instanceof DefaultMutableTreeNode) {
                  node = ((DefaultMutableTreeNode) node).getUserObject();
          }
          if (node instanceof Person) return (Person) node;
          return null;
      }
     
      public boolean isCellEditable(Object node, int column) {
          return true;
      }
     
      public void setValueAt(Object value, Object node, int column) {
          Person person = getPerson(node);
          if (note == null) return;
          switch(column) {
          case 0:
                  person.setFamilyName(String.valueOf(value));
                  break;
          case 1:
                  person.setFirstName((String) value);
                  break;
          case 2:
                  person.setBirthDate((Date) value);
     
          }
     
      }
     
     
     }
     
    For usage in a read-only JXTreeTable:
    
     TreeTableModel treeTableModel = new TreeTableModelAdapter(personTreeModel,
             new PersonNodeModel());
     treeTable.setModel(treeTableModel);
     
    For usage in an editable JXTreeTable, you'll need to configure the adapter with an appropriate NodeChangedMediator which takes care of change notification by the adapted TreeModel. For TreeModels of type DefaultTreeModel, there's a pre-defined default mediator (for custom types you'll have to implement a custom mediator which knows how to trigger changed events on behalf of the TreeModel):
    
     treeTableModel.setNodeChangedMediator(NodeChangedMediator.DEFAULT);
     
    • Constructor Summary

      Constructors 
      Constructor and Description
      TreeTableModelAdapter(javax.swing.tree.TreeModel treeModel, NodeModel nodeModel)
      Instantiates a TreeTableModelAdapter from the given TreeModel and NodeModel.
      TreeTableModelAdapter(javax.swing.tree.TreeModel treeModel, NodeModel nodeModel, NodeChangedMediator mediator)
      Instantiates a TreeTableModelAdapter from the given TreeModel, NodeModel and NodeChangedMediator.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      void addTreeModelListener(javax.swing.event.TreeModelListener l)
      java.lang.Object getChild(java.lang.Object parent, int index)
      int getChildCount(java.lang.Object parent)
      java.lang.Class<?> getColumnClass(int columnIndex)
      Returns the most specific superclass for all the cell values in the column.
      int getColumnCount()
      Returns the number of columns in the model.
      java.lang.String getColumnName(int column)
      Returns the name of the column at columnIndex.
      int getHierarchicalColumn()
      Returns the column that is the "tree" column.
      int getIndexOfChild(java.lang.Object parent, java.lang.Object child)
      java.lang.Object getRoot()
      java.lang.Object getValueAt(java.lang.Object node, int column)
      Returns the value for the node at columnIndex.
      boolean isCellEditable(java.lang.Object node, int column)
      Returns true if the cell for the node at columnIndex is editable.
      boolean isLeaf(java.lang.Object node)
      void removeTreeModelListener(javax.swing.event.TreeModelListener l)
      void setNodeChangedMediator(NodeChangedMediator mediator)
      Sets the NodeChangedMediator.
      void setValueAt(java.lang.Object value, java.lang.Object node, int column)
      Sets the value for the node at columnIndex to value.
      void valueForPathChanged(javax.swing.tree.TreePath path, java.lang.Object newValue)
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TreeTableModelAdapter

        public TreeTableModelAdapter(javax.swing.tree.TreeModel treeModel,
                                     NodeModel nodeModel)
        Instantiates a TreeTableModelAdapter from the given TreeModel and NodeModel. The NodeChangedMediator is set to null.
        Parameters:
        treeModel - the TreeModel to adapt.
        nodeModel - the NodelModel which decorates the columnar properties.
      • TreeTableModelAdapter

        public TreeTableModelAdapter(javax.swing.tree.TreeModel treeModel,
                                     NodeModel nodeModel,
                                     NodeChangedMediator mediator)
        Instantiates a TreeTableModelAdapter from the given TreeModel, NodeModel and NodeChangedMediator. If the mediator is null, this adapter is not editable.
        Parameters:
        treeModel - the TreeModel to adapt.
        nodeModel - the NodelModel which decorates the columnar properties.
        mediator - the NodeChangedMediator responsible for nodeChanged notification.
    • Method Detail

      • setNodeChangedMediator

        public void setNodeChangedMediator(NodeChangedMediator mediator)
        Sets the NodeChangedMediator.
        Parameters:
        mediator - the mediator responsible for node changed notification on the adapted TreeModel.
      • getColumnClass

        public java.lang.Class<?> getColumnClass(int columnIndex)
        Returns the most specific superclass for all the cell values in the column. This is used by the JXTreeTable to set up a default renderer and editor for the column.

        Implemented to delegate to the associated NodeModel.

        Specified by:
        getColumnClass in interface TreeTableModel
        Parameters:
        columnIndex - the index of the column
        Returns:
        the common ancestor class of the object values in the model.
        See Also:
        TableModel.getColumnClass(int)
      • 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.

        Implemented to delegate to the associated NodeModel or return 0 if NodeModel is null.

        Specified by:
        getColumnCount in interface TreeTableModel
        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 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.

        Implemented to delegate to the associated NodeModel.

        Specified by:
        getColumnName in interface TreeTableModel
        Parameters:
        column - the index of the column
        Returns:
        the name of the column
        See Also:
        TableModel.getColumnName(int)
      • getHierarchicalColumn

        public int getHierarchicalColumn()
        Returns the column that is the "tree" column. While it is not required, most implementations will default the first column to be the hierarchical one.

        Implemented to delegate to the associated NodeModel or return -1 if nodeModel is null.

        Specified by:
        getHierarchicalColumn in interface TreeTableModel
        Returns:
        the index of the hierarchical column or -1 if no column is the hierarchical column.
      • getValueAt

        public java.lang.Object getValueAt(java.lang.Object node,
                                           int column)
        Returns the value for the node at columnIndex. The node must be managed by this model. Unamanaged nodes should throw an IllegalArgumentException.

        Implemented to delegate to the associated NodeModel.

        Specified by:
        getValueAt in interface TreeTableModel
        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
        See Also:
        TreeTableModel.setValueAt(java.lang.Object, java.lang.Object, int), TableModel.getValueAt(int, int)
      • isCellEditable

        public boolean isCellEditable(java.lang.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.

        Implemented to delegate to the associated NodeModel if the adapter is editable, returns false if the adapter is not editable.

        Specified by:
        isCellEditable in interface TreeTableModel
        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:
        isEditable()
      • setValueAt

        public void setValueAt(java.lang.Object value,
                               java.lang.Object node,
                               int column)
        Sets the value for the node at columnIndex to value. The node must be managed by this model. Unamanaged nodes should throw an IllegalArgumentException.

        Implemented to delegate to the associated NodeModel if the cell is editable. Does nothing if the cell is not editable.

        Specified by:
        setValueAt in interface TreeTableModel
        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:
        TreeTableModel.getValueAt(java.lang.Object, int), TreeTableModel.isCellEditable(java.lang.Object, int), TableModel.setValueAt(Object, int, int)
      • getChild

        public java.lang.Object getChild(java.lang.Object parent,
                                         int index)

        Implemented to delegate to the adapted TreeModel.

        Specified by:
        getChild in interface javax.swing.tree.TreeModel
      • getChildCount

        public int getChildCount(java.lang.Object parent)

        Implemented to delegate to the adapted TreeModel.

        Specified by:
        getChildCount in interface javax.swing.tree.TreeModel
      • getIndexOfChild

        public int getIndexOfChild(java.lang.Object parent,
                                   java.lang.Object child)

        Implemented to delegate to the adapted TreeModel.

        Specified by:
        getIndexOfChild in interface javax.swing.tree.TreeModel
      • getRoot

        public java.lang.Object getRoot()

        Implemented to delegate to the adapted TreeModel if available, or returns null if the TreeModel is null.

        Specified by:
        getRoot in interface javax.swing.tree.TreeModel
      • isLeaf

        public boolean isLeaf(java.lang.Object node)

        Implemented to delegate to the adapted TreeModel.

        Specified by:
        isLeaf in interface javax.swing.tree.TreeModel
      • valueForPathChanged

        public void valueForPathChanged(javax.swing.tree.TreePath path,
                                        java.lang.Object newValue)

        Implemented to delegate to the adapted TreeModel.

        Specified by:
        valueForPathChanged in interface javax.swing.tree.TreeModel
      • addTreeModelListener

        public void addTreeModelListener(javax.swing.event.TreeModelListener l)

        Specified by:
        addTreeModelListener in interface javax.swing.tree.TreeModel
      • removeTreeModelListener

        public void removeTreeModelListener(javax.swing.event.TreeModelListener l)

        Specified by:
        removeTreeModelListener in interface javax.swing.tree.TreeModel

DataMelt 3.0 © DataMelt by jWork.ORG

You see the box below because you did not login.