dsto.dfc.swing.table
Class MapTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by dsto.dfc.swing.table.AbstractDfcTableModel
          extended by dsto.dfc.swing.table.MapTableModel
All Implemented Interfaces:
DfcTableModel, Serializable, TableModel

public class MapTableModel
extends AbstractDfcTableModel

Present a java.util.Map as a Swing TableModel with two columns ("Property" and "Value"). Also supports editing map: see setEditMode(int).

Version:
$Revision$
Author:
Matthew Phillips
See Also:
MapTableView, Serialized Form

Nested Class Summary
protected  class MapTableModel.MapEntryComparator
          Compare Map.Entry's by using keyComparator on the key.
 
Field Summary
static int EDIT_COPY
           
static int EDIT_DIRECT
           
static int EDIT_NONE
           
protected  int editMode
          The edit setting: EDIT_NONE, EDIT_DIRECT or EDIT_COPY.
protected  Comparator keyComparator
          The comparator used to sort orderedEntries by the keys of its Map.Entry's.
protected  Map map
          The map being presented by this model.
protected  ArrayList orderedEntries
          A list of Map.Entry's in the row order displayed by the table.
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
MapTableModel()
           
MapTableModel(Map map)
          Create a table model from map.
MapTableModel(Map map, Comparator keyComparator)
           
MapTableModel(Map map, Comparator keyComparator, int editMode)
          Create a table model from map.
MapTableModel(Map map, int editMode)
           
 
Method Summary
 boolean canDeleteRows(int rowStart, int rowEnd)
          Test if a deleteRows () operation is supported.
protected  void checkEditable()
          Throws UnsupportedOperationException if the table is not editable.
protected  Map copyMap(Map srcMap)
          Make a copy of the current map.
 void deleteRows(int rowStart, int rowEnd)
          Delete rows.
 int findKeyIndex(Object key)
          Find the index (row) that a key has in the table.
 Class getColumnClass(int columnIndex)
           
 int getColumnCount()
           
 String getColumnName(int column)
           
 int getEditMode()
           
 Map getMap()
           
 int getRowCount()
           
 Object getValueAt(int row, int col)
           
 boolean isCellEditable(int row, int column)
           
protected  void loadOrderedEntries()
          (Re)load orderedEntries from the map.
protected  void maybeCopyMap()
          Copy map if edit mode is EDIT_COPY by calling copyMap ().
 void putEntry(Object key, Object value)
          Put a new entry into the map, firing the appropriate events.
 void setEditMode(int mode)
          Set the edit mode for the table.
 void setMap(Map newMap)
           
 void setValueAt(Object value, int row, int column)
           
 
Methods inherited from class dsto.dfc.swing.table.AbstractDfcTableModel
canCopyCells, canCopyRows, canDeleteCells, canDeleteColumns, canInsertColumn, canInsertRows, canMoveRows, canPasteCells, canPasteRows, copyCells, copyRows, deleteCells, deleteColumns, insertColumn, insertRows, moveRows, pasteCells, pasteRows
 
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

EDIT_NONE

public static final int EDIT_NONE
See Also:
Constant Field Values

EDIT_DIRECT

public static final int EDIT_DIRECT
See Also:
Constant Field Values

EDIT_COPY

public static final int EDIT_COPY
See Also:
Constant Field Values

map

protected Map map
The map being presented by this model.


orderedEntries

protected ArrayList orderedEntries
A list of Map.Entry's in the row order displayed by the table.


keyComparator

protected Comparator keyComparator
The comparator used to sort orderedEntries by the keys of its Map.Entry's.


editMode

protected int editMode
The edit setting: EDIT_NONE, EDIT_DIRECT or EDIT_COPY.

Constructor Detail

MapTableModel

public MapTableModel()

MapTableModel

public MapTableModel(Map map)
Create a table model from map. Model is sorted by string value of keys.


MapTableModel

public MapTableModel(Map map,
                     Comparator keyComparator)

MapTableModel

public MapTableModel(Map map,
                     int editMode)

MapTableModel

public MapTableModel(Map map,
                     Comparator keyComparator,
                     int editMode)
Create a table model from map. Model is sorted using keyComparator on keys.

Method Detail

loadOrderedEntries

protected void loadOrderedEntries()
(Re)load orderedEntries from the map.


setMap

public void setMap(Map newMap)

getMap

public Map getMap()

setEditMode

public void setEditMode(int mode)
Set the edit mode for the table.

Parameters:
mode - The new mode: EDIT_NONE (no editing allowed), EDIT_DIRECT (edit the map directly), or EDIT_COPY (edit the map by copying it first using copyMap ()).

getEditMode

public int getEditMode()

putEntry

public void putEntry(Object key,
                     Object value)
              throws UnsupportedOperationException
Put a new entry into the map, firing the appropriate events.

Throws:
UnsupportedOperationException - if the model is not editable.

findKeyIndex

public int findKeyIndex(Object key)
Find the index (row) that a key has in the table.


getColumnCount

public int getColumnCount()

getColumnName

public String getColumnName(int column)
Specified by:
getColumnName in interface TableModel
Overrides:
getColumnName in class AbstractTableModel

getColumnClass

public Class getColumnClass(int columnIndex)
Specified by:
getColumnClass in interface TableModel
Overrides:
getColumnClass in class AbstractTableModel

getValueAt

public Object getValueAt(int row,
                         int col)

getRowCount

public int getRowCount()

isCellEditable

public boolean isCellEditable(int row,
                              int column)
Specified by:
isCellEditable in interface TableModel
Overrides:
isCellEditable in class AbstractTableModel

setValueAt

public void setValueAt(Object value,
                       int row,
                       int column)
Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class AbstractTableModel

canDeleteRows

public boolean canDeleteRows(int rowStart,
                             int rowEnd)
Description copied from interface: DfcTableModel
Test if a deleteRows () operation is supported.

Specified by:
canDeleteRows in interface DfcTableModel
Overrides:
canDeleteRows in class AbstractDfcTableModel
Parameters:
rowStart - The row to begin inserting.
rowEnd - The last row to delete.
Returns:
True if the delete is possible.

deleteRows

public void deleteRows(int rowStart,
                       int rowEnd)
                throws UnsupportedOperationException
Description copied from interface: DfcTableModel
Delete rows.

Specified by:
deleteRows in interface DfcTableModel
Overrides:
deleteRows in class AbstractDfcTableModel
Parameters:
rowStart - The row to begin inserting.
rowEnd - The last row to delete.
Throws:
UnsupportedOperationException - if this is not supported.
See Also:
DfcTableModel.canDeleteRows(int, int)

checkEditable

protected void checkEditable()
                      throws UnsupportedOperationException
Throws UnsupportedOperationException if the table is not editable.

Throws:
UnsupportedOperationException

maybeCopyMap

protected void maybeCopyMap()
Copy map if edit mode is EDIT_COPY by calling copyMap ().


copyMap

protected Map copyMap(Map srcMap)
Make a copy of the current map. The default is to play safe and create a new HashMap copy. Subclasses may override this if different behaviour is needed.



Copyright © 2008 Commonwealth of Australia