com.ricebridge.csvman
Class CsvTableModel

java.lang.Object
  extended byjavax.swing.table.AbstractTableModel
      extended bycom.ricebridge.csvman.CsvTableModel
All Implemented Interfaces:
Serializable, TableModel

public class CsvTableModel
extends AbstractTableModel
implements TableModel

Implementation of a TableModel to hold CSV data.

This TableModel can be used directly with a JTable. Data is represented by String objects, and can be editable. Missing data is represented by an empty String in the table cell. You may subclass this class to provide more functionality.

The Source Code of this Java class is available under a BSD-style license.

See Also:
Serialized Form

Field Summary
protected  ArrayList iData
          Table data as a List of String[] arrays.
protected  boolean iEditable
          The table may be editable.
protected  boolean iHasHeaders
          The first line of the CSV file may contain headers.
protected  List iHeaders
          List of headers as Strings.
protected  boolean iHeadersStored
          Flag to indicate that headers have been loaded.
protected  int iMaxNumFields
          Maximum number of fields encountered.
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
CsvTableModel()
          Support Class.newInstance object instantiation.
CsvTableModel(List pData)
          Create a new TableModel with specified a List of String[] arrays.
 
Method Summary
 void addLine(String[] pLine, int pNumFields)
          Add a data line to the internal data containers for this TableModel.
 int getColumnCount()
          Get number of columns in table.
 String getColumnName(int pIndex)
          Get the name of the column.
 int getRowCount()
          Get number of rows in table.
 Object getValueAt(int pRow, int pColumn)
          Get the value of a table cell.
 void init()
          Initialise the internal data containers before loading new CSV data.
 boolean isCellEditable(int pRow, int pColumn)
          Indicate that table cell value can be changed.
 void setEditable(boolean pEditable)
          You can use this setting to make your JTable editable.
 void setHasHeaders(boolean pHasHeaders)
          The data provided via addLine(java.lang.String[], int) includes column headers.
 void setValueAt(Object pValue, int pRow, int pColumn)
          Set the value of a table cell.
 String toString()
          String description of object instance.
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getListeners, getTableModelListeners, removeTableModelListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface javax.swing.table.TableModel
addTableModelListener, getColumnClass, removeTableModelListener
 

Field Detail

iEditable

protected boolean iEditable
The table may be editable.


iHasHeaders

protected boolean iHasHeaders
The first line of the CSV file may contain headers.


iHeadersStored

protected boolean iHeadersStored
Flag to indicate that headers have been loaded.


iHeaders

protected List iHeaders
List of headers as Strings.


iData

protected ArrayList iData
Table data as a List of String[] arrays.


iMaxNumFields

protected int iMaxNumFields
Maximum number of fields encountered.

Constructor Detail

CsvTableModel

public CsvTableModel()
Support Class.newInstance object instantiation.


CsvTableModel

public CsvTableModel(List pData)
Create a new TableModel with specified a List of String[] arrays.

Method Detail

init

public void init()
Initialise the internal data containers before loading new CSV data. If CSV data is reloading using the same CsvManager object and hence the same TableModelLineListener, the same CsvTableModel instance will be used, so that this method must be called prior to loading new data. TableModelLineListener calls this method in TableModelLineListener.startProcessImpl(). If you subclass this class to provide your own TableModel, you should provide your own implementation of this method.


addLine

public void addLine(String[] pLine,
                    int pNumFields)
Add a data line to the internal data containers for this TableModel.

The parameter pNumFields indicates the number of actual data fields found on the current line, which may be less that the number expected, thus pNumFields <= pLine.length. The remaining members of pLine will be empty Strings.

Parameters:
pLine - data fields
pNumFields - number of actual data fields

setHasHeaders

public void setHasHeaders(boolean pHasHeaders)
The data provided via addLine(java.lang.String[], int) includes column headers.

When true, the first call to addLine is assumed to be the list of column headers.

When false, the table has no headers.

Parameters:
pHasHeaders - data has headers
See Also:
TableModelLineListener.startProcessImpl(), HeadersListenerSupport.setHasHeaders(boolean), CsvManager.loadTableModel(File,boolean)

setEditable

public void setEditable(boolean pEditable)
You can use this setting to make your JTable editable.

You will need to access this object first, like so:

     TableModelLineListener tmln 
       = csvManager.getCsvManagerStore().getTableModelLineListener();
     tmln.setEditable( true );
  

Parameters:
pEditable - editable table

getColumnName

public String getColumnName(int pIndex)
Get the name of the column.

Specified by:
getColumnName in interface TableModel
Parameters:
pIndex - index of column (from zero)

getRowCount

public int getRowCount()
Get number of rows in table.

Specified by:
getRowCount in interface TableModel

getColumnCount

public int getColumnCount()
Get number of columns in table.

Specified by:
getColumnCount in interface TableModel

getValueAt

public Object getValueAt(int pRow,
                         int pColumn)
Get the value of a table cell.

Specified by:
getValueAt in interface TableModel
Parameters:
pRow - index of row (from zero)
pColumn - index of column (from zero)

isCellEditable

public boolean isCellEditable(int pRow,
                              int pColumn)
Indicate that table cell value can be changed.

Specified by:
isCellEditable in interface TableModel
Parameters:
pRow - index of row (from zero)
pColumn - index of column (from zero)

setValueAt

public void setValueAt(Object pValue,
                       int pRow,
                       int pColumn)
Set the value of a table cell.

Specified by:
setValueAt in interface TableModel
Parameters:
pValue - new value of cell
pRow - index of row (from zero)
pColumn - index of column (from zero)

toString

public String toString()
String description of object instance.



Copyright © 2003-2006 Ricebridge