at.spardat.xma.mdl.table
Class TableWM

java.lang.Object
  extended byat.spardat.xma.mdl.WModel
      extended byat.spardat.xma.mdl.table.TableBaseWM
          extended byat.spardat.xma.mdl.table.TableWM
All Implemented Interfaces:
Descriptive, ISelectable, ITableWM, Synchronization, Transactional
Direct Known Subclasses:
TableWMClient

public class TableWM
extends TableBaseWM
implements ISelectable, ITableWM

A table widget model where the programmer is in full control on what rows the table has. Rows may be added, replaced or removed. Every row may have an additional image displayed with the row. Rows are stored in an ordered collection and may be accessed either by a unique String key or by a zero based row index.

Besides managing the rows, a single or multiple selection state is controlled (interface ISelectable). The selected rows are identified by their String keys.


Field Summary
protected  int updateCount_
          A counter which is incremented after every update.
 
Fields inherited from class at.spardat.xma.mdl.table.TableBaseWM
columnCount_, isAtServer_
 
Fields inherited from class at.spardat.xma.mdl.WModel
S_NULL
 
Fields inherited from interface at.spardat.xma.mdl.table.ITableWM
S_LAST, S_MULTI_SELECT, S_ONE_WAY
 
Constructor Summary
TableWM(short id, Page pm, int numColumns, int style)
          Constructor
 
Method Summary
protected  boolean add(int rowIndex, java.lang.String key, Atom[] atoms)
          Adds a row to the table at a provided zero based row index.
 boolean changed()
          Yields true if this WidgetModel has changed since the last server side event (or creation).
 void cleanUpAfterServerEvent()
          If this table is one-way, we remove all rows after a server event finished.
 void clear()
          Removes all rows from the table and deselects all rows.
 void commit()
          This method clears the change history.
 boolean containsKey(java.lang.String key)
          Returns true if this table contains a row with the provided key.
 void describe(DNode n)
          describe puts all relevant information about this into the provided node.
 void deselect(java.lang.String key)
          Deselects a particular entry.
 void deselectAll()
          All selected entries get deselected.
 void equalsCS(WModel mServer, int syncPoint)
          Compares this widget model (which must be a client side one) with the server buddy mServer and returns true if they are correct with respect to syncPoint.
 int estimateMemory()
          Estimates the number of bytes this object consumes in memory.
 void externalize(XmaOutput xo, boolean forceFull)
          Externalizing either saves the actual state or deltas (changes) in a provided XMAObjectOutput.
 TableRow getRow(int rowIndex)
          Returns the table row at a provided zero based row index.
 TableRow getRow(java.lang.String key)
          Returns the table row for a provided key.
 java.lang.String getSelected()
          Returns the id of the selected element.
 java.lang.String[] getSelection()
          Returns a newly created array containing the ids of the selected entries or an empty array, if nothing is selected.
 int getSelectionCount()
          Returns the number of selected elements.
 boolean handle(ModelChangeEvent event)
          All events that might change a WidgetModel are executed via this method.
 int indexOf(java.lang.String key)
          Returns the index at which the row with the provided key is located or -1 if no row with key is here.
 void internalize(XmaInput in)
          Updates the state of this with information of the given XMAObjectInput.
 void internalRemoveRows()
          Removes only the rows but leave selection unchanged.
 boolean isMultiSelect()
          Returns true if more than one entry may be selected.
 boolean isOneWay()
          Returns if this table is one way.
 boolean isSelected(java.lang.String key)
          Returns true if the entry with the provided key is selected, false otherwise.
 boolean isStrict()
          Returns if this Selectable has strict behaviour, that is, the selected keys must always be in the underlying domain of this.
 void randomlyChange()
          Make random changes to this
 void removeRow(int rowIndex)
          Removes a row from this table at a provided index.
 boolean removeRow(java.lang.String key)
          Removes a row with a particular key.
 void rollback()
          This method restores this Widget Model to its state immediately after the last server side event (or creation).
 void select(java.lang.String key)
          Sets the entry with a provided key as selected.
 void selectByModelIndex(int index)
          Selects the row at the provided zero-based index.
 int size()
          Returns the number of rows in this table.
 
