com.jgoodies.binding.adapter

Class SingleListSelectionAdapter

Implemented Interfaces:
ListSelectionModel

public final class SingleListSelectionAdapter
extends java.lang.Object
implements ListSelectionModel

A ListSelectionModel implementation that has the list index bound to a ValueModel. Therefore this class supports only the SINGLE_SELECTION mode where only one list index can be selected at a time. In this mode the setSelectionInterval and addSelectionInterval methods are equivalent, and only the second index argument (the "lead index") is used.

Example:

 SelectionInList selectionInList = new SelectionInList(...);
 JList list = new JList();
 list.setModel(selectionInList);
 list.setSelectionModel(new SingleListSelectionAdapter(
               selectionInList.getSelectionIndexHolder()));
 
Version:
$Revision: 1.9 $
Authors:
Karsten Lentzsch
Jeanette Winzenburg
See Also:
ValueModel, javax.swing.JList, javax.swing.JTable

Constructor Summary

SingleListSelectionAdapter(ValueModel selectionIndexHolder)
Constructs a SingleListSelectionAdapter with the given selection index holder.

Method Summary

void
addListSelectionListener(ListSelectionListener listener)
Add a listener to the list that's notified each time a change to the selection occurs.
void
addSelectionInterval(int index0, int index1)
Sets the selection interval using the given indices.
void
clearSelection()
Changes the selection to have no index selected.
int
getAnchorSelectionIndex()
Returns the selection index.
int
getLeadSelectionIndex()
Returns the selection index.
ListSelectionListener[]
getListSelectionListeners()
Returns an array of all the list selection listeners registered on this DefaultListSelectionModel.
int
getMaxSelectionIndex()
Returns the selection index.
int
getMinSelectionIndex()
Returns the selection index.
int
getSelectionMode()
Returns the fixed selection mode SINGLE_SELECTION.
boolean
getValueIsAdjusting()
Returns true if the value is undergoing a series of changes.
void
insertIndexInterval(int index, int length, boolean before)
Inserts length indices beginning before/after index.
boolean
isSelectedIndex(int index)
Checks and answers if the given index is selected or not.
boolean
isSelectionEmpty()
Returns true if no index is selected.
void
removeIndexInterval(int index0, int index1)
Remove the indices in the interval index0,index1 (inclusive) from the selection model.
void
removeListSelectionListener(ListSelectionListener listener)
Remove a listener from the list that's notified each time a change to the selection occurs.
void
removeSelectionInterval(int index0, int index1)
Clears the selection if it is equals to index0.
void
setAnchorSelectionIndex(int newSelectionIndex)
Sets the selection index.
void
setLeadSelectionIndex(int newSelectionIndex)
Sets the selection index.
void
setSelectionInterval(int index0, int index1)
Sets the selection index to index1.
void
setSelectionMode(int selectionMode)
Sets the selection mode.
void
setValueIsAdjusting(boolean newValueIsAdjusting)
This property is true if upcoming changes to the value of the model should be considered a single event.

Constructor Details

SingleListSelectionAdapter

public SingleListSelectionAdapter(ValueModel selectionIndexHolder)
Constructs a SingleListSelectionAdapter with the given selection index holder.
Parameters:
selectionIndexHolder - holds the selection index

Method Details

addListSelectionListener

public void addListSelectionListener(ListSelectionListener listener)
Add a listener to the list that's notified each time a change to the selection occurs.
Parameters:
listener - the ListSelectionListener

addSelectionInterval

public void addSelectionInterval(int index0,
                                 int index1)
Sets the selection interval using the given indices.

If this represents a change to the current selection, then notify each ListSelectionListener. Note that index0 doesn't have to be less than or equal to index1.

Parameters:
index0 - one end of the interval.
index1 - other end of the interval

clearSelection

public void clearSelection()
Changes the selection to have no index selected. If this represents a change to the current selection then notify each ListSelectionListener.

getAnchorSelectionIndex

public int getAnchorSelectionIndex()
Returns the selection index.
Returns:
the selection index

getLeadSelectionIndex

public int getLeadSelectionIndex()
Returns the selection index.
Returns:
the selection index

getListSelectionListeners

