org.jdesktop.jdnc.table
Class TableModelExtTextLoader

java.lang.Object
  extended by org.jdesktop.jdnc.DataLoader
      extended by org.jdesktop.jdnc.table.TableModelExtTextLoader
All Implemented Interfaces:
MessageSource, ProgressSource

public class TableModelExtTextLoader
extends DataLoader

Data loader class which reads data from a text input stream and loads it into a DefaultTableModelExt instance. Common examples of tabular text data are comma-separated-values (CSV) and tab-separated-values (TSV) files.

Each line of text in the data stream is treated as a single row of data in the tabular data model. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. Within each row, column values are delimited by a regular expression string, which is a configurable "columnDelimiter" property on the Loader class. The default column delimiter is a tab ('\t') character.

Some text files encode the column names as the first row of data, thus this loader class supports a configurable "columnNamesInFirstRow" boolean property.

This class supports loading the data into the model in increments so that it may be displayed immediately, rather than waiting until the entire stream has been read. The number of rows in an increment can be specified via the "blockIncrementSize" property.

The following example configures a TableModelExtTextLoader to read CSV formatted data at the URL location, "http://myapp/employees.csv", and load the data by roughly 75 line increments:


     DefaultTableModelExt data = new DefaultTableModelExt("http://myapp/employees.csv");
     TableModelExtTextLoader loader = new TableModelExtTextLoader(",", false, 75);
     data.setLoader(loader);
     data.startLoading();
 

Note that properties on a TableModelExtTextLoader instance should not be modified while a load operation is executing, else synchronization errors will occur.


Field Summary
 
Fields inherited from class org.jdesktop.jdnc.DataLoader
READER_PRIORITY_KEY
 
Constructor Summary
TableModelExtTextLoader()
          Creates a TableModelExtTextLoader object with a default tab ('\t') column delimeter, "columnNameInFirstRow" set to false, and a default block increment size of 50.
TableModelExtTextLoader(java.lang.String columnDelimiter, boolean isFirstRowHeader, int blockIncrementSize)
          Creates a TableModelExtTextLoader object with the specified column delimiter, "columnNamesInFirstRow" value, and block increment size.
 
Method Summary
 int getBlockIncrementSize()
           
 java.lang.String getColumnDelimiter()
           
 boolean isFirstRowHeader()
           
protected  void loadData(java.lang.Object model)
          Invoked internally once the readData method calls scheduleLoad to schedule the loading of an increment of data to the model.
 void loadMetaData(java.lang.Object model, java.io.InputStream is)
          Initializes the the number of columns in the tabular data model by reading the first line in the stream and counting the columns.
protected  void readData(java.io.InputStream is)
          Invoked by the startLoading method.
 void setBlockIncrementSize(int blockIncrementSize)
          Sets the "blockIncrementSize" property.
 void setColumnDelimiter(java.lang.String regex)
          Sets the "columnDelimiter" property.
 void setFirstRowHeader(boolean isFirstRowHeader)
          Sets the "isFirstRowHeader" property.
 void startLoading(java.lang.Object model, java.io.InputStream is)
          Starts the asynchronous load operation by spinning up a separate thread that will read the data from the input stream.
 
Methods inherited from class org.jdesktop.jdnc.DataLoader
addMessageListener, addProgressListener, fireException, fireMessage, fireProgressEnded, fireProgressIncremented, fireProgressStarted, getMessageListeners, getProgressListeners, getReaderThreadPriority, removeMessageListener, removeProgressListener, scheduleLoad
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TableModelExtTextLoader

public TableModelExtTextLoader()
Creates a TableModelExtTextLoader object with a default tab ('\t') column delimeter, "columnNameInFirstRow" set to false, and a default block increment size of 50.


TableModelExtTextLoader

public TableModelExtTextLoader(java.lang.String columnDelimiter,
                               boolean isFirstRowHeader,
                               int blockIncrementSize)
Creates a TableModelExtTextLoader object with the specified column delimiter, "columnNamesInFirstRow" value, and block increment size.

