Package org.beryx.textio.web
Class WebTextTerminal
- java.lang.Object
-
- org.beryx.textio.AbstractTextTerminal<WebTextTerminal>
-
- org.beryx.textio.web.WebTextTerminal
-
- All Implemented Interfaces:
TextTerminal<WebTextTerminal>
,DataApi
public class WebTextTerminal extends AbstractTextTerminal<WebTextTerminal> implements DataApi
ATextTerminal
that allows accessing the application via a browser. It works only in conjunction with a web server supporting theDataApi
(such asSparkDataServer
) and a web component that accesses this API (typically via textterm.js).
-
-
Field Summary
Fields Modifier and Type Field Description static long
DEFAULT_TIMEOUT_DATA_CLEARED
static long
DEFAULT_TIMEOUT_HAS_ACTION
static long
DEFAULT_TIMEOUT_NOT_EMPTY
-
Fields inherited from class org.beryx.textio.AbstractTextTerminal
DEFAULT_USER_INTERRUPT_KEY, SYSPROP_PROPERTIES_FILE_LOCATION
-
-
Constructor Summary
Constructors Constructor Description WebTextTerminal()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
abort()
This method is typically called when a text-based input/output session has been aborted by the user or when a severe error occurred.void
addSetting(java.lang.String key, java.lang.Object value)
void
addSettings(TextTerminalData.KeyValue... keyValues)
WebTextTerminal
createCopy()
void
dispose(java.lang.String resultData)
This method is typically called at the end of a text-based input/output session in order to allow the terminal to release its screen resources.TextTerminalData
getTextTerminalData()
This method is called by the web component while polling for databoolean
moveToLineStart()
Moves the cursor to the start of the current line of text in order to allow overwriting the current line.void
postHandlerCall(java.lang.String handlerId, java.lang.String partialInput)
This method is called by the web component in response to a handler call (triggered by typing its associated key combination).void
postUserInput(java.lang.String newInput)
This method is called by the web component to post the user inputvoid
postUserInput(java.lang.String newInput, boolean userInterrupt, java.lang.String handlerId)
void
postUserInterrupt(java.lang.String partialInput)
This method is called by the web component in response to a user interrupt (typically triggered by typing Ctrl+Q).void
println()
Terminates the current line by writing the line separator string.void
rawPrint(java.lang.String message)
Prints the message in its raw form.java.lang.String
read(boolean masking)
Reads a line of textboolean
registerHandler(java.lang.String keyStroke, java.util.function.Function<WebTextTerminal,ReadHandlerData> handler)
Associates a key combination to a handler.boolean
registerUserInterruptHandler(java.util.function.Consumer<WebTextTerminal> handler, boolean abortRead)
Registers a handler that will be called in response to a user interrupt.boolean
resetLine()
Clears the current line of text.boolean
resetToBookmark(java.lang.String bookmark)
Clears the text after the given bookmark.protected void
setAction(TextTerminalData.Action action)
protected void
setAction(TextTerminalData.Action action, java.lang.String actionData)
boolean
setBookmark(java.lang.String bookmark)
Sets a bookmark with the given name at the current position.void
setOnAbort(java.lang.Runnable onAbort)
void
setOnDispose(java.lang.Runnable onDispose)
void
setTimeoutDataCleared(long timeoutDataCleared)
void
setTimeoutHasAction(long timeoutHasAction)
void
setTimeoutNotEmpty(long timeoutNotEmpty)
void
setUserInterruptKey(int code, boolean ctrl, boolean shift, boolean alt)
void
setUserInterruptKey(java.lang.String keyStroke)
-
Methods inherited from class org.beryx.textio.AbstractTextTerminal
addDefaultProperty, getDefaultProperties, getProperties, getPropertiesPrefixes, getPropertiesReader, init, initProperties, initProperties, initProperties
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.beryx.textio.TextTerminal
applyWithPropertiesConfigurator, dispose, executeWithPropertiesConfigurator, executeWithPropertiesPrefix, print, print, printf, printf, println, println, rawPrint
-
-
-
-
Field Detail
-
DEFAULT_TIMEOUT_NOT_EMPTY
public static final long DEFAULT_TIMEOUT_NOT_EMPTY
- See Also:
- Constant Field Values
-
DEFAULT_TIMEOUT_HAS_ACTION
public static final long DEFAULT_TIMEOUT_HAS_ACTION
- See Also:
- Constant Field Values
-
DEFAULT_TIMEOUT_DATA_CLEARED
public static final long DEFAULT_TIMEOUT_DATA_CLEARED
- See Also:
- Constant Field Values
-
-
Method Detail
-
setTimeoutNotEmpty
public void setTimeoutNotEmpty(long timeoutNotEmpty)
-
setTimeoutHasAction
public void setTimeoutHasAction(long timeoutHasAction)
-
setTimeoutDataCleared
public void setTimeoutDataCleared(long timeoutDataCleared)
-
createCopy
public WebTextTerminal createCopy()
-
dispose
public void dispose(java.lang.String resultData)
Description copied from interface:TextTerminal
This method is typically called at the end of a text-based input/output session in order to allow the terminal to release its screen resources. The terminal should be able to rebuild the released resources when a print or read method is subsequently called. The default implementation does nothing.- Specified by:
dispose
in interfaceTextTerminal<WebTextTerminal>
- Parameters:
resultData
- stringified information about the outcome of the input/output session; may be null,
-
abort
public void abort()
Description copied from interface:TextTerminal
This method is typically called when a text-based input/output session has been aborted by the user or when a severe error occurred. The default implementation does nothing.- Specified by:
abort
in interfaceTextTerminal<WebTextTerminal>
-
resetLine
public boolean resetLine()
Description copied from interface:TextTerminal
Clears the current line of text. Since not all terminals support this feature, the default implementation callsTextTerminal.println()
and returns false.- Specified by:
resetLine
in interfaceTextTerminal<WebTextTerminal>
- Returns:
- true, if the terminal supports this feature and the current line has been successfully cleared.
-
moveToLineStart
public boolean moveToLineStart()
Description copied from interface:TextTerminal
Moves the cursor to the start of the current line of text in order to allow overwriting the current line. Since not all terminals support this feature, the default implementation callsTextTerminal.println()
and returns false.- Specified by:
moveToLineStart
in interfaceTextTerminal<WebTextTerminal>
- Returns:
- true, if the terminal supports this feature and the cursor has been moved to the start of the current line.
-
setBookmark
public boolean setBookmark(java.lang.String bookmark)
Description copied from interface:TextTerminal
Sets a bookmark with the given name at the current position. The bookmark name can be subsequently used in a call toTextTerminal.resetToBookmark(String)
in order to clear the text after this bookmark. If a bookmark with this name already exists, it will be overwritten. Since not all terminals support this feature, the default implementation does nothing and returns false.- Specified by:
setBookmark
in interfaceTextTerminal<WebTextTerminal>
- Returns:
- true, if the terminal supports bookmarking and the bookmark has been successfully set.
-
resetToBookmark
public boolean resetToBookmark(java.lang.String bookmark)
Description copied from interface:TextTerminal
Clears the text after the given bookmark. The bookmark name can be subsequently used in a call toTextTerminal.resetToBookmark(String)
in order to clear the text after this bookmark. Since not all terminals support this feature, the default implementation callsTextTerminal.println()
and returns false.- Specified by:
resetToBookmark
in interfaceTextTerminal<WebTextTerminal>
- Returns:
- true, if the terminal supports bookmarking, the given bookmark exists and the text after it has been successfully cleared.
-
setOnDispose
public void setOnDispose(java.lang.Runnable onDispose)
-
setOnAbort
public void setOnAbort(java.lang.Runnable onAbort)
-
read
public java.lang.String read(boolean masking)
Description copied from interface:TextTerminal
Reads a line of text- Specified by:
read
in interfaceTextTerminal<WebTextTerminal>
- Parameters:
masking
- true, if the input should be masked (for example to enter a password)- Returns:
- the entered text
-
setAction
protected void setAction(TextTerminalData.Action action)
-
setAction
protected void setAction(TextTerminalData.Action action, java.lang.String actionData)
-
rawPrint
public void rawPrint(java.lang.String message)
Description copied from interface:TextTerminal
Prints the message in its raw form. This method expects a single line of text. The behavior is undefined if the string contains line separators.- Specified by:
rawPrint
in interfaceTextTerminal<WebTextTerminal>
-
println
public void println()
Description copied from interface:TextTerminal
Terminates the current line by writing the line separator string.- Specified by:
println
in interfaceTextTerminal<WebTextTerminal>
-
registerUserInterruptHandler
public boolean registerUserInterruptHandler(java.util.function.Consumer<WebTextTerminal> handler, boolean abortRead)
Description copied from interface:TextTerminal
Registers a handler that will be called in response to a user interrupt. The event that triggers a user interrupt is usually Ctrl+C, but in general it is terminal specific. For example, a Swing based terminal may send a user interrupt when the X close button of its window is hit. Since not all terminals support this feature, the default implementation just returns false.- Specified by:
registerUserInterruptHandler
in interfaceTextTerminal<WebTextTerminal>
- Parameters:
handler
- the action to be performed in response to a user interrupt.abortRead
- true, if the current read operation should be aborted on user interrupt.- Returns:
- true, if the terminal supports this feature and the handler has been registered; false, otherwise.
-
registerHandler
public boolean registerHandler(java.lang.String keyStroke, java.util.function.Function<WebTextTerminal,ReadHandlerData> handler)
Description copied from interface:TextTerminal
Associates a key combination to a handler. Since not all terminals support this feature, the default implementation just returns false.- Specified by:
registerHandler
in interfaceTextTerminal<WebTextTerminal>
- Parameters:
keyStroke
- the key combination associated with the handler. It should have the same format as the argument ofKeyStroke.getKeyStroke(String)
.handler
- the action to be performed when thekeyStroke
is detected during a read operation.- Returns:
- true, if the terminal supports this feature and the handler has been associated with the given key combination; false, otherwise.
-
getTextTerminalData
public TextTerminalData getTextTerminalData()
Description copied from interface:DataApi
This method is called by the web component while polling for data- Specified by:
getTextTerminalData
in interfaceDataApi
-
postUserInput
public void postUserInput(java.lang.String newInput, boolean userInterrupt, java.lang.String handlerId)
-
postUserInput
public void postUserInput(java.lang.String newInput)
Description copied from interface:DataApi
This method is called by the web component to post the user input- Specified by:
postUserInput
in interfaceDataApi
-
postUserInterrupt
public void postUserInterrupt(java.lang.String partialInput)
Description copied from interface:DataApi
This method is called by the web component in response to a user interrupt (typically triggered by typing Ctrl+Q).- Specified by:
postUserInterrupt
in interfaceDataApi
- Parameters:
partialInput
- the partially entered input when the user interrupt occurred.
-
postHandlerCall
public void postHandlerCall(java.lang.String handlerId, java.lang.String partialInput)
Description copied from interface:DataApi
This method is called by the web component in response to a handler call (triggered by typing its associated key combination).- Specified by:
postHandlerCall
in interfaceDataApi
- Parameters:
handlerId
- the id of the handler to be called.partialInput
- the partially entered input when the hander call occurred.
-
setUserInterruptKey
public void setUserInterruptKey(java.lang.String keyStroke)
-
addSetting
public void addSetting(java.lang.String key, java.lang.Object value)
-
addSettings
public void addSettings(TextTerminalData.KeyValue... keyValues)
-
setUserInterruptKey
public void setUserInterruptKey(int code, boolean ctrl, boolean shift, boolean alt)
-
-