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 theComponentRendererdeclaration
- 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
Componentis to pass in itself as the generic parameter, like this:
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.public class MyComponent extends AbstractComponent<MyComponent> { ... }
-
-
Field Summary
Fields Modifier and Type Field Description private ComponentRenderer<T>defaultRendererIf the theme had nothing for this component and no override is set, this is the third fallbackprivate TerminalSizeexplicitPreferredSizeprivate booleaninvalidprivate LayoutDatalayoutDataprivate ComponentRenderer<T>overrideRendererManually set rendererprivate Containerparentprivate TerminalPositionpositionprivate TerminalSizesizeprivate ThemethemeOverrideprivate ComponentRenderer<T>themeRendererIf overrideRenderer is not set, this is used instead if not null, set by the themeprivate ThemethemeRenderersThemeTo keep track of the theme that created the themeRenderer, so we can reset it if the theme changesprivate booleanvisible
-
Constructor Summary
Constructors Constructor Description AbstractComponent()Default constructor
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description TaddTo(Panel panel)Same as callingpanel.addComponent(thisComponent)protected TerminalSizecalculatePreferredSize()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).voiddraw(TextGUIGraphics graphics)Draws the GUI element using the supplied TextGUIGraphics object.BasePanegetBasePane()Returns the BasePane that this container belongs to.TerminalPositiongetGlobalPosition()Returns the top-left corner of this component in global coordinates space usingTerminalPosition.TOP_LEFT_CORNERwithComponent.toGlobal(TerminalPosition)LayoutDatagetLayoutData()Returns the layout data associated with this component.ContainergetParent()Returns the container which is holding this container, ornullif it's not assigned to anything.TerminalPositiongetPosition()Returns the top-left corner of this component, measured from its parent.TerminalSizegetPreferredSize()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.TerminalSizegetSize()Returns how large this component is.TextGUIgetTextGUI()Returns the TextGUI that this component is currently part of.ThemegetTheme()Returns theThemethis component should be rendered using.ThemeDefinitiongetThemeDefinition()Returns theThemeDefinitiondefined in the currentThemefor this component class.booleanhasParent(Container parent)Returnstrueif the supplied Container is either the direct or indirect Parent of this component.voidinvalidate()Marks the component as invalid and requiring to be re-drawn at next opportunity.booleanisInside(Container container)Returns true if this component is inside of the specified Container.booleanisInvalid()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.booleanisVisible()Returns the component's own state as to if it should be visible or not.voidonAdded(Container container)Called 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 voidonBeforeDrawing()This method is called just before the component's renderer is invoked for the drawing operation.voidonRemoved(Container container)Called by the GUI system when you remove a component from a container; DO NOT CALL THIS YOURSELF!protected voidrunOnGUIThreadIfExistsOtherwiseRunDirect(java.lang.Runnable runnable)Takes 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.private java.lang.Class<T>selfClass()TsetLayoutData(LayoutData data)Sets optional layout data associated with this component.TsetPosition(TerminalPosition position)This method will be called by the layout manager when it has decided where the component is to be located.TsetPreferredSize(TerminalSize explicitPreferredSize)Overrides the components preferred size calculation and makes thegetPreferredSize()always return the value passed in here.TsetRenderer(ComponentRenderer<T> renderer)Explicitly sets theComponentRendererto be used when drawing this component.TsetSize(TerminalSize size)This method will be called by the layout manager when it has decided how large the component will be.ComponentsetTheme(Theme theme)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.TsetVisible(boolean visible)Sets the component's own state as to if it should be visible or not.TerminalPositiontoBasePane(TerminalPosition position)Translates a position local to the container to the base pane's coordinate space.TerminalPositiontoGlobal(TerminalPosition position)Translates a position local to the container to global coordinate space.BorderwithBorder(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 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
public T setRenderer(ComponentRenderer<T> renderer)
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
public ComponentRenderer<T> 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
public T setSize(TerminalSize size)
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
public TerminalSize 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
public final TerminalSize 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
public final T setPreferredSize(TerminalSize explicitPreferredSize)
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
public T setVisible(boolean visible)
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
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: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
public TerminalPosition 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
public TerminalPosition 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
public final void draw(TextGUIGraphics graphics)
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
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. 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
public T setLayoutData(LayoutData data)
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
public LayoutData 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
public Container getParent()
Description copied from interface:ComponentReturns the container which is holding this container, ornullif it's not assigned to anything.
-
hasParent
public boolean hasParent(Container parent)
Description copied from interface:ComponentReturnstrueif the supplied Container is either the direct or indirect Parent of this component.
-
getTextGUI
public TextGUI 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
public Theme 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
public ThemeDefinition 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
public Component setTheme(Theme theme)
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
public boolean isInside(Container container)
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
public BasePane 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
public TerminalPosition toBasePane(TerminalPosition position)
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
public TerminalPosition toGlobal(TerminalPosition position)
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
public Border withBorder(Border border)
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
public T addTo(Panel panel)
Description copied from interface:ComponentSame as callingpanel.addComponent(thisComponent)
-
onAdded
public void onAdded(Container container)
Description copied from interface:ComponentCalled 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:ComponentCalled 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()
-
-