Class OptionsListModel<T>

  • All Implemented Interfaces:
    java.lang.Iterable<T>, java.util.Collection<T>, java.util.List<T>, javax.swing.ListModel<T>

    public class OptionsListModel<T>
    extends java.util.AbstractList<T>
    implements javax.swing.ListModel<T>
    Provides storage for a list of options with listeners. This class implements both List and ListModel (which ListModel really ought to do itself), and also provides methods to create some useful models like JComboBoxModel based on the same data.
    Author:
    Mark Taylor (Starlink)
    • Field Summary

      • Fields inherited from class java.util.AbstractList

        modCount
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(T obj)  
      void addListDataListener​(javax.swing.event.ListDataListener l)  
      void fireContentsChanged​(int i0, int i1)  
      void fireIntervalAdded​(int i0, int i1)  
      void fireIntervalRemoved​(int i0, int i1)  
      T get​(int index)  
      T getElementAt​(int index)  
      int getSize()  
      int idToIndex​(int id)
      Returns the index at which an option with the given ID currently resides.
      int indexToId​(int index)
      Returns the unique ID value for the option currently at a given index in this list.
      CheckBoxMenu makeCheckBoxMenu​(java.lang.String menuName)  
      javax.swing.JComboBox<T> makeComboBox()
      Makes a new JComboBox from this model.
      javax.swing.ComboBoxModel<T> makeComboBoxModel()
      Constructs a new ComboBoxModel backed by this list.
      javax.swing.JMenu makeJMenu​(java.lang.String menuName, javax.swing.Action menuAction)
      Constructs a new JMenu backed by this list.
      T remove​(int irow)  
      void removeListDataListener​(javax.swing.event.ListDataListener l)  
      T set​(int irow, T obj)  
      int size()  
      • Methods inherited from class java.util.AbstractList

        add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, removeRange, subList
      • Methods inherited from class java.util.AbstractCollection

        addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, 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

        addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
    • Constructor Detail

      • OptionsListModel

        public OptionsListModel()
    • Method Detail

      • get

        public T get​(int index)
        Specified by:
        get in interface java.util.List<T>
        Specified by:
        get in class java.util.AbstractList<T>
      • getElementAt

        public T getElementAt​(int index)
        Specified by:
        getElementAt in interface javax.swing.ListModel<T>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<T>
        Specified by:
        size in interface java.util.List<T>
        Specified by:
        size in class java.util.AbstractCollection<T>
      • getSize

        public int getSize()
        Specified by:
        getSize in interface javax.swing.ListModel<T>
      • add

        public boolean add​(T obj)
        Specified by:
        add in interface java.util.Collection<T>
        Specified by:
        add in interface java.util.List<T>
        Overrides:
        add in class java.util.AbstractList<T>
      • set

        public T set​(int irow,
                     T obj)
        Specified by:
        set in interface java.util.List<T>
        Overrides:
        set in class java.util.AbstractList<T>
      • remove

        public T remove​(int irow)
        Specified by:
        remove in interface java.util.List<T>
        Overrides:
        remove in class java.util.AbstractList<T>
      • indexToId

        public int indexToId​(int index)
        Returns the unique ID value for the option currently at a given index in this list. The ID value for a given option is a small integer which does not change. ID values are not re-used within a given instance of this class.
        Parameters:
        index - current index for option
        Returns:
        identifier for option
      • idToIndex

        public int idToIndex​(int id)
        Returns the index at which an option with the given ID currently resides. If no option with the given ID is present, -1 is returned.
        Parameters:
        id - unique identifier
        Returns:
        index of option with id, or -1
        See Also:
        indexToId(int)
      • makeComboBoxModel

        public javax.swing.ComboBoxModel<T> makeComboBoxModel()
        Constructs a new ComboBoxModel backed by this list.
        See Also:
        makeComboBox()
      • makeComboBox

        public javax.swing.JComboBox<T> makeComboBox()
        Makes a new JComboBox from this model. This adds to the functionality of makeComboBoxModel() by ensuring that the box is revalidated when new items are added to the model; otherwise the box can end up too small.

        Note however that no renderer is installed, so custom rendering must be as required handled by client code.

        Returns:
        a combo box from which items in this model can be selected
      • makeJMenu

        public javax.swing.JMenu makeJMenu​(java.lang.String menuName,
                                           javax.swing.Action menuAction)
        Constructs a new JMenu backed by this list. One entry is added to the menu for each option in this list; the menu item will be labelled by the list item (using its toString method) and will activate the supplied menuAction action if selected. In this case the action's actionPerformed method will be called with an ActionEvent that has an id corresponding to its position in this list and a command string which is the same as its toString method.
        Parameters:
        menuName - the name of the menu
        menuAction - the action to activate
      • makeCheckBoxMenu

        public CheckBoxMenu makeCheckBoxMenu​(java.lang.String menuName)
      • addListDataListener

        public void addListDataListener​(javax.swing.event.ListDataListener l)
        Specified by:
        addListDataListener in interface javax.swing.ListModel<T>
      • removeListDataListener

        public void removeListDataListener​(javax.swing.event.ListDataListener l)
        Specified by:
        removeListDataListener in interface javax.swing.ListModel<T>
      • fireContentsChanged

        public void fireContentsChanged​(int i0,
                                        int i1)
      • fireIntervalAdded

        public void fireIntervalAdded​(int i0,
                                      int i1)
      • fireIntervalRemoved

        public void fireIntervalRemoved​(int i0,
                                        int i1)