JIDE 3.5.15

com.jidesoft.grid
Class CachedTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by com.jidesoft.grid.TableModelWrapperImpl
          extended by com.jidesoft.grid.CachedTableModel
All Implemented Interfaces:
ColumnIdentifierTableModel, ContextSensitiveTableModel, EditorStyleTableModel, IndexChangeEventGenerator, MultiTableModel, NavigableModel, RowTableModelWrapper, SpanModel, StyleModel, TableModelWrapper, Serializable, EventListener, TableModelListener, TableModel

public class CachedTableModel
extends TableModelWrapperImpl
implements Serializable, RowTableModelWrapper

CachedTableModel is a table model that can cache the table data. If you use DefaultTableModel, there is no need to cache the data because the data are actually in the data list. However if you are using AbstractTableModel and getValueAt() gets the data on fly, you will suffer from performance issue if getValueAt() is complex. CachedTableModel can solve this issue by caching the value. So each getValueAt() will be called only once. When data changes, as long as the underlying table model fires the correct table model event, the cache will update automatically to fetch the new value. If you want to invalidate the cache yourself, you can always use methods such as invalidateCache(), invalidateCache(int) or invalidateCache(int,int) to invalidate all the cache or portions of it.

At any time, you can call setCacheEnabled(boolean) to disable the caching. Or you can call setCachedColumns(int[]) to specify which columns you want to cache. For example, if the cell value in a column is a direct value without calculation, you can disable the cache on that column to save memory.

See Also:
Serialized Form

Field Summary
protected  int[] _cachedColumns
           
protected  List<Object> _cachedValueList
          The List of Lists of Object values.
protected  int[] _conversionIndex
           
 
Fields inherited from class com.jidesoft.grid.TableModelWrapperImpl
_model
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Fields inherited from interface com.jidesoft.grid.MultiTableModel
FOOTER_COLUMN, HEADER_COLUMN, REGULAR_COLUMN
 
Fields inherited from interface com.jidesoft.grid.EditorStyleTableModel
EDITOR_STYLE_EDITABLE, EDITOR_STYLE_NORMAL, EDITOR_STYLE_READ_ONLY, EDITOR_STYLE_SELECT_ONLY
 
Constructor Summary
CachedTableModel(TableModel model)
          Constructs a default DefaultTableModel which is a table of zero columns and zero rows.
 
Method Summary
 void cacheIt()
          Cached all the values.
 int getActualRowAt(int visualRow)
          Gets the actual row index in the underlying table that corresponds the specified row index.
 int[] getCachedColumns()
           
protected  List<Object> getCachedRows()
          Gets the cached rows.
 Class<?> getCellClassAt(int row, int column)
          Gets the cell class of the underlying table model.
 ConverterContext getConverterContextAt(int row, int column)
          Gets the converter context of the underlying table model.
 EditorContext getEditorContextAt(int row, int column)
          Gets the editor context of the underlying table model.
 Object getValueAt(int row, int column)
          Returns an attribute value for the cell at row and column.
 int getVisualRowAt(int actualRow)
          Gets the visual row index representing the specified actual row.
protected  void insertCache(int rowIndex)
          Inserts a new row data at the specified rowIndex.
 void invalidateCache()
          Invalidate the whole cache.
 void invalidateCache(int row)
          Invalidate the cache at the specified row index.
 void invalidateCache(int row, int column)
          Invalidate the cache at the specified row and column.
protected  boolean isCachedValueValid(int row, int column)
          Checks if the cached value at the specified row and column is valid.
 boolean isCacheEnabled()
          Checks if cache is enabled.
 boolean isCellEditable(int row, int column)
           
 boolean isColumnCached(int column)
          Checks if the column is cached.
protected  void removeCache(int rowIndex)
          Removes the cached row data at the specified rowIndex.
 void setCachedColumns(int[] cachedColumns)
          Sets the columns you want to cache.
 void setCacheEnabled(boolean cacheEnabled)
          Enables or disables the cache.
 void setValueAt(Object value, int row, int column)
           
protected  void tableCellsUpdated(int column, int firstRow, int lastRow)
          Overrides the method in DefaultTableModelWrapper to update the cache when underlying table model is changed.
protected  void tableDataChanged()
          Overrides the method in DefaultTableModelWrapper to update the cache when underlying table model is changed.
protected  void tableRowsDeleted(int firstRow, int lastRow)
          Overrides the method in DefaultTableModelWrapper to update the cache when underlying table model is changed.
