com.sas.storage.jdbc
Class JDBCToTreeModelAdapter

com.sas.storage.jdbc.JDBCToTreeModelAdapter
All Implemented Interfaces:
LevelDataInterface, com.sas.table.StaticColumnInfoInterface, com.sas.util.transforms.DisplayTransformInterface, javax.swing.tree.TreeModel
Direct Known Subclasses:
JDBCToTreeViewModelAdapter

public class JDBCToTreeModelAdapter
implements javax.swing.tree.TreeModel, LevelDataInterface

The JDBCToTreeModelAdapter allows JDBC data sources to be displayed in any viewer that requires a model of type javax.swing.tree.TreeModel, mainly being a javax.swing.JTree. The data source must have three specific columns within it: text, level, and index. These three columns must be designated in order for the levels within the tree to be displayed correctly.

The adapter requires a java.sql.Connection object and a query statement. They can be passed in on the constructor or set via methods. The setConnection() method will apply the connection object. The setQueryStatement() method will apply the SQL query statement. When non-null values for both connection and query statement have been applied, the adapter is initialized and ready to use.

The query statement must be a SQL query that is valid for the JDBC data source. This may include a blank statement, if valid.

The setTextColumnName method can be used to specify a single column from the result set that contains the values to be displayed in the tree nodes. If more control is desired on what is displayed in the tree nodes, then a column values transform can be set on the adapter, via the setColumnValuesTransform method. The retrieveAllColumnValues property must be set to true in order for the column values transform to be used. If a column values transform has been specified and the retrieveAllColumnValues is true, then the adapter will use the specified column values transform to transform the Map containing column names (all uppercased) mapped to column values into a single Object which is used in the tree node. If the retrieveAllColumnValues property is true, but no column values transform has been set, then the Object used in the tree node will be a Map containing the column names (all uppercased) mapped to the column values.

The adapter can be configured and used within a webAF project. Add a com.sas.storage.jdbc.JDBCConnection component to your project. Open the customizer and specify the values needed for your JDBC connection.These include driver name and URL. Now open the customizer for the adapter. Specify the value for JDBC Connection using the top combo box. The drop-down list will show all JDBC Connection objects that exist in your project. You can set the properties for query statement, the three designated columns, autoCommit, and read-only, as well.


Field Summary
protected  java.lang.String indexColumnName
          Column name of index column
protected  boolean inInit
          True if the adapter is in initializion to prevent re-initialization from taking place concurrently
protected  int initialLevelsRead
          Number of levels initially read
protected  java.lang.String levelColumnName
          Column name of level column
protected  javax.swing.event.EventListenerList listenerList
          List of listeners
protected  com.sas.util.transforms.TransformInterface nodeTransform
          The transform used to transform a row of column values into a Node to be added to the tree model
static java.lang.String RB_KEY
           
protected  java.lang.String textColumnName
          Column name of text column
protected  javax.swing.tree.DefaultTreeModel treeModel
          TreeModel to store node values
protected  java.lang.String whereClause
          The where clause applied for this particular node
 
Fields inherited from class com.sas.storage.jdbc.JDBCColumnValuesAdapter
columnValuesTransform, retrieveAllColumnValues
 
Fields inherited from class com.sas.storage.jdbc.JDBCAdapter
autoCommit, CLASS_NAME, columnInfoUsed, conn, designTime, displayTransform, forwardOnly, initialized, isRowCountKnown, LABEL, meta, NAME, printWarnings, queryRowCountDisabled, queryStatement, readOnly, result, resultSetRequeryUsed, resultSetType, resultSetUsed, retrieveNumericTypesAsDouble, rowCount, rowCountQueryString, rowNumber, spcs, stmt, trimUsed
 
Constructor Summary
JDBCToTreeModelAdapter()
          Default constructor
JDBCToTreeModelAdapter(java.sql.Connection conn, java.lang.String queryStatement)
          Constructor used to create an initialized adapter.
JDBCToTreeModelAdapter(java.sql.ResultSet result)
          Constructor used to connect an adapter directly to a previously created ResultSet.
 
Method Summary
 void addChildren(java.lang.Object parentNode, java.sql.ResultSet queryResult)
          Creates and adds children to the parentNode based on level, index, and text values in the specified resultSet.
 void addTreeModelListener(javax.swing.event.TreeModelListener l)
          Adds a listener for the TreeModelEvent posted after the tree changes.
 void fireCellEvent(int row, int column)
          Fire a specific event for a model signaling that one cell has changed.
 void fireModelEvent()
          Fire a default event for a model signaling the some aspect of the model has changed.
 void fireModelEvent(java.util.EventObject event)
          Fire a specific event for a model signaling that some aspect of the model has changed.
 java.lang.Object getChild(java.lang.Object parent, int index)
          Returns the child of parent at the index given in the parent's child array.
 int getChildCount(java.lang.Object parent)
          Returns the number of children of parent.
