com.bbn.openmap.dataAccess.shape
Class DbfTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by com.bbn.openmap.dataAccess.shape.DbfTableModel
All Implemented Interfaces:
ShapeConstants, java.io.Serializable, java.lang.Iterable<java.util.List<java.lang.Object>>, java.util.EventListener, javax.swing.event.TableModelListener, javax.swing.table.TableModel
Direct Known Subclasses:
DbfFile, MetaDbfTableModel

public class DbfTableModel
extends javax.swing.table.AbstractTableModel
implements ShapeConstants, javax.swing.event.TableModelListener, java.lang.Iterable<java.util.List<java.lang.Object>>

An implementation of TableModel that manages tabular data read from a dbf file and enables the user to stored data store herein to be saved to a file conforming to the DBF III file format specification. To create a three one column model:

 DbfTableModel model = new DbfTableModel(1);
 
 model.setDecimalCount(0, (byte) 0);
 model.setLength(0, (byte) 10);
 model.setColumnName(0, "Column1");
 model.setType(0, (byte) DbfTableModel.TYPE_CHARACTER);
 
 model.setDecimalCount(1, (byte) 0);
 model.setLength(1, (byte) 10);
 model.setColumnName(1, "Column1");
 model.setType(1, (byte) DbfTableModel.TYPE_NUMERIC);
 
 model.setDecimalCount(2, (byte) 0);
 model.setLength(2, (byte) 10);
 model.setColumnName(2, "Column1");
 model.setType(2, (byte) DbfTableModel.TYPE_CHARACTER);
 
 esriLayer.setModel(model);
 

Author:
Doug Van Auken
See Also:
Serialized Form

Field Summary
protected  int _columnCount
          Class scope variable for the number of columns that exist in the model
protected  byte[] _decimalCounts
          An array of bytes that contain the number of decimal places for each column
protected  int[] _lengths
          An array of bytes that contain the character lengths for each column
protected  java.lang.String[] _names
          An array of bytes that contain the names for each column
protected  java.util.List<java.util.List<java.lang.Object>> _records
          Class scope reference to a list of data formatted by row
protected  byte[] _types
          An array of bytes that contain the column types for each column
protected  boolean DEBUG
           
protected  boolean dirty
           
static int DONE_MASK
          Button mask to drop the frame quietly, with the modifications to the table complete.
protected  boolean exitOnClose
           
protected  java.lang.StringBuffer filePath
           
protected  javax.swing.JFrame frame
           
static java.util.logging.Logger logger
           
static int MODIFY_COLUMN_MASK
          Edit button mask, to allow adding/removing columns in the attribute table.
static int MODIFY_ROW_MASK
          Edit button mask, to allow adding/removing rows.
protected  DbfTableModel parent
           
static int SAVE_MASK
          Button mask to show a save button to write out any changes.
protected  javax.swing.JTable table
           
static byte TYPE_AUTOINCREMENT
          Same as a Long
static byte TYPE_BINARY
          10 digits representing a .DBT block number.
static byte TYPE_CHARACTER
          All OEM code page characters - padded with blanks to the width of the field.
static byte TYPE_DATE
          8 bytes - date stored as a string in the format YYYYMMDD.
static byte TYPE_DOUBLE
          8 bytes - no conversions, stored as a double.
static byte TYPE_FLOAT
          Number stored as a string, right justified, and padded with blanks to the width of the field.
static byte TYPE_LOGICAL
          1 byte - initialized to 0x20 (space) otherwise T or F.
static byte TYPE_LONG
          4 bytes.
static byte TYPE_MEMO
          10 digits (bytes) representing a .DBT block number.
static byte TYPE_NUMERIC
          Number stored as a string, right justified, and padded with blanks to the width of the field.
static byte TYPE_OLE
          10 digits (bytes) representing a .DBT block number.
static byte TYPE_TIMESTAMP
          8 bytes - two longs, first for date, second for time.
protected  boolean writable
           