protected  void tableRowsInserted(int firstRow, int lastRow)
          Overrides the method in DefaultTableModelWrapper to update the cache when underlying table model is changed.
protected  void tableRowsUpdated(int firstRow, int lastRow)
          Overrides the method in DefaultTableModelWrapper to update the cache when underlying table model is changed.
protected  void tableStructureChanged()
          Overrides the method in DefaultTableModelWrapper to update the cache when underlying table model is changed.
protected  void updateCachedValue(Object value, int row, int column)
          Updates the cache at the specified row and column with a new value.
 
Methods inherited from class com.jidesoft.grid.TableModelWrapperImpl
addIndexChangeListener, createCompoundTableModelEvent, fireIndexChanged, fireIndexChanging, fireTableCellsUpdated, fireTableChanged, getActualModel, getCellSpanAt, getCellStyleAt, getColumnClass, getColumnCount, getColumnIdentifier, getColumnName, getColumnType, getEditorStyleAt, getIndexChangeListeners, getRowCount, getTableIndex, isCellSpanOn, isCellStyleOn, isNavigableAt, isNavigationOn, removeIndexChangeListener, tableChanged, tableDataChanged
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, 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 com.jidesoft.grid.TableModelWrapper
getActualModel
 
Methods inherited from interface javax.swing.table.TableModel
addTableModelListener, removeTableModelListener
 

Field Detail

_cachedValueList

protected transient List<Object> _cachedValueList
The List of Lists of Object values.


_cachedColumns

protected int[] _cachedColumns

_conversionIndex

protected int[] _conversionIndex
Constructor Detail

CachedTableModel

public CachedTableModel(TableModel model)
Constructs a default DefaultTableModel which is a table of zero columns and zero rows.

Parameters:
model - the table model.
Method Detail

isCacheEnabled

public boolean isCacheEnabled()
Checks if cache is enabled. Usually you want the cache to be enabled since you are using CacheTableModel. However you still can disable it temporarily if you want the data to be retrieved directly from the underlying table model.

Returns:
true if cache is enabled. Otherwise false.

setCacheEnabled

public void setCacheEnabled(boolean cacheEnabled)
Enables or disables the cache. If cache is enabled, it will create cache to cache all values retrieved from getValueAt method. Next time when the getValueAt is called on the same cell, the value from the cache will be returned instead asking the table model again.

By default, the cache is enabled. If this method is called, the whole cache will be invalidated.

Parameters:
cacheEnabled - true or false.

tableRowsInserted

protected void tableRowsInserted(int firstRow,
                                 int lastRow)
Overrides the method in DefaultTableModelWrapper to update the cache when underlying table model is changed.

Overrides:
tableRowsInserted in class TableModelWrapperImpl
Parameters:
firstRow - the index of the first row that was inserted
lastRow - the index of the last row that was inserted

tableRowsDeleted

protected void tableRowsDeleted(int firstRow,
                                int lastRow)
Overrides the method in DefaultTableModelWrapper to update the cache when underlying table model is changed.

Overrides:
tableRowsDeleted in class TableModelWrapperImpl
Parameters:
firstRow - the index of the first row that was deleted
lastRow - the index of the last row that was deleted

tableRowsUpdated

protected void tableRowsUpdated(int firstRow,
                                int lastRow)
Overrides the method in DefaultTableModelWrapper to update the cache when underlying table model is changed.

Overrides:
tableRowsUpdated in class TableModelWrapperImpl
Parameters:
firstRow - the index of the first row that was updated
lastRow - the index of the last row that was updated

tableCellsUpdated

protected void tableCellsUpdated(int column,
                                 int firstRow,
                                 int lastRow)
Overrides the method in DefaultTableModelWrapper to update the cache when underlying table model is changed.

Overrides:
tableCellsUpdated in class TableModelWrapperImpl
Parameters:
column - the index of the column that was updated
firstRow - the index of the first row in the above column that was updated
lastRow - the index of the last row in the above column that was updated

tableDataChanged

protected void tableDataChanged()
Overrides the method in DefaultTableModelWrapper to update the cache when underlying table model is changed.

Overrides:
tableDataChanged in class TableModelWrapperImpl

tableStructureChanged

protected void tableStructureChanged()
Overrides the method in DefaultTableModelWrapper to update the cache when underlying table model is changed.

Overrides:
tableStructureChanged in class TableModelWrapperImpl

