Class DefaultListSelectModel<V>

  • Type Parameters:
    V - The type of values contained in the model.
    All Implemented Interfaces:
    com.globalmentor.beans.PropertyBindable, com.globalmentor.beans.PropertyConstrainable, com.globalmentor.model.MutableValued<V>, com.globalmentor.model.Valued<V>, ListListenable<V>, ListSelectModel<V>, Model, SelectModel<V>, ValueModel<V>, java.lang.Iterable<V>, java.util.Collection<V>, java.util.List<V>
    Direct Known Subclasses:
    AbstractListSelectTableModel

    public class DefaultListSelectModel<V>
    extends AbstractValueModel<V>
    implements ListSelectModel<V>
    The default implementation of a model for selecting one or more values from a list. The model is thread-safe, synchronized on itself. Any iteration over values should include synchronization on the instance of this class. This implementation has a default value of null.
    Author:
    Garret Wilson
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected class  DefaultListSelectModel.ValueState
      An encapsulation of the state of a value in the model.
    • Field Summary

      • Fields inherited from class com.globalmentor.beans.BoundPropertyObject

        NO_PROPERTY_CHANGE_LISTENERS, NO_VETOABLE_CHANGE_LISTENERS
    • Constructor Summary

      Constructors 
      Constructor Description
      DefaultListSelectModel​(java.lang.Class<V> valueClass)
      Constructs a list select model indicating the type of values it can hold, using a default multiple selection strategy.
      DefaultListSelectModel​(java.lang.Class<V> valueClass, ListSelectionPolicy<V> listSelectionStrategy)
      Constructs a list select model indicating the type of values it can hold.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, V value)  
      boolean add​(V value)
      boolean addAll​(int index, java.util.Collection<? extends V> collection)  
      boolean addAll​(java.util.Collection<? extends V> collection)  
      void addListListener​(ListListener<V> listListener)
      Adds a list listener.
      void addListSelectionListener​(ListSelectionListener<V> selectionListener)
      Adds a list selection listener.
      void addSelectedIndexes​(int... indexes)
      Adds a selection at the given indices.
      void clear()  
      void clearValue()
      Clears the value by setting the value to null, which may be invalid according to any installed validators.
      boolean contains​(java.lang.Object value)  
      boolean containsAll​(java.util.Collection<?> collection)  
      protected void fireListModified​(int index, V addedElement, V removedElement)
      Fires an event to all registered list listeners indicating the list was modified.
      protected void fireSelectionChanged​(java.lang.Integer addedIndex, java.lang.Integer removedIndex)
      Fires an event to all registered selection listeners indicating the selection changed.
      V get​(int index)  
      V getDefaultValue()  
      int getSelectedIndex()
      Determines the selected index.
      int[] getSelectedIndexes()
      Determines the selected indices.
      V getSelectedValue()
      Determines the selected value.
      V[] getSelectedValues()
      Determines the selected values.
      ListSelectionPolicy<V> getSelectionPolicy()  
      V getValue()  
      int indexOf​(java.lang.Object value)  
      boolean isEmpty()  
      boolean isIndexDisplayed​(int index)
      Determines the displayed status of a given index.
      boolean isIndexEnabled​(int index)
      Determines the enabled status of a given index.
      boolean isValueDisplayed​(V value)
      Determines the displayed status of the first occurrence of a given value.
      boolean isValueEnabled​(V value)
      Determines the enabled status of the first occurrence of a given value.
      java.util.Iterator<V> iterator()  
      int lastIndexOf​(java.lang.Object value)  
      java.util.ListIterator<V> listIterator()  
      java.util.ListIterator<V> listIterator​(int index)  
      protected void listModified​(int index, V addedElement, V removedElement)
      Called when the list is modified.
      V remove​(int index)  
      boolean remove​(java.lang.Object value)
      boolean removeAll​(java.util.Collection<?> collection)  
      void removeListListener​(ListListener<V> listListener)
      Removes a list listener.
      void removeListSelectionListener​(ListSelectionListener<V> selectionListener)
      Removes a list selection listener.
      void removeSelectedIndexes​(int... indexes)
      Removes a selection at the given indices.
      boolean replace​(V oldValue, V newValue)
      Replaces the first occurrence in the of the given value with its replacement.
      void resetValue()
      Resets the value to a default value, which may be invalid according to any installed validators.
      boolean retainAll​(java.util.Collection<?> collection)  
      V set​(int index, V value)  
      void setIndexDisplayed​(int index, boolean newDisplayed)
      Sets the displayed status of a given index.
      void setIndexEnabled​(int index, boolean newEnabled)
      Sets the enabled status of a given index.
      void setSelectedIndexes​(int... indexes)
      Sets the selected indices.
      void setSelectedValues​(V... values)
      Sets the selected values.
      void setValue​(V newValue)
      Sets the new value.
      void setValueDisplayed​(V value, boolean newDisplayed)
      Sets the displayed status of the first occurrence of a given value.
      void setValueEnabled​(V value, boolean newEnabled)
      Sets the enabled status of the first occurrence of a given value.
      int size()
      The list of value states, all access to which will be synchronized on this.
      java.util.List<V> subList​(int fromIndex, int toIndex)  
      java.lang.Object[] toArray()  
      <T> T[] toArray​(T[] array)  
      • Methods inherited from class com.globalmentor.beans.BoundPropertyObject

        addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, createPostponedPropertyChangeEvent, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, getForwardPropertyChangeListener, getPropertyChangeListeners, getPropertyChangeListeners, getPropertyChangeSupport, getRepeatPropertyChangeListener, getRepeatVetoableChangeListener, getVetoableChangeListeners, getVetoableChangeListeners, getVetoableChangeSupport, hasPropertyChangeListeners, hasVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.List

        equals, hashCode, replaceAll, sort, spliterator
      • Methods inherited from interface com.globalmentor.beans.PropertyBindable

        addPropertyChangeListener, addPropertyChangeListener, getPropertyChangeListeners, getPropertyChangeListeners, hasPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
      • Methods inherited from interface com.globalmentor.beans.PropertyConstrainable

        addVetoableChangeListener, addVetoableChangeListener, getVetoableChangeListeners, getVetoableChangeListeners, hasVetoableChangeListeners, removeVetoableChangeListener, removeVetoableChangeListener
    • Constructor Detail

      • DefaultListSelectModel

        public DefaultListSelectModel​(java.lang.Class<V> valueClass)
        Constructs a list select model indicating the type of values it can hold, using a default multiple selection strategy.
        Parameters:
        valueClass - The class indicating the type of values held in the model.
        Throws:
        java.lang.NullPointerException - if the given value class is null.
      • DefaultListSelectModel

        public DefaultListSelectModel​(java.lang.Class<V> valueClass,
                                      ListSelectionPolicy<V> listSelectionStrategy)
        Constructs a list select model indicating the type of values it can hold. The selection strategy is not added as a listener to this model but is rather notified manually so that the event won't be delayed and/or sent out of order
        Parameters:
        valueClass - The class indicating the type of values held in the model.
        listSelectionStrategy - The strategy for selecting values in the model.
        Throws:
        java.lang.NullPointerException - if the given value class and/or selection strategy is null.
    • Method Detail

      • getValue

        public V getValue()
        Specified by:
        getValue in interface com.globalmentor.model.Valued<V>
        Specified by:
        getValue in interface ValueModel<V>
        Returns:
        The input value, or null if there is no input value.
      • setValue

        public void setValue​(V newValue)
                      throws java.beans.PropertyVetoException
        Description copied from interface: ValueModel
        Sets the new value. This is a bound property that only fires a change event when the new value is different via the equals() method. If a validator is installed, the value will first be validated before the current value is changed. Validation always occurs if a validator is installed, even if the value is not changing. If the value change is vetoed by the installed validator, the validation exception will be accessible via Throwable.getCause().
        Specified by:
        setValue in interface com.globalmentor.model.MutableValued<V>
        Specified by:
        setValue in interface ValueModel<V>
        Parameters:
        newValue - The new value.
        Throws:
        java.beans.PropertyVetoException - if the provided value is not valid or the change has otherwise been vetoed.
        See Also:
        ValueModel.getValidator(), ValueModel.VALUE_PROPERTY
      • clearValue

        public void clearValue()
        Description copied from interface: ValueModel
        Clears the value by setting the value to null, which may be invalid according to any installed validators. No validation occurs.
        Specified by:
        clearValue in interface ValueModel<V>
        See Also:
        ValueModel.VALUE_PROPERTY
      • resetValue

        public void resetValue()
        Resets the value to a default value, which may be invalid according to any installed validators. No validation occurs.

        This implementation calls clearValue().

        Specified by:
        resetValue in interface ValueModel<V>
        See Also:
        clearValue()
      • size

        public int size()
        The list of value states, all access to which will be synchronized on this.
        Specified by:
        size in interface java.util.Collection<V>
        Specified by:
        size in interface java.util.List<V>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<V>
        Specified by:
        isEmpty in interface java.util.List<V>
      • contains

        public boolean contains​(java.lang.Object value)
        Specified by:
        contains in interface java.util.Collection<V>
        Specified by:
        contains in interface java.util.List<V>
      • iterator

        public java.util.Iterator<V> iterator()
        Specified by:
        iterator in interface java.util.Collection<V>
        Specified by:
        iterator in interface java.lang.Iterable<V>
        Specified by:
        iterator in interface java.util.List<V>
        Returns:
        A read-only iterator over the values in this model.
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<V>
        Specified by:
        toArray in interface java.util.List<V>
      • toArray

        public <T> T[] toArray​(T[] array)
        Specified by:
        toArray in interface java.util.Collection<V>
        Specified by:
        toArray in interface java.util.List<V>
      • add

        public boolean add​(V value)

        This version delegates to add(int, Object).

        Specified by:
        add in interface java.util.Collection<V>
        Specified by:
        add in interface java.util.List<V>
      • remove

        public boolean remove​(java.lang.Object value)

        This version delegates to remove(int).

        Specified by:
        remove in interface java.util.Collection<V>
        Specified by:
        remove in interface java.util.List<V>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> collection)
        Specified by:
        containsAll in interface java.util.Collection<V>
        Specified by:
        containsAll in interface java.util.List<V>
      • addAll

        public boolean addAll​(java.util.Collection<? extends V> collection)
        Specified by:
        addAll in interface java.util.Collection<V>
        Specified by:
        addAll in interface java.util.List<V>
      • addAll

        public boolean addAll​(int index,
                              java.util.Collection<? extends V> collection)
        Specified by:
        addAll in interface java.util.List<V>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> collection)
        Specified by:
        removeAll in interface java.util.Collection<V>
        Specified by:
        removeAll in interface java.util.List<V>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> collection)
        Specified by:
        retainAll in interface java.util.Collection<V>
        Specified by:
        retainAll in interface java.util.List<V>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<V>
        Specified by:
        clear in interface java.util.List<V>
      • get

        public V get​(int index)
        Specified by:
        get in interface java.util.List<V>
      • set

        public V set​(int index,
                     V value)
        Specified by:
        set in interface java.util.List<V>
      • add

        public void add​(int index,
                        V value)
        Specified by:
        add in interface java.util.List<V>
      • remove

        public V remove​(int index)
        Specified by:
        remove in interface java.util.List<V>
      • indexOf

        public int indexOf​(java.lang.Object value)
        Specified by:
        indexOf in interface java.util.List<V>
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object value)
        Specified by:
        lastIndexOf in interface java.util.List<V>
      • listIterator

        public java.util.ListIterator<V> listIterator()
        Specified by:
        listIterator in interface java.util.List<V>
      • listIterator

        public java.util.ListIterator<V> listIterator​(int index)
        Specified by:
        listIterator in interface java.util.List<V>
      • subList

        public java.util.List<V> subList​(int fromIndex,
                                         int toIndex)
        Specified by:
        subList in interface java.util.List<V>
      • replace

        public boolean replace​(V oldValue,
                               V newValue)
        Description copied from interface: SelectModel
        Replaces the first occurrence in the of the given value with its replacement. This method ensures that another thread does not change the model while the search and replace operation occurs.
        Specified by:
        replace in interface SelectModel<V>
        Parameters:
        oldValue - The value for which to search.
        newValue - The replacement value.
        Returns:
        Whether the operation resulted in a modification of the model.
      • getSelectedIndex

        public int getSelectedIndex()
        Description copied from interface: ListSelectModel
        Determines the selected index. If more than one index is selected, the lead selected index will be returned.
        Specified by:
        getSelectedIndex in interface ListSelectModel<V>
        Returns:
        The index currently selected, or -1 if no index is selected.
        See Also:
        SelectModel.getSelectedValue()
      • getSelectedValue

        public V getSelectedValue()
        Description copied from interface: SelectModel
        Determines the selected value. This method delegates to the selection strategy. If more than one value is selected, the lead selected value will be returned.
        Specified by:
        getSelectedValue in interface SelectModel<V>
        Returns:
        The value currently selected, or null if no value is currently selected.
      • getSelectedValues

        public V[] getSelectedValues()
        Description copied from interface: SelectModel
        Determines the selected values. This method delegates to the selection strategy.
        Specified by:
        getSelectedValues in interface SelectModel<V>
        Returns:
        The values currently selected.
      • setSelectedValues

        public void setSelectedValues​(V... values)
                               throws java.beans.PropertyVetoException
        Sets the selected values. If a value occurs more than one time in the model, all occurrences of the value will be selected. Values that do not occur in the select model will be ignored. If the value change is vetoed by the installed validator, the validation exception will be accessible via Throwable.getCause().
        Specified by:
        setSelectedValues in interface SelectModel<V>
        Parameters:
        values - The values to select.
        Throws:
        java.beans.PropertyVetoException - if the provided value is not valid or the change has otherwise been vetoed.
        See Also:
        setSelectedIndexes(int[])
      • isValueDisplayed

        public boolean isValueDisplayed​(V value)
        Description copied from interface: ListSelectModel
        Determines the displayed status of the first occurrence of a given value.
        Specified by:
        isValueDisplayed in interface ListSelectModel<V>
        Parameters:
        value - The value for which the displayed status is to be determined.
        Returns:
        true if the value is displayed, else false.
      • setValueDisplayed

        public void setValueDisplayed​(V value,
                                      boolean newDisplayed)
        Sets the displayed status of the first occurrence of a given value. This is a bound value state property.
        Specified by:
        setValueDisplayed in interface ListSelectModel<V>
        Parameters:
        value - The value to display.
        newDisplayed - Whether the value should be displayed.
        See Also:
        Displayable.DISPLAYED_PROPERTY
      • isIndexDisplayed

        public boolean isIndexDisplayed​(int index)
        Description copied from interface: ListSelectModel
        Determines the displayed status of a given index.
        Specified by:
        isIndexDisplayed in interface ListSelectModel<V>
        Parameters:
        index - The index of the value for which the displayed status is to be determined.
        Returns:
        true if the value at the given index is displayed, else false.
      • setIndexDisplayed

        public void setIndexDisplayed​(int index,
                                      boolean newDisplayed)
        Sets the displayed status of a given index. This is a bound value state property.
        Specified by:
        setIndexDisplayed in interface ListSelectModel<V>
        Parameters:
        index - The index of the value to display.
        newDisplayed - Whether the value at the given index should be displayed.
        Throws:
        java.lang.IndexOutOfBoundsException - if the given index is not within the range of the list.
        See Also:
        Displayable.DISPLAYED_PROPERTY
      • isValueEnabled

        public boolean isValueEnabled​(V value)
        Description copied from interface: ListSelectModel
        Determines the enabled status of the first occurrence of a given value.
        Specified by:
        isValueEnabled in interface ListSelectModel<V>
        Parameters:
        value - The value for which the enabled status is to be determined.
        Returns:
        true if the value is enabled, else false.
      • setValueEnabled

        public void setValueEnabled​(V value,
                                    boolean newEnabled)
        Sets the enabled status of the first occurrence of a given value. This is a bound value state property.
        Specified by:
        setValueEnabled in interface ListSelectModel<V>
        Parameters:
        value - The value to enable or disable.
        newEnabled - Whether the value should be enabled.
        See Also:
        Enableable.ENABLED_PROPERTY
      • isIndexEnabled

        public boolean isIndexEnabled​(int index)
        Description copied from interface: ListSelectModel
        Determines the enabled status of a given index.
        Specified by:
        isIndexEnabled in interface ListSelectModel<V>
        Parameters:
        index - The index of the value for which the enabled status is to be determined.
        Returns:
        true if the value at the given index is enabled, else false.
      • setIndexEnabled

        public void setIndexEnabled​(int index,
                                    boolean newEnabled)
        Sets the enabled status of a given index. This is a bound value state property.
        Specified by:
        setIndexEnabled in interface ListSelectModel<V>
        Parameters:
        index - The index of the value to enable or disable.
        newEnabled - Whether the value at the given index should be enabled.
        Throws:
        java.lang.IndexOutOfBoundsException - if the given index is not within the range of the list.
        See Also:
        Enableable.ENABLED_PROPERTY
      • listModified

        protected void listModified​(int index,
                                    V addedElement,
                                    V removedElement)
        Called when the list is modified. This method calls the method for notifying listeners that the list was modified.
        Parameters:
        index - The index at which an element was added and/or removed, or -1 if the index is unknown.
        addedElement - The element that was added to the list, or null if no element was added or it is unknown whether or which elements were added.
        removedElement - The element that was removed from the list, or null if no element was removed or it is unknown whether or which elements were removed.
        See Also:
        fireListModified(int, Object, Object)
      • fireListModified

        protected void fireListModified​(int index,
                                        V addedElement,
                                        V removedElement)
        Fires an event to all registered list listeners indicating the list was modified.
        Parameters:
        index - The index at which an element was added and/or removed, or -1 if the index is unknown.
        addedElement - The element that was added to the list, or null if no element was added or it is unknown whether or which elements were added.
        removedElement - The element that was removed from the list, or null if no element was removed or it is unknown whether or which elements were removed.
        See Also:
        ListListener, ListEvent
      • fireSelectionChanged

        protected void fireSelectionChanged​(java.lang.Integer addedIndex,
                                            java.lang.Integer removedIndex)
        Fires an event to all registered selection listeners indicating the selection changed.
        Parameters:
        addedIndex - The index that was added to the selection, or null if no index was added or it is unknown whether or which indices were added.
        removedIndex - The index that was removed from the list, or null if no index was removed or it is unknown whether or which indices were removed.
        See Also:
        ListSelectionListener, ListSelectionEvent