Parameters:
columnDelimiter - regular expression string used to delimit column values within a row
isFirstRowHeader - boolean indicating whether the first row should be treated as the column header names
blockIncrementSize - integer indicating the number of rows to be read between incremental load requests
Method Detail

getColumnDelimiter

public java.lang.String getColumnDelimiter()
Returns:
regular expression string used to delimit column values within a row

setColumnDelimiter

public void setColumnDelimiter(java.lang.String regex)
Sets the "columnDelimiter" property.

Parameters:
regex - regular expression string used to delimit column values within a row

isFirstRowHeader

public boolean isFirstRowHeader()
Returns:
boolean indicating whether the first row should be treated as header column names

setFirstRowHeader

public void setFirstRowHeader(boolean isFirstRowHeader)
Sets the "isFirstRowHeader" property.

Parameters:
isFirstRowHeader - boolean indicating whether the first row should be treated as header column names

getBlockIncrementSize

public int getBlockIncrementSize()
Returns:
integer indicating the number of rows to be read between incremental load requests

setBlockIncrementSize

public void setBlockIncrementSize(int blockIncrementSize)
Sets the "blockIncrementSize" property.

Parameters:
blockIncrementSize - integer indicating the number of rows to be read between incremental load requests

loadMetaData

public void loadMetaData(java.lang.Object model,
                         java.io.InputStream is)
                  throws java.io.IOException
Initializes the the number of columns in the tabular data model by reading the first line in the stream and counting the columns. If "columnNamesInFirstRow" is true then this method will also initialize the column names for the tabular data model with the column values contained in the first line of the stream. This method is synchronous and may be invoked from the event dispatch thread.

Specified by:
loadMetaData in class DataLoader
Parameters:
model - the tabular data model being loaded from the input stream
is - the input stream containing the meta-data
Throws:
java.io.IOException - if errors occur while reading from the stream
java.lang.ClassCastException - if the model is not an instance of DefaultTableModelExt

startLoading

public void startLoading(java.lang.Object model,
                         java.io.InputStream is)
Starts the asynchronous load operation by spinning up a separate thread that will read the data from the input stream. This method will return immediately. Prior to calling this method, a MessageListener should be registered to be notified as data is loaded or errors occur. If the data model being loaded requires meta-data to describe its structure (such as the number of columns in a tabular data row), then that meta-data must be initialized before startLoading is called and must not be modified while the load operation executes, otherwise synchronization errors will occur.

Overrides:
startLoading in class DataLoader
Parameters:
model - the data model being loaded from the input stream
is - the input stream containing the data
See Also:
DataLoader.addProgressListener(org.jdesktop.swingx.event.ProgressListener)

readData

protected void readData(java.io.InputStream is)
                 throws java.io.IOException,
                        ConversionException
Invoked by the startLoading method. This method will be called on a separate "reader" thread. This method will invoke scheduleLoad between reading each incremental block of rows. This reader will use the "elementClass" and "Converter" properties on a given column's meta-data object in order to convert the data from string to object values.

Specified by:
readData in class DataLoader
Parameters:
is - the input stream containing the data
Throws:
java.io.IOException - if errors occur while reading data from the input stream
ConversionException - if errors occur while converting data values from string to object
See Also:
DataLoader.scheduleLoad()

loadData

protected void loadData(java.lang.Object model)
Invoked internally once the readData method calls scheduleLoad to schedule the loading of an increment of data to the model. This method is called on the event dispatch thread, therefore it will add the current list of rows read to the tabular data model. After the data is added to the model, it will fire a progress event to indicate whether the load operation is complete.

Specified by:
loadData in class DataLoader
Parameters:
model - the data model being loaded from the input stream
Throws:
java.lang.ClassCastException - if model is not an instance of DefaultTableModelExt
See Also:
DataLoader.fireProgressStarted(int, int), DataLoader.fireProgressEnded(), DataLoader.fireException(java.lang.Throwable)


Copyright © 2005 Sun Microsystems All Rights Reserved.