Methods inherited from class at.spardat.xma.mdl.table.TableBaseWM
getColumnCount, getPage
 
Methods inherited from class at.spardat.xma.mdl.WModel
getId, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface at.spardat.xma.mdl.table.ITableWM
getColumnCount
 

Field Detail

updateCount_

protected int updateCount_
A counter which is incremented after every update. The purpose is to detect illegal deferred usage of TableRow objects (which cannot be updated anymore).

Constructor Detail

TableWM

public TableWM(short id,
               Page pm,
               int numColumns,
               int style)
Constructor

Parameters:
id - uniquely identifies the model within its page
pm - the enclosing page model this widget model belongs to.
numColumns - number of columns this table has
style - bit or combination of the style constants S_*.
Method Detail

changed

public boolean changed()
Description copied from class: WModel
Yields true if this WidgetModel has changed since the last server side event (or creation). Some models have a more complex state, e.g., a table has a domain and a selection, where either may change. In this case, subclasses will provide more detailed information on what aspect of the model really has changed.

Specified by:
changed in interface Transactional
Specified by:
changed in class WModel
Returns:
true if changed, false otherwise
See Also:
Transactional.changed()

rollback

public void rollback()
Description copied from class: WModel
This method restores this Widget Model to its state immediately after the last server side event (or creation).

Specified by:
rollback in interface Transactional
Specified by:
rollback in class WModel
See Also:
Transactional.rollback()

commit

public void commit()
Description copied from class: WModel
This method clears the change history. Calling rollback afterwards won't have any effect.

Specified by:
commit in interface Transactional
Specified by:
commit in class WModel
See Also:
Transactional.commit()

handle

public boolean handle(ModelChangeEvent event)
Description copied from class: WModel
All events that might change a WidgetModel are executed via this method.

Specified by:
handle in class WModel
Parameters:
event - the event carrying the information of a change.
Returns:
true if change has been applied, false if it has been rejected.
See Also:
WModel.handle(at.spardat.xma.mdl.ModelChangeEvent)

select

public void select(java.lang.String key)
Description copied from interface: ISelectable
Sets the entry with a provided key as selected. If this model does not allow multiselection, a previous selected element is deselected before.

If isStrict() and the provided key is not contained in the underlying domain, this method does nothing.

Specified by:
select in interface ISelectable
Parameters:
key - the entry to select
See Also:
ISelectable.select(java.lang.String)

deselect

public void deselect(java.lang.String key)
Description copied from interface: ISelectable
Deselects a particular entry. If the entry for the provided key is not selected, this method does nothing.

Specified by:
deselect in interface ISelectable
Parameters:
key - identifies the entry to deselect. If the entry with the provided key is not selected, this method does nothing.
See Also:
ISelectable.deselect(java.lang.String)

deselectAll

public void deselectAll()
Description copied from interface: ISelectable
All selected entries get deselected. After calling this method, the condition getSelectionCount() == 0 holds.

Specified by:
deselectAll in interface ISelectable
See Also:
ISelectable.deselectAll()

isMultiSelect

public boolean isMultiSelect()
Description copied from interface: ISelectable
Returns true if more than one entry may be selected.

Specified by:
isMultiSelect in interface ISelectable
Returns:
boolean true if the widget model realizing this interface allows multiselection.
See Also:
ISelectable.isMultiSelect()

getSelected

public java.lang.String getSelected()
Description copied from interface: ISelectable
Returns the id of the selected element. If nothing is selected, null is returned. If more than one entry is selected, some of them is returned. Note that the selected id need not have to be in the underlying domain of this if !isStrict().

Usually, this method is the preferred way of querying the selection state if !isMultiSelect().

Specified by:
getSelected in interface ISelectable
Returns:
key of selected entry or null if nothing is selected
See Also:
ISelectable.getSelected()

getSelection

public java.lang.String[] getSelection()
Description copied from interface: ISelectable
Returns a newly created array containing the ids of the selected entries or an empty array, if nothing is selected.

Specified by:
getSelection in interface ISelectable
Returns:
array of the selected keys. The length of the array equals getSelectionCount().
See Also:
ISelectable.getSelection()

getSelectionCount

