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 theComponentRendererdeclaration
- 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 TerminalSizeprivate booleanprivate LayoutDataprivate ComponentRenderer<T> Manually set rendererprivate Containerprivate TerminalPositionprivate TerminalSizeprivate Themeprivate ComponentRenderer<T> If overrideRenderer is not set, this is used instead if not null, set by the themeprivate ThemeTo 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 TerminalSizeInvokes 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 voiddraw(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_CORNERwithComponent.toGlobal(TerminalPosition)Returns the layout data associated with this component.Returns the container which is holding this container, ornullif it's not assigned to anything.Returns the top-left corner of this component, measured from its parent.final TerminalSizeReturns 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 theThemethis component should be rendered using.Returns theThemeDefinitiondefined in the currentThemefor this component class.booleanReturnstrueif the supplied Container is either the direct or indirect Parent of this component.voidMarks the component as invalid and requiring to be re-drawn at next opportunity.booleanReturns true if this component is inside of the specified Container.booleanChecks 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.booleanReturns the component's own state as to if it should be visible or not.voidCalled by the GUI system when you add a component to a container; DO NOT CALL THIS YOURSELF!protected voidonAfterDrawing(TextGUIGraphics graphics) This method is called immediately after the component's renderer has finished the drawing operation.protected voidThis method is called just before the component's renderer is invoked for the drawing operation.voidCalled by the GUI system when you remove a component from a container; DO NOT CALL THIS YOURSELF!protected voidTakes aRunnableand immediately executes it if this is called on the designated GUI thread, otherwise schedules it for later invocation.protected Tself()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 TsetPreferredSize(TerminalSize explicitPreferredSize) Overrides the components preferred size calculation and makes thegetPreferredSize()always return the value passed in here.setRenderer(ComponentRenderer<T> renderer) Explicitly sets theComponentRendererto 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 theThemethis 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 aRunnableand immediately executes it if this is called on the designated GUI thread, otherwise schedules it for later invocation.- Parameters:
runnable-Runnableto execute on the GUI thread
-
setRenderer
Explicitly sets theComponentRendererto 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-ComponentRendererto be used when drawing this component- Returns:
- Itself
-
getRenderer
Description copied from interface:ComponentReturns 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:
getRendererin interfaceComponent- Returns:
- Renderer this component is using
-
invalidate
public void invalidate()Description copied from interface:ComponentMarks 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:
invalidatein interfaceComponent
-
setSize
Description copied from interface:ComponentThis 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:ComponentReturns 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:ComponentReturns 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:
getPreferredSizein interfaceComponent- Returns:
- Size we would like to be
-
setPreferredSize
Description copied from interface:ComponentOverrides 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:
setPreferredSizein interfaceComponent- Parameters:
explicitPreferredSize- Preferred size we want to use for this component- Returns:
- Itself
-
isVisible
public boolean isVisible()Description copied from interface:ComponentReturns 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:ComponentSets 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:
setVisiblein interfaceComponent- Parameters:
visible-trueif the component should be visible,falseotherwise- 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:ComponentThis 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:
setPositionin interfaceComponent- Parameters:
position- Top-left position of the component, relative to its parent- Returns:
- Itself
-
getPosition
Description copied from interface:ComponentReturns the top-left corner of this component, measured from its parent.- Specified by:
getPositionin interfaceComponent- Returns:
- Position of this component
-
getGlobalPosition
Description copied from interface:ComponentReturns the top-left corner of this component in global coordinates space usingTerminalPosition.TOP_LEFT_CORNERwithComponent.toGlobal(TerminalPosition)- Specified by:
getGlobalPositionin interfaceComponent- Returns:
- global position of this component
-
isInvalid
public boolean isInvalid()Description copied from interface:TextGUIElementChecks 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:
isInvalidin interfaceTextGUIElement- Returns:
trueif the component is invalid and needs redrawing,falseotherwise
-
draw
Description copied from interface:TextGUIElementDraws 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:
drawin 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. TheTextGUIGraphicssupplied 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:ComponentSets 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:
setLayoutDatain interfaceComponent- Parameters:
data- Layout data associated with this component- Returns:
- Itself
-
getLayoutData
Description copied from interface:ComponentReturns 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:
getLayoutDatain interfaceComponent- Returns:
- This component's layout data
-
getParent
-
hasParent
Description copied from interface:ComponentReturnstrueif the supplied Container is either the direct or indirect Parent of this component. -
getTextGUI
Description copied from interface:ComponentReturns 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:
getTextGUIin interfaceComponent- Returns:
- The TextGUI that this component belongs to, or null if none
-
getTheme
Description copied from interface:ComponentReturns theThemethis component should be rendered using. The default implementation throughAbstractComponentwill retrieve this from theWindowthe 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:ComponentReturns theThemeDefinitiondefined in the currentThemefor this component class. The is the same as calling:component.getTheme().getThemeDefinition(ComponentClassType.class); // i.e button.getTheme().getThemeDefinition(Button.class);- Specified by:
getThemeDefinitionin interfaceComponent- Returns:
ThemeDefinitiondefined in the currentThemefor this component class
-
setTheme
Description copied from interface:ComponentOverrides theThemethis 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:ComponentReturns 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:ComponentReturns the BasePane that this container belongs to. In a window-based GUI system, this will be a Window.- Specified by:
getBasePanein interfaceComponent- Returns:
- The base pane this component is placed on, or
nullif none
-
toBasePane
Description copied from interface:ComponentTranslates 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:
toBasePanein interfaceComponent- Parameters:
position- Position to translate (relative to the container's top-left corner)- Returns:
- Position in base pane space, or
nullif the component is an orphan
-
toGlobal
Description copied from interface:ComponentTranslates 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:ComponentTakes 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:
withBorderin interfaceComponent- Parameters:
border- Border to wrap the component with- Returns:
- The border with this component wrapped
-
addTo
-
onAdded
-
onRemoved
Description copied from interface:ComponentCalled 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
-