org.jdesktop.swingx.treetable
Class FileSystemModel

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

public class FileSystemModel
extends AbstractTreeTableModel

A tree table model to simulate a file system.

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

A comparison of this class with SimpleFileSystemModel, shows that extending AbstractTreeTableModel is often easier than creating a model from scratch.

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
 
Fields inherited from class org.jdesktop.swingx.treetable.AbstractTreeTableModel
modelSupport, root
 
Constructor Summary
FileSystemModel()
          Creates a file system model using the root directory as the model root.
FileSystemModel(File root)
          Creates a file system model using the specified root.
 
Method Summary
 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 getIndexOfChild(Object parent, Object child)
          Returns the index of child in parent.
 File getRoot()
          Returns the root of the tree.
 Object getValueAt(Object node, int column)
          Returns the value for the node at columnIndex.
 boolean isLeaf(Object node)
          Returns true if node is a leaf.
 void setRoot(File root)
          Sets the root for this tree table model.
 
Methods inherited from class org.jdesktop.swingx.treetable.AbstractTreeTableModel
addTreeModelListener, getHierarchicalColumn, getTreeModelListeners, isCellEditable, removeTreeModelListener, setValueAt, valueForPathChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileSystemModel

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


FileSystemModel

public FileSystemModel(File root)
Creates a file system model using the specified root.

Parameters:
root - the root for this model; this may be different than the root directory for a file system.
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)).

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.

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
Overrides:
getColumnClass in class AbstractTreeTableModel
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()
Description copied from interface: TreeTableModel
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.

Returns:
the number of columns in the model
See Also:
TableModel.getColumnCount()

getColumnName

public String getColumnName(int column)
Description copied from class: AbstractTreeTableModel
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
Overrides:
getColumnName in class AbstractTreeTableModel
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)
Description copied from interface: TreeTableModel
Returns the value for the node at columnIndex. The node must be managed by this model. Unamanaged nodes should throw an IllegalArgumentException.

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)

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.

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
Overrides:
getRoot in class AbstractTreeTableModel
Returns:
the root of the tree

setRoot

public void setRoot(File root)
Sets the root for this tree table model. This method will notify listeners that a change has taken place.

Parameters:
root - the new root node to set

isLeaf

public boolean isLeaf(Object node)
Returns true if node is a leaf.

Specified by:
isLeaf in interface TreeModel
Overrides:
isLeaf in class AbstractTreeTableModel
Parameters:
node - a node in the tree, obtained from this data source
Returns:
true if node is a leaf