com.infoviz.datamodel
Class SQLTableModel

java.lang.Object
  extended bycom.infoviz.datamodel.SQLTableModel
All Implemented Interfaces:
javax.swing.table.TableModel

public class SQLTableModel
extends java.lang.Object
implements javax.swing.table.TableModel

This class implements java swing Table Model and provides a powerful SQL like interface for queries, updates and deletes. It also provides the facility to create unique constraints on a set columns.

$Id$ $Revision$ $Author$


Constructor Summary
SQLTableModel()
          creates an empty model
SQLTableModel(java.util.Map columnNameClassMap)
          creates a table model using the specified column names and corresponding classes
SQLTableModel(javax.swing.table.TableModel model)
          converts any TableModel into a SQLTableModel
 
Method Summary
 void addColumn(java.lang.String name, java.lang.Class columnClass)
          add a new column to the table
 void addRow(java.util.List rowValues)
          adds a row to the table using the values specified
 void addRow(java.util.Map nameValueMap)
          adds a row to the table
 void addTableModelListener(javax.swing.event.TableModelListener l)
          Adds a listener to the list that is notified each time a change to the data model occurs.
 void addUniqueConstraint(java.lang.String[] columnNames)
          makes sure that combination of the column values are unique.
 void delete(java.lang.String predicate, java.util.List bindObjects)
          deletes all the rows that satisfy the given predicate
 java.lang.Class getColumnClass(int columnIndex)
          Returns the most specific superclass for all the cell values in the column.
 int getColumnCount()
          Returns the number of columns in the model.
 java.lang.String getColumnName(int columnIndex)
          Returns the name of the column at columnIndex.
 int getRowCount()
          Returns the number of rows in the model.
 java.lang.Object getValueAt(int rowIndex, int columnIndex)
          Returns the value for the cell at columnIndex and rowIndex.
 boolean isCellEditable(int rowIndex, int columnIndex)
          Returns true if the cell at rowIndex and columnIndex is editable.
 void removeColumn(java.lang.String name)
          removes the column specified
 void removeTableModelListener(javax.swing.event.TableModelListener l)
          Removes a listener from the list that is notified each time a change to the data model occurs.
 javax.swing.table.TableModel select(java.lang.String predicate, java.util.List bindObjects)
          returns a SQLTableModel containing rows/columns matching the specified predicate and binding objects
 void setValueAt(java.lang.Object aValue, int rowIndex, int columnIndex)
          Sets the value in the cell at columnIndex and rowIndex to aValue.
 void update(java.lang.String predicate, java.util.List bindObjects, java.util.Map nameValueMap)
          updates all table rows that satisfy the predicate, with the values specified in nameValueMap
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SQLTableModel

public SQLTableModel()
creates an empty model


SQLTableModel

public SQLTableModel(java.util.Map columnNameClassMap)
creates a table model using the specified column names and corresponding classes

Parameters:
columnNameClassMap - - key: columnn name, valu: column class

SQLTableModel

public SQLTableModel(javax.swing.table.TableModel model)
converts any TableModel into a SQLTableModel

Parameters:
model - - any TableModel
Method Detail

addColumn

public void addColumn(java.lang.String name,
                      java.lang.Class columnClass)
add a new column to the table

Parameters:
name - - The name of the column.
columnClass - -

removeColumn

public void removeColumn(java.lang.String name)
removes the column specified

Parameters:
name - - the name of the column

addRow

public void addRow(java.util.Map nameValueMap)
adds a row to the table

