Module com.googlecode.lanterna
Package com.googlecode.lanterna.gui2
Class AbstractListBox<V,T extends AbstractListBox<V,T>>
- java.lang.Object
-
- com.googlecode.lanterna.gui2.AbstractComponent<T>
-
- com.googlecode.lanterna.gui2.AbstractInteractableComponent<T>
-
- com.googlecode.lanterna.gui2.AbstractListBox<V,T>
-
- Type Parameters:
T
- Should always be itself, seeAbstractComponent
V
- Type of items this list box contains
- 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AbstractListBox.DefaultListBoxRenderer<V,T extends AbstractListBox<V,T>>
The default renderer forAbstractListBox
and all its subclasses.static class
AbstractListBox.ListItemRenderer<V,T extends AbstractListBox<V,T>>
The default list item renderer class, this can be extended and customized it needed.-
Nested classes/interfaces inherited from interface com.googlecode.lanterna.gui2.Interactable
Interactable.FocusChangeDirection, Interactable.Result
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<V>
items
private AbstractListBox.ListItemRenderer<V,T>
listItemRenderer
protected TerminalPosition
scrollOffset
private int
selectedIndex
-
Constructor Summary
Constructors Modifier Constructor Description 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.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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description T
addItem(V item)
Adds one more item to the list box, at the end.protected void
afterEnterFocus(Interactable.FocusChangeDirection direction, Interactable previouslyInFocus)
Called byAbstractInteractableComponent
automatically after this component has received input focus.T
clearItems()
Removes all items from the list boxprotected AbstractListBox.ListItemRenderer<V,T>
createDefaultListItemRenderer()
Method that constructs theListItemRenderer
that this list box should use to draw the elements of the list box.protected InteractableRenderer<T>
createDefaultRenderer()
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).protected int
getIndexByMouseAction(MouseAction click)
By convertingTerminalPosition
s toAbstractComponent.toGlobal(TerminalPosition)
gets index clicked on by mouse action.V
getItemAt(int index)
Retrieves the item at the specified index in the list boxint
getItemCount()
Returns the number of items currently in the list boxjava.util.List<V>
getItems()
Returns a copy of the items in the list box as aList
(package private) AbstractListBox.ListItemRenderer<V,T>
getListItemRenderer()
int
getSelectedIndex()
Returns the index of the currently selected item in the list box.V
getSelectedItem()
Returns the currently selected item in the list box.Interactable.Result
handleKeyStroke(KeyStroke keyStroke)
This method can be overridden to handle various user input (mostly from the keyboard) when this component is in focus.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 otherwiseboolean
isEmpty()
Checks if the list box has no itemsboolean
isFocusable()
Returnstrue
if this interactable component is currently able to receive input focus.V
removeItem(int index)
Removes an item from the list box by its index.private boolean
selectByCharacter(java.lang.Character character)
T
setListItemRenderer(AbstractListBox.ListItemRenderer<V,T> listItemRenderer)
This method overrides theListItemRenderer
that is used to draw each element in the list box.T
setSelectedIndex(int index)
Sets which item in the list box that is currently selected.-
Methods inherited from class com.googlecode.lanterna.gui2.AbstractInteractableComponent
afterLeaveFocus, getCursorLocation, getInputFilter, getRenderer, handleInput, isActivationStroke, isEnabled, isFocused, isKeyboardActivationStroke, isMouseActivationStroke, isMouseDown, isMouseDrag, isMouseMove, isMouseUp, onEnterFocus, onLeaveFocus, setEnabled, setInputFilter, takeFocus
-
Methods inherited from class com.googlecode.lanterna.gui2.AbstractComponent
addTo, calculatePreferredSize, draw, getBasePane, getGlobalPosition, getLayoutData, getParent, getPosition, getPreferredSize, getSize, getTextGUI, getTheme, getThemeDefinition, hasParent, invalidate, isInside, isInvalid, isVisible, onAdded, onAfterDrawing, onBeforeDrawing, onRemoved, runOnGUIThreadIfExistsOtherwiseRunDirect, self, setLayoutData, setPosition, setPreferredSize, setRenderer, setSize, setTheme, setVisible, toBasePane, toGlobal, withBorder
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.googlecode.lanterna.gui2.Component
addTo, getBasePane, getGlobalPosition, getLayoutData, getParent, getPosition, getPreferredSize, getSize, getTextGUI, getTheme, getThemeDefinition, hasParent, invalidate, isInside, isVisible, onAdded, onRemoved, setLayoutData, setPosition, setPreferredSize, setSize, setTheme, setVisible, toBasePane, toGlobal, withBorder
-
Methods inherited from interface com.googlecode.lanterna.gui2.TextGUIElement
draw, isInvalid
-
-
-
-
Field Detail
-
items
private final java.util.List<V> items
-
selectedIndex
private int selectedIndex
-
listItemRenderer
private AbstractListBox.ListItemRenderer<V,T extends AbstractListBox<V,T>> listItemRenderer
-
scrollOffset
protected TerminalPosition scrollOffset
-
-
Constructor Detail
-
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 anull
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 tonull
will ask to be big enough to display all items.
-
-
Method Detail
-
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 classAbstractInteractableComponent<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 theListItemRenderer
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 theListItemRenderer
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 inAbstractInteractableComponent
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 classAbstractInteractableComponent<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 convertingTerminalPosition
s toAbstractComponent.toGlobal(TerminalPosition)
gets index clicked on by mouse action.- Returns:
- index of a item that was clicked on with
MouseAction
-
selectByCharacter
private boolean selectByCharacter(java.lang.Character character)
-
afterEnterFocus
protected void afterEnterFocus(Interactable.FocusChangeDirection direction, Interactable previouslyInFocus)
Description copied from class:AbstractInteractableComponent
Called byAbstractInteractableComponent
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 classAbstractInteractableComponent<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 abovepreviouslyInFocus
- 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:
java.lang.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
Returnstrue
if this interactable component is currently able to receive input focus. This is similar but different fromInteractable.isEnabled()
, which tells lanterna that the entire component is disabled when it is returningfalse
, compared to this method which simply claims that the component is currently not ready to handle input. TheAbstractInteractableComponent
implementation always returntrue
here but for example the list box components will override and returnfalse
here if they are empty. Note that you can still programmatically force input focus onto the component, returningfalse
here won't prevent that.- Specified by:
isFocusable
in interfaceInteractable
- Overrides:
isFocusable
in classAbstractInteractableComponent<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:
java.lang.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 java.util.List<V> getItems()
Returns a copy of the items in the list box as aList
- 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 asCheckBoxList
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 asCheckBoxList
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 asCheckBoxList
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
-
-