com.ricebridge.csvman
Class TableModelLineListener

java.lang.Object
  extended bycom.ricebridge.csvman.LineListenerSupportImpl
      extended bycom.ricebridge.csvman.CustomLineListener
          extended bycom.ricebridge.csvman.HeadersListenerSupport
              extended bycom.ricebridge.csvman.TableModelLineListener
All Implemented Interfaces:
LineListener

public class TableModelLineListener
extends HeadersListenerSupport

An implementation of LineListener that inserts loaded data lines into a TableModel.

You can then use this TableModel directly with a JTable to create a tabular display of your data.. The easiest way to get your CSV data as a TableModel is to use the CsvManager.loadTableModel method.

You can find more information about the TableModel implementation here: CsvTableModel.

TableModelLineListener is designed to be subclassed. You can change the default implementation by calling the CsvManagerStore.setTableModelLineListenerClass method, and providing a subclass of TableModelLineListener. CsvManagerStore can be accessed using CsvManager.getCsvManagerStore().

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

See Also:
LineListener, CustomLineListener, BasicLineListener, AsListsLineListener, ResultSetLineListener, BeanLineListener

Field Summary
static boolean DEFAULT_TableModel_dataHasHeaders
          Default value of TableModel.dataHasHeaders).
static boolean DEFAULT_TableModel_editable
          Default value of TableModel.editable).
protected  CsvTableModel iCsvTableModel
          TableModel instance to hold CSV data
protected  Class iCsvTableModelClass
          TableModel implementation class used to create new instances.
protected  boolean iDataHasHeaders
          When true, the first line of CSV data should be a header line specifying the data field names.
protected  boolean iEditable
          Make the TableModel editable.
protected  int iNumFields
          Number of data fields.
protected  boolean iPropEditable
          When true, the TableModel returned will be editable.
static String PROP_TableModel_dataHasHeaders
          Specify that the first row of data is a header line containing the names of the data fields (name for CsvSpec.setProperty: TableModel.dataHasHeaders).
static String PROP_TableModel_editable
          Specify that table cells can be edited (name for CsvSpec.setProperty: TableModel.editable).
 
Fields inherited from class com.ricebridge.csvman.HeadersListenerSupport
iHasHeaders
 
Constructor Summary
TableModelLineListener()
           
 
Method Summary
 CsvTableModel getCsvTableModel()
          Get the new CsvTableModel instance containing the CSV data.
protected  BadLine handleLineImpl(String[] pLine, int pNumFields, long pLineNumber, String pOriginalLine)
          Add the data line String[] array to the list of TableModel rows.
protected  void setCsvSpecImpl(CsvSpec pCsvSpec)
          Handle property settings for loading data into TableModel.
 void setCsvTableModelClass(Class pCsvTableModelClass)
          Use your own subclass of CsvTableModel for loading CSV data.
 void setEditable(boolean pEditable)
          Make the TableModel editable.
 void setHasHeaders(boolean pHasHeaders)
          The data cells of the first line of the CSV file may be column headers.
protected  void startProcessImpl()
          Start the load process.
 
Methods inherited from class com.ricebridge.csvman.HeadersListenerSupport
getHasHeaders
 
Methods inherited from class com.ricebridge.csvman.CustomLineListener
endProcessImpl, handleBadLineImpl, setLineSpecImpl
 
Methods inherited from class com.ricebridge.csvman.LineListenerSupportImpl
endProcess, handleBadLine, handleLine, setCsvSpec, setLineSpec, startProcess
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROP_TableModel_dataHasHeaders

public static final String PROP_TableModel_dataHasHeaders
Specify that the first row of data is a header line containing the names of the data fields (name for CsvSpec.setProperty: TableModel.dataHasHeaders).

See Also:
Constant Field Values

DEFAULT_TableModel_dataHasHeaders

public static final boolean DEFAULT_TableModel_dataHasHeaders
Default value of TableModel.dataHasHeaders).

See Also:
Constant Field Values

PROP_TableModel_editable

public static final String PROP_TableModel_editable
Specify that table cells can be edited (name for CsvSpec.setProperty: TableModel.editable).

See Also:
Constant Field Values

DEFAULT_TableModel_editable

public static final boolean DEFAULT_TableModel_editable
Default value of TableModel.editable).

See Also:
Constant Field Values

iCsvTableModelClass

protected Class iCsvTableModelClass
TableModel implementation class used to create new instances.


