public class TypeAhead
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
private class |
TypeAhead.KeyReader
Type ahead buffer feeding class.
|
Modifier and Type | Field and Description |
---|---|
private boolean |
exit
Reader thread kill flag.
|
private java.lang.Object |
exitLock
Lock access to the exit flag.
|
private static ContextLocal<TypeAhead> |
local
Context local instance of this class.
|
private OutputManager<?> |
mgr
Output manager used to obtain incoming keyboard events.
|
private AssociatedThread |
reader
Key reading thread.
|
private boolean |
suspended
Suspend/resume flag.
|
private java.util.List<java.lang.Integer> |
taBuf
The type-ahead buffer which keeps a strict FIFO ordering.
|
private Watcher |
watch
Watcher instance.
|
Modifier | Constructor and Description |
---|---|
private |
TypeAhead(Watcher watch)
Constructor which ensures that this class can only be used on a
static basis. |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Purges the type ahead buffer.
|
void |
closeReader()
Modify state to cause the key reading thread to exit.
|
private KeyInput |
dequeueEvent()
Dequeue a key code from the head of the queue (if the queue is not
empty), convert it to an event (which resolves the source component)
and return that event.
|
static OutputManager<?> |
getInputSource()
Gets the key reading source currently in use.
|
KeyInput |
getKeystroke(int msec,
boolean honorServerEvents)
Returns the next event from the type ahead buffer or blocks up to the specified number of
milliseconds waiting for input.
|
static TypeAhead |
init(Watcher watch)
Initializes (or re-initializes) the type ahead buffer.
|
static TypeAhead |
instance()
Obtain the context-local instance of this class.
|
void |
notifyOSEvent()
Notify the type-ahead that a OS event was posted.
|
void |
notifyServerEvent()
Notify the type-ahead that a server event was posted.
|
void |
resume()
Resume the type ahead buffer activity and keyboard reading thread.
|
static void |
setInputSource(OutputManager<?> newMgr)
Sets new source to read the key events.
|
void |
setPendingContextSwitch(java.lang.Object context)
Save the given context as a pending context switch which will be
processed later by the key reading thread.
|
private void |
startReader()
Create and start the key reading thread.
|
void |
suspend()
Suspends the type ahead buffer activity and keyboard reading thread.
|
void |
waitForInput()
Conditionally blocks until an input appears in the buffer.
|
private static final ContextLocal<TypeAhead> local
private java.util.List<java.lang.Integer> taBuf
private volatile boolean suspended
private Watcher watch
private AssociatedThread reader
private boolean exit
private java.lang.Object exitLock
private volatile OutputManager<?> mgr
private TypeAhead(Watcher watch)
static
basis.watch
- The CTRL-C signal handler to use.public static TypeAhead instance()
null
if the class was never
initialized.public static TypeAhead init(Watcher watch)
watch
- The CTRL-C signal handler to use.public static OutputManager<?> getInputSource()
public static void setInputSource(OutputManager<?> newMgr)
newMgr
- The output manager to use for key events reading.public void suspend()
public void resume()
public void clear()
public void waitForInput()
public void notifyServerEvent()
public void notifyOSEvent()
public KeyInput getKeystroke(int msec, boolean honorServerEvents)
If no key is received and the honorServerEvents
parameter is set to
true
, this will return null
if the
EventManager.hasServerEvents()
reports that there are pending server events; this
will allow the server events to be processed.
The downside of this implementation (as 4GL shows no fixed order for mixed UI and
server-side events, see EventManager
javadoc for more details) is that keys have
precedence over server-side events and a constant key press would delay server event
processing.
getKeystroke(int, boolean)
was coded this way because the server events are pushed in a
global queue, separated from the UI/key events, as server-events can be posted even when a
WAIT-FOR-like loop is not in effect.
msec
- The maximum time in milliseconds to wait if there is no input.
Less than 0 means an indefinite wait, 0 means no wait (return
an event only if there is already one in the queue) and any
positive value is the maximum number of milliseconds to wait.honorServerEvents
- flag indicating if server events should be honored or not.null
if the read
timed out.public void closeReader()
public void setPendingContextSwitch(java.lang.Object context)
context
- The security context to switch to (if non-null).private void startReader()
private KeyInput dequeueEvent()
null
if the queue is empty.