Class ComponentList<E extends ComponentProducer>

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Iterable<E>, Accessible, Scrollable

public class ComponentList<E extends ComponentProducer> extends JPanel implements Scrollable, Iterable<E>

The ComponentList class is similar to a Swing JList but with a simplified model and operations. It lays out a list of ComponentProducer objects from top to bottom and allows them to be selected, removed, added, and so on. A change in the list selection is signaled using a property change event given by SELECTION_PROPERTY.

The list may be placed inside a scrollable window. The scrolling behaviour works under the assumption that all components in the list have the same height. The user may set the number of visible components using the setVisibleRowCount() method.

Since:
3.1.7
Author:
Peter Hollemans
See Also:
  • Field Details

    • SELECTION_PROPERTY

      public static final String SELECTION_PROPERTY
      The selection property event used to signal changes in the selected elements.
      See Also:
  • Constructor Details

    • ComponentList

      public ComponentList()
      Creates a new empty component list.
  • Method Details

    • setSelectable

      public void setSelectable(boolean selectableFlag)
      Sets the selectable flag. When on, the list rows are selectable and change their background colour when selected. When off, rows are not selectable and keep their default background color even when clicked.
      Parameters:
      selectableFlag - the selectable flag, true if rows should be selectable or false if not.
    • clear

      public void clear()
      Clears the list of all components.
    • addElement

      public void addElement(E producer)
      Adds a new component to the end of the list.
      Parameters:
      producer - the component producer to add to the list.
    • addElement

      public void addElement(int index, E producer)
      Adds a new component at the specified index in the list.
      Parameters:
      index - the index to add the component at. The component is inserted at the index and all component indices after the inserted index are incremented.
      producer - the component producer to add to the list.
    • removeElement

      public boolean removeElement(E producer)
      Removes the specified component from the list.
      Parameters:
      producer - the component producer to remove.
      Returns:
      true if the component was removed, false otherwise.
    • removeElement

      public E removeElement(int index)
      Removes the component at the specified index.
      Parameters:
      index - the index of the component to remove.
      Returns:
      the removed component producer.
    • moveElement

      public void moveElement(int source, int dest)
      Moves the component between the specified indices.
      Parameters:
      source - the source index for the component to move.
      dest - the destination index to insert a new component. The destination index is relative to the indices of elements after the removal operation has happened.
    • getElements

      public int getElements()
      Gets the number of components in the list.
      Returns:
      the number of components.
    • getSelectedIndices

      public int[] getSelectedIndices()
      Gets the currently selected indices.
      Returns:
      the array of selected indices, possible zero length if no rows are selected.
    • setSelectionInterval

      public void setSelectionInterval(int start, int end)
      Sets the selection indices between the specified start and end inclusive.
      Parameters:
      start - the starting index for the selection.
      end - the ending index for selection.
      Throws:
      IndexOutOfBoundsException - if either index is out of bounds.
    • getElement

      public E getElement(int index)
      Gets the component at the specified index.
      Parameters:
      index - to index of the component producer to retrieve.
      Returns:
      the component producer at the specified index.
      Throws:
      IndexOutOfBoundsException - if the index is out of range.
    • iterator

      public Iterator<E> iterator()
      Gets an iterator over the component producers in the list.
      Specified by:
      iterator in interface Iterable<E extends ComponentProducer>
      Returns:
      the iterator over component producers.
    • indexOf

      public int indexOf(E producer)
      Gets the index of a component producer in the list.
      Parameters:
      producer - the component producer to search for.
      Returns:
      the index of the first occurrence of the producer, or -1 if this list does not contain the producer.
    • setVisibleRowCount

      public void setVisibleRowCount(int visibleRowCount)
      Sets the number of visible rows in this list. If the list is inside a scroll window, the preferred size of the viewport is set to display the specified number of rows. By default, the visible row count is 8.
      Parameters:
      visibleRowCount - the desired number of visible rows.
    • getVisibleRowCount

      public int getVisibleRowCount()
      Gets the visible rows in this list.
      Returns:
      the number of visible rows in the component list.
    • getPreferredScrollableViewportSize

      public Dimension getPreferredScrollableViewportSize()
      Specified by:
      getPreferredScrollableViewportSize in interface Scrollable
    • getScrollableBlockIncrement

      public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
      Specified by:
      getScrollableBlockIncrement in interface Scrollable
    • getScrollableTracksViewportHeight

      public boolean getScrollableTracksViewportHeight()
      Specified by:
      getScrollableTracksViewportHeight in interface Scrollable
    • getScrollableTracksViewportWidth

      public boolean getScrollableTracksViewportWidth()
      Specified by:
      getScrollableTracksViewportWidth in interface Scrollable
    • getScrollableUnitIncrement

      public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
      Specified by:
      getScrollableUnitIncrement in interface Scrollable
    • main

      public static void main(String[] argv)
      Tests this class.
      Parameters:
      argv - the array of command line parameters.