iCsvTableModel

protected CsvTableModel iCsvTableModel
TableModel instance to hold CSV data


iEditable

protected boolean iEditable
Make the TableModel editable. See setEditable(boolean).


iNumFields

protected int iNumFields
Number of data fields.


iDataHasHeaders

protected boolean iDataHasHeaders
When true, the first line of CSV data should be a header line specifying the data field names.


iPropEditable

protected boolean iPropEditable
When true, the TableModel returned will be editable.

Constructor Detail

TableModelLineListener

public TableModelLineListener()
Method Detail

setCsvSpecImpl

protected void setCsvSpecImpl(CsvSpec pCsvSpec)
                       throws Exception
Handle property settings for loading data into TableModel.

You can change these using CsvSpec.

Compatibility note: the correct way to change the settings for a LineListener is by using the CsvSpec.setProperty method. CSV Manager 1.1 also allowed you to set certain settings as load method parameters (for example: pHasHeaders in CsvManager.loadTableModel(File, boolean). These parameters will be removed in CSV Manager 2.0, so you should not use them anymore. They are only retained for compatibility with CSV Manager 1.1.

The properties you can use here are: PROP_TableModel_dataHasHeaders and PROP_TableModel_editable.

Overrides:
setCsvSpecImpl in class CustomLineListener
Parameters:
pCsvSpec - CsvSpec object
Throws:
Exception
See Also:
LineListener.setCsvSpec(com.ricebridge.csvman.CsvSpec), PROP_TableModel_dataHasHeaders

startProcessImpl

protected void startProcessImpl()
                         throws Exception
Start the load process.

Overrides:
startProcessImpl in class CustomLineListener
Throws:
Exception
See Also:
LineListener.startProcess()

handleLineImpl

protected BadLine handleLineImpl(String[] pLine,
                                 int pNumFields,
                                 long pLineNumber,
                                 String pOriginalLine)
                          throws Exception
Add the data line String[] array to the list of TableModel rows.

Specified by:
handleLineImpl in class CustomLineListener
Parameters:
pLine - String values of data fields in line
pNumFields - Number of data fields actually found on the current line
pLineNumber - Count of lines processed so far.
pOriginalLine - Text of original data line from data source
Returns:
null if line is OK, BadLine object if line was bad in some way
Throws:
Exception
See Also:
LineListener.handleLine, BadLine, handleBadLineImpl

setHasHeaders

public void setHasHeaders(boolean pHasHeaders)
The data cells of the first line of the CSV file may be column headers.

When pHasHeaders is true, the first line of the CSV file is considered to consist of column header names rather than normal data.

When pHasHeaders is false, the first line of the CSV file is considered to be normal data and default column names are used.

In the case where there are no column names, or not enough column names, the default form Column X is used, where X is the number of the column, counting from 1.

Compatibility note: please use CsvSpec.setProperty to set this setting instead of this method, which will be remove in CSV Manager 1.2. See setCsvSpecImpl for more details.

Overrides:
setHasHeaders in class HeadersListenerSupport
Parameters:
pHasHeaders - first line is header names
See Also:
CsvTableModel.setHasHeaders, setCsvSpecImpl

setEditable

public void setEditable(boolean pEditable)
Make the TableModel editable.

When true, the method TableModel interface method TableModel.isCellEditable(int, int) returns true for every table cell. This makes the JTable editable.

When false, the JTable is not editable.

Parameters:
pEditable - make the JTable editable
See Also:
CsvTableModel.setEditable(boolean)

setCsvTableModelClass

public void setCsvTableModelClass(Class pCsvTableModelClass)
Use your own subclass of CsvTableModel for loading CSV data.

You may subclass CsvTableModel so that the CSV data is loaded into your own TableModel. You will need to provide an empty constructor so that the newInstance method will work. You must also provide, via this method, the class of your TableModel. Do this like so:

    csvManager.getCsvManagerStore()
      .getTableModelLineListener().setCsvTableModelClass( MyCsvTableModel.class );
  

If subclassing does not fit with your requirements, consider implementing your own variation of CsvTableModel (source) and TableModelLineListener (source) based on the source code provided as part of this API documentation.

Parameters:
pCsvTableModelClass - class of CsvTableModel subclass
See Also:
CsvTableModel

getCsvTableModel

public CsvTableModel getCsvTableModel()
Get the new CsvTableModel instance containing the CSV data.



Copyright © 2003-2006 Ricebridge