public class MultiSelectionInList<T> extends AbstractValueModel<List<? extends T>> implements ChangeListener
The major differences to SelectionInList are:
ExternalUpdate.signalExternalUpdate().
The MultiSelectionInList is based on a ValueModel that has a
List as value.
The value of the MultiSelectionInList is the same list as the base list.
Values can be selected and deselected using various methods in this class which result in a change to the selection index set and the selection values list. Alternatively the selected index set or the selected values list can be modified to change the selection.
When changes in the selected values list are registered (via our special list implementation), the content of the selected value list is checked as follows:
By default, the comparisons are made by reference, not by equals. That means that if you have a list of primitive objects, e.g. Integer, you must use the same instance of an integer in both the base list and in the selected values list, not simply any equal instance.
If you do want a comparison by equals or by any other Comparator,
you can explicitly set one using #setSelectionComparator
When changes in the selected index set are registered, the content of the selected value list is updated to reflect the changes. If an index outside the allowed range is added, an exception will be thrown.
When changes in the base model are registered, we try to keep the selection as similar as possible by value, i.e. we try to keep the same selected objects in the list, even if we have to change our index values.
Both the selected index set and the selected values list may be modified in content but not replaced in their value model.
| Modifier and Type | Field and Description |
|---|---|
static Comparator<Object> |
EQUALS_COMPARATOR
compares two objects and returns 0 when they are
Object.equals(Object). |
static Comparator<Object> |
IDENTITY_COMPARATOR
compares two objects and returns 0 when they are the same reference.
|
| Constructor and Description |
|---|
MultiSelectionInList(List<? extends T> pList)
Creates a new MultiSelectionInList with the given list
as our base.
|
MultiSelectionInList(ValueModel<List<? extends T>> pListHolder)
Creates a new MultiSelectionInList with the given value model
as our base.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clearSelection()
clears the selection and
makes the appropriate notifications.
|
int |
determineIndexInBaseList(Object pElement)
Checks if the same element is in the base list (by using
mSelectionComparator). |
void |
dispose()
Cleanup all resources: disconnect from any input sources (like
other ValueModel's ...), and remove all listeners.
|
protected List<? extends T> |
getBaseList() |
ValueModel<? extends SortedSet<Integer>> |
getSelectedIndexSetValueModel() |
ValueModel<? extends List<T>> |
getSelectedValuesHolder()
Returns the value model that contains the
List
with the selected values from the base list. |
List<? extends T> |
getValue()
Get the current list.
|
boolean |
isEditable()
delegates to our base model
Returns true if a setValue() is possible, and doesn't throw an
UnsupportedOperationException,
|
void |
selectAll()
selects all entries
and makes the appropriate notifications.
|
void |
setSelectedElements(Collection<? extends T> pSelectedValues)
Sets the selection to the given set of values.
|
void |
setSelectedIndexes(Collection<Integer> pSelectedIndexes)
Sets the selection to the given set of indexes.
|
void |
setSelectionComparator(Comparator<Object> pSelectionComparator)
sets the selection comparator to use when we try to match
a selected value to a value in the base list.
|
void |
setValue(List<? extends T> pValue,
boolean pIsSetForced)
Set a new value, this will fire a ChangeEvent if the new value
is different from the old value.
|
void |
stateChanged(ChangeEvent pEvent)
Invoked when the value model holding our list changed its state.
|
addChangeListener, booleanValue, checkDisposed, doubleValue, fireStateChanged, floatValue, getDependents, getName, intValue, isDisposed, isInSetValue, isSetForced, longValue, onChangeSend, removeChangeListener, retractInterestsFor, setInSetValue, setName, setObjectValue, setValue, setValue, setValue, setValue, setValue, setValue, setValueForced, signalExternalUpdate, stringValuepublic static final Comparator<Object> EQUALS_COMPARATOR
Object.equals(Object).
Otherwise -1 is returned (in violation of the Comparator.compare(Object, Object)
specification, but it doesn't matter since we use it only as ==0).
null values are only equal to other null values.public static final Comparator<Object> IDENTITY_COMPARATOR
Comparator.compare(Object, Object)
specification, but it doesn't matter since we use it only as ==0).public MultiSelectionInList(ValueModel<List<? extends T>> pListHolder)
pListHolder - the value model, must contain a List as valueIllegalArgumentException - if pListHolder is null or pListHolder.getValue()
is not null and not a Listpublic void setSelectionComparator(Comparator<Object> pSelectionComparator)
IDENTITY_COMPARATOR).
If you want a comparison by Object.equals(Object),
you can specify the EQUALS_COMPARATOR.pSelectionComparator - the comparator to use when matching selection values
to base list values, may be nullpublic boolean isEditable()
isEditable in interface ValueModel<List<? extends T>>public void dispose()
dispose in interface ValueModel<List<? extends T>>dispose in class AbstractValueModel<List<? extends T>>public void setValue(List<? extends T> pValue, boolean pIsSetForced)
setValue in interface ValueModel<List<? extends T>>pValue - the new list (null is o.k.)pIsSetForced - true if a forced setValue should be doneIllegalArgumentException - if pValue is neither a List nor nullpublic List<? extends T> getValue()
getValue in interface ValueModel<List<? extends T>>public void stateChanged(ChangeEvent pEvent)
stateChanged in interface ChangeListenerpEvent - a ChangeEvent object, not usedprotected List<? extends T> getBaseList()
mListHolder or null if the value is nullIllegalArgumentException - if the value is not null and not a Listpublic ValueModel<? extends SortedSet<Integer>> getSelectedIndexSetValueModel()
Set
with the Integers that correspond to the
selected indexes in the base list, sorted by index.
The ValueModel itself is not modifiable,
but the Set is actively working with
the MultiSelectionInList so that a notification
of changes is not necessary.
Note that the following restrictions apply:
Integers that are a valid index to the base list.
public ValueModel<? extends List<T>> getSelectedValuesHolder()
List
with the selected values from the base list.
The ValueModel itself is not modifiable,
but the List is actively working with
the MultiSelectionInList so that a notification
of changes is not necessary.
Note that the following restrictions apply:
mSelectionComparator)
will result in an exception.
mSelectionComparator,
we internally replace the reference in this list by the matching one from the base list.
List
with the selected values from the base list.public int determineIndexInBaseList(Object pElement)
mSelectionComparator).pElement - the element to checkpublic void selectAll()
public void clearSelection()
public void setSelectedIndexes(Collection<Integer> pSelectedIndexes)
pSelectedIndexes - a collection of Integers
which must be valid indexes for the base listpublic void setSelectedElements(Collection<? extends T> pSelectedValues)
pSelectedValues - a collection of Objects, must
have the same references as the base list or be equal using
the mSelectionComparator.Copyright © 2017 Jürgen Zeller (privat). All rights reserved.