java.lang.Object
com.googlecode.lanterna.gui2.AbstractTextGUI
- All Implemented Interfaces:
TextGUI
- Direct Known Subclasses:
MultiWindowTextGUI
This abstract implementation of TextGUI contains some basic management of the underlying Screen and other common code
that can be shared between different implementations.
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.googlecode.lanterna.gui2.TextGUI
TextGUI.Listener
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private boolean
private Theme
private final List
<TextGUI.Listener> private final Screen
private TextGUIThread
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractTextGUI
(TextGUIThreadFactory textGUIThreadFactory, Screen screen) Constructor forAbstractTextGUI
that requires aScreen
and a factory for creating the GUI thread -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(TextGUI.Listener listener) Adds a listener to this TextGUI to fire events on.protected abstract void
drawGUI
(TextGUIGraphics graphics) Draws the entire GUI using aTextGUIGraphics
objectprotected final boolean
fireUnhandledKeyStroke
(KeyStroke keyStroke) This method should be called when there was user input that wasn't handled by the GUI.protected abstract TerminalPosition
Top-level method for drilling in to the GUI and figuring out, in global coordinates, where to place the text cursor on the screen at this time.The first time this method is called, it will create a new TextGUIThread object that you can use to automatically manage this TextGUI instead of manually callingprocessInput()
andupdateScreen()
.Returns theScreen
for thisWindowBasedTextGUI
getTheme()
Returns the theme currently assigned to thisTextGUI
protected abstract boolean
handleInput
(KeyStroke key) This method should take the user input and feed it to the focused component for handling.protected void
Marks the whole text GUI as invalid and that it needs to be redrawn at next opportunityboolean
Checks if blocking I/O is enabled or notboolean
This method can be used to determine if any component has requested a redraw.protected KeyStroke
Polls the underlying input queue for user input, returning either aKeyStroke
ornull
boolean
Drains the input queue and passes the key strokes to the GUI system for processing.protected KeyStroke
Reads one key from the input queue, blocking or non-blocking depending on if blocking I/O has been enabled.void
removeListener
(TextGUI.Listener listener) Removes a listener from this TextGUI so that it will no longer receive eventsvoid
setBlockingIO
(boolean blockingIO) Enables blocking I/O, causing calls toreadKeyStroke()
to block until there is input available.void
Sets the global theme to be used by this TextGUI.void
Updates the screen, to make any changes visible to the user.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.googlecode.lanterna.gui2.TextGUI
getFocusedInteractable, setVirtualScreenEnabled
-
Field Details
-
screen
-
listeners
-
blockingIO
private boolean blockingIO -
dirty
private boolean dirty -
textGUIThread
-
guiTheme
-
-
Constructor Details
-
AbstractTextGUI
Constructor forAbstractTextGUI
that requires aScreen
and a factory for creating the GUI thread- Parameters:
textGUIThreadFactory
- Factory class to use for creating theTextGUIThread
classscreen
- What underlyingScreen
to use for this text GUI
-
-
Method Details
-
readKeyStroke
Reads one key from the input queue, blocking or non-blocking depending on if blocking I/O has been enabled. To enable blocking I/O (disabled by default), usesetBlockingIO(true)
.- Returns:
- One piece of user input as a
KeyStroke
ornull
if blocking I/O is disabled and there was no input waiting - Throws:
IOException
- In case of an I/O error while reading input
-
pollInput
Polls the underlying input queue for user input, returning either aKeyStroke
ornull
- Returns:
KeyStroke
representing the user input ornull
if there was none- Throws:
IOException
- In case of an I/O error while reading input
-
processInput
Description copied from interface:TextGUI
Drains the input queue and passes the key strokes to the GUI system for processing. For window-based system, it will send each key stroke to the active window for processing. If the input read gives an EOF, it will throw EOFException and this is normally the signal to shut down the GUI (any command coming in before the EOF will be processed as usual before this).- Specified by:
processInput
in interfaceTextGUI
- Returns:
true
if at least one key stroke was read and processed,false
if there was nothing on the input queue (only for non-blocking IO)- Throws:
IOException
- In case there was an underlying I/O error
-
setTheme
Description copied from interface:TextGUI
Sets the global theme to be used by this TextGUI. This value will be set on every TextGUIGraphics object created for drawing the GUI, but individual components can override this if they want. If you don't call this method you should assume that a default theme is assigned by the library. -
getTheme
Description copied from interface:TextGUI
Returns the theme currently assigned to thisTextGUI
-
updateScreen
Description copied from interface:TextGUI
Updates the screen, to make any changes visible to the user.- Specified by:
updateScreen
in interfaceTextGUI
- Throws:
IOException
- In case there was an underlying I/O error
-
getScreen
Description copied from interface:TextGUI
Returns theScreen
for thisWindowBasedTextGUI
- Specified by:
getScreen
in interfaceTextGUI
- Returns:
- the
Screen
used by thisWindowBasedTextGUI
-
isPendingUpdate
public boolean isPendingUpdate()Description copied from interface:TextGUI
This method can be used to determine if any component has requested a redraw. If this method returnstrue
, you may want to callupdateScreen()
.- Specified by:
isPendingUpdate
in interfaceTextGUI
- Returns:
true
if this TextGUI has a change and is waiting for someone to callupdateScreen()
-
getGUIThread
Description copied from interface:TextGUI
The first time this method is called, it will create a new TextGUIThread object that you can use to automatically manage this TextGUI instead of manually callingprocessInput()
andupdateScreen()
. After the initial call, it will return the same object as it was originally returning.- Specified by:
getGUIThread
in interfaceTextGUI
- Returns:
- A
TextGUIThread
implementation that can be used to asynchronously manage the GUI
-
addListener
Description copied from interface:TextGUI
Adds a listener to this TextGUI to fire events on.- Specified by:
addListener
in interfaceTextGUI
- Parameters:
listener
- Listener to add
-
removeListener
Description copied from interface:TextGUI
Removes a listener from this TextGUI so that it will no longer receive events- Specified by:
removeListener
in interfaceTextGUI
- Parameters:
listener
- Listener to remove
-
setBlockingIO
public void setBlockingIO(boolean blockingIO) Enables blocking I/O, causing calls toreadKeyStroke()
to block until there is input available. Notice that you can still poll for input usingpollInput()
.- Parameters:
blockingIO
- Set this totrue
if blocking I/O should be enabled, otherwisefalse
-
isBlockingIO
public boolean isBlockingIO()Checks if blocking I/O is enabled or not- Returns:
true
if blocking I/O is enabled, otherwisefalse
-
fireUnhandledKeyStroke
This method should be called when there was user input that wasn't handled by the GUI. It will fire theonUnhandledKeyStroke(..)
method on any registered listener.- Parameters:
keyStroke
- TheKeyStroke
that wasn't handled by the GUI- Returns:
true
if at least one of the listeners handled the key stroke, this will signal to the GUI that it needs to be redrawn again.
-
invalidate
protected void invalidate()Marks the whole text GUI as invalid and that it needs to be redrawn at next opportunity -
drawGUI
Draws the entire GUI using aTextGUIGraphics
object- Parameters:
graphics
- Graphics object to draw using
-
getCursorPosition
Top-level method for drilling in to the GUI and figuring out, in global coordinates, where to place the text cursor on the screen at this time.- Returns:
- Where to place the text cursor, or
null
if the cursor should be hidden
-
handleInput
This method should take the user input and feed it to the focused component for handling.- Parameters:
key
-KeyStroke
representing the user input- Returns:
true
if the input was recognized and handled by the GUI, indicating that the GUI should be redrawn
-