Class DefaultEventSelectionModel<E>
- java.lang.Object
-
- ca.odell.glazedlists.swing.DefaultEventSelectionModel<E>
-
- All Implemented Interfaces:
AdvancedListSelectionModel<E>,javax.swing.ListSelectionModel
public final class DefaultEventSelectionModel<E> extends java.lang.Object implements AdvancedListSelectionModel<E>
AnDefaultEventSelectionModelis a class that performs two simulaneous services. It is aListSelectionModelto provide selection tracking for aJTable. It is also aEventListthat contains the table's selection.As elements are selected or deselected, the
EventListaspect of thisDefaultEventSelectionModelchanges. Changes to thatListwill change the sourceEventList. To modify only the selection, use theListSelectionModel's methods.Alongside
MULTIPLE_INTERVAL_SELECTION, thisListSelectionModelsupports an additional selection mode.MULTIPLE_INTERVAL_SELECTION_DEFENSIVEis a new selection mode. It is identical toMULTIPLE_INTERVAL_SELECTIONin every way but one. When a row is inserted immediately before a selected row in theMULTIPLE_INTERVAL_SELECTIONmode, it becomes selected. But in theMULTIPLE_INTERVAL_SELECTION_DEFENSIVEmode, it does not become selected. To set this mode, usesetSelectionMode(ListSelection.MULTIPLE_INTERVAL_SELECTION_DEFENSIVE).DefaultEventSelectionModelis not thread-safe. Unless otherwise noted, all methods are only safe to be called from the event dispatch thread. To do this programmatically, useSwingUtilities.invokeAndWait(Runnable)and wrap the source list (or some part of the source list's pipeline) using GlazedListsSwing#swingThreadProxyList(EventList).
-
-
Constructor Summary
Constructors Modifier Constructor Description DefaultEventSelectionModel(EventList<E> source)Creates a new selection model that also presents a list of the selection.protectedDefaultEventSelectionModel(EventList<E> source, boolean disposeSource)Creates a new selection model that also presents a list of the selection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListSelectionListener(javax.swing.event.ListSelectionListener listener)Add a listener to the list that's notified each time a change to the selection occurs.voidaddSelectionInterval(int index0, int index1)Change the selection to be the set union of the current selection and the indices between index0 and index1 inclusivevoidaddValidSelectionMatcher(Matcher<E> validSelectionMatcher)Add a matcher which decides when source elements are valid for selection.voidclearSelection()Change the selection to the empty set.voiddispose()Releases the resources consumed by thisAdvancedListSelectionModelso that it may eventually be garbage collected.intgetAnchorSelectionIndex()Return the first index argument from the most recent call to setSelectionInterval(), addSelectionInterval() or removeSelectionInterval().EventList<E>getDeselected()Gets anEventListthat contains only deselected values and modifies the source list on mutation.booleangetEnabled()Returns whether the EventSelectionModel is editable or not.intgetLeadSelectionIndex()Return the second index argument from the most recent call to setSelectionInterval(), addSelectionInterval() or removeSelectionInterval().intgetMaxSelectionIndex()Gets the index of the last selected element.intgetMinSelectionIndex()Gets the index of the first selected element.EventList<E>getSelected()Gets anEventListthat contains only selected values and modifies the source list on mutation.intgetSelectionMode()Returns the current selection mode.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.booleangetValueIsAdjusting()Returns true if the value is undergoing a series of changes.voidinsertIndexInterval(int index, int length, boolean before)Insert length indices beginning before/after index.voidinvertSelection()Inverts the current selection.booleanisSelectedIndex(int index)Returns true if the specified index is selected.booleanisSelectionEmpty()Returns true if no indices are selected.voidremoveIndexInterval(int index0, int index1)Remove the indices in the interval index0,index1 (inclusive) from the selection model.voidremoveListSelectionListener(javax.swing.event.ListSelectionListener listener)Remove a listener from the list that's notified each time a change to the selection occurs.voidremoveSelectionInterval(int index0, int index1)Change the selection to be the set difference of the current selection and the indices between index0 and index1 inclusive.voidremoveValidSelectionMatcher(Matcher<E> validSelectionMatcher)Remove a matcher which decides when source elements are valid for selection.voidsetAnchorSelectionIndex(int anchorSelectionIndex)Set the anchor selection index.voidsetEnabled(boolean enabled)Set the EventSelectionModel as editable or not.voidsetLeadSelectionIndex(int leadSelectionIndex)Set the lead selection index.voidsetSelectionInterval(int index0, int index1)Change the selection to be between index0 and index1 inclusive.voidsetSelectionMode(int selectionMode)Set the selection mode.voidsetValueIsAdjusting(boolean valueIsAdjusting)This property is true if upcoming changes to the value of the model should be considered a single event.
-
-
-
Constructor Detail
-
DefaultEventSelectionModel
public DefaultEventSelectionModel(EventList<E> source)
Creates a new selection model that also presents a list of the selection. TheDefaultEventSelectionModellistens to thisEventListin order to adjust selection when theEventListis modified. For example, when an element is added to theEventList, this may offset the selection of the following elements.- Parameters:
source- theEventListwhose selection will be managed. This should be the sameEventListpassed to the constructor of yourDefaultEventTableModelorDefaultEventListModel.
-
DefaultEventSelectionModel
protected DefaultEventSelectionModel(EventList<E> source, boolean disposeSource)
Creates a new selection model that also presents a list of the selection. TheDefaultEventSelectionModellistens to thisEventListin order to adjust selection when theEventListis modified. For example, when an element is added to theEventList, this may offset the selection of the following elements.- Parameters:
source- theEventListwhose selection will be managed. This should be the sameEventListpassed to the constructor of yourDefaultEventTableModelorDefaultEventListModel.diposeSource-trueif the source list should be disposed when disposing this model,falseotherwise
-
-
Method Detail
-
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.- Specified by:
getSelectedin interfaceAdvancedListSelectionModel<E>
-
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 anIllegalArgumentExceptionis thrown.- Specified by:
getTogglingSelectedin interfaceAdvancedListSelectionModel<E>
-
getDeselected
public EventList<E> getDeselected()
Gets anEventListthat contains only deselected values and modifies the source list on mutation. Adding and removing items from this list performs the same operation on the source list.- Specified by:
getDeselectedin interfaceAdvancedListSelectionModel<E>
-
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 anIllegalArgumentExceptionis thrown- Specified by:
getTogglingDeselectedin interfaceAdvancedListSelectionModel<E>
-
setEnabled
public void setEnabled(boolean enabled)
Set the EventSelectionModel as editable or not. This means that the user cannot manipulate the selection by clicking. The selection can still be changed as the source list changes.Note that this will also disable the selection from being modified programatically. Therefore you must use setEnabled(true) to modify the selection in code.
- Specified by:
setEnabledin interfaceAdvancedListSelectionModel<E>
-
getEnabled
public boolean getEnabled()
Returns whether the EventSelectionModel is editable or not.- Specified by:
getEnabledin interfaceAdvancedListSelectionModel<E>
-
invertSelection
public void invertSelection()
Inverts the current selection.- Specified by:
invertSelectionin interfaceAdvancedListSelectionModel<E>
-
setSelectionInterval
public void setSelectionInterval(int index0, int index1)Change the selection to be between index0 and index1 inclusive.First this calculates the smallest range where changes occur. This includes the union of the selection range before and the selection range specified. It then walks through the change and sets each index as selected or not based on whether the index is in the new range. Finally it fires events to both the listening lists and selection listeners about what changes happened.
If the selection does not change, this will not fire any events.
- Specified by:
setSelectionIntervalin interfacejavax.swing.ListSelectionModel
-
addSelectionInterval
public void addSelectionInterval(int index0, int index1)Change the selection to be the set union of the current selection and the indices between index0 and index1 inclusive- Specified by:
addSelectionIntervalin interfacejavax.swing.ListSelectionModel
-
removeSelectionInterval
public void removeSelectionInterval(int index0, int index1)Change the selection to be the set difference of the current selection and the indices between index0 and index1 inclusive.- Specified by:
removeSelectionIntervalin interfacejavax.swing.ListSelectionModel
-
isSelectedIndex
public boolean isSelectedIndex(int index)
Returns true if the specified index is selected. If the specified index has not been seen before, this will return false. This is in the case where the table painting and selection have fallen out of sync. Usually in this case there is an update event waiting in the event queue that notifies this model of the change in table size.- Specified by:
isSelectedIndexin interfacejavax.swing.ListSelectionModel
-
getAnchorSelectionIndex
public int getAnchorSelectionIndex()
Return the first index argument from the most recent call to setSelectionInterval(), addSelectionInterval() or removeSelectionInterval().- Specified by:
getAnchorSelectionIndexin interfacejavax.swing.ListSelectionModel
-
setAnchorSelectionIndex
public void setAnchorSelectionIndex(int anchorSelectionIndex)
Set the anchor selection index.- Specified by:
setAnchorSelectionIndexin interfacejavax.swing.ListSelectionModel
-
getLeadSelectionIndex
public int getLeadSelectionIndex()
Return the second index argument from the most recent call to setSelectionInterval(), addSelectionInterval() or removeSelectionInterval().- Specified by:
getLeadSelectionIndexin interfacejavax.swing.ListSelectionModel
-
setLeadSelectionIndex
public void setLeadSelectionIndex(int leadSelectionIndex)
Set the lead selection index.- Specified by:
setLeadSelectionIndexin interfacejavax.swing.ListSelectionModel
-
getMinSelectionIndex
public int getMinSelectionIndex()
Gets the index of the first selected element.- Specified by:
getMinSelectionIndexin interfacejavax.swing.ListSelectionModel
-
getMaxSelectionIndex
public int getMaxSelectionIndex()
Gets the index of the last selected element.- Specified by:
getMaxSelectionIndexin interfacejavax.swing.ListSelectionModel
-
clearSelection
public void clearSelection()
Change the selection to the empty set.- Specified by:
clearSelectionin interfacejavax.swing.ListSelectionModel
-
isSelectionEmpty
public boolean isSelectionEmpty()
Returns true if no indices are selected.- Specified by:
isSelectionEmptyin interfacejavax.swing.ListSelectionModel
-
insertIndexInterval
public void insertIndexInterval(int index, int length, boolean before)Insert length indices beginning before/after index.- Specified by:
insertIndexIntervalin interfacejavax.swing.ListSelectionModel
-
removeIndexInterval
public void removeIndexInterval(int index0, int index1)Remove the indices in the interval index0,index1 (inclusive) from the selection model.- Specified by:
removeIndexIntervalin interfacejavax.swing.ListSelectionModel
-
setValueIsAdjusting
public void setValueIsAdjusting(boolean valueIsAdjusting)
This property is true if upcoming changes to the value of the model should be considered a single event.- Specified by:
setValueIsAdjustingin interfacejavax.swing.ListSelectionModel
-
getValueIsAdjusting
public boolean getValueIsAdjusting()
Returns true if the value is undergoing a series of changes.- Specified by:
getValueIsAdjustingin interfacejavax.swing.ListSelectionModel
-
setSelectionMode
public void setSelectionMode(int selectionMode)
Set the selection mode.- Specified by:
setSelectionModein interfacejavax.swing.ListSelectionModel
-
getSelectionMode
public int getSelectionMode()
Returns the current selection mode.- Specified by:
getSelectionModein interfacejavax.swing.ListSelectionModel
-
addValidSelectionMatcher
public void addValidSelectionMatcher(Matcher<E> validSelectionMatcher)
Add a matcher which decides when source elements are valid for selection.- Specified by:
addValidSelectionMatcherin interfaceAdvancedListSelectionModel<E>- Parameters:
validSelectionMatcher- returns true if a source element can be selected; false otherwise
-
removeValidSelectionMatcher
public void removeValidSelectionMatcher(Matcher<E> validSelectionMatcher)
Remove a matcher which decides when source elements are valid for selection.- Specified by:
removeValidSelectionMatcherin interfaceAdvancedListSelectionModel<E>- Parameters:
validSelectionMatcher- returns true if a source element can be selected; false otherwise
-
addListSelectionListener
public void addListSelectionListener(javax.swing.event.ListSelectionListener listener)
Add a listener to the list that's notified each time a change to the selection occurs. Note that the change events fired by this class may include rows that have been removed from the table. For this reason it is advised not tofor()through the changed range without also verifying that each row is still in the table.- Specified by:
addListSelectionListenerin interfacejavax.swing.ListSelectionModel
-
removeListSelectionListener
public void removeListSelectionListener(javax.swing.event.ListSelectionListener listener)
Remove a listener from the list that's notified each time a change to the selection occurs.- Specified by:
removeListSelectionListenerin interfacejavax.swing.ListSelectionModel
-
dispose
public void dispose()
Releases the resources consumed by thisAdvancedListSelectionModelso that it may eventually be garbage collected.An
AdvancedListSelectionModelwill be garbage collected without a call toAdvancedListSelectionModel.dispose(), but not before its sourceEventListis garbage collected. By callingAdvancedListSelectionModel.dispose(), you allow theAdvancedListSelectionModelto be garbage collected before its sourceEventList. This is necessary for situations where anAdvancedListSelectionModelis short-lived but its sourceEventListis long-lived.Warning: It is an error to call any method on a
AdvancedListSelectionModelafter it has been disposed.- Specified by:
disposein interfaceAdvancedListSelectionModel<E>
-
-