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
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 Summary
FieldsModifier and TypeFieldDescriptionprivate ComponentRenderer
<T> If the theme had nothing for this component and no override is set, this is the third fallbackprivate TerminalSize
private boolean
private LayoutData
private ComponentRenderer
<T> Manually set rendererprivate Container
private TerminalPosition
private TerminalSize
private Theme
private ComponentRenderer
<T> If overrideRenderer is not set, this is used instead if not null, set by the themeprivate Theme
To keep track of the theme that created the themeRenderer, so we can reset it if the theme changesprivate boolean
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionSame as callingpanel.addComponent(thisComponent)
protected TerminalSize
Invokes the component renderer's size calculation logic and returns the result.protected abstract ComponentRenderer
<T> 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).final void
draw
(TextGUIGraphics graphics) Draws the GUI element using the supplied TextGUIGraphics object.Returns the BasePane that this container belongs to.Returns the top-left corner of this component in global coordinates space usingTerminalPosition.TOP_LEFT_CORNER
withComponent.toGlobal(TerminalPosition)
Returns the layout data associated with this component.Returns the container which is holding this container, ornull
if it's not assigned to anything.Returns the top-left corner of this component, measured from its parent.final TerminalSize
Returns the ideal size this component would like to have, in order to draw itself properly.Returns the renderer used to draw this component and measure its preferred size.getSize()
Returns how large this component is.Returns the TextGUI that this component is currently part of.getTheme()
Returns theTheme
this component should be rendered using.Returns theThemeDefinition
defined in the currentTheme
for this component class.boolean
Returnstrue
if the supplied Container is either the direct or indirect Parent of this component.void
Marks the component as invalid and requiring to be re-drawn at next opportunity.boolean
Returns true if this component is inside of the specified Container.boolean
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
Returns the component's own state as to if it should be visible or not.void
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
This method is called just before the component's renderer is invoked for the drawing operation.void
Called by the GUI system when you remove a component from a container; DO NOT CALL THIS YOURSELF!protected void
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
.setLayoutData
(LayoutData data) Sets optional layout data associated with this component.setPosition
(TerminalPosition position) This method will be called by the layout manager when it has decided where the component is to be located.final T
setPreferredSize
(TerminalSize explicitPreferredSize) Overrides the components preferred size calculation and makes thegetPreferredSize()
always return the value passed in here.setRenderer
(ComponentRenderer<T> renderer) Explicitly sets theComponentRenderer
to be used when drawing this component.setSize
(TerminalSize size) This method will be called by the layout manager when it has decided how large the component will be.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.setVisible
(boolean visible) Sets the component's own state as to if it should be visible or not.toBasePane
(TerminalPosition position) Translates a position local to the container to the base pane's coordinate space.toGlobal
(TerminalPosition position) Translates a position local to the container to global coordinate space.withBorder
(Border border) Takes a border object and moves this component inside it and then returns it again.
-
Field Details
-
overrideRenderer
Manually set renderer -
themeRenderer
If overrideRenderer is not set, this is used instead if not null, set by the theme -
themeRenderersTheme
To keep track of the theme that created the themeRenderer, so we can reset it if the theme changes -
defaultRenderer
If the theme had nothing for this component and no override is set, this is the third fallback -
parent
-
size
-
explicitPreferredSize
-
position
-
themeOverride
-
layoutData
-
visible
private boolean visible -
invalid
private boolean invalid
-
-
Constructor Details
-
AbstractComponent
public AbstractComponent()Default constructor
-
-
Method Details
-
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Description copied from interface:Component
Returns the container which is holding this container, ornull
if it's not assigned to anything. -
hasParent
Description copied from interface:Component
Returnstrue
if the supplied Container is either the direct or indirect Parent of this component. -
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
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
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
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
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
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
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
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
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
Description copied from interface:Component
Same as callingpanel.addComponent(thisComponent)
-
onAdded
Description copied from interface:Component
Called by the GUI system when you add a component to a container; DO NOT CALL THIS YOURSELF! -
onRemoved
Description copied from interface:Component
Called by the GUI system when you remove a component from a container; DO NOT CALL THIS YOURSELF! -
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
-