CubeTwister 2.0alpha142 2012-02-11

ch.randelshofer.gui.table
Class TreeNodeTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by ch.randelshofer.gui.table.TreeNodeTableModel
All Implemented Interfaces:
MutableTableModel, java.io.Serializable, java.util.EventListener, javax.swing.event.TreeModelListener, javax.swing.table.TableModel
Direct Known Subclasses:
CubeColorsTableModel, NotationMacrosTableModel, ScriptMacrosTableModel

public class TreeNodeTableModel
extends javax.swing.table.AbstractTableModel
implements MutableTableModel, javax.swing.event.TreeModelListener

Wraps a TreeNode of a MutableTreeModel into a MutableTableModel.

Version:
1.1 2011-01-22 Use DefaultMutableTreeNode instead of MutableTreeNode.
1.0 2001-10-08
Author:
Werner Randelshofer
See Also:
Serialized Form

Field Summary
protected  MutableTreeModel treeModel
           
protected  javax.swing.tree.DefaultMutableTreeNode treeNode
           
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
TreeNodeTableModel()
          Creates new TreeNodeTableModel.
TreeNodeTableModel(MutableTreeModel m, javax.swing.tree.DefaultMutableTreeNode n)
          Creates new TreeNodeTableModel.
 
Method Summary
 void createRow(int row, java.lang.Object type)
          Invoke this to create a new row into the table.
 java.awt.datatransfer.Transferable exportRowTransferable(int[] rows)
          Creates a Transferable to use as the source for a data transfer of the specified elements.
 int getColumnCount()
          Returns the number of columns in the model.
 java.lang.Object getCreatableRowType(int row)
          Returns the default type of elements that can be created at the specified index of the list.
 java.lang.Object[] getCreatableRowTypes(int row)
          Returns the types of children that may be created at this row.
 javax.swing.tree.DefaultMutableTreeNode getRow(int row)
           
 javax.swing.Action[] getRowActions(int[] rows)
          Gets actions for the indicated row.
 int getRowCount()
          Returns the number of rows in the model.
 java.lang.Object getValueAt(int row, int column)
          Returns the value for the cell at columnIndex and rowIndex.
 int importRowTransferable(java.awt.datatransfer.Transferable t, int action, int row, boolean asChild)
          Causes a transfer to the model from a clipboard or a DND drop operation.
 boolean isCellEditable(int row, int column)
          Returns true if the cell at rowIndex and columnIndex is editable.
 boolean isRowAddable(int row)
          Returns true if a row can be added.
 boolean isRowImportable(java.awt.datatransfer.DataFlavor[] transferFlavors, int action, int row, boolean asChild)
          Indicates whether the model would accept an import of the given set of data flavors prior to actually attempting to import it.
 boolean isRowRemovable(int row)
          Returns wether the specified row may be removed.
 void removeRow(int row)
          Remove a row from the table.
 void setModel(MutableTreeModel treeModel, javax.swing.tree.DefaultMutableTreeNode treeNode)
          Sets the model.
 void treeNodesChanged(javax.swing.event.TreeModelEvent e)
          Invoked after a node (or a set of siblings) has changed in some way.
 void treeNodesInserted(javax.swing.event.TreeModelEvent e)
          Invoked after nodes have been inserted into the tree.
 void treeNodesRemoved(javax.swing.event.TreeModelEvent e)
          Invoked after nodes have been removed from the tree.
 void treeStructureChanged(javax.swing.event.TreeModelEvent e)
          Invoked after the tree has drastically changed structure from a given node down.
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getColumnName, getListeners, 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, getColumnClass, getColumnName, removeTableModelListener, setValueAt
 

Field Detail

treeModel

protected MutableTreeModel treeModel

treeNode

protected javax.swing.tree.DefaultMutableTreeNode treeNode
Constructor Detail

TreeNodeTableModel

public TreeNodeTableModel()
Creates new TreeNodeTableModel.


TreeNodeTableModel

public TreeNodeTableModel(MutableTreeModel m,
                          javax.swing.tree.DefaultMutableTreeNode n)
Creates new TreeNodeTableModel.

Method Detail

setModel

public void setModel(MutableTreeModel treeModel,
                     javax.swing.tree.DefaultMutableTreeNode treeNode)
Sets the model.


getRow

public javax.swing.tree.DefaultMutableTreeNode getRow(int row)

getValueAt

public java.lang.Object getValueAt(int row,
                                   int column)
Returns the value for the cell at columnIndex and rowIndex.

Specified by:
getValueAt in interface javax.swing.table.TableModel
Parameters:
row - the row whose value is to be looked up
column - the column whose value is to be looked up
Returns:
the value Object at the specified cell

getRowCount

public int getRowCount()
Returns the number of rows in the model. A JTable uses this method to determine how many rows it should display. This method should be quick, as it is called frequently during rendering.

Specified by:
getRowCount in interface javax.swing.table.TableModel
Returns:
the number or rows in the model
See Also:
getColumnCount()

getColumnCount

public int getColumnCount()
Returns the number of columns in the model. A JTable uses this method to determine how many columns it should create and display by default.

Specified by:
getColumnCount in interface javax.swing.table.TableModel
Returns:
the number or columns in the model
See Also:
getRowCount()

getRowActions

public javax.swing.Action[] getRowActions(int[] rows)
Gets actions for the indicated row.

Specified by:
getRowActions in interface MutableTableModel
Parameters:
rows - The rows.

createRow

public void createRow(int row,
                      java.lang.Object type)
Invoke this to create a new row into the table.

Specified by:
createRow in interface MutableTableModel
Parameters:
row - index of the new row.
type - element type to be inserted.
Throws:
java.lang.IllegalStateException - if the row may not be inserted.

isCellEditable