static java.lang.Double ZERO
          Old Object value held for every NUMERIC cell that had a problem importing.
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Fields inherited from interface com.bbn.openmap.dataAccess.shape.ShapeConstants
DBF_ATTRIBUTE, DBF_AUTOINCREMENT, DBF_BINARY, DBF_CHARACTER, DBF_DATE, DBF_DOUBLE, DBF_FLOAT, DBF_LOGICAL, DBF_LONG, DBF_MEMO, DBF_NUMERIC, DBF_OLE, DBF_TIMESTAMP, DBF_TYPE_AUTOINCREMENT, DBF_TYPE_BINARY, DBF_TYPE_CHARACTER, DBF_TYPE_DATE, DBF_TYPE_DOUBLE, DBF_TYPE_FLOAT, DBF_TYPE_LOGICAL, DBF_TYPE_LONG, DBF_TYPE_MEMO, DBF_TYPE_NUMERIC, DBF_TYPE_OLE, DBF_TYPE_TIMESTAMP, PARAM_DBF, PARAM_SHP, PARAM_SHX, SHAPE_BOUNDS_ATTRIBUTE, SHAPE_DBF_DASHPATTERN, SHAPE_DBF_DASHPHASE, SHAPE_DBF_DESCRIPTION, SHAPE_DBF_FILLCOLOR, SHAPE_DBF_INFO_ATTRIBUTE, SHAPE_DBF_LINECOLOR, SHAPE_DBF_LINEWIDTH, SHAPE_DBF_SELECTCOLOR, SHAPE_FILE_HEADER_LENGTH, SHAPE_FILE_RECORD_HEADER_LENGTH, SHAPE_INDEX_ATTRIBUTE, SHAPE_MAX_MEASURE_ATTRIBUTE, SHAPE_MAX_Z_ATTRIBUTE, SHAPE_MEASURE_ATTRIBUTE, SHAPE_MIN_MEASURE_ATTRIBUTE, SHAPE_MIN_Z_ATTRIBUTE, SHAPE_TYPE_ARC, SHAPE_TYPE_MULTIPATCH, SHAPE_TYPE_MULTIPOINT, SHAPE_TYPE_MULTIPOINTM, SHAPE_TYPE_MULTIPOINTZ, SHAPE_TYPE_NULL, SHAPE_TYPE_POINT, SHAPE_TYPE_POINTM, SHAPE_TYPE_POINTZ, SHAPE_TYPE_POLYGON, SHAPE_TYPE_POLYGONM, SHAPE_TYPE_POLYGONZ, SHAPE_TYPE_POLYLINE, SHAPE_TYPE_POLYLINEM, SHAPE_TYPE_POLYLINEZ, SHAPE_Z_ATTRIBUTE
 
Constructor Summary
protected DbfTableModel()
           
  DbfTableModel(DbfInputStream is)
          Creates a DbfTableModel based on an InputStream
  DbfTableModel(int columnCount)
          Creates a blank DbfTableModel
 
Method Summary
protected  byte[] add(byte[] current, byte nb)
           
protected  int[] add(int[] current, byte nb)
           
protected  java.lang.String[] add(java.lang.String[] current, java.lang.String string)
           
 void addBlankRecord()
           
protected  void addColumn(java.util.List<java.lang.Object> recordColumn)
          The types in the ArrayList are set - String, Byte, Integer, Integer - to match the format of the header.
 void addRecord(java.util.List<java.lang.Object> columns)
          Adds a row of data to the the model
 void append(DbfTableModel dbf)
           
static java.lang.String appendWhitespaceOrTrim(java.lang.String s, int length)
          A method that looks at the length of String s and returns a copy of it with whitespace appended to the end to allow it to have the provided length.
 void cleanupChanges()
           
protected  void commitEvents(DbfTableModel model)
           
