public class ScreenBuffer
extends java.lang.Object
implements java.io.Externalizable
The reviewed implementation adds more control over the contents of the buffer. The total number of widgets is stored as well as the frame ID. This information allows for calculating widget index into a byte array that keeps widget state: uninitialized or changed.
Only those widgets marked as changed are present in the buffer to minimize data exchange.
Modifier and Type | Class and Description |
---|---|
private static class |
ScreenBuffer.StatefulWidgetRecord
Adds a state bitflag to a widget record.
|
private static class |
ScreenBuffer.WidgetRecord
Stores the details of a given widget.
|
Modifier and Type | Field and Description |
---|---|
private int |
baseId
Frame ID where this screen buffer belongs.
|
private ScreenBuffer.StatefulWidgetRecord[] |
buffer
Core storage (widget ID, current value, state flags).
|
static byte |
CHANGED
State flag
|
private int |
column
Value of dynamically calculated frame column.
|
private UIStatement |
currentStatement
The frame's UI statement being executed.
|
private int |
down
Rows in a dynamic down frame or -1 if no runtime override exists.
|
private WidgetConfig[] |
downConfigs
The widget configs for the current iteration in a down frame.
|
static byte |
ENTERED
State flag
|
private java.lang.String |
frameValue
Value of the last active widget.
|
private ScreenBuffer.WidgetRecord[] |
headers
All header widget values for this frame.
|
private java.util.Map<java.lang.Integer,java.lang.Integer> |
indices
Mapping of widget ID to array index position.
|
private int |
keyCode
Key code which will be transferred in key related events.
|
private int |
keyState
State of extended keys: Alt, Ctrl, Shift, Meta.
|
private int |
line
Current frame line or -1 if it is undefined.
|
private int |
MAX_SPARSE_ARRAY_SIZE
Maximum number of elements to allocate for a sparse array.
|
private int |
MIN_ELEMENTS
Minimum number of elements to allocate when growing the array.
|
private int |
offset
Optimized index lookup for contiguous IDs.
|
private boolean |
permissive
Mark an "extra-sparse" mode.
|
private int |
row
Value of dynamically calculated frame row.
|
private int |
size
Number of non-header widgets in the buffer.
|
static byte |
STATE_MAX
State flag
|
static byte |
STATE_MIN
State flag
|
private java.lang.String |
title
Dynamic title text or
null for no dynamic support. |
static byte |
UNCHANGED
State flag
|
static byte |
UNINITIALIZED
State flag
|
private java.util.Map<java.lang.Integer,java.lang.Object> |
values
The map for some screen values.
|
private int |
widgetId
ID of the last active widget.
|
Constructor and Description |
---|
ScreenBuffer()
The default constructor creates a rudimentary ScreenBuffer which remains empty.
|
ScreenBuffer(int frameId,
int num)
Constructor.
|
ScreenBuffer(int frameId,
int[] ids)
Constructor.
|
ScreenBuffer(int frameId,
int min,
int max)
Constructor which allocates a sparse array buffer (if possible) which will allow all widgets within
the minimum and maximum ID range to be stored/retrieved via the offset method (avoiding the slow
map method of storage).
|
Modifier and Type | Method and Description |
---|---|
void |
addWidget(int widgetID)
Adds a dynamic widget into the screen buffer.
|
private void |
addWidgetInt(int id,
int idx)
Add an uninitialized widget to the buffer at the given index.
|
void |
allocateHeaders(FrameElement[] elements)
Allocate and copy the given header widgets.
|
private int |
calcNextIndex(int widgetID)
Ensure that there is enough capacity in the widget array and return a valid, empty array index.
|
private int |
calcWidgetArraySize(int min,
int max)
Calculate the buffer array size based on the expected widget ID range.
|
void |
clearHeaders()
Removes all header values from the buffer.
|
WidgetConfig[] |
getAndClearDownConfigs()
Get the widget configs for the current frame row, in case this is a DOWN frame.
|
int |
getBaseId()
Gets the frame ID from this screen buffer.
|
ScreenBuffer |
getChanged()
Gets a new
ScreenBuffer where only widgets with the
changed values are present. |
int |
getColumn()
Get current value for dynamically calculated frame column.
|
UIStatement |
getCurrentStatement()
Get the frame's current statement.
|
int |
getDown()
Extracts the caller-specified number of rows for the associated down
frame.
|
java.lang.String |
getFrameValue()
Get the value of the last active widget in the frame.
|
int |
getKeyCode()
Extracts the key code for the current event.
|
int |
getKeyState()
Returns extended keys state: Alt, Ctrl, Shift, Meta.
|
int |
getLine()
Extracts the current
FRAME-LINE for the given frame. |
int |
getRow()
Get current value for dynamically calculated frame row.
|
java.lang.Object |
getScreenValue(int id)
Get current screen value.
|
byte |
getState(int widgetID)
Gets the current state for the given widget ID.
|
java.lang.String |
getTitle()
Extracts the caller-specified dynamic title for the associated frame.
|
int |
getValueWidgetId()
Get ID of the last active widget.
|
java.lang.Object |
getWidgetValue(int widgetID)
Getting the value in the Object form for the given widget ID.
|
private int |
idToIndex(int id)
The array index of the given widget.
|
boolean |
isChanged(int widgetID)
Check if the widget state isn't unchanged or uninitialized.
|
boolean |
isEntered(int widgetID)
Checks the ENTERED state of a widget.
|
boolean |
isUninitialized(int widgetID)
Checks the UNINITIALIZED state of a widget.
|
int[] |
markAllChanged()
Mark all widgets (which had their screen-value set) as changed and return an array of all widget IDs.
|
void |
markAllChangedStatesAsEntered()
Mark all widgets having CHANGED state as entered widgets to protect server states.
|
void |
mergeChanged(ScreenBuffer diff)
Merges this ScreenBuffer with the changed widgets from the specified
one.
|
private int |
offsetIndex(int id)
Calculate the index for the given widget in contiguous node.
|
void |
processHeaders(java.util.function.BiConsumer<java.lang.Integer,java.lang.Object> consume)
Executes the given consumer for each header widget.
|
void |
putColumn(int column)
Store new value for dynamically calculated frame column.
|
void |
putDown(int down)
Stores the caller-specified number of rows for the associated down
frame for client-server transfer.
|
void |
putKeyCode(int keyCode,
int keyState)
Stores the key code for a client-server transfer.
|
void |
putLine(int line)
Stores the current
FRAME-LINE for the given frame. |
void |
putRow(int row)
Store new value for dynamically calculated frame row.
|
void |
putScreenValue(int id,
java.lang.Object screenValue)
Set screen value for specified widget.
|
void |
putTitle(java.lang.String title)
Sets the caller-specified dynamic title for the associated frame.
|
boolean |
putWidgetValue(int widgetID,
java.lang.Object value)
Store the new value for the given widget ID.
|
boolean |
putWidgetValue(int widgetID,
java.lang.Object value,
boolean preserveState)
Store the new value for the given widget ID.
|
void |
readExternal(java.io.ObjectInput in)
Replacement for the default object reading method.
|
private boolean |
reallocate(int wid)
Reallocate the buffer to accommodate the given widget in contiguous mode.
|
void |
removeWidget(int widgetID)
Removing existed widget from screen buffer for the given widget ID.
|
void |
removeWidgetIDs(java.util.Set<java.lang.Integer> widgets)
Remove all widget IDs from the given set if the widget has a non-null value in the screen buffer.
|
private void |
removeWidgetInt(int idx)
Remove the widget at the given index position.
|
void |
reset()
Resets all widgets to the UNINITIALIZED state.
|
void |
resetChanged()
Resets all widgets having CHANGED state.
|
void |
resetEntered(int[] ids)
Resets all widgets having ENTERED state.
|
void |
setCurrentStatement(UIStatement stmt)
Set the frame's current statement, to transport it to the client-side.
|
void |
setDownConfigs(WidgetConfig[] wcfgs)
Used by client-side to set the widget configs for the current frame row.
|
void |
setFrameValue(java.lang.String frameValue)
Save the value of the last active widget in the frame.
|
void |
setState(int widgetID,
byte state)
Stores the new state for the widget with the given widget ID.
|
void |
setValueWidgetId(int widgetID)
Save the ID of last active widget.
|
int |
size()
Gets the current number of widgets in this screen buffer.
|
java.lang.String[] |
view()
Debugging aid to visualize the contents of this instance.
|
void |
writeExternal(java.io.ObjectOutput out)
Replacement for the default object writing method.
|
public static final byte STATE_MIN
public static final byte UNCHANGED
public static final byte UNINITIALIZED
public static final byte CHANGED
public static final byte ENTERED
public static final byte STATE_MAX
private final int MAX_SPARSE_ARRAY_SIZE
private final int MIN_ELEMENTS
private int baseId
private int size
private int offset
private transient java.util.Map<java.lang.Integer,java.lang.Integer> indices
private transient boolean permissive
private ScreenBuffer.StatefulWidgetRecord[] buffer
private ScreenBuffer.WidgetRecord[] headers
private java.util.Map<java.lang.Integer,java.lang.Object> values
private int keyCode
private int keyState
private int down
private int line
private int column
private int row
private java.lang.String title
null
for no dynamic support.private java.lang.String frameValue
private int widgetId
private WidgetConfig[] downConfigs
private UIStatement currentStatement
public ScreenBuffer()
public ScreenBuffer(int frameId, int num)
Avoid using this constructor for any cases that are performance critical. This approach does not easily enable all widgets to be stored by offset and often will result in the slower mapping approach. The exception is when you know the size of the range (maximum widget ID minus the minimum widget ID). In that case the result will work for offset mode.
frameId
- The ID of the frame to which this screen buffer belongs.num
- The total number of widgets expected in the frame.public ScreenBuffer(int frameId, int min, int max)
frameId
- The ID of the frame to which this screen buffer belongs.min
- The smallest widget ID to be used for this screen buffer.max
- The largest widget ID to be used for this screen buffer.public ScreenBuffer(int frameId, int[] ids)
frameId
- the ID of the frame where this ScreenBuffer belongsids
- the array of the widget IDs to put inside the bufferpublic int size()
public int getBaseId()
public void addWidget(int widgetID)
widgetID
- The ID of the widget to be added.public void removeWidget(int widgetID)
widgetID
- The ID of the widget to be added.public boolean putWidgetValue(int widgetID, java.lang.Object value)
In permissive mode, this method will not fail if the widget id is not already known. Instead, the widget is added implicitly. This does not occur under the normal (non-permissive) mode.
widgetID
- The ID of the widget to which the data is related.value
- New value as an Object instance.true
if this was a valid/existing widget.public boolean putWidgetValue(int widgetID, java.lang.Object value, boolean preserveState)
In permissive mode, this method will not fail if the widget id is not already known. Instead, the widget is added implicitly. This does not occur under the normal (non-permissive) mode.
widgetID
- The ID of the widget to which the data is related.value
- New value as an Object instance.preserveState
- Flag indicating if the widget's state is preserved.
When false
, the widget state is set to CHANGED
. Otherwise
it remains unchanged.true
if this was a valid/existing widget.public void allocateHeaders(FrameElement[] elements)
elements
- The header elements to copy.public byte getState(int widgetID)
widgetID
- The ID of the widget to which the data is related.-1
if such ID does not exist.public void setState(int widgetID, byte state)
In permissive mode, this method will not fail if the widget id is not already known. Instead, the widget is added implicitly. This does not occur under the normal (non-permissive) mode.
widgetID
- The ID of the widget to which the data is related.state
- The new state flags.public void removeWidgetIDs(java.util.Set<java.lang.Integer> widgets)
widgets
- The set from which the IDs will be removed.public void processHeaders(java.util.function.BiConsumer<java.lang.Integer,java.lang.Object> consume)
consume
- The function that consumes the header widget values.public java.lang.Object getWidgetValue(int widgetID)
widgetID
- The numeric ID of a widget whose value must be returned.public void putScreenValue(int id, java.lang.Object screenValue)
id
- Widget id.screenValue
- Widget screen value.public java.lang.Object getScreenValue(int id)
id
- Widget id.public void putKeyCode(int keyCode, int keyState)
keyCode
- The P2J key code for the current event.keyState
- State of extended keys: Alt, Ctrl, Shift, Meta. The reason for extra state field is for the
cases when the legacy program depends on native OS APIs.
The layout is as follows:
Bit Index Key
------------------
0 Alt
1 Ctrl
2 Shift
3 Metapublic int getKeyCode()
public int getKeyState()
The layout is as follows: Bit Index Key ------------------ 0 Alt 1 Ctrl 2 Shift 3 Meta
public void putDown(int down)
down
- The number of rows in the frame or 0 for forcing a calculated
value or -1 for no dynamic override of the down size.public int getDown()
public void putLine(int line)
FRAME-LINE
for the given frame.line
- The frame line or -1 for undefined.public int getLine()
FRAME-LINE
for the given frame.public void putRow(int row)
row
- The new value for the frame row or -1 for no explicit value.public int getRow()
public void putColumn(int column)
column
- The new value for the frame column or -1 for no explicitly
set value.public int getColumn()
public void putTitle(java.lang.String title)
title
- The current value of the dynamic title text or
null
if no dynamic title support exists for this
frame.public java.lang.String getTitle()
null
if no dynamic title support exists for this
frame.public ScreenBuffer getChanged()
ScreenBuffer
where only widgets with the
changed values are present. As a side effect the current instance's
CHANGED
flags are reset.ScreenBuffer
public void mergeChanged(ScreenBuffer diff)
diff
- a differential ScreenBuffer
to copy widget values from.public void resetEntered(int[] ids)
ids
- Array of IDs of entered widgets.public void markAllChangedStatesAsEntered()
public int[] markAllChanged()
null
) when there are no
widgets in the buffer.public void resetChanged()
public void reset()
public boolean isEntered(int widgetID)
widgetID
- The ID of the widget to which the data is related.true
if the widget has been modified during the
last input operation.public boolean isChanged(int widgetID)
widgetID
- The ID of the widget to which the data is related.true
if the widget state is not unchanged or uninitialized.public boolean isUninitialized(int widgetID)
widgetID
- The ID of the widget to which the data is related.true
if the widget is in the uninitialized state.public java.lang.String getFrameValue()
public void setFrameValue(java.lang.String frameValue)
frameValue
- The frame value to set.public int getValueWidgetId()
-1
if no such widget was
detected.public void setValueWidgetId(int widgetID)
widgetID
- The widgetId to set.public void setCurrentStatement(UIStatement stmt)
stmt
- The UIStatement
.public UIStatement getCurrentStatement()
currentStatement
.public void clearHeaders()
public java.lang.String[] view()
public void setDownConfigs(WidgetConfig[] wcfgs)
wcfgs
- The widget configs.public WidgetConfig[] getAndClearDownConfigs()
null
if no configs are transferred by the client-side.public void readExternal(java.io.ObjectInput in) throws java.io.IOException, java.lang.ClassNotFoundException
readExternal
in interface java.io.Externalizable
in
- The input source from which fields will be restored.java.io.IOException
- In case of I/O errors.java.lang.ClassNotFoundException
- If payload can't be instantiated.public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException
writeExternal
in interface java.io.Externalizable
out
- The output destination to which fields will be written.java.io.IOException
- In case of I/O errors.private int calcNextIndex(int widgetID)
widgetID
- The ID of the widget to which the data is related.private boolean reallocate(int wid)
wid
- Widget ID.private void addWidgetInt(int id, int idx)
id
- The widget ID to add.idx
- The element index at which to add.private void removeWidgetInt(int idx)
idx
- The array index to remove.private int idToIndex(int id)
id
- The widget to lookup.private int offsetIndex(int id)
id
- The widget ID.private int calcWidgetArraySize(int min, int max)
min
- The smallest widget ID to be used for this screen buffer.max
- The largest widget ID to be used for this screen buffer.