static com.sas.beans.ExtendedBeanInfo getExtendedBeanInfo()
          Returns information used by the com.sas.beans.Introspector to augment the automatically introspected information about this JDBCToTreeModelAdapter.
 java.lang.String getIndexColumnName()
          Returns the name of the column in the data sourcet that contains the index values.
 int getIndexOfChild(java.lang.Object parent, java.lang.Object child)
          Returns the index of child in parent.
 int getInitialLevelsRead()
          Returns the number of initial levels that should be read when the adapter initializes.
 java.lang.String getLevelColumnName()
          Returns the name of the column in the data source that contains the level values.
 com.sas.util.transforms.TransformInterface getNodeTransform()
          Retruns the transform that will be used to transform a Map containing column names (all uppercased) mapped to column values from a row in the result set into an IndexedMutableTreeNode.
 java.lang.Object getRoot()
          Returns the root of the tree.
 java.lang.String getTextColumnName()
          Returns the name of the column in the data source that contains the text values.
protected  void init()
          Creates the Statement, ResultSet, and ResultSetMetaData objects using the queryStatement whenever the initialized field is false.
 boolean isLeaf(java.lang.Object node)
          Returns true if node is a leaf.
protected  boolean readChildren(java.lang.Object node)
          Read the children for this node and load them into the tree model for future access if the node has not yet been read.
 void removeTreeModelListener(javax.swing.event.TreeModelListener l)
          Removes a listener previously added with addTreeModelListener().
 void setIndexColumnName(java.lang.String indexColumnName)
          Sets the name of the column in the data source that contains the index values.
 void setInitialLevelsRead(int initialLevelsRead)
          Sets the number of initial levels that should be read when the adapter initializes.
 void setLevelColumnName(java.lang.String levelColumnName)
          Sets the name of the column in the data source that contains the level values.
 void setNodeTransform(com.sas.util.transforms.TransformInterface nodeTransform)
          Sets the transform that will be used to transform a Map containing column names (all uppercased) mapped to column values from a row in the result set into an IndexedMutableTreeNode.
 void setTextColumnName(java.lang.String textColumnName)
          Sets the name of the column in the data source that contains the text values.
 void valueForPathChanged(javax.swing.tree.TreePath path, java.lang.Object newValue)
          Messaged when the user has altered the value for the item identified by path to newValue.
 
Methods inherited from class com.sas.storage.jdbc.JDBCColumnValuesAdapter
getColumnValuesMap, getColumnValuesTransform, isRetrieveAllColumnValues, retrieveColumnValueAt, retrieveValueAt, setColumnValuesTransform, setRetrieveAllColumnValues
 
Methods inherited from class com.sas.storage.jdbc.JDBCAdapter
addPropertyChangeListener, addPropertyChangeListener, calculateRowCount, close, doAbsolute, doCountColumns, doUpdateCell, finalize, getAutoCommit, getColumnInfo, getColumnInfoNames, getConnection, getDisplayTransform, getModel, getPrintWarnings, getQueryStatement, getResultSetConcurrency, getResultSetRowCount, getResultSetType, getRowCountByCountFunction, getRowCountQueryString, initializeData, isQueryRowCountDisabled, isReadOnly, isResultSetRequeryUsed, isRetrieveNumericTypesAsDouble, isTrimUsed, printSQLWarning, putColumnInfoUsed, putValueAt, refresh, removePropertyChangeListener, removePropertyChangeListener, retrieveColumnClass, retrieveColumnCount, retrieveColumnIndex, retrieveColumnInfoUsed, retrieveColumnLabel, retrieveValueAt, setAutoCommit, setConnection, setDisplayTransform, setModel, setPrintWarnings, setQueryRowCountDisabled, setQueryStatement, setReadOnly, setResultSetRequeryUsed, setResultSetType, setRetrieveNumericTypesAsDouble, setRowCountQueryString, setTrimUsed
 

Field Detail

RB_KEY

public static final java.lang.String RB_KEY
See Also:
Constant Field Values

listenerList

protected javax.swing.event.EventListenerList listenerList
List of listeners


initialLevelsRead

