Class AbstractInteractableComponent<T extends AbstractInteractableComponent<T>>

java.lang.Object
com.googlecode.lanterna.gui2.AbstractComponent<T>
com.googlecode.lanterna.gui2.AbstractInteractableComponent<T>
Type Parameters:
T - Should always be itself, see AbstractComponent
All Implemented Interfaces:
Component, Interactable, TextGUIElement
Direct Known Subclasses:
AbstractListBox, Button, CheckBox, ComboBox, ImageComponent, MenuItem, Table, TextBox

public abstract class AbstractInteractableComponent<T extends AbstractInteractableComponent<T>> extends AbstractComponent<T> implements Interactable
Default implementation of Interactable that extends from AbstractComponent. If you want to write your own component that is interactable, i.e. can receive keyboard (and mouse) input, you probably want to extend from this class as it contains some common implementations of the methods from Interactable interface
  • Field Details

    • inputFilter

      private InputFilter inputFilter
    • inFocus

      private boolean inFocus
    • enabled

      private boolean enabled
  • Constructor Details

    • AbstractInteractableComponent

      protected AbstractInteractableComponent()
      Default constructor
  • Method Details

    • takeFocus

      public T takeFocus()
      Description copied from interface: Interactable
      Moves focus in the BasePane to this component. If the component has not been added to a BasePane (i.e. a Window most of the time), does nothing. If the component has been disabled through a call to Interactable.setEnabled(boolean), this call also does nothing.
      Specified by:
      takeFocus in interface Interactable
      Returns:
      Itself
    • onEnterFocus

      public final void onEnterFocus(Interactable.FocusChangeDirection direction, Interactable previouslyInFocus)
      Method called when this component gained keyboard focus.

      This method is final in AbstractInteractableComponent, please override afterEnterFocus instead

      Specified by:
      onEnterFocus in interface Interactable
      Parameters:
      direction - What direction did the focus come from
      previouslyInFocus - Which component had focus previously (null if none)
    • afterEnterFocus

      protected void afterEnterFocus(Interactable.FocusChangeDirection direction, Interactable previouslyInFocus)
      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.
      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
    • onLeaveFocus

      public final void onLeaveFocus(Interactable.FocusChangeDirection direction, Interactable nextInFocus)
      Method called when keyboard focus moves away from this component

      This method is final in AbstractInteractableComponent, please override afterLeaveFocus instead

      Specified by:
      onLeaveFocus in interface Interactable
      Parameters:
      direction - What direction is focus going in
      nextInFocus - Which component is receiving focus next (or null if none)
    • afterLeaveFocus

      protected void afterLeaveFocus(Interactable.FocusChangeDirection direction, Interactable nextInFocus)
      Called by AbstractInteractableComponent automatically after this component has lost input focus. You can override this method if you need to trigger some action based on this.
      Parameters:
      direction - How focus was transferred, keep in mind this is from the this component's point of view so if this parameter has value DOWN, focus is moving down to a component below
      nextInFocus - Which interactable component is going to receive focus
    • createDefaultRenderer

      protected abstract 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 AbstractComponent<T extends AbstractInteractableComponent<T>>
      Returns:
      Renderer to use when sizing and drawing this component
    • getRenderer

      public InteractableRenderer<T> getRenderer()
      Description copied from interface: Component
      Returns the renderer used to draw this component and measure its preferred size. You probably won't need to call this method unless you know exactly which ComponentRenderer implementation is used and you need to customize it.
      Specified by:
      getRenderer in interface Component
      Overrides:
      getRenderer in class AbstractComponent<T extends AbstractInteractableComponent<T>>
      Returns:
      Renderer this component is using
    • isFocused

      public boolean isFocused()
      Description copied from interface: Interactable
      Returns true if this component currently has input focus in its root container.
      Specified by:
      isFocused in interface Interactable
      Returns:
      true if the interactable has input focus, false otherwise
    • setEnabled

      public T setEnabled(boolean enabled)
      Description copied from interface: Interactable
      Prevents the component from receiving input focus if this is called with a false value. The component will then behave as a mainly non-interactable component. Input focus can be re-enabled by calling this with true. If the component already has input focus when calling this method, it will release focus and no component is focused until there is user action or code that chooses a new focus.
      Specified by:
      setEnabled in interface Interactable
      Parameters:
      enabled - If called with false, this interactable won't receive input focus until it's called again with true.
      Returns:
      Itself
    • isEnabled

      public boolean isEnabled()
      Description copied from interface: Interactable
      Returns true if this component is able to receive input as a regular interactable component. This will return false if input focus has been disabled through calling Interactable.setEnabled(boolean).
      Specified by:
      isEnabled in interface Interactable
      Returns:
      true if this component can receive input focus, false otherwise
    • 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
      Returns:
      true if this component wants to receive input focus, false otherwise.
    • handleInput

      public final Interactable.Result handleInput(KeyStroke keyStroke)
      Description copied from interface: Interactable
      Accepts a KeyStroke as input and processes this as a user input. Depending on what the component does with this key-stroke, there are several results passed back to the GUI system that will decide what to do next. If the event was not handled or ignored, Result.UNHANDLED should be returned. This will tell the GUI system that the key stroke was not understood by this component and may be dealt with in another way. If event was processed properly, it should return Result.HANDLED, which will make the GUI system stop processing this particular key-stroke. Furthermore, if the component understood the key-stroke and would like to move focus to a different component, there are the Result.MOVE_FOCUS_* values. This method should be invoking the input filter, if it is set, to see if the input should be processed or not.

      Notice that most of the built-in components in Lanterna extends from AbstractInteractableComponent which has a final implementation of this method. The method to override to handle input in that case is handleKeyStroke(KeyStroke).

      Specified by:
      handleInput in interface Interactable
      Parameters:
      keyStroke - What input was entered by the user
      Returns:
      Result of processing the key-stroke
    • handleKeyStroke

      protected 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. 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.
      Parameters:
      keyStroke - What input was entered by the user
      Returns:
      Result of processing the key-stroke
    • getCursorLocation

      public TerminalPosition getCursorLocation()
      Description copied from interface: Interactable
      Returns, in local coordinates, where to put the cursor on the screen when this component has focus. If null, the cursor should be hidden. If you component is 5x1 and you want to have the cursor in the middle (when in focus), return [2,0]. The GUI system will convert the position to global coordinates.
      Specified by:
      getCursorLocation in interface Interactable
      Returns:
      Coordinates of where to place the cursor when this component has focus
    • getInputFilter

      public InputFilter getInputFilter()
      Description copied from interface: Interactable
      Returns the input filter currently assigned to the interactable component. This will intercept any user input and decide if the input should be passed on to the component or not. null means there is no filter.
      Specified by:
      getInputFilter in interface Interactable
      Returns:
      Input filter currently assigned to the interactable component
    • setInputFilter

      public T setInputFilter(InputFilter inputFilter)
      Description copied from interface: Interactable
      Assigns an input filter to the interactable component. This will intercept any user input and decide if the input should be passed on to the component or not. null means there is no filter.
      Specified by:
      setInputFilter in interface Interactable
      Parameters:
      inputFilter - Input filter to assign to the interactable
      Returns:
      Itself
    • isKeyboardActivationStroke

      public boolean isKeyboardActivationStroke(KeyStroke keyStroke)
    • isMouseActivationStroke

      public boolean isMouseActivationStroke(KeyStroke keyStroke)
    • isActivationStroke

      public boolean isActivationStroke(KeyStroke keyStroke)
    • isMouseDown

      public boolean isMouseDown(KeyStroke keyStroke)
    • isMouseDrag

      public boolean isMouseDrag(KeyStroke keyStroke)
    • isMouseMove

      public boolean isMouseMove(KeyStroke keyStroke)
    • isMouseUp

      public boolean isMouseUp(KeyStroke keyStroke)