org.josql.contrib
Class JoSQLSwingTableModel

java.lang.Object
  extended by org.josql.Query
      extended by org.josql.contrib.JoSQLSwingTableModel
All Implemented Interfaces:
TableModel

public class JoSQLSwingTableModel
extends Query
implements TableModel

A table model suitable for use with Swing JTable. This is basically just an extension to Query that allows the results to be iterated over, thereby providing the ability for objects to be reported on that are held in memory.

One limitation here is that the SQL query must return columns rather than the objects since the values need to be mapped by the renderer and editor. For example:

   SELECT lastModified,
          name
   FROM   java.io.File
   WHERE  name LIKE '%.html'
 

This query would work but it should be noted that the select "columns" (since they do not have aliases assigned) will be labeled 1, 2, X and so on. You can assign aliases to the "columns" and then use them in the report definition file.


Field Summary
 
Fields inherited from class org.josql.Query
ALL, ALL_OBJS_VAR_NAME, CURR_OBJ_VAR_NAME, GROUP_BY_RESULTS, GRPBY_OBJ_VAR_NAME, GRPBY_OBJ_VAR_NAME_SYNONYM, HAVING_RESULTS, INT_BIND_VAR_PREFIX, nullQueryList, ORDER_BY_ASC, ORDER_BY_DESC, PARENT_BIND_VAR_NAME, QUERY_BIND_VAR_NAME, RESULTS, WHERE_RESULTS
 
Constructor Summary
JoSQLSwingTableModel()
           
 
Method Summary
 void addTableModelListener(TableModelListener l)
           
 void clearResults()
          Clear any results.
 QueryResults execute(List l)
          Exectute the query and return the results.
 Class getColumnClass(int i)
          The expected class of the object at column i.
 int getColumnCount()
          Get the number of columns.
 String getColumnName(int ind)
          Get the name of the column, if the query has not yet been parsed then null is returned, if the column does not have an alias then "ind + 1" is returned.
 QueryResults getResults()
          Get any results, will be null unless execute(List) has been called.
 int getRowCount()
          Number of rows.
 Object getValueAt(int r, int c)
          Get the object at row r, column c.
 boolean isCellEditable(int r, int c)
          Cells are not editable since we do not store the results separately.
 void parse(String sql)
          Parse the SQL.
 void removeTableModelListener(TableModelListener l)
           
 QueryResults reorder(List objs, SortedMap dirs)
          Re-order the columns according to the column indices provided in dirs.
 QueryResults reorder(List objs, String orderBys)
          Re-order the columns according to the string representation provided by orderBys.
 void setValueAt(Object v, int r, int c)
          Not supported, always throws a: UnsupportedOperationException.
 
Methods inherited from class org.josql.Query
addBindVariableChangedListener, addFunctionHandler, addSaveValueChangedListener, addTiming, doExecuteOn, execute, execute, fireBindVariableChangedEvent, fireSaveValueChangedEvent, getAliases, getAllObjects, getAnonymousBindVariableName, getClassLoader, getColumns, getCurrentObject, getDefaultFunctionHandlers, getExecuteOnFunctions, getFrom, getFromObjectClass, getFunctionHandler, getFunctionHandlers, getGroupByColumns, getGroupBySaveValue, getGroupBySaveValues, getGroupByVariable, getHavingClause, getLimit, getOrderByColumns, getOrderByComparator, getParent, getQuery, getQueryResults, getSaveValue, getTopLevelQuery, getVariable, getVariable, getVariableClass, getVariables, getWhereClause, getWildcardCharacter, init, initOrderByComparator, isWantObjects, isWhereTrue, loadClass, parseAndExec, parsed, removeBindVariableChangedListener, removeSaveValueChangedListener, setAllObjects, setClassLoader, setClassName, setColumns, setCurrentGroupByObjects, setCurrentObject, setExecuteOnFunctions, setFrom, setFromObjectClass, setGroupByColumns, setGroupByLimit, setGroupByOrderColumns, setHaving, setLimit, setOrderByColumns, setParent, setSaveValue, setSaveValues, setVariable, setVariable, setVariables, setWantDistinctResults, setWantObjects, setWantTimings, setWhere, setWildcardCharacter, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JoSQLSwingTableModel

