CubeTwister 2.0alpha142 2012-02-11

ch.randelshofer.gui.table
Class DefaultMutableTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by ch.randelshofer.gui.table.DefaultMutableTableModel
All Implemented Interfaces:
MutableTableModel, java.io.Serializable, javax.swing.table.TableModel

public class DefaultMutableTableModel
extends javax.swing.table.AbstractTableModel
implements MutableTableModel

Default implementation of a MutableTableModel. FIXME: Should override more methods of the superclass.

Version:
2.3 2002-04-08 Support for transferables added.
2.2 2001-09-22 Method getRowActions added.
2.1 2001-07-27 Duplication of rows added.
2.0 2001-07-18
Author:
Werner Randelshofer
See Also:
Serialized Form

Field Summary
protected  java.util.ArrayList columnClasses
          The ArrayList of column classes.
protected  java.util.ArrayList columnIdentifiers
          The ArrayList of column identifiers.
protected  java.util.ArrayList dataList
          The ArrayList of ArrayList of Object values.
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
DefaultMutableTableModel()
          Constructs a DefaultMutableTableModel which has a table of zero columns and zero rows.
DefaultMutableTableModel(int rowCount, int columnCount)
          Constructs a DefaultMutableTableModel with numRows and numColumns of null object values.
DefaultMutableTableModel(java.util.List columnNames, int rowCount)
          Constructs a DefaultMutableTableModel with as many columns as there are elements in columNames and numRows of null object values.
DefaultMutableTableModel(java.util.List dataList, java.util.List columnNames)
          Constructs a DefaultMutableTableModel and initializes the table by passing dataList and columnNames to the setDataVector() method.
DefaultMutableTableModel(java.lang.Object[][] dataList, java.lang.Object[] columnNames)
          Constructs a DefaultMutableTableModel and initializes the table by passing dataList and columnNames to the setDataList() method.
DefaultMutableTableModel(java.lang.Object[][] dataList, java.lang.Object[] columnNames, java.lang.Class[] columnClasses)
          Constructs a DefaultMutableTableModel and initializes the table by passing dataList and columnNames to the setDataList() method.
DefaultMutableTableModel(java.lang.Object[] columnNames, int rowCount)
          Constructs a DefaultMutableTableModel wit as many columns as there are elements in columnNames and numRows of null object values.
 
Method Summary
 void addRow(int row, java.lang.Object element)
          Inserts an element at the specified index in the model.
 void createRow(int row, java.lang.Object type)
          Invoke this to insert a new row into the table.
 java.awt.datatransfer.Transferable exportRowTransferable(int[] rows)
          Creates a Transferable to use as the source for a dataList transfer of the specified elements.
 java.lang.Class getColumnClass(int column)
           
 int getColumnCount()
          Returns the number of columns in this data table.
 java.lang.String getColumnName(int column)
          Returns the column name.
 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 elements that can be created at the specified index of the list.
 javax.swing.Action[] getRowActions(int[] rows)
          Returns an array of compound actions for the indicated rows.
 int getRowCount()
           
 java.lang.Object getValueAt(int row, int column)
          Returns an attribute value for the cell at row and column.
 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 rowIndex, int columnIndex)
          Returns true if the cell at rowIndex and columnIndex is editable.
 boolean isEnabled()
          Gets the enabled state of the model.
 boolean isRowAddable(int row)
          Returns wether a row may be inserted.
 boolean isRowDuplicateable(int row)
          Returns wether the specified row may be duplicated.
 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 dataList flavors prior to actually attempting to import it.
 boolean isRowRemovable(int row)
          Returns wether the specified node may be removed.
 void removeRow(int row)
          Message this to remove a row from the table.
 void setDataVector(java.util.List dataVector, java.util.List columnIdentifiers)
          Replaces the current dataVector instance variable with the new Vector of rows, dataVector.
 void setDataVector(java.lang.Object[][] dataVector, java.lang.Object[] columnIdentifiers)
          Replaces the value in the dataVector instance variable with the values in the array dataVector.
 void setEnabled(boolean b)
          Sets the enabled state of the model.
 void setValueAt(java.lang.Object aValue, int row, int column)
          Sets the object value for the cell at column and row.
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener
 
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, removeTableModelListener
 