Parameters:
nameValueMap - -
  • key: name of the column
  • value: any object of the Class specified in the addColumn method for the column.

  • select

    public javax.swing.table.TableModel select(java.lang.String predicate,
                                               java.util.List bindObjects)

    returns a SQLTableModel containing rows/columns matching the specified predicate and binding objects

    Parameters:
    predicate - - very similar to JDBC statements

    example: "employeeNo, employeeName where salary > ? and location = ?" group by designation order by salary

    example: "avg(salary > ?) where designation like "%PRESIDENT%"

    SQL functions like avg and sum are also supported. todo: create a separate document

    bindObjects - - the objects to substitute for the "?"s in the predicate

    example: a list containing Double(40000) and String("New York")

    Returns:
    a Table model Matching the criteria as specified using the predicate and bind objects

    addRow

    public void addRow(java.util.List rowValues)
    adds a row to the table using the values specified

    Parameters:
    rowValues - - the list should contain objects in same order in which columns were added to the table. Also, the Class of the objects must match the corresponding column Classes

    update

    public void update(java.lang.String predicate,
                       java.util.List bindObjects,
                       java.util.Map nameValueMap)
    updates all table rows that satisfy the predicate, with the values specified in nameValueMap

    Parameters:
    predicate - - very similar to JDBC SQL statements

    example: "employeeSalary > ? and employeeProductivity < ?"

    bindObjects - - the objects to be used for the "?"s in the predicate.
    nameValueMap - - name of the column and corresponding value to use for the update

    delete

    public void delete(java.lang.String predicate,
                       java.util.List bindObjects)
    deletes all the rows that satisfy the given predicate

    Parameters:
    predicate - - very similar to JDBC SQL statements

    example: "employeeSalary > ? and employeeProductivity < ?"

    bindObjects - - the objects to be used for the "?"s in the predicate.

    addUniqueConstraint

    public void addUniqueConstraint(java.lang.String[] columnNames)
    makes sure that combination of the column values are unique. If the table is already populated with

    Parameters:
    columnNames - - the name of the columns to include in the constraint

    getRowCount

    public int getRowCount()
    Returns the number of rows in the model. A JTable uses this method to determine how many rows it should display. This method should be quick, as it is called frequently during rendering.

    Specified by:
    getRowCount in interface javax.swing.table.TableModel
    Returns:
    the number of rows in the model
    See Also:
    getColumnCount()

    getColumnCount

    public int getColumnCount()
    Returns the number of columns in the model. A JTable uses this method to determine how many columns it should create and display by default.

    Specified by:
    getColumnCount in interface javax.swing.table.TableModel
    Returns:
    the number of columns in the model
    See Also:
    getRowCount()

    getColumnName

    public java.lang.String getColumnName(int columnIndex)
    Returns the name of the column at columnIndex. This is used to initialize the table's column header name. Note: this name does not need to be unique; two columns in a table can have the same name.

    Specified by:
    getColumnName in interface javax.swing.table.TableModel
    Parameters:
    columnIndex - the index of the column
    Returns:
    the name of the column

    getColumnClass

    public java.lang.Class getColumnClass(int columnIndex)
    Returns the most specific superclass for all the cell values in the column. This is used by the JTable to set up a default renderer and editor for the column.

    Specified by:
    getColumnClass in interface javax.swing.table.TableModel
    Parameters:
    columnIndex - the index of the column
    Returns:
    the common ancestor class of the object values in the model.

    isCellEditable

    public boolean isCellEditable(int rowIndex,
                                  int columnIndex)
    Returns true if the cell at rowIndex and columnIndex is editable. Otherwise, setValueAt on the cell will not change the value of that cell.

    Specified by:
    isCellEditable in interface javax.swing.table.TableModel
    Parameters:
    rowIndex - the row whose value to be queried
    columnIndex - the column whose value to be queried
    Returns:
    true if the cell is editable
    See Also:
    setValueAt(java.lang.Object, int, int)

    getValueAt

    public java.lang.Object getValueAt(int rowIndex,
                                       int columnIndex)
    Returns the value for the cell at columnIndex and rowIndex.

    Specified by:
    getValueAt in interface javax.swing.table.TableModel
    Parameters:
    rowIndex - the row whose value is to be queried
    columnIndex - the column whose value is to be queried
    Returns:
    the value Object at the specified cell

    setValueAt

    public void setValueAt(java.lang.Object aValue,
                           int rowIndex,
                           int columnIndex)
    Sets the value in the cell at columnIndex and rowIndex to aValue.

    Specified by:
    setValueAt in interface javax.swing.table.TableModel
    Parameters:
    aValue - the new value
    rowIndex - the row whose value is to be changed
    columnIndex - the column whose value is to be changed
    See Also:
    getValueAt(int, int), isCellEditable(int, int)

    addTableModelListener

    public void addTableModelListener(javax.swing.event.TableModelListener l)
    Adds a listener to the list that is notified each time a change to the data model occurs.

    Specified by:
    addTableModelListener in interface javax.swing.table.TableModel
    Parameters:
    l - the TableModelListener

    removeTableModelListener

    public void removeTableModelListener(javax.swing.event.TableModelListener l)
    Removes a listener from the list that is notified each time a change to the data model occurs.

    Specified by:
    removeTableModelListener in interface javax.swing.table.TableModel
    Parameters:
    l - the TableModelListener