public JoSQLSwingTableModel()
Method Detail

parse

public void parse(String sql)
           throws QueryParseException
Parse the SQL. Note: this will cause a TableModelEvent to be fired to all registered listeners indicating that the table header has changed.

Overrides:
parse in class Query
Parameters:
sql - The SQL.
Throws:
QueryParseException - If the sql cannot be parsed or if the query will not return columns.

reorder

public QueryResults reorder(List objs,
                            SortedMap dirs)
                     throws QueryExecutionException,
                            QueryParseException
Re-order the columns according to the column indices provided in dirs.

Overrides:
reorder in class Query
Parameters:
objs - The objects to reorder.
dirs - The columns to order by.
Returns:
The results.
Throws:
QueryExecutionException - If something goes wrong during execution of the query.
QueryParseException - If the column indices are out of range for the statement.
See Also:
Query.reorder(List,SortedMap)

reorder

public QueryResults reorder(List objs,
                            String orderBys)
                     throws QueryParseException,
                            QueryExecutionException
Re-order the columns according to the string representation provided by orderBys.

Overrides:
reorder in class Query
Parameters:
objs - The objects to reorder.
orderBys - The columns to order by.
Returns:
The results.
Throws:
QueryExecutionException - If something goes wrong during execution of the query.
QueryParseException - If the column indices are out of range for the statement.
See Also:
Query.reorder(List,String)

execute

public QueryResults execute(List l)
                     throws QueryExecutionException
Exectute the query and return the results. A reference to the results is also held to allow them to be iterated over. Note: this will cause a TableModelEvent to be fired to all registered listeners indicating that ALL the table data has changed.

Overrides:
execute in class Query
Parameters:
l - The List of objects to execute the query on.
Returns:
The results.
Throws:
QueryExecutionException - If the query cannot be executed, or if the query is set to return objects rather than "columns".

getResults

public QueryResults getResults()
Get any results, will be null unless execute(List) has been called.

Returns:
The results.

clearResults

public void clearResults()
Clear any results.


getColumnName

public String getColumnName(int ind)
Get the name of the column, if the query has not yet been parsed then null is returned, if the column does not have an alias then "ind + 1" is returned.

Specified by:
getColumnName in interface TableModel
Returns:
The column name.

getColumnClass

public Class getColumnClass(int i)
The expected class of the object at column i.

Specified by:
getColumnClass in interface TableModel
Returns:
The class of the column.

getValueAt

public Object getValueAt(int r,
                         int c)
Get the object at row r, column c.

Specified by:
getValueAt in interface TableModel
Parameters:
r - The row.
c - The column.
Returns:
The object at that location.

setValueAt

public void setValueAt(Object v,
                       int r,
                       int c)
                throws UnsupportedOperationException
Not supported, always throws a: UnsupportedOperationException.

Specified by:
setValueAt in interface TableModel
Parameters:
v - The object to set at the location.
r - The row.
c - The column.
Throws:
UnsupportedOperationException - Not supported.

isCellEditable

public boolean isCellEditable(int r,
                              int c)
Cells are not editable since we do not store the results separately.

Specified by:
isCellEditable in interface TableModel
Parameters:
r - The row.
c - The columns.
Returns:
Always returns false.

getRowCount

public int getRowCount()
Number of rows.

Specified by:
getRowCount in interface TableModel
Returns:
The row count.

getColumnCount

public int getColumnCount()
Get the number of columns.

Specified by:
getColumnCount in interface TableModel
Returns:
The column count, returns 0 if the query has not yet been parsed.

removeTableModelListener

public void removeTableModelListener(TableModelListener l)
Specified by:
removeTableModelListener in interface TableModel

addTableModelListener

public void addTableModelListener(TableModelListener l)
Specified by:
addTableModelListener in interface TableModel


  Copyright © 2008 Gary Bentley. All Rights Reserved.