protected int initialLevelsRead
Number of levels initially read


levelColumnName

protected java.lang.String levelColumnName
Column name of level column


indexColumnName

protected java.lang.String indexColumnName
Column name of index column


textColumnName

protected java.lang.String textColumnName
Column name of text column


treeModel

protected javax.swing.tree.DefaultTreeModel treeModel
TreeModel to store node values


inInit

protected boolean inInit
True if the adapter is in initializion to prevent re-initialization from taking place concurrently


nodeTransform

protected com.sas.util.transforms.TransformInterface nodeTransform
The transform used to transform a row of column values into a Node to be added to the tree model


whereClause

protected java.lang.String whereClause
The where clause applied for this particular node

Constructor Detail

JDBCToTreeModelAdapter

public JDBCToTreeModelAdapter()
Default constructor


JDBCToTreeModelAdapter

public JDBCToTreeModelAdapter(java.sql.ResultSet result)
Constructor used to connect an adapter directly to a previously created ResultSet.

Parameters:
result - ResultSet to use.

JDBCToTreeModelAdapter

public JDBCToTreeModelAdapter(java.sql.Connection conn,
                              java.lang.String queryStatement)
Constructor used to create an initialized adapter.

Parameters:
conn - A java.sql.Connection object.
queryStatement - A SQL query that is valid for the database represented by conn.
Method Detail

addTreeModelListener

public void addTreeModelListener(javax.swing.event.TreeModelListener l)
Adds a listener for the TreeModelEvent posted after the tree changes.

Specified by:
addTreeModelListener in interface javax.swing.tree.TreeModel
Parameters:
l - The listener to add

removeTreeModelListener

public void removeTreeModelListener(javax.swing.event.TreeModelListener l)
Removes a listener previously added with addTreeModelListener().

Specified by:
removeTreeModelListener in interface javax.swing.tree.TreeModel
Parameters:
l - The listener to remove

getInitialLevelsRead

public int getInitialLevelsRead()
Returns the number of initial levels that should be read when the adapter initializes.

Returns:
The number of initial levels loaded

setInitialLevelsRead

public void setInitialLevelsRead(int initialLevelsRead)
Sets the number of initial levels that should be read when the adapter initializes.

Parameters:
initialLevelsRead - The number of levels that should be loaded By default, the initial number of levels read is three: one for the root, another for its children to be visible, and the third level to detect whether the children are leaf nodes. Setting initial values to any value less than one will read all levels.
In order for this method to run as expected, it must be called before all of the column names have been set.

getLevelColumnName

public java.lang.String getLevelColumnName()
Returns the name of the column in the data source that contains the level values.

Specified by:
getLevelColumnName in interface LevelDataInterface
Returns:
The name of column that contains the level values

setLevelColumnName

public void setLevelColumnName(java.lang.String levelColumnName)
Sets the name of the column in the data source that contains the level values.

Specified by:
setLevelColumnName in interface LevelDataInterface
Parameters:
levelColumnName - The name of column that contains level values

getIndexColumnName

public java.lang.String getIndexColumnName()
Returns the name of the column in the data sourcet that contains the index values.

Specified by:
getIndexColumnName in interface LevelDataInterface
Returns:
The name of column that contains the index values

setIndexColumnName

public void setIndexColumnName(java.lang.String indexColumnName)
Sets the name of the column in the data source that contains the index values.

Specified by:
setIndexColumnName in interface LevelDataInterface
Parameters:
indexColumnName - The name of column that contains index values

getTextColumnName

public java.lang.String getTextColumnName()
Returns the name of the column in the data source that contains the text values.

Specified by:
getTextColumnName in interface LevelDataInterface
Returns:
The name of column that contains the text values

setTextColumnName

public void setTextColumnName(java.lang.String textColumnName)
Sets the name of the column in the data source that contains the text values.

Specified by:
setTextColumnName in interface LevelDataInterface
Parameters:
textColumnName - The name of column that contains text values

init

protected void init()
Creates the Statement, ResultSet, and ResultSetMetaData objects using the queryStatement whenever the initialized field is false.

Overrides:
init in class JDBCAdapter
Throws:
java.lang.IllegalStateException - If the Connection or queryStatement are null.

addChildren

public void addChildren(java.lang.Object parentNode,
                        java.sql.ResultSet queryResult)
Creates and adds children to the parentNode based on level, index, and text values in the specified resultSet. The cursor of the resultSet should be positioned before the first row.

