Class AbstractListBox<V,T extends AbstractListBox<V,T>>

Type Parameters:
V - Type of items this list box contains
T - Should always be itself, see AbstractComponent
All Implemented Interfaces:
Component, Interactable, TextGUIElement
Direct Known Subclasses:
ActionListBox, CheckBoxList, RadioBoxList

public abstract class AbstractListBox<V,T extends AbstractListBox<V,T>> extends AbstractInteractableComponent<T>
Base class for several list box implementations, this will handle things like list of items and the scrollbar.
  • Field Details

  • Constructor Details

    • AbstractListBox

      protected AbstractListBox()
      This constructor sets up the component so it has no preferred size but will ask to be as big as the list is. If the GUI cannot accommodate this size, scrolling and a vertical scrollbar will be used.
    • AbstractListBox

      protected AbstractListBox(TerminalSize size)
      This constructor sets up the component with a preferred size that is will always request, no matter what items are in the list box. If there are more items than the size can contain, scrolling and a vertical scrollbar will be used. Calling this constructor with a null value has the same effect as calling the default constructor.
      Parameters:
      size - Preferred size that the list should be asking for instead of invoking the preferred size calculation, or if set to null will ask to be big enough to display all items.
  • Method Details

    • createDefaultRenderer

      protected InteractableRenderer<T> createDefaultRenderer()
      Description copied from class: AbstractComponent
      When you create a custom component, you need to implement this method and return a Renderer which is responsible for taking care of sizing the component, rendering it and choosing where to place the cursor (if Interactable). This value is intended to be overridden by custom themes.
      Specified by:
      createDefaultRenderer in class AbstractInteractableComponent<T extends AbstractListBox<V,T>>
      Returns:
      Renderer to use when sizing and drawing this component
    • createDefaultListItemRenderer

      protected AbstractListBox.ListItemRenderer<V,T> createDefaultListItemRenderer()
      Method that constructs the ListItemRenderer that this list box should use to draw the elements of the list box. This can be overridden to supply a custom renderer. Note that this is not the renderer used for the entire list box but for each item, called one by one.
      Returns:
      ListItemRenderer to use when drawing the items in the list
    • getListItemRenderer

      AbstractListBox.ListItemRenderer<V,T> getListItemRenderer()
    • setListItemRenderer

      public T setListItemRenderer(AbstractListBox.ListItemRenderer<V,T> listItemRenderer)
      This method overrides the ListItemRenderer that is used to draw each element in the list box. Note that this is not the renderer used for the entire list box but for each item, called one by one.
      Parameters:
      listItemRenderer - New renderer to use when drawing the items in the list box
      Returns:
      Itself
    • handleKeyStroke

      public Interactable.Result handleKeyStroke(KeyStroke keyStroke)
      Description copied from class: AbstractInteractableComponent
      This method can be overridden to handle various user input (mostly from the keyboard) when this component is in focus. The input method from the interface, handleInput(..) is final in AbstractInteractableComponent to ensure the input filter is properly handled. If the filter decides that this event should be processed, it will call this method.
      Overrides:
      handleKeyStroke in class AbstractInteractableComponent<T extends AbstractListBox<V,T>>
      Parameters:
      keyStroke - What input was entered by the user
      Returns:
      Result of processing the key-stroke
    • getIndexByMouseAction

      protected int getIndexByMouseAction(MouseAction click)
      By converting TerminalPositions to AbstractComponent.toGlobal(TerminalPosition) gets index clicked on by mouse action.
      Returns:
      index of a item that was clicked on with MouseAction
    • selectByCharacter

      private boolean selectByCharacter(Character character)
    • afterEnterFocus

      protected void afterEnterFocus(Interactable.FocusChangeDirection direction, Interactable previouslyInFocus)
      Description copied from class: AbstractInteractableComponent
      Called by AbstractInteractableComponent automatically after this component has received input focus. You can override this method if you need to trigger some action based on this.
      Overrides:
      afterEnterFocus in class AbstractInteractableComponent<T extends AbstractListBox<V,T>>
      Parameters:
      direction - How focus was transferred, keep in mind this is from the previous component's point of view so if this parameter has value DOWN, focus came in from above
      previouslyInFocus - Which interactable component had focus previously
    • addItem

      public T addItem(V item)
      Adds one more item to the list box, at the end.
      Parameters:
      item - Item to add to the list box
      Returns:
      Itself
    • removeItem

      public V removeItem(int index)
      Removes an item from the list box by its index. The current selection in the list box will be adjusted accordingly.
      Parameters:
      index - Index of the item to remove
      Returns:
      The item that was removed
      Throws:
      IndexOutOfBoundsException - if the index is out of bounds in regards to the list of items
    • clearItems

      public T clearItems()
      Removes all items from the list box
      Returns:
      Itself
    • isFocusable

      public boolean isFocusable()
      Description copied from interface: Interactable
      Returns true if this interactable component is currently able to receive input focus. This is similar but different from Interactable.isEnabled(), which tells lanterna that the entire component is disabled when it is returning false, compared to this method which simply claims that the component is currently not ready to handle input. The AbstractInteractableComponent implementation always return true here but for example the list box components will override and return false here if they are empty. Note that you can still programmatically force input focus onto the component, returning false here won't prevent that.
      Specified by:
      isFocusable in interface Interactable
      Overrides:
      isFocusable in class AbstractInteractableComponent<T extends AbstractListBox<V,T>>
      Returns:
      true if this component wants to receive input focus, false otherwise.
    • indexOf

      public int indexOf(V item)
      Looks for the particular item in the list and returns the index within the list (starting from zero) of that item if it is found, or -1 otherwise
      Parameters:
      item - What item to search for in the list box
      Returns:
      Index of the item in the list box or -1 if the list box does not contain the item
    • getItemAt

      public V getItemAt(int index)
      Retrieves the item at the specified index in the list box
      Parameters:
      index - Index of the item to fetch
      Returns:
      The item at the specified index
      Throws:
      IndexOutOfBoundsException - If the index is less than zero or equals/greater than the number of items in the list box
    • isEmpty

      public boolean isEmpty()
      Checks if the list box has no items
      Returns:
      true if the list box has no items, false otherwise
    • getItemCount

      public int getItemCount()
      Returns the number of items currently in the list box
      Returns:
      Number of items in the list box
    • getItems

      public List<V> getItems()
      Returns a copy of the items in the list box as a List
      Returns:
      Copy of all the items in this list box
    • setSelectedIndex

      public T setSelectedIndex(int index)
      Sets which item in the list box that is currently selected. Please note that in this context, selected simply means it is the item that currently has input focus. This is not to be confused with list box implementations such as CheckBoxList where individual items have a certain checked/unchecked state. This method will clip the supplied index to within 0 to items.size() -1.
      Parameters:
      index - Index of the item that should be currently selected
      Returns:
      Itself
    • getSelectedIndex

      public int getSelectedIndex()
      Returns the index of the currently selected item in the list box. Please note that in this context, selected simply means it is the item that currently has input focus. This is not to be confused with list box implementations such as CheckBoxList where individual items have a certain checked/unchecked state.
      Returns:
      The index of the currently selected row in the list box, or -1 if there are no items
    • getSelectedItem

      public V getSelectedItem()
      Returns the currently selected item in the list box. Please note that in this context, selected simply means it is the item that currently has input focus. This is not to be confused with list box implementations such as CheckBoxList where individual items have a certain checked/unchecked state.
      Returns:
      The currently selected item in the list box, or null if there are no items