javax.swing.tree

Class DefaultTreeSelectionModel

public class DefaultTreeSelectionModel extends Object implements Cloneable, Serializable, TreeSelectionModel

The implementation of the default tree selection model. The installed listeners are notified about the path and not the row changes. If you specifically need to track the row changes, register the listener for the expansion events.
Field Summary
protected SwingPropertyChangeSupportchangeSupport
Our Swing property change support.
protected intleadIndex
The index of the last added path.
protected TreePathleadPath
The path that has been added last.
protected intleadRow
The row of the last added path according to the RowMapper.
protected EventListenerListlistenerList
Our TreeSelectionListeners.
protected DefaultListSelectionModellistSelectionModel
The current listSelectionModel.
protected RowMapperrowMapper
The current RowMapper.
protected TreePath[]selection
The current selection.
protected intselectionMode
The current selection mode.
static StringSELECTION_MODE_PROPERTY
The name of the selection mode property.
Constructor Summary
DefaultTreeSelectionModel()
Constructs a new DefaultTreeSelectionModel.
Method Summary
voidaddPropertyChangeListener(PropertyChangeListener listener)
Adds a PropertyChangeListener object to this model.
voidaddSelectionPath(TreePath path)
Adds a path to the list of selected paths.
voidaddSelectionPaths(TreePath[] paths)
Adds the paths to the list of selected paths.
voidaddTreeSelectionListener(TreeSelectionListener listener)
Adds a TreeSelectionListener object to this model.
protected booleanarePathsContiguous(TreePath[] paths)
Returns true if the paths are contiguous (take subsequent rows in the diplayed tree view.
protected booleancanPathsBeAdded(TreePath[] paths)
Checks if the paths can be added.
protected booleancanPathsBeRemoved(TreePath[] paths)
Checks if the paths can be removed without breaking the continuity of the selection according to selectionMode.
voidclearSelection()
Removes all paths from the selection.
Objectclone()
Creates a clone of this DefaultTreeSelectionModel with the same selection.
protected voidfireValueChanged(TreeSelectionEvent event)
fireValueChanged
TreePathgetLeadSelectionPath()
getLeadSelectionPath
intgetLeadSelectionRow()
getLeadSelectionRow
<T extends EventListener> T[]getListeners(Class<T> listenerType)
Returns all added listeners of a special type.
intgetMaxSelectionRow()
Returns the largest row index from the selection.
intgetMinSelectionRow()
Returns the smallest row index from the selection.
PropertyChangeListener[]getPropertyChangeListeners()
Returns all added PropertyChangeListener objects.
RowMappergetRowMapper()
Returns the RowMapper that is currently used to map between paths and their rows.
intgetSelectionCount()
Returns the number of paths in the selection.
intgetSelectionMode()
Returns the current selection mode.
TreePathgetSelectionPath()
Returns the first path in the selection.
TreePath[]getSelectionPaths()
Returns the complete selection.
int[]getSelectionRows()
Returns the currently selected rows.
TreeSelectionListener[]getTreeSelectionListeners()
Returns all TreeSelectionListener added to this model.
protected voidinsureRowContinuity()
Makes sure the currently selected paths are valid according to the current selectionMode.
protected voidinsureUniqueness()
This method exists due historical reasons and returns without action (unless overridden).
booleanisPathSelected(TreePath path)
Checks if a given path is in the selection.
booleanisRowSelected(int row)
Checks if a particular row is selected.
booleanisSelectionEmpty()
Checks if the selection is empty.
protected voidnotifyPathChange(Vector vPaths, TreePath oldLeadSelection)
Notify the installed listeners that the given patches have changed.
voidremovePropertyChangeListener(PropertyChangeListener listener)
Removes a PropertyChangeListener object from this model.
voidremoveSelectionPath(TreePath path)
Removes the path from the selection.
voidremoveSelectionPaths(TreePath[] paths)
Removes the paths from the selection.
voidremoveTreeSelectionListener(TreeSelectionListener listener)
Removes a TreeSelectionListener object from this model.
voidresetRowSelection()
Updates the mappings from TreePaths to row indices.
voidsetRowMapper(RowMapper mapper)
Sets the RowMapper that should be used to map between paths and their rows.
voidsetSelectionMode(int mode)
Sets the current selection mode.
voidsetSelectionPath(TreePath path)
Sets this path as the only selection.
voidsetSelectionPaths(TreePath[] paths)
Sets the paths as selection.
StringtoString()
Returns a string that shows this object's properties.
protected voidupdateLeadIndex()
Updates the lead selection row number after changing the lead selection path.

Field Detail

changeSupport

protected SwingPropertyChangeSupport changeSupport
Our Swing property change support.

leadIndex

protected int leadIndex
The index of the last added path.

leadPath

protected TreePath leadPath
The path that has been added last.

leadRow

protected int leadRow
The row of the last added path according to the RowMapper.

listenerList

protected EventListenerList listenerList
Our TreeSelectionListeners.

listSelectionModel

protected DefaultListSelectionModel listSelectionModel
The current listSelectionModel.

rowMapper

protected transient RowMapper rowMapper
The current RowMapper.

selection

protected TreePath[] selection
The current selection.

selectionMode

protected int selectionMode
The current selection mode.

SELECTION_MODE_PROPERTY

public static final String SELECTION_MODE_PROPERTY
The name of the selection mode property.

Constructor Detail

DefaultTreeSelectionModel

public DefaultTreeSelectionModel()
Constructs a new DefaultTreeSelectionModel.

Method Detail

addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener listener)
Adds a PropertyChangeListener object to this model.