public int getSelectionCount()
Description copied from interface: ISelectable
Returns the number of selected elements.

Specified by:
getSelectionCount in interface ISelectable
Returns:
number of selected elements
See Also:
ISelectable.getSelectionCount()

isSelected

public boolean isSelected(java.lang.String key)
Description copied from interface: ISelectable
Returns true if the entry with the provided key is selected, false otherwise.

Specified by:
isSelected in interface ISelectable
See Also:
ISelectable.isSelected(java.lang.String)

isStrict

public boolean isStrict()
Description copied from interface: ISelectable
Returns if this Selectable has strict behaviour, that is, the selected keys must always be in the underlying domain of this. In the case of !isStrict(), the selected key may be freely choosen and need not be drawn from the domain.

Strict behaviour is only supported if !isMultiSelect(). That means, isMultiSelect() always implies isStrict().

Specified by:
isStrict in interface ISelectable
Returns:
true if the widget model implementing this interface supports strict behaviour, false otherwise.
See Also:
ISelectable.isStrict()

selectByModelIndex

public void selectByModelIndex(int index)
Description copied from interface: ITableWM
Selects the row at the provided zero-based index. If index is less than zero or greater equal size(), this method does nothing.

Note that this method selects using an index of the table-model. At the XMA client, if the table has been sorted, this need not necessarely correspond to be the index you see in the SWT-table.

Specified by:
selectByModelIndex in interface ITableWM
Parameters:
index - zero based index of the row to select.
See Also:
ITableWM.selectByModelIndex(int)

externalize

public void externalize(XmaOutput xo,
                        boolean forceFull)
                 throws java.io.IOException
Description copied from interface: Synchronization
Externalizing either saves the actual state or deltas (changes) in a provided XMAObjectOutput. The default behaviour is to serialize the changes if changes are beeing tracked. This behaviour may be overwritten by forceFull. If true, the complete state information is written.

If forceFull is false, the object implementing this interface must also implement Transactional. If the object implementing this is able to track changes, these are the changes since the last syncpoint.

Specified by:
externalize in interface Synchronization
Parameters:
xo - the serialization destination
forceFull - if true, the complete state is written and deltas are ignored.
Throws:
java.io.IOException - on serialization errors
See Also:
Synchronization.externalize(at.spardat.xma.serializer.XmaOutput, boolean)

internalize

public void internalize(XmaInput in)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException
Description copied from interface: Synchronization
Updates the state of this with information of the given XMAObjectInput. in either contains changes or the complete new state information. If in contains changes the implementing class also must implement Transactional and the state after calling internalize is !Transactional.changed(), e.g., a syncpoint is defined.

This method is not allowed to throw Exceptions because that would leave models in an inconsistent state. Thrown exceptions are considered to be programming errors. Internalize must not throw technical exceptions which indicate system or ressource failures. That implies that the person implementing internalize is not allowed to access ressources outside the JVM, either directly or via calls to other methods. Examples of ressource access which must not be triggered are calls to communication of file system services.

Specified by:
internalize in interface Synchronization
Parameters:
in - the XMAObjectInput. If a XMAObjectInputChangeListener is set in the object in, it will be notified after applying changes read from in.
Throws:
java.lang.ClassNotFoundException - on serialization errors
java.io.IOException - on serialization errors
See Also:
Synchronization.internalize(at.spardat.xma.serializer.XmaInput)

isOneWay

public boolean isOneWay()
Returns if this table is one way.

Specified by:
isOneWay in interface ITableWM
See Also:
ITableWM.S_ONE_WAY

size

public int size()
Returns the number of rows in this table.

Specified by:
size in interface ITableWM

add

protected boolean add(int rowIndex,
                      java.lang.String key,
                      Atom[] atoms)
Adds a row to the table at a provided zero based row index.

If the row is not added at the end of the table, runtime order is O(n) since an array is internally used to store the table.

Parameters:
rowIndex - the zero based index of the new row. Must be greater or equal to zero and not greater than size().
key - the key of the new row
atoms - Atom array
Returns:
true if row has been added or false if this table already contained a row with the specified key.
Throws:
java.lang.IllegalArgumentException - if rowIndex is invalid.

containsKey