protected  void deleteColumn(int columnIndex)
          Delete a column, iterating through all the records and deleting that part of each record.
 void doAction(OMGraphicList list, OMGraphic graphic, OMAction action, DbfTableModelFactory dbfFactory)
           
 void exitWindowClosed()
           
 java.util.List<java.lang.Object> getBlankRecord()
           
 java.lang.Class<? extends java.lang.Object> getColumnClass(int c)
          Retrieves the column class for the passed in column index
 int getColumnCount()
          Retrieves the number of columns that exist in the model
 int getColumnIndexForName(java.lang.String columnName)
          Find the column index of the column with the given name.
 java.lang.String getColumnName(int column)
          Retrieves the column name for the passed in column index
static DbfTableModel getDbfTableModel(java.net.URL dbf)
          Creates a DbfTableModel for a given .dbf file
 byte getDecimalCount(int column)
          Retrieves the number of decimal places for the passed in column index
 java.lang.Object getEmptyDefaultForType(byte type)
           
 java.awt.Component getGUI(java.lang.String filename, int actionMask)
           
 int getLength(int column)
          Retrieves the character length for the passed in column index
static java.lang.Object getObjectForType(java.lang.String cellContents, int type, java.text.DecimalFormat df, int columnLength)
           
 java.util.List<java.lang.Object> getRecord(int recordnumber)
          Retrieves the record array list for the passed record number.
 java.util.Iterator<java.util.List<java.lang.Object>> getRecords()
          Get an iterator over the records.
 int getRowCount()
          Retrieves the number of columns that exist in the model
static java.lang.String getStringForType(java.lang.Object obj, byte type, java.text.DecimalFormat df, int columnLength)
          Takes the object and converts it to a String for output.
protected  javax.swing.JTable getTable()
           
 javax.swing.JTable getTable(javax.swing.ListSelectionModel lsm)
          Needs to be called before displaying the DbfTableModel.
 byte getType(int column)
          Retrieves the column type for the passed in column index
 java.lang.Object getValueAt(int row, int column)
          Retrieves a value for a specific column and row index
 boolean getWritable()
           
 DbfTableModel headerClone()
          Create another DbfTableModel with the same structure as this one (number of columns, column names, lengths and decimal counts).
 void hideGUI()
           
 boolean isCellEditable(int rowIndex, int columnIndex)
           
 boolean isExitOnClose()
           
static boolean isNumericalType(byte type)
           
static boolean isValidType(byte type)
           
 java.util.Iterator<java.util.List<java.lang.Object>> iterator()
          Iterable method for the records.
static void main(java.lang.String[] args)
           
 boolean matches(DbfTableModel dbf)
           
static DbfTableModel read(java.net.URL dbf)
           
protected  byte[] remove(byte[] current, int index)
           
 java.util.List<java.lang.Object> remove(int index)
          Remove the record at the index.
protected  int[] remove(int[] current, int index)
           
protected  java.lang.String[] remove(java.lang.String[] current, int index)
           
 void setColumnName(int column, java.lang.String name)
          Sets the column name for the passed-in field index
 void setDecimalCount(int column, byte decimalCount)
          Sets the decimal count for the passed in field index
 void setExitOnClose(boolean exitOnClose)
           
 void setLength(int column, int length)
          Set the character length for the passed-in field index
 void setType(int column, byte type)
          Sets the column type for the passed-in field index
 void setValueAt(java.lang.Object object, int row, int column)
           
 void setWritable(boolean set)
           
 void showGUI(java.lang.String filename, int actionMask)
           
 void tableChanged(javax.swing.event.TableModelEvent e)
           
static void test()
           
static java.lang.String write(DbfTableModel model, java.lang.String location)
           
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, 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
 

Field Detail

logger

public static java.util.logging.Logger logger

TYPE_BINARY

public static final byte TYPE_BINARY
10 digits representing a .DBT block number. The number is stored as a string, right justified and padded with blanks.

See Also:
Constant Field Values

TYPE_CHARACTER

public static final byte TYPE_CHARACTER
All OEM code page characters - padded with blanks to the width of the field.

See Also:
Constant Field Values

TYPE_DATE

public static final byte TYPE_DATE
8 bytes - date stored as a string in the format YYYYMMDD.

See Also:
Constant Field Values

TYPE_NUMERIC

public static final byte TYPE_NUMERIC
Number stored as a string, right justified, and padded with blanks to the width of the field.