public ListSelectionListener[] getListSelectionListeners()
Returns an array of all the list selection listeners registered on this DefaultListSelectionModel.
Returns:
all of this model's ListSelectionListeners or an empty array if no list selection listeners are currently registered

getMaxSelectionIndex

public int getMaxSelectionIndex()
Returns the selection index.
Returns:
the selection index

getMinSelectionIndex

public int getMinSelectionIndex()
Returns the selection index.
Returns:
the selection index

getSelectionMode

public int getSelectionMode()
Returns the fixed selection mode SINGLE_SELECTION.
Returns:
SINGLE_SELECTION

getValueIsAdjusting

public boolean getValueIsAdjusting()
Returns true if the value is undergoing a series of changes.
Returns:
true if the value is currently adjusting

insertIndexInterval

public void insertIndexInterval(int index,
                                int length,
                                boolean before)
Inserts length indices beginning before/after index. If the value This method is typically called to synchronize the selection model with a corresponding change in the data model.
Parameters:
index - the index to start the insertion
length - the length of the inserted interval
before - true to insert before the start index

isSelectedIndex

public boolean isSelectedIndex(int index)
Checks and answers if the given index is selected or not.
Parameters:
index - the index to be checked
Returns:
true if selected, false if deselected
See Also:
javax.swing.ListSelectionModel.isSelectedIndex(int)

isSelectionEmpty

public boolean isSelectionEmpty()
Returns true if no index is selected.
Returns:
true if no index is selected

removeIndexInterval

public void removeIndexInterval(int index0,
                                int index1)
Remove the indices in the interval index0,index1 (inclusive) from the selection model. This is typically called to sync the selection model width a corresponding change in the data model.

Clears the selection if it is in the specified interval.

Parameters:
index0 - the first index to remove from the selection
index1 - the last index to remove from the selection

removeListSelectionListener

public void removeListSelectionListener(ListSelectionListener listener)
Remove a listener from the list that's notified each time a change to the selection occurs.
Parameters:
listener - the ListSelectionListener

removeSelectionInterval

public void removeSelectionInterval(int index0,
                                    int index1)
Clears the selection if it is equals to index0. Since this model supports only a single selection index, the index1 can be ignored for the selection.

If this represents a change to the current selection, then notify each ListSelectionListener. Note that index0 doesn't have to be less than or equal to index1.

Parameters:
index0 - one end of the interval.
index1 - other end of the interval

setAnchorSelectionIndex

public void setAnchorSelectionIndex(int newSelectionIndex)
Sets the selection index.
Parameters:
newSelectionIndex - the new selection index

setLeadSelectionIndex

public void setLeadSelectionIndex(int newSelectionIndex)
Sets the selection index.
Parameters:
newSelectionIndex - the new selection index

setSelectionInterval

public void setSelectionInterval(int index0,
                                 int index1)
Sets the selection index to index1. Since this model supports only a single selection index, the index0 is ignored. This is the behavior the DefaultListSelectionModel uses in single selection mode.

If this represents a change to the current selection, then notify each ListSelectionListener. Note that index0 doesn't have to be less than or equal to index1.

Parameters:
index0 - one end of the interval.
index1 - other end of the interval

setSelectionMode

public void setSelectionMode(int selectionMode)
Sets the selection mode. Only SINGLE_SELECTION is allowed in this implementation. Other modes are not supported and will throw an IllegalArgumentException.

With SINGLE_SELECTION only one list index can be selected at a time. In this mode the setSelectionInterval and addSelectionInterval methods are equivalent, and only the second index argument (the "lead index") is used.

Parameters:
selectionMode - the mode to be set
See Also:
getSelectionMode(), javax.swing.ListSelectionModel.setSelectionMode(int)

setValueIsAdjusting

public void setValueIsAdjusting(boolean newValueIsAdjusting)
This property is true if upcoming changes to the value of the model should be considered a single event. For example if the model is being updated in response to a user drag, the value of the valueIsAdjusting property will be set to true when the drag is initiated and be set to false when the drag is finished. This property allows listeners to to update only when a change has been finalized, rather than always handling all of the intermediate values.
Parameters:
newValueIsAdjusting - The new value of the property.

Copyright © 2002-2008 JGoodies Karsten Lentzsch. All Rights Reserved.