In order to be successfully added as a new child to the parentNode, the level value must be one greater than the level of the parentNode, and the index value must be greater than the parentNode's index and less than the parentNode's next relative's index.

Parameters:
parentNode - The node to which to add the new children

setNodeTransform

public void setNodeTransform(com.sas.util.transforms.TransformInterface nodeTransform)
Sets the transform that will be used to transform a Map containing column names (all uppercased) mapped to column values from a row in the result set into an IndexedMutableTreeNode. The input object into the transform method is the Map containing column names (all uppercased) mapped to column values from a row in the result set. The Object returned from the transform should be an IndexedMutableTreeNode.

Parameters:
nodeTransform - the transform used to transform a Map containing column names (all uppercased) mapped to column values from a row in the result set into an IndexedMutableTreeNode

getNodeTransform

public com.sas.util.transforms.TransformInterface getNodeTransform()
Retruns the transform that will be used to transform a Map containing column names (all uppercased) mapped to column values from a row in the result set into an IndexedMutableTreeNode.


readChildren

protected boolean readChildren(java.lang.Object node)
Read the children for this node and load them into the tree model for future access if the node has not yet been read.

Parameters:
node - The node in which the children should be read
Returns:
whether this node has successfully read in its children. False will be returned when the children have been previously read.

getChild

public java.lang.Object getChild(java.lang.Object parent,
                                 int index)
Returns the child of parent at the index given in the parent's child array. Parent must be a node previously obtained from this data source. This should not return null if index is a valid index for parent (that is index >= 0 && index < getChildCount(parent)).

Specified by:
getChild in interface javax.swing.tree.TreeModel
Parameters:
parent - A node in the tree, obtained from this data source
index - Valid index of parent
Returns:
The child of parent at given index

isLeaf

public boolean isLeaf(java.lang.Object node)
Returns true if node is a leaf. It is possible for this method to return false even if node has no children. A directory in a filesystem, for example, may contain no files; the node representing the directory is not a leaf, but it also has no children.

Specified by:
isLeaf in interface javax.swing.tree.TreeModel
Parameters:
node - A node in the tree, obtained from this data source
Returns:
True if node is a leaf

getChildCount

public int getChildCount(java.lang.Object parent)
Returns the number of children of parent. Returns 0 if the node is a leaf or if it has no children. parent must be a node previously obtained from this data source.

Specified by:
getChildCount in interface javax.swing.tree.TreeModel
Parameters:
parent - A node in the tree, obtained from this data source
Returns:
The number of children of the node parent

valueForPathChanged

public void valueForPathChanged(javax.swing.tree.TreePath path,
                                java.lang.Object newValue)
Messaged when the user has altered the value for the item identified by path to newValue. If newValue signifies a truly new value the model should post a treeNodesChanged event.

Specified by:
valueForPathChanged in interface javax.swing.tree.TreeModel
Parameters:
path - Path to the node that the user has altered.
newValue - The new value from the TreeCellEditor.

getIndexOfChild

public int getIndexOfChild(java.lang.Object parent,
                           java.lang.Object child)
Returns the index of child in parent.

Specified by:
getIndexOfChild in interface javax.swing.tree.TreeModel
Returns:
Index of child

getRoot

public java.lang.Object getRoot()
Returns the root of the tree. Returns null only if the tree has no nodes.

Specified by:
getRoot in interface javax.swing.tree.TreeModel
Returns:
The root of the tree

getExtendedBeanInfo

public static com.sas.beans.ExtendedBeanInfo getExtendedBeanInfo()
Returns information used by the com.sas.beans.Introspector to augment the automatically introspected information about this JDBCToTreeModelAdapter.


fireCellEvent

public void fireCellEvent(int row,
                          int column)
Fire a specific event for a model signaling that one cell has changed. Subclasses must override this method to fire specific events for their model.

Specified by:
fireCellEvent in class JDBCAdapter
Parameters:
row - row number of the cell that has been modified
column - column number of the cell that has been modified

fireModelEvent

public void fireModelEvent(java.util.EventObject event)
Fire a specific event for a model signaling that some aspect of the model has changed. Subclasses must override this method to fire specific events for their model.

Specified by:
fireModelEvent in class JDBCAdapter
Parameters:
event - Event that must be cast that should be fired

fireModelEvent

public void fireModelEvent()
Fire a default event for a model signaling the some aspect of the model has changed. Subclasses must override this method to fire a default events for their model.

Specified by:
fireModelEvent in class JDBCAdapter



Copyright © 2009 SAS Institute Inc. All Rights Reserved.