Field Detail

dataList

protected java.util.ArrayList dataList
The ArrayList of ArrayList of Object values.


columnIdentifiers

protected java.util.ArrayList columnIdentifiers
The ArrayList of column identifiers.


columnClasses

protected java.util.ArrayList columnClasses
The ArrayList of column classes. When this array list is null, then all columns are reported as of type Object.class.

Constructor Detail

DefaultMutableTableModel

public DefaultMutableTableModel()
Constructs a DefaultMutableTableModel which has a table of zero columns and zero rows.


DefaultMutableTableModel

public DefaultMutableTableModel(int rowCount,
                                int columnCount)
Constructs a DefaultMutableTableModel with numRows and numColumns of null object values.


DefaultMutableTableModel

public DefaultMutableTableModel(java.lang.Object[][] dataList,
                                java.lang.Object[] columnNames)
Constructs a DefaultMutableTableModel and initializes the table by passing dataList and columnNames to the setDataList() method.


DefaultMutableTableModel

public DefaultMutableTableModel(java.lang.Object[][] dataList,
                                java.lang.Object[] columnNames,
                                java.lang.Class[] columnClasses)
Constructs a DefaultMutableTableModel and initializes the table by passing dataList and columnNames to the setDataList() method.


DefaultMutableTableModel

public DefaultMutableTableModel(java.lang.Object[] columnNames,
                                int rowCount)
Constructs a DefaultMutableTableModel wit as many columns as there are elements in columnNames and numRows of null object values.


DefaultMutableTableModel

public DefaultMutableTableModel(java.util.List columnNames,
                                int rowCount)
Constructs a DefaultMutableTableModel with as many columns as there are elements in columNames and numRows of null object values.


DefaultMutableTableModel

public DefaultMutableTableModel(java.util.List dataList,
                                java.util.List columnNames)
Constructs a DefaultMutableTableModel and initializes the table by passing dataList and columnNames to the setDataVector() method.

Method Detail

setDataVector

public void setDataVector(java.util.List dataVector,
                          java.util.List columnIdentifiers)
Replaces the current dataVector instance variable with the new Vector of rows, dataVector. columnIdentifiers are the names of the new columns. The first name in columnIdentifiers is mapped to column 0 in dataVector. Each row in dataVector is adjusted to match the number of columns in columnIdentifiers either by truncating the Vector if it is too long, or adding null values if it is too short.

Note that passing in a null value for dataVector results in unspecified behavior, an possibly an exception.

Parameters:
dataVector - the new dataList vector
columnIdentifiers - the names of the columns

setDataVector

public void setDataVector(java.lang.Object[][] dataVector,
                          java.lang.Object[] columnIdentifiers)
Replaces the value in the dataVector instance variable with the values in the array dataVector. The first index in the Object[][] array is the row index and the second is the column index. columnIdentifiers are the names of the new columns.

Parameters:
dataVector - the new dataList vector
columnIdentifiers - the names of the columns

createRow

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

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

getCreatableRowTypes

public java.lang.Object[] getCreatableRowTypes(int row)
Description copied from interface: MutableTableModel
Returns the types of elements that can be created at the specified index of the list.

Specified by:
getCreatableRowTypes in interface MutableTableModel
Parameters:
row - The insertion point. 0 <= index <= getRowCount()
Returns:
an array of Object's that specify element types that can be inserted at the insertion point. Returns an empty array if no elements can be inserted here. Never returns null. This array must include the type returned by operation getCreatableType.

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.

isEnabled

public boolean isEnabled()
Gets the enabled state of the model.


isRowAddable

public boolean isRowAddable(int row)
Returns wether a row may be inserted.