Parameters: listener the listener to add.

addSelectionPath

public void addSelectionPath(TreePath path)
Adds a path to the list of selected paths. This method checks if the path is already selected and doesn't add the same path twice. If this changes the selection the registered TreeSelectionListeners are notified. The lead path is changed to the added path. This also happen if the passed path was already selected before.

Parameters: path the path to add to the selection

addSelectionPaths

public void addSelectionPaths(TreePath[] paths)
Adds the paths to the list of selected paths. This method checks if the paths are already selected and doesn't add the same path twice. If this changes the selection the registered TreeSelectionListeners are notified.

Parameters: paths the paths to add to the selection

addTreeSelectionListener

public void addTreeSelectionListener(TreeSelectionListener listener)
Adds a TreeSelectionListener object to this model.

Parameters: listener the listener to add

arePathsContiguous

protected boolean arePathsContiguous(TreePath[] paths)
Returns true if the paths are contiguous (take subsequent rows in the diplayed tree view. The method returns true if we have no RowMapper assigned.

Parameters: paths the paths to check for continuity

Returns: true if the paths are contiguous or we have no RowMapper assigned

canPathsBeAdded

protected boolean canPathsBeAdded(TreePath[] paths)
Checks if the paths can be added. This returns true if:

canPathsBeRemoved

protected boolean canPathsBeRemoved(TreePath[] paths)
Checks if the paths can be removed without breaking the continuity of the selection according to selectionMode.

Parameters: paths the paths to check

Returns: true if the paths can be removed with respect to the selectionMode

clearSelection

public void clearSelection()
Removes all paths from the selection. Fire the unselection event.

clone

public Object clone()
Creates a clone of this DefaultTreeSelectionModel with the same selection. The cloned instance will have the same registered listeners, the listeners themselves will not be cloned. The selection will be cloned.

Returns: a copy of this DefaultTreeSelectionModel

Throws: CloneNotSupportedException should not be thrown here

fireValueChanged

protected void fireValueChanged(TreeSelectionEvent event)
fireValueChanged

Parameters: event the event to fire.

getLeadSelectionPath

public TreePath getLeadSelectionPath()
getLeadSelectionPath

Returns: TreePath

getLeadSelectionRow

public int getLeadSelectionRow()
getLeadSelectionRow

Returns: int

getListeners

public <T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns all added listeners of a special type.

Parameters: listenerType the listener type

Returns: an array of listeners

Since: 1.3

getMaxSelectionRow

public int getMaxSelectionRow()
Returns the largest row index from the selection.

Returns: the largest row index from the selection

getMinSelectionRow

public int getMinSelectionRow()
Returns the smallest row index from the selection.

Returns: the smallest row index from the selection

getPropertyChangeListeners

public PropertyChangeListener[] getPropertyChangeListeners()
Returns all added PropertyChangeListener objects.

Returns: an array of listeners.

Since: 1.4

getRowMapper

public RowMapper getRowMapper()
Returns the RowMapper that is currently used to map between paths and their rows.

Returns: the current RowMapper

See Also: RowMapper

getSelectionCount

public int getSelectionCount()
Returns the number of paths in the selection.

Returns: the number of paths in the selection

getSelectionMode

public int getSelectionMode()
Returns the current selection mode.

Returns: the current selection mode

See Also: DefaultTreeSelectionModel DefaultTreeSelectionModel DefaultTreeSelectionModel DefaultTreeSelectionModel

getSelectionPath

public TreePath getSelectionPath()
Returns the first path in the selection. This is especially useful when the selectionMode is {@link #SINGLE_TREE_SELECTION}.

Returns: the first path in the selection

getSelectionPaths

public TreePath[] getSelectionPaths()
Returns the complete selection.

Returns: the complete selection

getSelectionRows

public int[] getSelectionRows()
Returns the currently selected rows.

Returns: the currently selected rows

getTreeSelectionListeners

public TreeSelectionListener[] getTreeSelectionListeners()
Returns all TreeSelectionListener added to this model.

Returns: an array of listeners

Since: 1.4

insureRowContinuity

protected void insureRowContinuity()
Makes sure the currently selected paths are valid according to the current selectionMode. If the selectionMode is set to {@link #CONTIGUOUS_TREE_SELECTION} and the selection isn't contiguous then the selection is reset to the first set of contguous paths. If the selectionMode is set to {@link #SINGLE_TREE_SELECTION} and the selection has more than one path, the selection is reset to the contain only the first path.

insureUniqueness

protected void insureUniqueness()
This method exists due historical reasons and returns without action (unless overridden). For compatibility with the applications that override it, it is still called from the {@link #setSelectionPaths(TreePath[])} and {@link #addSelectionPaths(TreePath[])}.

isPathSelected

public boolean isPathSelected(TreePath path)
Checks if a given path is in the selection.

Parameters: path the path to check

Returns: true if the path is in the selection, false otherwise

isRowSelected

public boolean isRowSelected(int row)
Checks if a particular row is selected.

Parameters: row the index of the row to check

Returns: true if the row is in this selection, false otherwise

Throws: NullPointerException if the row mapper is not set (can only happen if the user has plugged in the custom incorrect TreeUI implementation.

isSelectionEmpty

public boolean isSelectionEmpty()
Checks if the selection is empty.

Returns: true if the selection is empty, false otherwise

notifyPathChange

protected void notifyPathChange(Vector vPaths, TreePath oldLeadSelection)
Notify the installed listeners that the given patches have changed. This method will call listeners if invoked, but it is not called from the implementation of this class.

Parameters: vPaths the vector of the changed patches oldLeadSelection the old selection index

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)
Removes a PropertyChangeListener object from this model.

Parameters: listener the listener to remove.

removeSelectionPath

public void removeSelectionPath(TreePath path)
Removes the path from the selection. If this changes the selection the registered TreeSelectionListeners are notified.

Parameters: path the path to remove

removeSelectionPaths

public void removeSelectionPaths(TreePath[] paths)
Removes the paths from the selection. If this changes the selection the registered TreeSelectionListeners are notified.

Parameters: paths the paths to remove

removeTreeSelectionListener

public void removeTreeSelectionListener(TreeSelectionListener listener)
Removes a TreeSelectionListener object from this model.

Parameters: listener the listener to remove

resetRowSelection

public void resetRowSelection()
Updates the mappings from TreePaths to row indices.

setRowMapper

public void setRowMapper(RowMapper mapper)
Sets the RowMapper that should be used to map between paths and their rows.

Parameters: mapper the RowMapper to set

See Also: RowMapper

setSelectionMode

public void setSelectionMode(int mode)
Sets the current selection mode. Possible values are {@link #SINGLE_TREE_SELECTION}, {@link #CONTIGUOUS_TREE_SELECTION} and {@link #DISCONTIGUOUS_TREE_SELECTION}.

Parameters: mode the selection mode to be set

See Also: DefaultTreeSelectionModel DefaultTreeSelectionModel DefaultTreeSelectionModel DefaultTreeSelectionModel

setSelectionPath

public void setSelectionPath(TreePath path)
Sets this path as the only selection. If this changes the selection the registered TreeSelectionListeners are notified.

Parameters: path the path to set as selection

setSelectionPaths

public void setSelectionPaths(TreePath[] paths)
Sets the paths as selection. This method checks for duplicates and removes them. If this changes the selection the registered TreeSelectionListeners are notified.

Parameters: paths the paths to set as selection

toString

public String toString()
Returns a string that shows this object's properties. The returned string lists the selected tree rows, if any.

Returns: a string that shows this object's properties

updateLeadIndex

protected void updateLeadIndex()
Updates the lead selection row number after changing the lead selection path.