Package ca.odell.glazedlists
Class ListSelection<E>
- java.lang.Object
-
- ca.odell.glazedlists.ListSelection<E>
-
- All Implemented Interfaces:
ListEventListener<E>,java.util.EventListener
public class ListSelection<E> extends java.lang.Object implements ListEventListener<E>
A class to provide index-based selection features. This class maintains two lists derived from a singleEventList:Selected- anEventListthat contains only the selected values.Deselected- anEventListthat contains only the deselected values.
This design is intended to allow the sharing of selection logic between all supported GUI toolkits as well as being available for use in non-GUI applications and for index-based filtering.
Warning: This class is thread ready but not thread safe. See
EventListfor an example of thread safe code.- Author:
- Kevin Maltby
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceListSelection.ListenerA generic interface to respond to changes in selection that doesn't require including a particular GUI toolkit.
-
Field Summary
Fields Modifier and Type Field Description static intMULTIPLE_INTERVAL_SELECTIONA selection mode where any element may be selected and elements added adjacent to selected elements are selected.static intMULTIPLE_INTERVAL_SELECTION_DEFENSIVEA selection mode where any element may be selected and freshly added elements are always deselected.static intSINGLE_INTERVAL_SELECTIONA selection mode where at most one range of elements may be selected at one time.static intSINGLE_SELECTIONA selection mode where at most one element may be selected at one time.
-
Constructor Summary
Constructors Constructor Description ListSelection(EventList<E> source)Creates a new ListSelection that listens to changes on the given source.ListSelection(EventList<E> source, int[] initialSelection)Creates a new ListSelection that listens to changes on the given source and initializes selection with the given array of indices.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddSelectionListener(ListSelection.Listener selectionListener)Register aListenerthat will be notified when selection is changed.voidaddValidSelectionMatcher(Matcher<E> validSelectionMatcher)Add a matcher which decides when source elements are valid for selection.voiddeselect(int index)Deselects the element at the given index.voiddeselect(int[] indices)Deselects all of the elements in the given array of indices.voiddeselect(int start, int end)Deselects all of the elements within the given range.voiddeselectAll()Deselect all elements.voiddispose()Disposes of this ListSelection freeing up it's resources for garbage collection.intgetAnchorSelectionIndex()Return the anchor of the current selection.EventList<E>getDeselected()Gets anEventListthat contains only deselected values add modifies the source list on mutation.intgetLeadSelectionIndex()Return the lead of the current selection.intgetMaxSelectionIndex()Returns the last selected index or -1 if nothing is selected.intgetMinSelectionIndex()Returns the first selected index or -1 if nothing is selected.EventList<E>getSelected()Gets anEventListthat contains only selected values and modifies the source list on mutation.intgetSelectionMode()Returns the current selection mode.EventList<E>getSource()Get theEventListthat selection is being managed for.EventList<E>getTogglingDeselected()Gets anEventListthat contains only deselected values and modifies the selection state on mutation.EventList<E>getTogglingSelected()Gets anEventListthat contains only selected values and modifies the selection state on mutation.voidinvertSelection()Inverts the current selection.booleanisSelected(int sourceIndex)Returns whether or not the item with the given source index is selected.voidlistChanged(ListEvent<E> listChanges)Handle changes to the source list by adjusting our selection state and the contents of the selected and deselected lists.voidremoveSelectionListener(ListSelection.Listener selectionListener)Remove aListenerso that it will no longer be notified when selection changes.voidremoveValidSelectionMatcher(Matcher<E> validSelectionMatcher)Remove a matcher which decides when source elements are valid for selection.voidselect(int index)Selects the element at the given index.voidselect(int[] indices)Selects all of the elements in the given array of indices.voidselect(int start, int end)Selects all of the elements within the given range.intselect(E value)Select the specified element, if it exists.booleanselect(java.util.Collection<E> values)Select all of the specified values.voidselectAll()Selects all elements.voidsetAnchorSelectionIndex(int anchorSelectionIndex)Set the anchor selection index.voidsetLeadSelectionIndex(int leadSelectionIndex)Set the lead selection index.voidsetSelection(int index)Sets the selection to be only the element at the given index.voidsetSelection(int[] indices)Sets the selection to be only the element in the given array of indices.voidsetSelection(int start, int end)Sets the selection to be only elements within the given range.voidsetSelectionMode(int selectionMode)Set the selection mode.
-
-
-
Field Detail
-
SINGLE_SELECTION
public static final int SINGLE_SELECTION
A selection mode where at most one element may be selected at one time. For convenience, this value equalsListSelectionModel.SINGLE_SELECTION.- See Also:
- Constant Field Values
-
SINGLE_INTERVAL_SELECTION
public static final int SINGLE_INTERVAL_SELECTION
A selection mode where at most one range of elements may be selected at one time. For convenience, this value equalsListSelectionModel.SINGLE_INTERVAL_SELECTION.- See Also:
- Constant Field Values
-
MULTIPLE_INTERVAL_SELECTION
public static final int MULTIPLE_INTERVAL_SELECTION
A selection mode where any element may be selected and elements added adjacent to selected elements are selected. For convenience, this value equalsListSelectionModel.MULTIPLE_INTERVAL_SELECTION.- See Also:
- Constant Field Values
-
MULTIPLE_INTERVAL_SELECTION_DEFENSIVE
public static final int MULTIPLE_INTERVAL_SELECTION_DEFENSIVE
A selection mode where any element may be selected and freshly added elements are always deselected. No equivalent policy exists inListSelectionModel- See Also:
- Constant Field Values
-
-
Method Detail
-
listChanged
public void listChanged(ListEvent<E> listChanges)
Handle changes to the source list by adjusting our selection state and the contents of the selected and deselected lists.- Specified by:
listChangedin interfaceListEventListener<E>- Parameters:
listChanges- aListEventdescribing the changes to the list
-
addValidSelectionMatcher
public void addValidSelectionMatcher(Matcher<E> validSelectionMatcher)
Add a matcher which decides when source elements are valid for selection.- Parameters:
validSelectionMatcher- returns true if a source elements can be selected; false otherwise
-
removeValidSelectionMatcher
public void removeValidSelectionMatcher(Matcher<E> validSelectionMatcher)
Remove a matcher which decides when source elements are valid for selection.- Parameters:
validSelectionMatcher- returns true if a source elements can be selected; false otherwise
-
getSelected
public EventList<E> getSelected()
Gets anEventListthat contains only selected values and modifies the source list on mutation. Adding and removing items from this list performs the same operation on the source list.
-
getTogglingSelected
public EventList<E> getTogglingSelected()
Gets anEventListthat contains only selected values and modifies the selection state on mutation.Adding an item to this list selects it and removing an item deselects it. If an item not in the source list is added an
IllegalArgumentExceptionis thrown. This list does not support thesetmethod.
-
getDeselected
public EventList<E> getDeselected()
Gets anEventListthat contains only deselected values add modifies the source list on mutation. Adding and removing items from this list performs the same operation on the source list.
-
getTogglingDeselected
public EventList<E> getTogglingDeselected()
Gets anEventListthat contains only deselected values and modifies the selection state on mutation.Adding an item to this list deselects it and removing an item selects it. If an item not in the source list is added an
IllegalArgumentExceptionis thrown. This list does not support thesetmethod.
-
invertSelection
public void invertSelection()
Inverts the current selection.
-
isSelected
public boolean isSelected(int sourceIndex)
Returns whether or not the item with the given source index is selected.
-
deselect
public void deselect(int index)
Deselects the element at the given index.
-
deselect
public void deselect(int start, int end)Deselects all of the elements within the given range.
-
deselect
public void deselect(int[] indices)
Deselects all of the elements in the given array of indices. The array must contain indices in sorted, ascending order.
-
deselectAll
public void deselectAll()
Deselect all elements.
-
select
public void select(int index)
Selects the element at the given index.
-
select
public void select(int start, int end)Selects all of the elements within the given range.
-
select
public void select(int[] indices)
Selects all of the elements in the given array of indices. The array must contain indices in sorted, ascending order.
-
select
public int select(E value)
Select the specified element, if it exists.- Returns:
- the index of the newly selected element, or -1 if no element was found.
-
select
public boolean select(java.util.Collection<E> values)
Select all of the specified values.- Returns:
trueif the selection changed as a result of the call.
-
selectAll
public void selectAll()
Selects all elements.
-
setSelection
public void setSelection(int index)
Sets the selection to be only the element at the given index. If the given index is -1, the selection will be cleared.
-
setSelection
public void setSelection(int start, int end)Sets the selection to be only elements within the given range. If the endpoints of the range are -1, the selection will be cleared.
-
setSelection
public void setSelection(int[] indices)
Sets the selection to be only the element in the given array of indices. UnlikesetSelection(int)andsetSelection(int,int), providing a value of -1 is an error. The array must contain indices in sorted, ascending order.
-
getAnchorSelectionIndex
public int getAnchorSelectionIndex()
Return the anchor of the current selection.
-
setAnchorSelectionIndex
public void setAnchorSelectionIndex(int anchorSelectionIndex)
Set the anchor selection index.
-
getLeadSelectionIndex
public int getLeadSelectionIndex()
Return the lead of the current selection.
-
setLeadSelectionIndex
public void setLeadSelectionIndex(int leadSelectionIndex)
Set the lead selection index.
-
setSelectionMode
public void setSelectionMode(int selectionMode)
Set the selection mode.
-
getSelectionMode
public int getSelectionMode()
Returns the current selection mode.
-
getMinSelectionIndex
public int getMinSelectionIndex()
Returns the first selected index or -1 if nothing is selected.
-
getMaxSelectionIndex
public int getMaxSelectionIndex()
Returns the last selected index or -1 if nothing is selected.
-
addSelectionListener
public void addSelectionListener(ListSelection.Listener selectionListener)
Register aListenerthat will be notified when selection is changed.
-
removeSelectionListener
public void removeSelectionListener(ListSelection.Listener selectionListener)
Remove aListenerso that it will no longer be notified when selection changes.
-
dispose
public void dispose()
Disposes of this ListSelection freeing up it's resources for garbage collection. It is an error to use a ListSelection after dispose() has been called.
-
-