Class AbstractComponent<T extends Component>

java.lang.Object
com.googlecode.lanterna.gui2.AbstractComponent<T>
Type Parameters:
T - Should always be itself, this value will be used for the ComponentRenderer declaration
All Implemented Interfaces:
Component, TextGUIElement
Direct Known Subclasses:
AbstractComposite, AbstractInteractableComponent, EmptySpace, Label, MenuBar, Panel, ProgressBar, ScrollBar, Separator

public abstract class AbstractComponent<T extends Component> extends Object implements Component
AbstractComponent provides some good default behaviour for a Component, all components in Lanterna extends from this class in some way. If you want to write your own component that isn't interactable or theme:able, you probably want to extend from this class.

The way you want to declare your new Component is to pass in itself as the generic parameter, like this:

 
     public class MyComponent extends AbstractComponent<MyComponent> {
         ...
     }
 
 
This was, the component renderer will be correctly setup type-wise and you will need to do fewer typecastings when you implement the drawing method your new component.
  • Field Details

    • overrideRenderer

      private ComponentRenderer<T extends Component> overrideRenderer
      Manually set renderer
    • themeRenderer

      private ComponentRenderer<T extends Component> themeRenderer
      If overrideRenderer is not set, this is used instead if not null, set by the theme
    • themeRenderersTheme

      private Theme themeRenderersTheme
      To keep track of the theme that created the themeRenderer, so we can reset it if the theme changes
    • defaultRenderer

      private ComponentRenderer<T extends Component> defaultRenderer
      If the theme had nothing for this component and no override is set, this is the third fallback
    • parent

      private Container parent
    • size

      private TerminalSize size
    • explicitPreferredSize

      private TerminalSize explicitPreferredSize
    • position

      private TerminalPosition position
    • themeOverride

      private Theme themeOverride
    • layoutData

      private LayoutData layoutData
    • visible

      private boolean visible
    • invalid

      private boolean invalid
  • Constructor Details

    • AbstractComponent

      public AbstractComponent()
      Default constructor
  • Method Details

    • createDefaultRenderer

      protected abstract ComponentRenderer<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). This value is intended to be overridden by custom themes.
      Returns:
      Renderer to use when sizing and drawing this component
    • runOnGUIThreadIfExistsOtherwiseRunDirect

      protected void runOnGUIThreadIfExistsOtherwiseRunDirect(Runnable runnable)
      Takes a Runnable and immediately executes it if this is called on the designated GUI thread, otherwise schedules it for later invocation.
      Parameters:
      runnable - Runnable to execute on the GUI thread
    • setRenderer

      public T setRenderer(ComponentRenderer<T> renderer)
      Explicitly sets the ComponentRenderer to be used when drawing this component. This will override whatever the current theme is suggesting or what the default renderer is. If you call this with null, the override is cleared.
      Parameters:
      renderer - ComponentRenderer to be used when drawing this component
      Returns:
      Itself
    • getRenderer

      public ComponentRenderer<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
      Returns:
      Renderer this component is using
    • invalidate

      public void invalidate()
      Description copied from interface: Component
      Marks the component as invalid and requiring to be re-drawn at next opportunity. Container components should take this as a hint to layout the child components again.
      Specified by:
      invalidate in interface Component
    • setSize

      public T setSize(TerminalSize size)
      Description copied from interface: Component
      This method will be called by the layout manager when it has decided how large the component will be. If you call this method yourself, prepare for unexpected results.
      Specified by:
      setSize in interface Component
      Parameters:
      size - Current size of the component
      Returns:
      Itself
    • getSize

      public TerminalSize getSize()
      Description copied from interface: Component
      Returns how large this component is. If the layout manager has not yet laid this component out, it will return an empty size (0x0)
      Specified by:
      getSize in interface Component
      Returns:
      How large this component is
    • getPreferredSize

      public final TerminalSize getPreferredSize()
      Description copied from interface: Component
      Returns the ideal size this component would like to have, in order to draw itself properly. There are no guarantees the GUI system will decide to give it this size though.
      Specified by:
      getPreferredSize in interface Component
      Returns:
      Size we would like to be
    • setPreferredSize

      public final T setPreferredSize(TerminalSize explicitPreferredSize)
      Description copied from interface: Component
      Overrides the components preferred size calculation and makes the getPreferredSize() always return the value passed in here. If you call this will null, it will re-enable the preferred size calculation again. Please note that using this method on components that are not designed to work with arbitrary sizes make have unexpected behaviour.
      Specified by:
      setPreferredSize in interface Component
      Parameters:
      explicitPreferredSize - Preferred size we want to use for this component
      Returns:
      Itself
    • isVisible

      public boolean isVisible()
      Description copied from interface: Component
      Returns the component's own state as to if it should be visible or not. The exact behavior of an invisible component will depend on the layout manager; some will just ignore invisible components where others leave an empty space in their place. Invisible components cannot get input focus. The visibility value of the component has no effect if the component is not added to a container.
      Specified by:
      isVisible in interface Component
      Returns:
      The component's internal visibility state
    • setVisible

      public T setVisible(boolean visible)
      Description copied from interface: Component
      Sets the component's own state as to if it should be visible or not. The exact behavior of an invisible component will depend on the layout manager; some will just ignore invisible components where others leave an empty space in their place. Invisible components cannot get input focus. The visibility value of the component has no effect if the component is not added to a container.
      Specified by:
      setVisible in interface Component
      Parameters:
      visible - true if the component should be visible, false otherwise
      Returns:
      Itself
    • calculatePreferredSize

      protected TerminalSize calculatePreferredSize()
      Invokes the component renderer's size calculation logic and returns the result. This value represents the preferred size and isn't necessarily what it will eventually be assigned later on.
      Returns:
      Size that the component renderer believes the component should be
    • setPosition

      public T setPosition(TerminalPosition position)
      Description copied from interface: Component
      This method will be called by the layout manager when it has decided where the component is to be located. If you call this method yourself, prepare for unexpected results.
      Specified by:
      setPosition in interface Component
      Parameters:
      position - Top-left position of the component, relative to its parent
      Returns:
      Itself
    • getPosition

      public TerminalPosition getPosition()
      Description copied from interface: Component
      Returns the top-left corner of this component, measured from its parent.
      Specified by:
      getPosition in interface Component
      Returns:
      Position of this component
    • getGlobalPosition

      public TerminalPosition getGlobalPosition()
      Description copied from interface: Component
      Returns the top-left corner of this component in global coordinates space using TerminalPosition.TOP_LEFT_CORNER with Component.toGlobal(TerminalPosition)
      Specified by:
      getGlobalPosition in interface Component
      Returns:
      global position of this component
    • isInvalid

      public boolean isInvalid()
      Description copied from interface: TextGUIElement
      Checks if this element (or any of its child components, if any) has signaled that what it's currently displaying is out of date and needs re-drawing.
      Specified by:
      isInvalid in interface TextGUIElement
      Returns:
      true if the component is invalid and needs redrawing, false otherwise
    • draw

      public final void draw(TextGUIGraphics graphics)
      Description copied from interface: TextGUIElement
      Draws the GUI element using the supplied TextGUIGraphics object. This is the main method to implement when you want to create your own GUI components.
      Specified by:
      draw in interface TextGUIElement
      Parameters:
      graphics - Graphics object to use when drawing the component
    • onBeforeDrawing

      protected void onBeforeDrawing()
      This method is called just before the component's renderer is invoked for the drawing operation. You can use this hook to do some last-minute adjustments to the component, as an alternative to coding it into the renderer itself. The component should have the correct size and position at this point, if you call getSize() and getPosition().
    • onAfterDrawing

      protected void onAfterDrawing(TextGUIGraphics graphics)
      This method is called immediately after the component's renderer has finished the drawing operation. You can use this hook to do some post-processing if you need, as an alternative to coding it into the renderer. The TextGUIGraphics supplied is the same that was fed into the renderer.
      Parameters:
      graphics - Graphics object you can use to manipulate the appearance of the component
    • setLayoutData

      public T setLayoutData(LayoutData data)
      Description copied from interface: Component
      Sets optional layout data associated with this component. This meaning of this data is up to the layout manager to figure out, see each layout manager for examples of how to use it.
      Specified by:
      setLayoutData in interface Component
      Parameters:
      data - Layout data associated with this component
      Returns:
      Itself
    • getLayoutData

      public LayoutData getLayoutData()
      Description copied from interface: Component
      Returns the layout data associated with this component. This data will optionally be used by the layout manager, see the documentation for each layout manager for more details on valid values and their meaning.
      Specified by:
      getLayoutData in interface Component
      Returns:
      This component's layout data
    • getParent

      public Container getParent()
      Description copied from interface: Component
      Returns the container which is holding this container, or null if it's not assigned to anything.
      Specified by:
      getParent in interface Component
      Returns:
      Parent container or null
    • hasParent

      public boolean hasParent(Container parent)
      Description copied from interface: Component
      Returns true if the supplied Container is either the direct or indirect Parent of this component.
      Specified by:
      hasParent in interface Component
      Parameters:
      parent - Container to test if it's the parent or grand-parent of this component
      Returns:
      true if the container is either the direct or indirect parent of this component, otherwise false
    • getTextGUI

      public TextGUI getTextGUI()
      Description copied from interface: Component
      Returns the TextGUI that this component is currently part of. If the component hasn't been added to any container or in any other way placed into a GUI system, this method will return null.
      Specified by:
      getTextGUI in interface Component
      Returns:
      The TextGUI that this component belongs to, or null if none
    • getTheme

      public Theme getTheme()
      Description copied from interface: Component
      Returns the Theme this component should be rendered using. The default implementation through AbstractComponent will retrieve this from the Window the component belongs to, or return the default theme if the component has not been added to a window yet. You can override the theme this component is assigned to by calling Component.setTheme(Theme).
      Specified by:
      getTheme in interface Component
      Returns:
      The currently active Theme for this component
    • getThemeDefinition

      public ThemeDefinition getThemeDefinition()
      Description copied from interface: Component
      Returns the ThemeDefinition defined in the current Theme for this component class. The is the same as calling:
           component.getTheme().getThemeDefinition(ComponentClassType.class);
           // i.e button.getTheme().getThemeDefinition(Button.class);
       
      Specified by:
      getThemeDefinition in interface Component
      Returns:
      ThemeDefinition defined in the current Theme for this component class
    • setTheme

      public Component setTheme(Theme theme)
      Description copied from interface: Component
      Overrides the Theme this component will use so rather than deriving the theme from either the window or the GUI system, it will always return this theme. If you call this with null, it remove the override and the next call to Component.getTheme() will again try to derive the theme by looking at the window or the GUI system.
      Specified by:
      setTheme in interface Component
      Parameters:
      theme - Theme to assign to this component, or null to use whatever the window uses
      Returns:
      Itself
    • isInside

      public boolean isInside(Container container)
      Description copied from interface: Component
      Returns true if this component is inside of the specified Container. It might be a direct child or not, this method makes no difference. If getParent() is not the same instance as container, but if this method returns true, you can be sure that this component is not a direct child.
      Specified by:
      isInside in interface Component
      Parameters:
      container - Container to test if this component is inside
      Returns:
      True if this component is contained in some way within the container
    • getBasePane

      public BasePane getBasePane()
      Description copied from interface: Component
      Returns the BasePane that this container belongs to. In a window-based GUI system, this will be a Window.
      Specified by:
      getBasePane in interface Component
      Returns:
      The base pane this component is placed on, or null if none
    • toBasePane

      public TerminalPosition toBasePane(TerminalPosition position)
      Description copied from interface: Component
      Translates a position local to the container to the base pane's coordinate space. For a window-based GUI, this be a coordinate in the window's coordinate space. If the component belongs to no base pane, it will return null.
      Specified by:
      toBasePane in interface Component
      Parameters:
      position - Position to translate (relative to the container's top-left corner)
      Returns:
      Position in base pane space, or null if the component is an orphan
    • toGlobal

      public TerminalPosition toGlobal(TerminalPosition position)
      Description copied from interface: Component
      Translates a position local to the container to global coordinate space. This should be the absolute coordinate in the terminal screen, taking no windows or containers into account. If the component belongs to no base pane, it will return null.
      Specified by:
      toGlobal in interface Component
      Parameters:
      position - Position to translate (relative to the container's top-left corner)
      Returns:
      Position in global (or absolute) coordinates, or null if the component is an orphan
    • withBorder

      public Border withBorder(Border border)
      Description copied from interface: Component
      Takes a border object and moves this component inside it and then returns it again. This makes it easy to quickly wrap a component on creation, like this:
       container.addComponent(new Button("Test").withBorder(Borders.singleLine()));
       
      Specified by:
      withBorder in interface Component
      Parameters:
      border - Border to wrap the component with
      Returns:
      The border with this component wrapped
    • addTo

      public T addTo(Panel panel)
      Description copied from interface: Component
      Same as calling panel.addComponent(thisComponent)
      Specified by:
      addTo in interface Component
      Parameters:
      panel - Panel to add this component to
      Returns:
      Itself
    • onAdded

      public void onAdded(Container container)
      Description copied from interface: Component
      Called by the GUI system when you add a component to a container; DO NOT CALL THIS YOURSELF!
      Specified by:
      onAdded in interface Component
      Parameters:
      container - Container that this component was just added to
    • onRemoved

      public void onRemoved(Container container)
      Description copied from interface: Component
      Called by the GUI system when you remove a component from a container; DO NOT CALL THIS YOURSELF!
      Specified by:
      onRemoved in interface Component
      Parameters:
      container - Container that this component was just removed from
    • self

      protected T self()
      This is a little hack to avoid doing typecasts all over the place when having to return T. Credit to avl42 for this one!
      Returns:
      Itself, but as type T
    • selfClass

      private Class<T> selfClass()