Class TreeTableModelAdapter
- java.lang.Object
-
- org.jdesktop.swingx.treetable.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
For usage in a read-only JXTreeTable: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 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 treeTableModel = new TreeTableModelAdapter(personTreeModel, new PersonNodeModel()); treeTable.setModel(treeTableModel);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 voidaddTreeModelListener(javax.swing.event.TreeModelListener l)java.lang.ObjectgetChild(java.lang.Object parent, int index)intgetChildCount(java.lang.Object parent)java.lang.Class<?>getColumnClass(int columnIndex)Returns the most specific superclass for all the cell values in the column.intgetColumnCount()Returns the number of columns in the model.java.lang.StringgetColumnName(int column)Returns the name of the column atcolumnIndex.intgetHierarchicalColumn()Returns the column that is the "tree" column.intgetIndexOfChild(java.lang.Object parent, java.lang.Object child)java.lang.ObjectgetRoot()java.lang.ObjectgetValueAt(java.lang.Object node, int column)Returns the value for thenodeatcolumnIndex.booleanisCellEditable(java.lang.Object node, int column)Returns true if the cell for thenodeatcolumnIndexis editable.booleanisLeaf(java.lang.Object node)voidremoveTreeModelListener(javax.swing.event.TreeModelListener l)voidsetNodeChangedMediator(NodeChangedMediator mediator)Sets the NodeChangedMediator.voidsetValueAt(java.lang.Object value, java.lang.Object node, int column)Sets the value for thenodeatcolumnIndextovalue.voidvalueForPathChanged(javax.swing.tree.TreePath path, java.lang.Object newValue)
-
-
-
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 theJXTreeTableto set up a default renderer and editor for the column.Implemented to delegate to the associated NodeModel.
- Specified by:
getColumnClassin interfaceTreeTableModel- 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. AJXTreeTableuses 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:
getColumnCountin interfaceTreeTableModel- 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.Implemented to delegate to the associated NodeModel.
- Specified by:
getColumnNamein interfaceTreeTableModel- 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:
getHierarchicalColumnin interfaceTreeTableModel- 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 thenodeatcolumnIndex. Thenodemust be managed by this model. Unamanaged nodes should throw anIllegalArgumentException.Implemented to delegate to the associated NodeModel.
- Specified by:
getValueAtin interfaceTreeTableModel- 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
- 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 thenodeatcolumnIndexis editable. Otherwise,setValueAton the cell will not change the value of that cell. Thenodemust be managed by this model. Unamanaged nodes should throw anIllegalArgumentException.Implemented to delegate to the associated NodeModel if the adapter is editable, returns false if the adapter is not editable.
- Specified by:
isCellEditablein interfaceTreeTableModel- 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:
isEditable()
-
setValueAt
public void setValueAt(java.lang.Object value, java.lang.Object node, int column)Sets the value for thenodeatcolumnIndextovalue. Thenodemust be managed by this model. Unamanaged nodes should throw anIllegalArgumentException.Implemented to delegate to the associated NodeModel if the cell is editable. Does nothing if the cell is not editable.
- Specified by:
setValueAtin interfaceTreeTableModel- 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),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:
getChildin interfacejavax.swing.tree.TreeModel
-
getChildCount
public int getChildCount(java.lang.Object parent)
Implemented to delegate to the adapted TreeModel.
- Specified by:
getChildCountin interfacejavax.swing.tree.TreeModel
-
getIndexOfChild
public int getIndexOfChild(java.lang.Object parent, java.lang.Object child)Implemented to delegate to the adapted TreeModel.
- Specified by:
getIndexOfChildin interfacejavax.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:
getRootin interfacejavax.swing.tree.TreeModel
-
isLeaf
public boolean isLeaf(java.lang.Object node)
Implemented to delegate to the adapted TreeModel.
- Specified by:
isLeafin interfacejavax.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:
valueForPathChangedin interfacejavax.swing.tree.TreeModel
-
addTreeModelListener
public void addTreeModelListener(javax.swing.event.TreeModelListener l)
- Specified by:
addTreeModelListenerin interfacejavax.swing.tree.TreeModel
-
removeTreeModelListener
public void removeTreeModelListener(javax.swing.event.TreeModelListener l)
- Specified by:
removeTreeModelListenerin interfacejavax.swing.tree.TreeModel
-
-
DataMelt 3.0 © DataMelt by jWork.ORG