See Also:
Constant Field Values

TYPE_LOGICAL

public static final byte TYPE_LOGICAL
1 byte - initialized to 0x20 (space) otherwise T or F.

See Also:
Constant Field Values

TYPE_MEMO

public static final byte TYPE_MEMO
10 digits (bytes) representing a .DBT block number. The number is stored as a string, right justified and padded with blanks.

See Also:
Constant Field Values

TYPE_TIMESTAMP

public static final byte TYPE_TIMESTAMP
8 bytes - two longs, first for date, second for time. The date is the number of days since 01/01/4713 BC. Time is hours * 3600000L + minutes * 60000L + Seconds * 1000L

See Also:
Constant Field Values

TYPE_LONG

public static final byte TYPE_LONG
4 bytes. Leftmost bit used to indicate sign, 0 negative.

See Also:
Constant Field Values

TYPE_AUTOINCREMENT

public static final byte TYPE_AUTOINCREMENT
Same as a Long

See Also:
Constant Field Values

TYPE_FLOAT

public static final byte TYPE_FLOAT
Number stored as a string, right justified, and padded with blanks to the width of the field.

See Also:
Constant Field Values

TYPE_DOUBLE

public static final byte TYPE_DOUBLE
8 bytes - no conversions, stored as a double.

See Also:
Constant Field Values

TYPE_OLE

public static final byte TYPE_OLE
10 digits (bytes) representing a .DBT block number. The number is stored as a string, right justified and padded with blanks.

See Also:
Constant Field Values

MODIFY_ROW_MASK

public static final int MODIFY_ROW_MASK
Edit button mask, to allow adding/removing rows. Be very careful with this option if you plan on using this file with a shape file - the number of records has to match the number of graphics in a shape file, so if you add or delete, you should add/delete the graphic in the shape file, too.

See Also:
Constant Field Values

MODIFY_COLUMN_MASK

public static final int MODIFY_COLUMN_MASK
Edit button mask, to allow adding/removing columns in the attribute table.

See Also:
Constant Field Values

DONE_MASK

public static final int DONE_MASK
Button mask to drop the frame quietly, with the modifications to the table complete.

See Also:
Constant Field Values

SAVE_MASK

public static final int SAVE_MASK
Button mask to show a save button to write out any changes.

See Also:
Constant Field Values

ZERO

public static final java.lang.Double ZERO
Old Object value held for every NUMERIC cell that had a problem importing. Now, those cells are filled with whitespace.


_lengths

protected int[] _lengths
An array of bytes that contain the character lengths for each column


_decimalCounts

protected byte[] _decimalCounts
An array of bytes that contain the number of decimal places for each column


_types

protected byte[] _types
An array of bytes that contain the column types for each column


_names

protected java.lang.String[] _names
An array of bytes that contain the names for each column


_records

protected java.util.List<java.util.List<java.lang.Object>> _records
Class scope reference to a list of data formatted by row


_columnCount

protected int _columnCount
Class scope variable for the number of columns that exist in the model


writable

protected boolean writable

table

protected javax.swing.JTable table

parent

protected final DbfTableModel parent

dirty

protected boolean dirty

exitOnClose

protected boolean exitOnClose

DEBUG

protected boolean DEBUG

filePath

protected final java.lang.StringBuffer filePath

frame

protected javax.swing.JFrame frame
Constructor Detail

DbfTableModel

protected DbfTableModel()

DbfTableModel

public DbfTableModel(int columnCount)
Creates a blank DbfTableModel

Parameters:
columnCount - The number of columns this model will manage

DbfTableModel

public DbfTableModel(DbfInputStream is)
Creates a DbfTableModel based on an InputStream

Parameters:
is - The dbf file
Method Detail

addRecord

public void addRecord(java.util.List<java.lang.Object> columns)
Adds a row of data to the the model

Parameters:
columns - A collection of columns that comprise the row of data
Throws:
An - exception is thrown if the number of elements in the passed in collection does not match the number of columns in the model

remove

