- java.lang.Object
-
- com.googlecode.lanterna.gui2.AbstractTextGUI
-
- All Implemented Interfaces:
TextGUI
- Direct Known Subclasses:
MultiWindowTextGUI
public abstract class AbstractTextGUI extends java.lang.Object implements TextGUI
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
Fields Modifier and Type Field Description private boolean
blockingIO
private boolean
dirty
private Theme
guiTheme
private java.util.List<TextGUI.Listener>
listeners
private Screen
screen
private TextGUIThread
textGUIThread
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractTextGUI(TextGUIThreadFactory textGUIThreadFactory, Screen screen)
Constructor forAbstractTextGUI
that requires aScreen
and a factory for creating the GUI thread
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
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 boolean
fireUnhandledKeyStroke(KeyStroke keyStroke)
This method should be called when there was user input that wasn't handled by the GUI.protected abstract TerminalPosition
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.TextGUIThread
getGUIThread()
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()
.Screen
getScreen()
Returns theScreen
for thisWindowBasedTextGUI
Theme
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
invalidate()
Marks the whole text GUI as invalid and that it needs to be redrawn at next opportunityboolean
isBlockingIO()
Checks if blocking I/O is enabled or notboolean
isPendingUpdate()
This method can be used to determine if any component has requested a redraw.protected KeyStroke
pollInput()
Polls the underlying input queue for user input, returning either aKeyStroke
ornull
boolean
processInput()
Drains the input queue and passes the key strokes to the GUI system for processing.protected KeyStroke
readKeyStroke()
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
setTheme(Theme theme)
Sets the global theme to be used by this TextGUI.void
updateScreen()
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 Detail
-
screen
private final Screen screen
-
listeners
private final java.util.List<TextGUI.Listener> listeners
-
blockingIO
private boolean blockingIO
-
dirty
private boolean dirty
-
textGUIThread
private TextGUIThread textGUIThread
-
guiTheme
private Theme guiTheme
-
-
Constructor Detail
-
AbstractTextGUI
protected AbstractTextGUI(TextGUIThreadFactory textGUIThreadFactory, Screen screen)
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 Detail
-
readKeyStroke
protected KeyStroke readKeyStroke() throws java.io.IOException
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:
java.io.IOException
- In case of an I/O error while reading input
-
pollInput
protected KeyStroke pollInput() throws java.io.IOException
Polls the underlying input queue for user input, returning either aKeyStroke
ornull
- Returns:
KeyStroke
representing the user input ornull
if there was none- Throws:
java.io.IOException
- In case of an I/O error while reading input
-
processInput
public boolean processInput() throws java.io.IOException
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:
java.io.IOException
- In case there was an underlying I/O errorjava.io.EOFException
- In the input stream received an EOF marker
-
setTheme
public void setTheme(Theme theme)
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
public Theme getTheme()
Description copied from interface:TextGUI
Returns the theme currently assigned to thisTextGUI
-
updateScreen
public void updateScreen() throws java.io.IOException
Description copied from interface:TextGUI
Updates the screen, to make any changes visible to the user.- Specified by:
updateScreen
in interfaceTextGUI
- Throws:
java.io.IOException
- In case there was an underlying I/O error
-
getScreen
public Screen 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
public TextGUIThread 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
public void addListener(TextGUI.Listener listener)
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
public void removeListener(TextGUI.Listener listener)
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
protected final boolean fireUnhandledKeyStroke(KeyStroke keyStroke)
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
protected abstract void drawGUI(TextGUIGraphics graphics)
Draws the entire GUI using aTextGUIGraphics
object- Parameters:
graphics
- Graphics object to draw using
-
getCursorPosition
protected abstract TerminalPosition 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
protected abstract boolean handleInput(KeyStroke key)
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
-
-