- java.lang.Object
-
- javax.swing.tree.DefaultTreeSelectionModel
-
- All Implemented Interfaces:
Serializable
,Cloneable
,TreeSelectionModel
- Direct Known Subclasses:
JTree.EmptySelectionModel
public class DefaultTreeSelectionModel extends Object implements Cloneable, Serializable, TreeSelectionModel
Default implementation of TreeSelectionModel. Listeners are notified whenever the paths in the selection change, not the rows. In order to be able to track row changes you may wish to become a listener for expansion events on the tree and test for changes from there.resetRowSelection is called from any of the methods that update the selected paths. If you subclass any of these methods to filter what is allowed to be selected, be sure and message
resetRowSelection
if you do not message super. Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans™ has been added to thejava.beans
package. Please seeXMLEncoder
.- See Also:
JTree
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected SwingPropertyChangeSupport
changeSupport
Used to messaged registered listeners.protected int
leadIndex
Index of the lead path in selection.protected TreePath
leadPath
Last path that was added.protected int
leadRow
Lead row.protected EventListenerList
listenerList
Event listener list.protected DefaultListSelectionModel
listSelectionModel
Handles maintaining the list selection model.protected RowMapper
rowMapper
Provides a row for a given path.protected TreePath[]
selection
Paths that are currently selected.static String
SELECTION_MODE_PROPERTY
Property name for selectionMode.protected int
selectionMode
Mode for the selection, will be either SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.-
Fields declared in interface javax.swing.tree.TreeSelectionModel
CONTIGUOUS_TREE_SELECTION, DISCONTIGUOUS_TREE_SELECTION, SINGLE_TREE_SELECTION
-
-
Constructor Summary
Constructors Constructor Description DefaultTreeSelectionModel()
Creates a new instance of DefaultTreeSelectionModel that is empty, with a selection mode of DISCONTIGUOUS_TREE_SELECTION.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addPropertyChangeListener(PropertyChangeListener listener)
Adds a PropertyChangeListener to the listener list.void
addSelectionPath(TreePath path)
Adds path to the current selection.void
addSelectionPaths(TreePath[] paths)
Adds paths to the current selection.void
addTreeSelectionListener(TreeSelectionListener x)
Adds x to the list of listeners that are notified each time the set of selected TreePaths changes.protected boolean
arePathsContiguous(TreePath[] paths)
Returns true if the paths are contiguous, or this object has no RowMapper.protected boolean
canPathsBeAdded(TreePath[] paths)
Used to test if a particular set ofTreePath
s can be added.protected boolean
canPathsBeRemoved(TreePath[] paths)
Returns true if the paths can be removed without breaking the continuity of the model.void
clearSelection()
Empties the current selection.Object
clone()
Returns a clone of this object with the same selection.protected void
fireValueChanged(TreeSelectionEvent e)
Notifies all listeners that are registered for tree selection events on this object.TreePath
getLeadSelectionPath()
Returns the last path that was added.int
getLeadSelectionRow()
Returns the lead selection index.<T extends EventListener>
T[]getListeners(Class<T> listenerType)
Returns an array of all the objects currently registered asFooListener
s upon this model.int
getMaxSelectionRow()
Returns the largest value obtained from the RowMapper for the current set of selected TreePaths.int
getMinSelectionRow()
Returns the smallest value obtained from the RowMapper for the current set of selected TreePaths.PropertyChangeListener[]
getPropertyChangeListeners()
Returns an array of all the property change listeners registered on thisDefaultTreeSelectionModel
.RowMapper
getRowMapper()
Returns the RowMapper instance that is able to map a TreePath to a row.int
getSelectionCount()
Returns the number of paths that are selected.int
getSelectionMode()
Returns the selection mode, one ofSINGLE_TREE_SELECTION
,DISCONTIGUOUS_TREE_SELECTION
orCONTIGUOUS_TREE_SELECTION
.TreePath
getSelectionPath()
Returns the first path in the selection.TreePath[]
getSelectionPaths()
Returns the selection.int[]
getSelectionRows()
Returns the selection in terms of rows.TreeSelectionListener[]
getTreeSelectionListeners()
Returns an array of all the tree selection listeners registered on this model.protected void
insureRowContinuity()
Makes sure the currently selectedTreePath
s are valid for the current selection mode.protected void
insureUniqueness()
This method is obsolete and its implementation is now a noop.boolean
isPathSelected(TreePath path)
Returns true if the path,path
, is in the current selection.boolean
isRowSelected(int row)
Returns true if the row identified byrow
is selected.boolean
isSelectionEmpty()
Returns true if the selection is currently empty.protected void
notifyPathChange(Vector<?> changedPaths, TreePath oldLeadSelection)
Deprecated.As of JDK version 1.7void
removePropertyChangeListener(PropertyChangeListener listener)
Removes a PropertyChangeListener from the listener list.void
removeSelectionPath(TreePath path)
Removes path from the selection.void
removeSelectionPaths(TreePath[] paths)
Removes paths from the selection.void
removeTreeSelectionListener(TreeSelectionListener x)
Removes x from the list of listeners that are notified each time the set of selected TreePaths changes.void
resetRowSelection()
Updates this object's mapping from TreePath to rows.void
setRowMapper(RowMapper newMapper)
Sets the RowMapper instance.void
setSelectionMode(int mode)
Sets the selection model, which must be one of SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.void
setSelectionPath(TreePath path)
Sets the selection to path.void
setSelectionPaths(TreePath[] pPaths)
Sets the selection.String
toString()
Returns a string that displays and identifies this object's properties.protected void
updateLeadIndex()
Updates the leadIndex instance variable.
-
-
-
Field Detail
-
SELECTION_MODE_PROPERTY
public static final String SELECTION_MODE_PROPERTY
Property name for selectionMode.- See Also:
- Constant Field Values
-
changeSupport
protected SwingPropertyChangeSupport changeSupport
Used to messaged registered listeners.
-
selection
protected TreePath[] selection
Paths that are currently selected. Will be null if nothing is currently selected.
-
listenerList
protected EventListenerList listenerList
Event listener list.
-
rowMapper
protected transient RowMapper rowMapper
Provides a row for a given path.
-
listSelectionModel
protected DefaultListSelectionModel listSelectionModel
Handles maintaining the list selection model. The RowMapper is used to map from a TreePath to a row, and the value is then placed here.
-
selectionMode
protected int selectionMode
Mode for the selection, will be either SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.
-
leadPath
protected TreePath leadPath
Last path that was added.
-
leadIndex
protected int leadIndex
Index of the lead path in selection.
-
leadRow
protected int leadRow
Lead row.
-
-
Method Detail
-
setRowMapper
public void setRowMapper(RowMapper newMapper)
Sets the RowMapper instance. This instance is used to determine the row for a particular TreePath.- Specified by:
setRowMapper
in interfaceTreeSelectionModel
- Parameters:
newMapper
- RowMapper to be set
-
getRowMapper
public RowMapper getRowMapper()
Returns the RowMapper instance that is able to map a TreePath to a row.- Specified by:
getRowMapper
in interfaceTreeSelectionModel
- Returns:
- the RowMapper instance that is able to map a TreePath to a row
-
setSelectionMode
public void setSelectionMode(int mode)
Sets the selection model, which must be one of SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION. If mode is not one of the defined value,DISCONTIGUOUS_TREE_SELECTION
is assumed.This may change the selection if the current selection is not valid for the new mode. For example, if three TreePaths are selected when the mode is changed to
SINGLE_TREE_SELECTION
, only one TreePath will remain selected. It is up to the particular implementation to decide what TreePath remains selected.Setting the mode to something other than the defined types will result in the mode becoming
DISCONTIGUOUS_TREE_SELECTION
.- Specified by:
setSelectionMode
in interfaceTreeSelectionModel
- Parameters:
mode
- selection mode to be set
-
getSelectionMode
public int getSelectionMode()
Returns the selection mode, one ofSINGLE_TREE_SELECTION
,DISCONTIGUOUS_TREE_SELECTION
orCONTIGUOUS_TREE_SELECTION
.- Specified by:
getSelectionMode
in interfaceTreeSelectionModel
- Returns:
- the current selection mode
-
setSelectionPath
public void setSelectionPath(TreePath path)
Sets the selection to path. If this represents a change, then the TreeSelectionListeners are notified. Ifpath
is null, this has the same effect as invokingclearSelection
.- Specified by:
setSelectionPath
in interfaceTreeSelectionModel
- Parameters:
path
- new path to select
-
setSelectionPaths
public void setSelectionPaths(TreePath[] pPaths)
Sets the selection. Whether the supplied paths are taken as the new selection depends upon the selection mode. If the supplied array isnull
, or empty, the selection is cleared. If the selection mode isSINGLE_TREE_SELECTION
, only the first path inpPaths
is used. If the selection mode isCONTIGUOUS_TREE_SELECTION
and the supplied paths are not contiguous, then only the first path inpPaths
is used. If the selection mode isDISCONTIGUOUS_TREE_SELECTION
, then all paths are used.All
null
paths inpPaths
are ignored.If this represents a change, all registered
TreeSelectionListener
s are notified.The lead path is set to the last unique path.
The paths returned from
getSelectionPaths
are in the same order as those supplied to this method.- Specified by:
setSelectionPaths
in interfaceTreeSelectionModel
- Parameters:
pPaths
- the new selection
-
addSelectionPath
public void addSelectionPath(TreePath path)
Adds path to the current selection. If path is not currently in the selection the TreeSelectionListeners are notified. This has no effect ifpath
is null.- Specified by:
addSelectionPath
in interfaceTreeSelectionModel
- Parameters:
path
- the new path to add to the current selection
-
addSelectionPaths
public void addSelectionPaths(TreePath[] paths)
Adds paths to the current selection. If any of the paths in paths are not currently in the selection the TreeSelectionListeners are notified. This has no effect ifpaths
is null.The lead path is set to the last element in
paths
.If the selection mode is
CONTIGUOUS_TREE_SELECTION
, and adding the new paths would make the selection discontiguous. Then two things can result: if the TreePaths inpaths
are contiguous, then the selection becomes these TreePaths, otherwise the TreePaths aren't contiguous and the selection becomes the first TreePath inpaths
.- Specified by:
addSelectionPaths
in interfaceTreeSelectionModel
- Parameters:
paths
- the new path to add to the current selection
-
removeSelectionPath
public void removeSelectionPath(TreePath path)
Removes path from the selection. If path is in the selection The TreeSelectionListeners are notified. This has no effect ifpath
is null.- Specified by:
removeSelectionPath
in interfaceTreeSelectionModel
- Parameters:
path
- the path to remove from the selection
-
removeSelectionPaths
public void removeSelectionPaths(TreePath[] paths)
Removes paths from the selection. If any of the paths in paths are in the selection the TreeSelectionListeners are notified. This has no effect ifpaths
is null.- Specified by:
removeSelectionPaths
in interfaceTreeSelectionModel
- Parameters:
paths
- the paths to remove from the selection
-
getSelectionPath
public TreePath getSelectionPath()
Returns the first path in the selection. This is useful if there if only one item currently selected.- Specified by:
getSelectionPath
in interfaceTreeSelectionModel
- Returns:
- the first path in the selection
-
getSelectionPaths
public TreePath[] getSelectionPaths()
Returns the selection.- Specified by:
getSelectionPaths
in interfaceTreeSelectionModel
- Returns:
- the selection
-
getSelectionCount
public int getSelectionCount()
Returns the number of paths that are selected.- Specified by:
getSelectionCount
in interfaceTreeSelectionModel
- Returns:
- the number of paths that are selected
-
isPathSelected
public boolean isPathSelected(TreePath path)
Returns true if the path,path
, is in the current selection.- Specified by:
isPathSelected
in interfaceTreeSelectionModel
- Parameters:
path
- the path to be loked for- Returns:
- whether the
path
is in the current selection
-
isSelectionEmpty
public boolean isSelectionEmpty()
Returns true if the selection is currently empty.- Specified by:
isSelectionEmpty
in interfaceTreeSelectionModel
- Returns:
- whether the selection is currently empty
-
clearSelection
public void clearSelection()
Empties the current selection. If this represents a change in the current selection, the selection listeners are notified.- Specified by:
clearSelection
in interfaceTreeSelectionModel
-
addTreeSelectionListener
public void addTreeSelectionListener(TreeSelectionListener x)
Adds x to the list of listeners that are notified each time the set of selected TreePaths changes.- Specified by:
addTreeSelectionListener
in interfaceTreeSelectionModel
- Parameters:
x
- the new listener to be added
-
removeTreeSelectionListener
public void removeTreeSelectionListener(TreeSelectionListener x)
Removes x from the list of listeners that are notified each time the set of selected TreePaths changes.- Specified by:
removeTreeSelectionListener
in interfaceTreeSelectionModel
- Parameters:
x
- the listener to remove
-
getTreeSelectionListeners
public TreeSelectionListener[] getTreeSelectionListeners()
Returns an array of all the tree selection listeners registered on this model.- Returns:
- all of this model's
TreeSelectionListener
s or an empty array if no tree selection listeners are currently registered - Since:
- 1.4
- See Also:
addTreeSelectionListener(javax.swing.event.TreeSelectionListener)
,removeTreeSelectionListener(javax.swing.event.TreeSelectionListener)
-
fireValueChanged
protected void fireValueChanged(TreeSelectionEvent e)
Notifies all listeners that are registered for tree selection events on this object.- Parameters:
e
- the event that characterizes the change- See Also:
addTreeSelectionListener(javax.swing.event.TreeSelectionListener)
,EventListenerList
-
getListeners
public <T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns an array of all the objects currently registered asFooListener
s upon this model.FooListener
s are registered using theaddFooListener
method.You can specify the
listenerType
argument with a class literal, such asFooListener.class
. For example, you can query aDefaultTreeSelectionModel
m
for its tree selection listeners with the following code:TreeSelectionListener[] tsls = (TreeSelectionListener[])(m.getListeners(TreeSelectionListener.class));
If no such listeners exist, this method returns an empty array.- Type Parameters:
T
- the listener type- Parameters:
listenerType
- the type of listeners requested- Returns:
- an array of all objects registered as
FooListener
s on this component, or an empty array if no such listeners have been added - Throws:
ClassCastException
- iflistenerType
doesn't specify a class or interface that implementsjava.util.EventListener
- Since:
- 1.3
- See Also:
getTreeSelectionListeners()
,getPropertyChangeListeners()
-
getSelectionRows
public int[] getSelectionRows()
Returns the selection in terms of rows. There is not necessarily a one-to-one mapping between theTreePath
s returned fromgetSelectionPaths
and this method. In particular, if aTreePath
is not viewable (theRowMapper
returns-1
for the row corresponding to theTreePath
), then the corresponding row is not included in the returned array. For example, if the selection consists of two paths,A
andB
, withA
at row10
, andB
not currently viewable, then this method returns an array with the single entry10
.- Specified by:
getSelectionRows
in interfaceTreeSelectionModel
- Returns:
- the selection in terms of rows
-
getMinSelectionRow
public int getMinSelectionRow()
Returns the smallest value obtained from the RowMapper for the current set of selected TreePaths. If nothing is selected, or there is no RowMapper, this will return -1.- Specified by:
getMinSelectionRow
in interfaceTreeSelectionModel
- Returns:
- the smallest value obtained from the RowMapper for the current set of selected TreePaths
-
getMaxSelectionRow
public int getMaxSelectionRow()
Returns the largest value obtained from the RowMapper for the current set of selected TreePaths. If nothing is selected, or there is no RowMapper, this will return -1.- Specified by:
getMaxSelectionRow
in interfaceTreeSelectionModel
- Returns:
- the largest value obtained from the RowMapper for the current set of selected TreePaths
-
isRowSelected
public boolean isRowSelected(int row)
Returns true if the row identified byrow
is selected.- Specified by:
isRowSelected
in interfaceTreeSelectionModel
- Parameters:
row
- row to check- Returns:
- whether the row is selected
-
resetRowSelection
public void resetRowSelection()
Updates this object's mapping from TreePath to rows. This should be invoked when the mapping from TreePaths to integers has changed (for example, a node has been expanded).You do not normally have to call this, JTree and its associated Listeners will invoke this for you. If you are implementing your own View class, then you will have to invoke this.
This will invoke
insureRowContinuity
to make sure the currently selected TreePaths are still valid based on the selection mode.- Specified by:
resetRowSelection
in interfaceTreeSelectionModel
-
getLeadSelectionRow
public int getLeadSelectionRow()
Returns the lead selection index. That is the last index that was added.- Specified by:
getLeadSelectionRow
in interfaceTreeSelectionModel
- Returns:
- the lead selection index
-
getLeadSelectionPath
public TreePath getLeadSelectionPath()
Returns the last path that was added. This may differ from the leadSelectionPath property maintained by the JTree.- Specified by:
getLeadSelectionPath
in interfaceTreeSelectionModel
- Returns:
- the last path that was added
-
addPropertyChangeListener
public void addPropertyChangeListener(PropertyChangeListener listener)
Adds a PropertyChangeListener to the listener list. The listener is registered for all properties.A PropertyChangeEvent will get fired when the selection mode changes.
- Specified by:
addPropertyChangeListener
in interfaceTreeSelectionModel
- Parameters:
listener
- the PropertyChangeListener to be added
-
removePropertyChangeListener
public void removePropertyChangeListener(PropertyChangeListener listener)
Removes a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties.- Specified by:
removePropertyChangeListener
in interfaceTreeSelectionModel
- Parameters:
listener
- the PropertyChangeListener to be removed
-
getPropertyChangeListeners
public PropertyChangeListener[] getPropertyChangeListeners()
Returns an array of all the property change listeners registered on thisDefaultTreeSelectionModel
.- Returns:
- all of this model's
PropertyChangeListener
s or an empty array if no property change listeners are currently registered - Since:
- 1.4
- See Also:
addPropertyChangeListener(java.beans.PropertyChangeListener)
,removePropertyChangeListener(java.beans.PropertyChangeListener)
-
insureRowContinuity
protected void insureRowContinuity()
Makes sure the currently selectedTreePath
s are valid for the current selection mode. If the selection mode isCONTIGUOUS_TREE_SELECTION
and aRowMapper
exists, this will make sure all the rows are contiguous, that is, when sorted all the rows are in order with no gaps. If the selection isn't contiguous, the selection is reset to contain the first set, when sorted, of contiguous rows.If the selection mode is
SINGLE_TREE_SELECTION
and more than one TreePath is selected, the selection is reset to contain the first path currently selected.
-
arePathsContiguous
protected boolean arePathsContiguous(TreePath[] paths)
Returns true if the paths are contiguous, or this object has no RowMapper.- Parameters:
paths
- array of paths to check- Returns:
- whether the paths are contiguous, or this object has no RowMapper
-
canPathsBeAdded
protected boolean canPathsBeAdded(TreePath[] paths)
Used to test if a particular set ofTreePath
s can be added. This will return true ifpaths
is null (or empty), or this object has no RowMapper, or nothing is currently selected, or the selection mode isDISCONTIGUOUS_TREE_SELECTION
, or adding the paths to the current selection still results in a contiguous set ofTreePath
s.- Parameters:
paths
- array ofTreePaths
to check- Returns:
- whether the particular set of
TreePaths
can be added
-
canPathsBeRemoved
protected boolean canPathsBeRemoved(TreePath[] paths)
Returns true if the paths can be removed without breaking the continuity of the model. This is rather expensive.- Parameters:
paths
- array ofTreePath
to check- Returns:
- whether the paths can be removed without breaking the continuity of the model
-
notifyPathChange
@Deprecated protected void notifyPathChange(Vector<?> changedPaths, TreePath oldLeadSelection)
Deprecated.As of JDK version 1.7Notifies listeners of a change in path. changePaths should contain instances of PathPlaceHolder.- Parameters:
changedPaths
- the vector of the changed pathsoldLeadSelection
- the old selection path
-
updateLeadIndex
protected void updateLeadIndex()
Updates the leadIndex instance variable.
-
insureUniqueness
protected void insureUniqueness()
This method is obsolete and its implementation is now a noop. It's still called by setSelectionPaths and addSelectionPaths, but only for backwards compatibility.
-
toString
public String toString()
Returns a string that displays and identifies this object's properties.
-
clone
public Object clone() throws CloneNotSupportedException
Returns a clone of this object with the same selection. This method does not duplicate selection listeners and property listeners.- Overrides:
clone
in classObject
- Returns:
- a clone of this instance.
- Throws:
CloneNotSupportedException
- never thrown by instances of this class- See Also:
Cloneable
-
-