- 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 booleanblockingIOprivate booleandirtyprivate ThemeguiThemeprivate java.util.List<TextGUI.Listener>listenersprivate Screenscreenprivate TextGUIThreadtextGUIThread
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractTextGUI(TextGUIThreadFactory textGUIThreadFactory, Screen screen)Constructor forAbstractTextGUIthat requires aScreenand a factory for creating the GUI thread
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddListener(TextGUI.Listener listener)Adds a listener to this TextGUI to fire events on.protected abstract voiddrawGUI(TextGUIGraphics graphics)Draws the entire GUI using aTextGUIGraphicsobjectprotected booleanfireUnhandledKeyStroke(KeyStroke keyStroke)This method should be called when there was user input that wasn't handled by the GUI.protected abstract TerminalPositiongetCursorPosition()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.TextGUIThreadgetGUIThread()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().ScreengetScreen()Returns theScreenfor thisWindowBasedTextGUIThemegetTheme()Returns the theme currently assigned to thisTextGUIprotected abstract booleanhandleInput(KeyStroke key)This method should take the user input and feed it to the focused component for handling.protected voidinvalidate()Marks the whole text GUI as invalid and that it needs to be redrawn at next opportunitybooleanisBlockingIO()Checks if blocking I/O is enabled or notbooleanisPendingUpdate()This method can be used to determine if any component has requested a redraw.protected KeyStrokepollInput()Polls the underlying input queue for user input, returning either aKeyStrokeornullbooleanprocessInput()Drains the input queue and passes the key strokes to the GUI system for processing.protected KeyStrokereadKeyStroke()Reads one key from the input queue, blocking or non-blocking depending on if blocking I/O has been enabled.voidremoveListener(TextGUI.Listener listener)Removes a listener from this TextGUI so that it will no longer receive eventsvoidsetBlockingIO(boolean blockingIO)Enables blocking I/O, causing calls toreadKeyStroke()to block until there is input available.voidsetTheme(Theme theme)Sets the global theme to be used by this TextGUI.voidupdateScreen()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 forAbstractTextGUIthat requires aScreenand a factory for creating the GUI thread- Parameters:
textGUIThreadFactory- Factory class to use for creating theTextGUIThreadclassscreen- What underlyingScreento 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
KeyStrokeornullif 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 aKeyStrokeornull- Returns:
KeyStrokerepresenting the user input ornullif there was none- Throws:
java.io.IOException- In case of an I/O error while reading input
-
processInput
public boolean processInput() throws java.io.IOExceptionDescription copied from interface:TextGUIDrains 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:
processInputin interfaceTextGUI- Returns:
trueif at least one key stroke was read and processed,falseif 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:TextGUISets 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:TextGUIReturns the theme currently assigned to thisTextGUI
-
updateScreen
public void updateScreen() throws java.io.IOExceptionDescription copied from interface:TextGUIUpdates the screen, to make any changes visible to the user.- Specified by:
updateScreenin interfaceTextGUI- Throws:
java.io.IOException- In case there was an underlying I/O error
-
getScreen
public Screen getScreen()
Description copied from interface:TextGUIReturns theScreenfor thisWindowBasedTextGUI- Specified by:
getScreenin interfaceTextGUI- Returns:
- the
Screenused by thisWindowBasedTextGUI
-
isPendingUpdate
public boolean isPendingUpdate()
Description copied from interface:TextGUIThis method can be used to determine if any component has requested a redraw. If this method returnstrue, you may want to callupdateScreen().- Specified by:
isPendingUpdatein interfaceTextGUI- Returns:
trueif this TextGUI has a change and is waiting for someone to callupdateScreen()
-
getGUIThread
public TextGUIThread getGUIThread()
Description copied from interface:TextGUIThe 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:
getGUIThreadin interfaceTextGUI- Returns:
- A
TextGUIThreadimplementation that can be used to asynchronously manage the GUI
-
addListener
public void addListener(TextGUI.Listener listener)
Description copied from interface:TextGUIAdds a listener to this TextGUI to fire events on.- Specified by:
addListenerin interfaceTextGUI- Parameters:
listener- Listener to add
-
removeListener
public void removeListener(TextGUI.Listener listener)
Description copied from interface:TextGUIRemoves a listener from this TextGUI so that it will no longer receive events- Specified by:
removeListenerin 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 totrueif blocking I/O should be enabled, otherwisefalse
-
isBlockingIO
public boolean isBlockingIO()
Checks if blocking I/O is enabled or not- Returns:
trueif 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- TheKeyStrokethat wasn't handled by the GUI- Returns:
trueif 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 aTextGUIGraphicsobject- 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
nullif 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-KeyStrokerepresenting the user input- Returns:
trueif the input was recognized and handled by the GUI, indicating that the GUI should be redrawn
-
-