Holger's
Java API

com.antelmann.util.table
Class FileTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by javax.swing.table.DefaultTableModel
          extended by com.antelmann.util.table.FileTableModel
All Implemented Interfaces:
PatternExtractor<Object,Object>, Serializable, TableModel

public class FileTableModel
extends DefaultTableModel
implements PatternExtractor<Object,Object>

a table model that enables importing/exporting from/to a CSV file easily. When read from a file, the model is completely placed in-memory

Author:
Holger Antelmann
See Also:
Strings.getFields(String, String, boolean), Serialized Form

Field Summary
 
Fields inherited from class javax.swing.table.DefaultTableModel
columnIdentifiers, dataVector
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Fields inherited from interface com.antelmann.util.PatternExtractor
SELF_EXTRACTOR, STRING_EXTRACTOR
 
Constructor Summary
FileTableModel(File file, boolean firstLineContainsHeader, String fieldSeparator, boolean usesQuotes)
          creates an in-memory TableModel based on an import file where the first line contains headers
FileTableModel(File file, boolean firstLineContainsHeader, String fieldSeparator, boolean usesQuotes, String charSet)
           
FileTableModel(File file, String fieldSeparator, boolean usesQuotes)
          assumes that the first line contains header
FileTableModel(Reader input, boolean firstLineContainsHeader, String fieldSeparator, boolean usesQuotes)
           
FileTableModel(TableModel model)
          enables to create a FileTableModel from any model, so that it can be saved as a file
 
Method Summary
 Object extractPattern(Object cellValue)
          responsible by default for converting a cell value into the value that is used when saving the value to a file.
protected  String quote(Object cellValue)
          this implementation uses quoteValue(Object)
static String quoteValue(Object value)
          if the given value is a String, it is placed in quotes (while containing quotes are double-quoted); null values are converted to an empty String and other values are returned as their toString() results.
 void saveToFile(File file, String fieldSeparator, boolean usesQuotes)
           
 void saveToFile(File file, String fieldSeparator, boolean usesQuotes, PatternExtractor<Object,Object> extractor)
           
 void saveToWriter(Writer writer, String fieldSeparator, boolean usesQuotes, PatternExtractor<Object,Object> extractor)
          will always write a header line.
 
Methods inherited from class javax.swing.table.DefaultTableModel
addColumn, addColumn, addColumn, addRow, addRow, convertToVector, convertToVector, getColumnCount, getColumnName, getDataVector, getRowCount, getValueAt, insertRow, insertRow, isCellEditable, moveRow, newDataAvailable, newRowsAdded, removeRow, rowsRemoved, setColumnCount, setColumnIdentifiers, setColumnIdentifiers, setDataVector, setDataVector, setNumRows, setRowCount, setValueAt
 
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, toString, wait, wait, wait
 

Constructor Detail

FileTableModel

public FileTableModel(TableModel model)
enables to create a FileTableModel from any model, so that it can be saved as a file


FileTableModel

public FileTableModel(File file,
                      String fieldSeparator,
                      boolean usesQuotes)
               throws IOException
assumes that the first line contains header

Throws:
IOException

FileTableModel

public FileTableModel(File file,
                      boolean firstLineContainsHeader,
                      String fieldSeparator,
                      boolean usesQuotes)
               throws IOException
creates an in-memory TableModel based on an import file where the first line contains headers

Throws:
IOException

FileTableModel

public FileTableModel(File file,
                      boolean firstLineContainsHeader,
                      String fieldSeparator,
                      boolean usesQuotes,
                      String charSet)
               throws IOException
Throws:
IOException

FileTableModel

@Todo(value="doesn\'t yet support csv files that contain quoted line breaks")
public FileTableModel(Reader input,
                                boolean firstLineContainsHeader,
                                String fieldSeparator,
                                boolean usesQuotes)
               throws IOException
Throws:
IOException
Method Detail

extractPattern

public Object extractPattern(Object cellValue)
responsible by default for converting a cell value into the value that is used when saving the value to a file. The return value's toString()-method is used to be written to the file; String values are quoted if 'usesQuotes' is true.

Specified by:
extractPattern in interface PatternExtractor<Object,Object>
Returns:
the pattern that results from extracting the relevant information from the original type.
See Also:
quote(Object), saveToFile(File, String, boolean)

quote

protected String quote(Object cellValue)
this implementation uses quoteValue(Object)


quoteValue

public static String quoteValue(Object value)
if the given value is a String, it is placed in quotes (while containing quotes are double-quoted); null values are converted to an empty String and other values are returned as their toString() results.

Returns:
a non-null String

saveToFile

public void saveToFile(File file,
                       String fieldSeparator,
                       boolean usesQuotes)
                throws IOException
Throws:
IOException

saveToFile

public void saveToFile(File file,
                       String fieldSeparator,
                       boolean usesQuotes,
                       PatternExtractor<Object,Object> extractor)
                throws IOException
Throws:
IOException

saveToWriter

public void saveToWriter(Writer writer,
                         String fieldSeparator,
                         boolean usesQuotes,
                         PatternExtractor<Object,Object> extractor)
                  throws IOException
will always write a header line. If the model didn't contain header, some default header will be provided (1, 2, ..). If quotes are used, each occurrence of a quote within a field is replaced by a single quote.

Throws:
IOException


(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads