org.jdesktop.swingx.treetable
Class SimpleFileSystemModel

java.lang.Object
  extended by org.jdesktop.swingx.treetable.SimpleFileSystemModel
All Implemented Interfaces:
TreeModel, TreeTableModel

public class SimpleFileSystemModel
extends Object
implements TreeTableModel

A tree table model to simulate a file system.

This tree table model implementation does not extends AbstractTreeTableModel. The file system metaphor demonstrates that it is often easier to directly implement tree structures directly instead of using intermediaries, such as TreeNode.

It would be possible to create this same class by extending AbstractTreeTableModel, however the number of methods that you would need to override almost precludes that means of implementation.

A "full" version of this model might allow editing of file names, the deletion of files, and the movement of files. This simple implementation does not intend to tackle such problems, but this implementation may be extended to handle such details.


Field Summary
protected  EventListenerList listenerList
           
 
Constructor Summary
SimpleFileSystemModel()
          Creates a file system model, using the root directory as the model root.
SimpleFileSystemModel(File root)
          Creates a file system model, using the specified root as the model root.
 
Method Summary
 void addTreeModelListener(TreeModelListener l)
          Adds a listener for the TreeModelEvent posted after the tree changes.
 File getChild(Object parent, int index)
          Returns the child of parent at index index in the parent's child array.
 int getChildCount(Object parent)
          Returns the number of children of parent.
 Class<?> getColumnClass(int column)
          Returns the most specific superclass for all the cell values in the column.
 int getColumnCount()
          Returns the number of columns in the model.
 String getColumnName(int column)
          Returns the name of the column at columnIndex.
 int getHierarchicalColumn()
          Returns the column that is the "tree" column.
 int getIndexOfChild(Object parent, Object child)
          Returns the index of child in parent.
 File getRoot()
          Returns the root of the tree.
 TreeModelListener[] getTreeModelListeners()
          Gets a an array of all the listeners attached to this model.
 Object getValueAt(Object node, int column)
          Returns the value for the node at columnIndex.
 boolean isCellEditable(Object node, int column)
          Returns true if the cell for the node at columnIndex is editable.
 boolean isLeaf(Object node)
          Returns true if node is a leaf.
 void removeTreeModelListener(TreeModelListener l)
          Removes a listener previously added with addTreeModelListener.
 void setValueAt(Object value, Object node, int column)
          Sets the value for the node at columnIndex to value.
 void valueForPathChanged(TreePath path, Object newValue)
          Messaged when the user has altered the value for the item identified by path to newValue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

listenerList

protected EventListenerList listenerList
Constructor Detail

SimpleFileSystemModel

public SimpleFileSystemModel()
Creates a file system model, using the root directory as the model root.


SimpleFileSystemModel

public SimpleFileSystemModel(File root)
Creates a file system model, using the specified root as the model root.

Method Detail

getChild

public File getChild(Object parent,
                     int index)
Returns the child of parent at index index 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 TreeModel
Parameters:
parent - a node in the tree, obtained from this data source
Returns:
the child of parent at index index

getChildCount

public int getChildCount(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 TreeModel
Parameters:
parent - a node in the tree, obtained from this data source
Returns:
the number of children of the node parent

getColumnClass

public Class<?> getColumnClass(int column)
Returns the most specific superclass for all the cell values in the column. This is used by the JXTreeTable to set up a default renderer and editor for the column.

Specified by:
getColumnClass in interface TreeTableModel
Parameters:
column - the index of the column
Returns:
the common ancestor class of the object values in the model.
See Also:
TableModel.getColumnClass(int)

getColumnCount

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

Specified by:
getColumnCount in interface TreeTableModel
Returns:
the number of columns in the model
See Also:
TableModel.getColumnCount()

getColumnName

public String getColumnName(int column)
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 TreeTableModel
Parameters:
column - the index of the column
Returns:
the name of the column
See Also:
TableModel.getColumnName(int)

getValueAt

public Object getValueAt(Object node,
                         int column)
Returns the value for the node at columnIndex. The node must be managed by this model. Unamanaged nodes should throw an IllegalArgumentException.

Specified by:
getValueAt in interface TreeTableModel
Parameters:
node - the node whose value is to be queried
column - the column whose value is to be queried
Returns:
the value Object at the specified cell
See Also:
TreeTableModel.setValueAt(java.lang.Object, java.lang.Object, int), TableModel.getValueAt(int, int)

getHierarchicalColumn

public int getHierarchicalColumn()
Returns the column that is the "tree" column. While it is not required, most implementations will default the first column to be the hierarchical one.

Specified by:
getHierarchicalColumn in interface TreeTableModel
Returns:
the index of the hierarchical column or -1 if no column is the hierarchical column.

isCellEditable

public boolean isCellEditable(Object node,
                              int column)
Returns true if the cell for the node at columnIndex is editable. Otherwise, setValueAt on the cell will not change the value of that cell. The node must be managed by this model. Unamanaged nodes should throw an IllegalArgumentException.

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

setValueAt

public void setValueAt(Object value,
                       Object node,
                       int column)
Sets the value for the node at columnIndex to value. The node must be managed by this model. Unamanaged nodes should throw an IllegalArgumentException.

Specified by:
setValueAt in interface TreeTableModel
Parameters:
value - the new value
node - the node whose value is to be changed
column - the column whose value is to be changed
See Also:
TreeTableModel.getValueAt(java.lang.Object, int), TreeTableModel.isCellEditable(java.lang.Object, int), TableModel.setValueAt(Object, int, int)

addTreeModelListener

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

Specified by:
addTreeModelListener in interface TreeModel
Parameters:
l - the listener to add
See Also:
TreeModel.removeTreeModelListener(javax.swing.event.TreeModelListener)

getIndexOfChild

public int getIndexOfChild(Object parent,
                           Object child)
Returns the index of child in parent. If either parent or child is null, returns -1. If either parent or child don't belong to this tree model, returns -1.

Specified by:
getIndexOfChild in interface TreeModel
Parameters:
parent - a note in the tree, obtained from this data source
child - the node we are interested in
Returns:
the index of the child in the parent, or -1 if either child or parent are null or don't belong to this tree model

getRoot

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

Specified by:
getRoot in interface TreeModel
Returns:
the root of the tree

isLeaf

public boolean isLeaf(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 TreeModel
Parameters:
node - a node in the tree, obtained from this data source
Returns:
true if node is a leaf

removeTreeModelListener

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

Specified by:
removeTreeModelListener in interface TreeModel
Parameters:
l - the listener to remove
See Also:
TreeModel.addTreeModelListener(javax.swing.event.TreeModelListener)

valueForPathChanged

public void valueForPathChanged(TreePath path,
                                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 TreeModel
Parameters:
path - path to the node that the user has altered
newValue - the new value from the TreeCellEditor

getTreeModelListeners

public TreeModelListener[] getTreeModelListeners()
Gets a an array of all the listeners attached to this model.

Returns:
an array of listeners; this array is guaranteed to be non-null