isCachedValueValid

protected boolean isCachedValueValid(int row,
                                     int column)
Checks if the cached value at the specified row and column is valid.

Parameters:
row - the row index.
column - the column index.
Returns:
true if it is valid. Otherwise false.

invalidateCache

public void invalidateCache()
Invalidate the whole cache.


invalidateCache

public void invalidateCache(int row)
Invalidate the cache at the specified row index.

Parameters:
row - the row index.

invalidateCache

public void invalidateCache(int row,
                            int column)
Invalidate the cache at the specified row and column.

Parameters:
row - the row index.
column - the column index.

removeCache

protected void removeCache(int rowIndex)
Removes the cached row data at the specified rowIndex.

Parameters:
rowIndex - the row index

insertCache

protected void insertCache(int rowIndex)
Inserts a new row data at the specified rowIndex.

Parameters:
rowIndex - the row index

updateCachedValue

protected void updateCachedValue(Object value,
                                 int row,
                                 int column)
Updates the cache at the specified row and column with a new value.

Parameters:
value - the new value
row - the row index
column - the column index

getValueAt

public Object getValueAt(int row,
                         int column)
Returns an attribute value for the cell at row and column. If the cached is enabled, it will get the data from the cache first. If the data is not in cache or it is not valid, it will then get the data from the actual table model and update the cache.

Specified by:
getValueAt in interface TableModel
Overrides:
getValueAt in class TableModelWrapperImpl
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:
ArrayIndexOutOfBoundsException - if an invalid row or column was given

isCellEditable

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

getCellClassAt

public Class<?> getCellClassAt(int row,
                               int column)
Description copied from class: TableModelWrapperImpl
Gets the cell class of the underlying table model. This will return a valid value only if underlying table model is instance of ContextSensitiveTableModel. If not, it will return underlying model's getColumnClass(column).

Specified by:
getCellClassAt in interface ContextSensitiveTableModel
Overrides:
getCellClassAt in class TableModelWrapperImpl
Parameters:
row - the row index.
column - the column index.
Returns:
cell class of the underlying table model.

getConverterContextAt

public ConverterContext getConverterContextAt(int row,
                                              int column)
Description copied from class: TableModelWrapperImpl
Gets the converter context of the underlying table model. This will return a valid value only if underlying table model is instance of ContextSensitiveTableModel. If not, it will return null.

Specified by:
getConverterContextAt in interface ContextSensitiveTableModel
Overrides:
getConverterContextAt in class TableModelWrapperImpl
Parameters:
row - the row index.
column - the column index.
Returns:
converter context of the underlying table model.

getEditorContextAt

public EditorContext getEditorContextAt(int row,
                                        int column)
Description copied from class: TableModelWrapperImpl
Gets the editor context of the underlying table model. This will return a valid value only if underlying table model is instance of ContextSensitiveTableModel. If not, it will return null.

Specified by:
getEditorContextAt in interface ContextSensitiveTableModel
Overrides:
getEditorContextAt in class TableModelWrapperImpl
Parameters:
row - the row index.
column - the column index.
Returns:
editor context of the underlying table model.

getCachedRows

protected List<Object> getCachedRows()
Gets the cached rows.

Returns:
the cached rows.

cacheIt

public void cacheIt()
Cached all the values.


getCachedColumns

public int[] getCachedColumns()

setCachedColumns

public void setCachedColumns(int[] cachedColumns)
Sets the columns you want to cache.

Parameters:
cachedColumns - the column index array. Null means all columns will be cached.

isColumnCached

public boolean isColumnCached(int column)
Checks if the column is cached.

Parameters:
column - the column index
Returns:
true if the column is cached.

getActualRowAt

public int getActualRowAt(int visualRow)
Description copied from interface: RowTableModelWrapper
Gets the actual row index in the underlying table that corresponds the specified row index.

Specified by:
getActualRowAt in interface RowTableModelWrapper
Parameters:
visualRow - the visual row index.
Returns:
the actual row. If the actual row doesn't exist, return -1.

getVisualRowAt

public int getVisualRowAt(int actualRow)
Description copied from interface: RowTableModelWrapper
Gets the visual row index representing the specified actual row.

Specified by:
getVisualRowAt in interface RowTableModelWrapper
Parameters:
actualRow - the actual row index.
Returns:
the visual row index. -1 if there is no visual row representing it.

setValueAt

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

JIDE 3.5.15