public java.util.List<java.lang.Object> remove(int index)
Remove the record at the index.


addBlankRecord

public void addBlankRecord()

getBlankRecord

public java.util.List<java.lang.Object> getBlankRecord()

getEmptyDefaultForType

public java.lang.Object getEmptyDefaultForType(byte type)

isNumericalType

public static boolean isNumericalType(byte type)

isValidType

public static boolean isValidType(byte type)

getRecord

public java.util.List<java.lang.Object> getRecord(int recordnumber)
Retrieves the record array list for the passed record number. In OpenMap, Shape file records start their indexes at 0. This is the number attribute stored in the EsriGraphic. Note: The Shape Specification and Shape files contain indexes starting at 0. Make sure you don't get bit by an off-by-one situation.

Parameters:
recordnumber - The record number
Returns:
An ArrayList for the given record number

getRecords

public java.util.Iterator<java.util.List<java.lang.Object>> getRecords()
Get an iterator over the records.


iterator

public java.util.Iterator<java.util.List<java.lang.Object>> iterator()
Iterable method for the records.

Specified by:
iterator in interface java.lang.Iterable<java.util.List<java.lang.Object>>

getColumnClass

public java.lang.Class<? extends java.lang.Object> getColumnClass(int c)
Retrieves the column class for the passed in column index

Specified by:
getColumnClass in interface javax.swing.table.TableModel
Overrides:
getColumnClass in class javax.swing.table.AbstractTableModel
Parameters:
c - The column index
Returns:
The column class for the given column index

getColumnCount

public int getColumnCount()
Retrieves the number of columns that exist in the model

Specified by:
getColumnCount in interface javax.swing.table.TableModel
Returns:
The number of columns that exist in the model

getDecimalCount

public byte getDecimalCount(int column)
Retrieves the number of decimal places for the passed in column index

Parameters:
column - The column index
Returns:
The number of decimal places for the given column index

getColumnName

public java.lang.String getColumnName(int column)
Retrieves the column name for the passed in column index

Specified by:
getColumnName in interface javax.swing.table.TableModel
Overrides:
getColumnName in class javax.swing.table.AbstractTableModel
Parameters:
column - The column index
Returns:
The column name for the given column index

getColumnIndexForName

public int getColumnIndexForName(java.lang.String columnName)
Find the column index of the column with the given name.

Parameters:
columnName -
Returns:
If the columnName is valid, some number between 0-column count. Otherwise, -1 for non-valid names.

getLength

public int getLength(int column)
Retrieves the character length for the passed in column index

Parameters:
column - The column index
Returns:
The character length for the given column index

getRowCount

public int getRowCount()
Retrieves the number of columns that exist in the model

Specified by:
getRowCount in interface javax.swing.table.TableModel
Returns:
The number column that exist in the model

getType

public byte getType(int column)
Retrieves the column type for the passed in column index

Parameters:
column - The column index
Returns:
The column type for the given column index

getValueAt

public java.lang.Object getValueAt(int row,
                                   int column)
Retrieves a value for a specific column and row index

Specified by:
getValueAt in interface javax.swing.table.TableModel
Returns:
Object A value for a specific column and row index

setColumnName

public void setColumnName(int column,
                          java.lang.String name)
Sets the column name for the passed-in field index

Parameters:
column - The column index
name - The name to assign for the passed-in column index

setDecimalCount

public void setDecimalCount(int column,
                            byte decimalCount)
Sets the decimal count for the passed in field index

Parameters:
column - The index to the column
decimalCount - The number of decimals places to assign to the passed in column

setLength

public void setLength(int column,
                      int length)
Set the character length for the passed-in field index

Parameters:
column - The column index
length - The character length to assign for the passed-in column index

setType

public void setType(int column,
                    byte type)
Sets the column type for the passed-in field index

Parameters:
column - The column index
type - The type of column to assign for the passed-in column index

setValueAt

public void setValueAt(java.lang.Object object,
                       int row,
                       int column)
Specified by:
setValueAt in interface javax.swing.table.TableModel
Overrides:
setValueAt in class javax.swing.table.AbstractTableModel