Specified by:
isRowAddable in interface MutableTableModel
Parameters:
row - row of the new row.
Returns:
Returns true for all rows. Returns false if the model is disabled.

isRowRemovable

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

Specified by:
isRowRemovable in interface MutableTableModel
Parameters:
row - row of the row.
Returns:
Returns true for all rows. Returns false if the model is disabled.
See Also:
MutableTableModel.removeRow(int)

removeRow

public void removeRow(int row)
Message this to remove a row from the table.

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

isRowDuplicateable

public boolean isRowDuplicateable(int row)
Returns wether the specified row may be duplicated.

Parameters:
row - row of the row.
Returns:
Returns true for all rows. Returns false if the model is disabled.

setEnabled

public void setEnabled(boolean b)
Sets the enabled state of the model. By default the model is not enabled.


getRowActions

public javax.swing.Action[] getRowActions(int[] rows)
Returns an array of compound actions for the indicated rows.

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

exportRowTransferable

public java.awt.datatransfer.Transferable exportRowTransferable(int[] rows)
Creates a Transferable to use as the source for a dataList 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 rows.

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 dataList flavors prior to actually attempting to import it.

Specified by:
isRowImportable in interface MutableTableModel
Parameters:
transferFlavors - the dataList formats available
action - The action DnDConstants.ACTION_COPY, .ACTION_MOVE or .ACTION_LINK.
row - The insertion point. 0 <= index <= getSize()
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 dataList 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 dataList.
action - The action DnDConstants.ACTION_COPY, .ACTION_MOVE or .ACTION_LINK.
row - The insertion point. 0 <= index <= getSize()
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

getColumnCount

public int getColumnCount()
Returns the number of columns in this data table.

Specified by:
getColumnCount in interface javax.swing.table.TableModel
Returns:
the number of columns in the model

getRowCount

public int getRowCount()
Specified by:
getRowCount in interface javax.swing.table.TableModel

getValueAt

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

Specified by:
getValueAt in interface javax.swing.table.TableModel
Parameters:
row - the row whose value is to be queried
column - the column whose value is to be queried
Returns:
the value Object at the specified cell
Throws:
java.lang.ArrayIndexOutOfBoundsException - if an invalid row or column was given

setValueAt

public void setValueAt(java.lang.Object aValue,
                       int row,
                       int column)
Sets the object value for the cell at column and row. aValue is the new value. This method will generate a tableChanged notification.

Specified by:
setValueAt in interface javax.swing.table.TableModel
Overrides:
setValueAt in class javax.swing.table.AbstractTableModel
Parameters:
aValue - the new value; this can be null
row - the row whose value is to be changed
column - the column whose value is to be changed
Throws:
java.lang.ArrayIndexOutOfBoundsException - if an invalid row or column was given

addRow

public void addRow(int row,
                   java.lang.Object element)
Inserts an element at the specified index in the model.

Parameters:
row - index at which the specified element is to be inserted.
element - element to be inserted.
Throws:
java.lang.ClassCastException - if the element is not compatible with one of the data flavors type returned by getInsertableDataFlavors(int)
java.lang.IllegalStateException - if isRowInsertable(int) returns false.
java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()).

getColumnClass

public java.lang.Class getColumnClass(int column)
Specified by:
getColumnClass in interface javax.swing.table.TableModel
Overrides:
getColumnClass in class javax.swing.table.AbstractTableModel

getColumnName

public java.lang.String getColumnName(int column)
Returns the column name.

Specified by:
getColumnName in interface javax.swing.table.TableModel
Overrides:
getColumnName in class javax.swing.table.AbstractTableModel
Returns:
a name for this column using the string value of the appropriate member in columnIdentifiers. If columnIdentifiers does not have an entry for this index, returns the default name provided by the superclass

isCellEditable

public boolean isCellEditable(int rowIndex,
                              int columnIndex)
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:
rowIndex - - the row whose value to be queried
columnIndex - - the column whose value to be queried
Returns:
true if the model is enabled

(c) Werner Randelshofer.
All rights reserved.