T
- Should always be itself, this value will be used for the ComponentRenderer
declarationComponent
, TextGUIElement
AbstractComposite
, AbstractInteractableComponent
, EmptySpace
, Label
, Panel
, ProgressBar
, ScrollBar
, Separator
public abstract class AbstractComponent<T extends Component> extends Object implements Component
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.Constructor | Description |
---|---|
AbstractComponent() |
Default constructor
|
Modifier and Type | Method | Description |
---|---|---|
T |
addTo(Panel panel) |
Same as calling
panel.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.
|
LayoutData |
getLayoutData() |
Returns the layout data associated with this component.
|
Container |
getParent() |
Returns the container which is holding this container, or
null 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 the
Theme this component should be rendered using. |
ThemeDefinition |
getThemeDefinition() |
Returns the
ThemeDefinition defined in the current Theme for this component class. |
boolean |
hasParent(Container parent) |
Returns
true 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.
|
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(Runnable runnable) |
Takes a
Runnable 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 return
T . |
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 the
getPreferredSize() always return the
value passed in here. |
T |
setRenderer(ComponentRenderer<T> renderer) |
Explicitly sets the
ComponentRenderer 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 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. |
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.
|
protected abstract ComponentRenderer<T> createDefaultRenderer()
protected void runOnGUIThreadIfExistsOtherwiseRunDirect(Runnable runnable)
Runnable
and immediately executes it if this is called on the designated GUI thread, otherwise
schedules it for later invocation.runnable
- Runnable
to execute on the GUI threadpublic T setRenderer(ComponentRenderer<T> renderer)
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.renderer
- ComponentRenderer
to be used when drawing this componentpublic ComponentRenderer<T> getRenderer()
Component
getRenderer
in interface Component
public void invalidate()
Component
invalidate
in interface Component
public T setSize(TerminalSize size)
Component
public TerminalSize getSize()
Component
public final TerminalSize getPreferredSize()
Component
getPreferredSize
in interface Component
public final T setPreferredSize(TerminalSize explicitPreferredSize)
Component
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.setPreferredSize
in interface Component
explicitPreferredSize
- Preferred size we want to use for this componentprotected TerminalSize calculatePreferredSize()
public T setPosition(TerminalPosition position)
Component
setPosition
in interface Component
position
- Top-left position of the component, relative to its parentpublic TerminalPosition getPosition()
Component
getPosition
in interface Component
public boolean isInvalid()
TextGUIElement
isInvalid
in interface TextGUIElement
true
if the component is invalid and needs redrawing, false
otherwisepublic final void draw(TextGUIGraphics graphics)
TextGUIElement
draw
in interface TextGUIElement
graphics
- Graphics object to use when drawing the componentprotected void onBeforeDrawing()
getSize()
and
getPosition()
.protected void onAfterDrawing(TextGUIGraphics graphics)
TextGUIGraphics
supplied is the same that was fed into the renderer.graphics
- Graphics object you can use to manipulate the appearance of the componentpublic T setLayoutData(LayoutData data)
Component
setLayoutData
in interface Component
data
- Layout data associated with this componentpublic LayoutData getLayoutData()
Component
getLayoutData
in interface Component
public Container getParent()
Component
null
if it's not assigned to anything.public boolean hasParent(Container parent)
Component
true
if the supplied Container is either the direct or indirect Parent of this component.public TextGUI getTextGUI()
Component
getTextGUI
in interface Component
public Theme getTheme()
Component
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)
.public ThemeDefinition getThemeDefinition()
Component
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);
getThemeDefinition
in interface Component
ThemeDefinition
defined in the current Theme
for this component classpublic Component setTheme(Theme theme)
Component
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.public boolean isInside(Container container)
Component
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.public BasePane getBasePane()
Component
getBasePane
in interface Component
null
if nonepublic TerminalPosition toBasePane(TerminalPosition position)
Component
null
.toBasePane
in interface Component
position
- Position to translate (relative to the container's top-left corner)null
if the component is an orphanpublic TerminalPosition toGlobal(TerminalPosition position)
Component
null
.public Border withBorder(Border border)
Component
container.addComponent(new Button("Test").withBorder(Borders.singleLine()));
withBorder
in interface Component
border
- Border to wrap the component withpublic T addTo(Panel panel)
Component
panel.addComponent(thisComponent)
public void onAdded(Container container)
Component
public void onRemoved(Container container)
Component
protected T self()
T
. Credit to
avl42 for this one!Copyright © 2020. All rights reserved.