public boolean isCellEditable(int row,
                              int column)
Returns true if the cell at rowIndex and columnIndex is editable. Otherwise, setValueAt() on the cell will not change the value of that cell.

Specified by:
isCellEditable in interface MutableTableModel
Specified by:
isCellEditable in interface javax.swing.table.TableModel
Overrides:
isCellEditable in class javax.swing.table.AbstractTableModel
Parameters:
row - the row whose value is to be looked up
column - the column whose value is to be looked up
Returns:
true if the cell is editable.
See Also:
AbstractTableModel.setValueAt(java.lang.Object, int, int)

removeRow

public void removeRow(int row)
Remove a row from the table.

Specified by:
removeRow in interface MutableTableModel
Parameters:
row - index of the row.
Throws:
java.lang.IllegalStateException - if the row may not be removed.
See Also:
MutableTableModel.isRowRemovable(int)

isRowRemovable

public boolean isRowRemovable(int row)
Returns wether the specified row may be removed.

Specified by:
isRowRemovable in interface MutableTableModel
Parameters:
row - index of the row.
See Also:
MutableTableModel.removeRow(int)

treeStructureChanged

public void treeStructureChanged(javax.swing.event.TreeModelEvent e)

Invoked after the tree has drastically changed structure from a given node down. If the path returned by e.getPath() is of length one and the first element does not identify the current root node the first element should become the new root of the tree.

e.path() holds the path to the node.

e.childIndices() returns null.

Specified by:
treeStructureChanged in interface javax.swing.event.TreeModelListener

treeNodesInserted

public void treeNodesInserted(javax.swing.event.TreeModelEvent e)

Invoked after nodes have been inserted into the tree.

e.path() returns the parent of the new nodes

e.childIndices() returns the indices of the new nodes in ascending order.

Specified by:
treeNodesInserted in interface javax.swing.event.TreeModelListener

treeNodesRemoved

public void treeNodesRemoved(javax.swing.event.TreeModelEvent e)

Invoked after nodes have been removed from the tree. Note that if a subtree is removed from the tree, this method may only be invoked once for the root of the removed subtree, not once for each individual set of siblings removed.

e.path() returns the former parent of the deleted nodes.

e.childIndices() returns the indices the nodes had before they were deleted in ascending order.

Specified by:
treeNodesRemoved in interface javax.swing.event.TreeModelListener

treeNodesChanged

public void treeNodesChanged(javax.swing.event.TreeModelEvent e)

Invoked after a node (or a set of siblings) has changed in some way. The node(s) have not changed locations in the tree or altered their children arrays, but other attributes have changed and may affect presentation. Example: the name of a file has changed, but it is in the same location in the file system.

To indicate the root has changed, childIndices and children will be null.

e.path() returns the path the parent of the changed node(s).

e.childIndices() returns the index(es) of the changed node(s).

Specified by:
treeNodesChanged in interface javax.swing.event.TreeModelListener

getCreatableRowTypes

public java.lang.Object[] getCreatableRowTypes(int row)
Returns the types of children that may be created at this row.

Specified by:
getCreatableRowTypes in interface MutableTableModel
Parameters:
row - a row.
Returns:
an array of objects that specify a child type that may be added at this row.

getCreatableRowType

public java.lang.Object getCreatableRowType(int row)
Description copied from interface: MutableTableModel
Returns the default type of elements that can be created at the specified index of the list.

Specified by:
getCreatableRowType in interface MutableTableModel
Parameters:
row - The insertion point. 0 <= index <= getRowCount()
Returns:
an Object that specifies the default element type that can be inserted at the insertion point. Returns null if no elements can be inserted here. The value must be one of the types returned by operation getCreatableTypes.

exportRowTransferable

public java.awt.datatransfer.Transferable exportRowTransferable(int[] rows)
Creates a Transferable to use as the source for a data transfer of the specified elements. Returns the representation of the rows to be transferred, or null if transfer is not possible.

Specified by:
exportRowTransferable in interface MutableTableModel
Parameters:
rows - Row indices.

isRowImportable

public boolean isRowImportable(java.awt.datatransfer.DataFlavor[] transferFlavors,
                               int action,
                               int row,
                               boolean asChild)
Indicates whether the model would accept an import of the given set of data flavors prior to actually attempting to import it.

Specified by:
isRowImportable in interface MutableTableModel
Parameters:
transferFlavors - the data formats available
action - The action DnDConstants.ACTION_COPY, .ACTION_MOVE or .ACTION_LINK.
row - The insertion point. 0 <= row <= getRowCount()
asChild - Indicates that the item is to be imported as a child of the list item rather than as an individual element / of the list.
Returns:
true if the data can be imported.
See Also:
DnDConstants

importRowTransferable

public int importRowTransferable(java.awt.datatransfer.Transferable t,
                                 int action,
                                 int row,
                                 boolean asChild)
                          throws java.awt.datatransfer.UnsupportedFlavorException,
                                 java.io.IOException
Causes a transfer to the model from a clipboard or a DND drop operation.

Specified by:
importRowTransferable in interface MutableTableModel
Parameters:
t - The transfer data.
action - The action DnDConstants.ACTION_COPY, .ACTION_MOVE or .ACTION_LINK.
row - The insertion point. 0 <= row <= getRowCount()
asChild - Indicates that the item is to be imported as a child of the list item rather than as an individual element of the list.
Returns:
The number of imported elements.
Throws:
java.awt.datatransfer.UnsupportedFlavorException
java.io.IOException

isRowAddable

public boolean isRowAddable(int row)
Description copied from interface: MutableTableModel
Returns true if a row can be added.

Specified by:
isRowAddable in interface MutableTableModel
Parameters:
row - index of the element. 0 <= index <= getSize()

(c) Werner Randelshofer.
All rights reserved.