isCellEditable

public boolean isCellEditable(int rowIndex,
                              int columnIndex)
Specified by:
isCellEditable in interface javax.swing.table.TableModel
Overrides:
isCellEditable in class javax.swing.table.AbstractTableModel

setWritable

public void setWritable(boolean set)

getWritable

public boolean getWritable()

getTable

public javax.swing.JTable getTable(javax.swing.ListSelectionModel lsm)
Needs to be called before displaying the DbfTableModel.


getTable

protected javax.swing.JTable getTable()

getGUI

public java.awt.Component getGUI(java.lang.String filename,
                                 int actionMask)

hideGUI

public void hideGUI()

showGUI

public void showGUI(java.lang.String filename,
                    int actionMask)

exitWindowClosed

public void exitWindowClosed()

tableChanged

public void tableChanged(javax.swing.event.TableModelEvent e)
Specified by:
tableChanged in interface javax.swing.event.TableModelListener

commitEvents

protected void commitEvents(DbfTableModel model)

deleteColumn

protected void deleteColumn(int columnIndex)
Delete a column, iterating through all the records and deleting that part of each record.


remove

protected int[] remove(int[] current,
                       int index)

remove

protected byte[] remove(byte[] current,
                        int index)

remove

protected java.lang.String[] remove(java.lang.String[] current,
                                    int index)

addColumn

protected void addColumn(java.util.List<java.lang.Object> recordColumn)
The types in the ArrayList are set - String, Byte, Integer, Integer - to match the format of the header.


add

protected int[] add(int[] current,
                    byte nb)

add

protected byte[] add(byte[] current,
                     byte nb)

add

protected java.lang.String[] add(java.lang.String[] current,
                                 java.lang.String string)

cleanupChanges

public void cleanupChanges()

headerClone

public DbfTableModel headerClone()
Create another DbfTableModel with the same structure as this one (number of columns, column names, lengths and decimal counts).


getDbfTableModel

public static DbfTableModel getDbfTableModel(java.net.URL dbf)
Creates a DbfTableModel for a given .dbf file

Parameters:
dbf - The url of the file to retrieve.
Returns:
The DbfTableModel, null if there is a problem.

read

public static DbfTableModel read(java.net.URL dbf)
                          throws java.lang.Exception
Throws:
java.lang.Exception

write

public static java.lang.String write(DbfTableModel model,
                                     java.lang.String location)
                              throws java.io.FileNotFoundException,
                                     java.io.IOException
Throws:
java.io.FileNotFoundException
java.io.IOException

getStringForType

public static java.lang.String getStringForType(java.lang.Object obj,
                                                byte type,
                                                java.text.DecimalFormat df,
                                                int columnLength)
Takes the object and converts it to a String for output.

Parameters:
obj -
type -
df -
columnLength - the length of the column entries, so whitespace can be added if needed.
Returns:
a string value representing the object. If the object is null, a whitespace string will be returned.

appendWhitespaceOrTrim

public static java.lang.String appendWhitespaceOrTrim(java.lang.String s,
                                                      int length)
A method that looks at the length of String s and returns a copy of it with whitespace appended to the end to allow it to have the provided length.

Parameters:
s -
length -
Returns:
String with whitespace added if needed.

getObjectForType

public static java.lang.Object getObjectForType(java.lang.String cellContents,
                                                int type,
                                                java.text.DecimalFormat df,
                                                int columnLength)
                                         throws java.text.ParseException
Throws:
java.text.ParseException

isExitOnClose

public boolean isExitOnClose()

setExitOnClose

public void setExitOnClose(boolean exitOnClose)

main

public static void main(java.lang.String[] args)

test

public static void test()

matches

public boolean matches(DbfTableModel dbf)

append

public void append(DbfTableModel dbf)

doAction

public void doAction(OMGraphicList list,
                     OMGraphic graphic,
                     OMAction action,
                     DbfTableModelFactory dbfFactory)


Copyright (C) BBNT Solutions LLC; See http://openmap.bbn.com/ for details