public boolean containsKey(java.lang.String key)
Returns true if this table contains a row with the provided key.

Specified by:
containsKey in interface ITableWM

indexOf

public int indexOf(java.lang.String key)
Returns the index at which the row with the provided key is located or -1 if no row with key is here. This is a time consuming operation of O(n).

Specified by:
indexOf in interface ITableWM
Parameters:
key - the key of the row that is looked up.
Returns:
zero based index of the key or -1 if this table does not contain a row with the provided key.

getRow

public TableRow getRow(java.lang.String key)
Returns the table row for a provided key.

The returned TableRow may be used to query and modify the row. It must not be cached outside for later reuse because the returned row becomes invalid if the table is modified in other ways.

Specified by:
getRow in interface ITableWM
Parameters:
key - the key whose row is wanted
Returns:
the TableRow or null, if there is no row with the provided key.

getRow

public TableRow getRow(int rowIndex)
Returns the table row at a provided zero based row index.

The returned TableRow may be used to query and modify the row. It must not be cached outside for later reuse because the returned row becomes invalid if the table is modified in other ways.

Specified by:
getRow in interface ITableWM
Parameters:
rowIndex - the index of the row
Returns:
a TableRow, never null
Throws:
java.lang.ArrayIndexOutOfBoundsException - if rowIndex invalid

removeRow

public void removeRow(int rowIndex)
Removes a row from this table at a provided index.

Specified by:
removeRow in interface ITableWM
Parameters:
rowIndex - the zero based row index.
Throws:
java.lang.IndexOutOfBoundsException - if rowIndex invalid.

removeRow

public boolean removeRow(java.lang.String key)
Removes a row with a particular key.

Note that this method is of runtime order O(n). If performance matters, please use removeRow(int) instead.

Specified by:
removeRow in interface ITableWM
Parameters:
key - the key whose row is to be removed.
Returns:
true if removed, false if this table does not contain a row for the provided key.

clear

public void clear()
Removes all rows from the table and deselects all rows.

Specified by:
clear in interface ITableWM
Specified by:
clear in class WModel

internalRemoveRows

public void internalRemoveRows()
Removes only the rows but leave selection unchanged. Internal method for test purpose. This method is not intended to be called from outside and result in undefined behaviour!!!


describe

public void describe(DNode n)
Description copied from interface: Descriptive
describe puts all relevant information about this into the provided node. The implementing method may construct subnodes of node.

Specified by:
describe in interface Descriptive
Overrides:
describe in class WModel
See Also:
Descriptive.describe(at.spardat.xma.mdl.util.DNode)

randomlyChange

public void randomlyChange()
Make random changes to this

Specified by:
randomlyChange in class WModel

equalsCS

public void equalsCS(WModel mServer,
                     int syncPoint)
Description copied from class: WModel
Compares this widget model (which must be a client side one) with the server buddy mServer and returns true if they are correct with respect to syncPoint. If syncPoint is zero, the client model has been externalized and the changes have been applied to the server model. If synPoint is one, the server model has been externalized and the chagnes have been applied to the client model.

This method is for debugging purpose and assumes that client and server models are running within the same VM. It tests if the models the programmer views after a synchronization operation are consistent.

This method requires that this is a client side model.

This method does not throw an exception if models are the same in terms of their contained model data. If it is allowed that models differ (for example some one way tables), this method must take this fact into account. If the models differ, this indicates a programming error and a RuntimeException is thrown.

Specified by:
equalsCS in class WModel
Parameters:
mServer - the server side model partner for this.
syncPoint - 0 if the point in time is after a client to server sychnronization, 1 if the point is after a server to client synchronization.
See Also:
WModel.equalsCS(at.spardat.xma.mdl.WModel, int)

estimateMemory

public int estimateMemory()
Estimates the number of bytes this object consumes in memory.

Specified by:
estimateMemory in class WModel
Returns:
estimated number of bytes this model consumes in memory

cleanUpAfterServerEvent

public void cleanUpAfterServerEvent()
If this table is one-way, we remove all rows after a server event finished.

Overrides:
cleanUpAfterServerEvent in class WModel
See Also:
WModel.cleanUpAfterServerEvent()