Module com.googlecode.lanterna
Package com.googlecode.lanterna.gui2
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 theComponentRenderer
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 java.lang.Object implements Component
AbstractComponent provides some good default behaviour for aComponent
, 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> { ... }
-
-
Field Summary
Fields Modifier and Type Field Description private ComponentRenderer<T>
defaultRenderer
If the theme had nothing for this component and no override is set, this is the third fallbackprivate TerminalSize
explicitPreferredSize
private boolean
invalid
private LayoutData
layoutData
private ComponentRenderer<T>
overrideRenderer
Manually set rendererprivate Container
parent
private TerminalPosition
position
private TerminalSize
size
private Theme
themeOverride
private ComponentRenderer<T>
themeRenderer
If overrideRenderer is not set, this is used instead if not null, set by the themeprivate Theme
themeRenderersTheme
To keep track of the theme that created the themeRenderer, so we can reset it if the theme changesprivate boolean
visible
-
Constructor Summary
Constructors Constructor Description AbstractComponent()
Default constructor
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description T
addTo(Panel panel)
Same as callingpanel.addComponent(thisComponent)
protected TerminalSize
calculatePreferredSize()
Invokes the component renderer's size calculation logic and returns the result.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).void
draw(TextGUIGraphics graphics)
Draws the GUI element using the supplied TextGUIGraphics object.BasePane
getBasePane()
Returns the BasePane that this container belongs to.TerminalPosition
getGlobalPosition()
Returns the top-left corner of this component in global coordinates space usingTerminalPosition.TOP_LEFT_CORNER
withComponent.toGlobal(TerminalPosition)
LayoutData
getLayoutData()
Returns the layout data associated with this component.Container
getParent()
Returns the container which is holding this container, ornull
if it's not assigned to anything.TerminalPosition
getPosition()
Returns the top-left corner of this component, measured from its parent.TerminalSize
getPreferredSize()
Returns the ideal size this component would like to have, in order to draw itself properly.ComponentRenderer<T>
getRenderer()
Returns the renderer used to draw this component and measure its preferred size.TerminalSize
getSize()
Returns how large this component is.TextGUI
getTextGUI()
Returns the TextGUI that this component is currently part of.Theme
getTheme()
Returns theTheme
this component should be rendered using.ThemeDefinition
getThemeDefinition()
Returns theThemeDefinition
defined in the currentTheme
for this component class.boolean
hasParent(Container parent)
Returnstrue
if the supplied Container is either the direct or indirect Parent of this component.void
invalidate()
Marks the component as invalid and requiring to be re-drawn at next opportunity.boolean
isInside(Container container)
Returns true if this component is inside of the specified Container.boolean
isInvalid()
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.boolean
isVisible()
Returns the component's own state as to if it should be visible or not.void
onAdded(Container container)
Called by the GUI system when you add a component to a container; DO NOT CALL THIS YOURSELF!protected void
onAfterDrawing(TextGUIGraphics graphics)
This method is called immediately after the component's renderer has finished the drawing operation.protected void
onBeforeDrawing()
This method is called just before the component's renderer is invoked for the drawing operation.void
onRemoved(Container container)
Called by the GUI system when you remove a component from a container; DO NOT CALL THIS YOURSELF!protected void
runOnGUIThreadIfExistsOtherwiseRunDirect(java.lang.Runnable runnable)
Takes aRunnable
and immediately executes it if this is called on the designated GUI thread, otherwise schedules it for later invocation.protected T
self()
This is a little hack to avoid doing typecasts all over the place when having to returnT
.private java.lang.Class<T>
selfClass()
T
setLayoutData(LayoutData data)
Sets optional layout data associated with this component.T
setPosition(TerminalPosition position)
This method will be called by the layout manager when it has decided where the component is to be located.T
setPreferredSize(TerminalSize explicitPreferredSize)
Overrides the components preferred size calculation and makes thegetPreferredSize()
always return the value passed in here.T
setRenderer(ComponentRenderer<T> renderer)
Explicitly sets theComponentRenderer
to be used when drawing this component.T
setSize(TerminalSize size)
This method will be called by the layout manager when it has decided how large the component will be.Component
setTheme(Theme theme)
Overrides theTheme
this component will use so rather than deriving the theme from either the window or the GUI system, it will always return this theme.T
setVisible(boolean visible)
Sets the component's own state as to if it should be visible or not.TerminalPosition
toBasePane(TerminalPosition position)
Translates a position local to the container to the base pane's coordinate space.TerminalPosition
toGlobal(TerminalPosition position)
Translates a position local to the container to global coordinate space.Border
withBorder(Border border)
Takes a border object and moves this component inside it and then returns it again.
-
-
-
Field Detail
-
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
-
-
Method Detail
-
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(java.lang.Runnable runnable)
Takes aRunnable
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 theComponentRenderer
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 withnull
, 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 interfaceComponent
- 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 interfaceComponent
-
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.
-
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)
-
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 interfaceComponent
- 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 thegetPreferredSize()
always return the value passed in here. If you call this willnull
, 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 interfaceComponent
- 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.
-
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 interfaceComponent
- 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 interfaceComponent
- 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 interfaceComponent
- 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 usingTerminalPosition.TOP_LEFT_CORNER
withComponent.toGlobal(TerminalPosition)
- Specified by:
getGlobalPosition
in interfaceComponent
- 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 interfaceTextGUIElement
- 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 interfaceTextGUIElement
- 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 callgetSize()
andgetPosition()
.
-
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. TheTextGUIGraphics
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 interfaceComponent
- 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 interfaceComponent
- Returns:
- This component's layout data
-
getParent
public Container getParent()
Description copied from interface:Component
Returns the container which is holding this container, ornull
if it's not assigned to anything.
-
hasParent
public boolean hasParent(Container parent)
Description copied from interface:Component
Returnstrue
if the supplied Container is either the direct or indirect Parent of this component.
-
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 interfaceComponent
- Returns:
- The TextGUI that this component belongs to, or null if none
-
getTheme
public Theme getTheme()
Description copied from interface:Component
Returns theTheme
this component should be rendered using. The default implementation throughAbstractComponent
will retrieve this from theWindow
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 callingComponent.setTheme(Theme)
.
-
getThemeDefinition
public ThemeDefinition getThemeDefinition()
Description copied from interface:Component
Returns theThemeDefinition
defined in the currentTheme
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 interfaceComponent
- Returns:
ThemeDefinition
defined in the currentTheme
for this component class
-
setTheme
public Component setTheme(Theme theme)
Description copied from interface:Component
Overrides theTheme
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 withnull
, it remove the override and the next call toComponent.getTheme()
will again try to derive the theme by looking at the window or the GUI system.
-
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. IfgetParent()
is not the same instance ascontainer
, but if this method returns true, you can be sure that this component is not a direct child.
-
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 interfaceComponent
- 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 returnnull
.- Specified by:
toBasePane
in interfaceComponent
- 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 returnnull
.
-
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 interfaceComponent
- 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 callingpanel.addComponent(thisComponent)
-
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!
-
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!
-
self
protected T self()
This is a little hack to avoid doing typecasts all over the place when having to returnT
. Credit to avl42 for this one!- Returns:
- Itself, but as type T
-
selfClass
private java.lang.Class<T> selfClass()
-
-