at.spardat.xma.mdl.table.TableRow Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
// @(#) $Id: TableRow.java 6379 2010-08-23 15:11:50Z gub $
package at.spardat.xma.mdl.table;
import java.util.Date;
import at.spardat.enterprise.util.Types;
import at.spardat.xma.mdl.Atom;
import at.spardat.xma.mdl.AtomTransferServer;
/**
* Represents a row in a TableWM. The constructor must be used if a new
* row is to be inserted in a table. TableWM.getRow is used to retrieve
* a particular row, which may be updated immediately after calling getRow.
*
* Rows become invalid if the table is modified in any way. All methods
* in this class will throw an IllegalStateException if a TableRow
* object is accessed again after the table has been modified.
*
* @author YSD, 02.05.2003 10:54:25
*/
public class TableRow {
/**
* Creates a new row using the provided parameters and inserts it at a given index.
*
* @param table the table where the row should be inserted.
* @param rowIndex the zero based row index. Must be greater than zero and less
* than or equal to table.size().
* @param key the key of the new row.
* @param cells array of objects to store in the cells. The objects may be either
* AtomicAttrVals (if used at the server) or a JDK object as defined in
* {@link at.spardat.xma.mdl.Atom#newInstance(Object) newInstance}.
* @param imageId an id of an image that should be displayed in front of the row.
* Use zero to indicate that no image should be used.
* @exception IllegalArgumentException if rowIndex is out of range,
* the key is already in the table or cells contains
* to few values.
*/
public TableRow (ITableWM table, int rowIndex, String key, Object [] cells, int imageId) {
table_ = (TableWM)table;
atoms_ = newAtomArray ((TableWM)table, cells, imageId);
boolean success = table_.add(rowIndex, key, atoms_);
if (!success) throw new IllegalArgumentException("duplicate key");
updateCount_ = table_.updateCount_;
index_ = rowIndex;
key_ = key;
}
/**
* Creates a new row using the provided parameters and inserts it at the end of the table.
*
* @param table the table where the row should be inserted.
* @param key the key of the new row.
* @param cells array of objects to store in the cells.
* @param imageId an id of an image that should be displayed in front of the row.
* Use zero to indicate that no image should be used.
* @exception IllegalArgumentException if a row with
* key is already in the table or cells contains
* to few values.
*/
public TableRow (ITableWM table, String key, Object [] cells, int imageId) {
this (table, table.size(), key, cells, imageId);
}
/**
* Sets the image id of this row. An image may be used to be displayed at
* a row level, i.e, different rows may have different images. The
* images must be registered in the component.
*
* @param imageId which identifies an image at the component level.
* @exception IllegalStateException if the table was modified after retrieving this row.
*/
public void setImageId (int imageId) {
checkUnchanged();
atoms_[table_.columnCount_] = new Atom(imageId);
table_.updateRow(this);
tableChanged();
}
/**
* Returns the image id if one has been set before or 0 if none
* has been set.
*
* @exception IllegalStateException if the table was modified after retrieving this row.
*/
public int getImageId () {
checkUnchanged();
Atom a = atoms_[table_.columnCount_];
if (a == null) return 0;
else return a.toInt();
}
/**
* Returns the cell at a provided column index. This method is intended to be used
* at the client side of XMA, if the user wants to extract the value of a particular
* cell.
*
* @param colIndex zero based column index
* @return Atom representing the value of the cell or null, if the cell
* value has not been set.
* @exception IllegalArgumentException if colIndex invalid
* @exception IllegalStateException if the table was modified after retrieving this row.
*/
public Atom getCell (int colIndex) {
checkUnchanged();
if (colIndex >= table_.columnCount_) throw new IllegalArgumentException();
return atoms_[colIndex];
}
/**
* Sets the value of a cell from a provided object. Do not use this method
* if you want to update many cells of a row. In this case, use
* setCells instead.
*
* @param colIndex zero based index of the column
* @param value the value to set. May be either an AtomicAttrVal
* (if used at the server) or a JDK object as defined in
* {@link at.spardat.xma.mdl.Atom#newInstance(Object) newInstance}.
* If value is null, the cell is reset.
* @exception IllegalArgumentException if value is of unsupported
* type or colIndex is invalid.
* @exception IllegalStateException if the table was modified after retrieving this row.
*/
public void setCell (int colIndex, Object value) {
checkUnchanged();
if (colIndex >= table_.columnCount_) throw new IllegalArgumentException();
setCellInAtomArray(atoms_, colIndex, value);
table_.updateRow(this);
tableChanged();
}
/**
* Sets the value of all cells of this rows from the objects provided
* in an array. This method has the same effect as calling setCell on each
* array entry.
*
* @param values array of objects. The length of the array must be
* equal to the number of columns in the table.
* @exception IllegalArgumentException if value is of unsupported
* type or the length of values is less than the
* number of columns.
* @exception IllegalStateException if the table was modified after retrieving this row.
*/
public void setCells (Object [] values) {
checkUnchanged();
for (int i=0; iTableRow has a representation in a UI library if
* the UI is already created.
* In the case of SWT, every TableRow has a SWT TableItem assigned,
* that may also be accessed by a zero based offset in the SWT table.
* If you want to set some visual attributes on the TableItem, you
* may call this method and retrieve a TableItem from the SWT table in
* order to change some visual attributes.
*
* Warnings:
*
* - Note that the returned UI index differs from the offset in the widget model
* if the table has been sorted.
*
- Note that you must not change attributes in the SWT table that may also be
* changed in the widget model. I.e., you must not change the row data or the
* image of the rows in any way, you must not add or remove rows from the SWT table.
*
- This method is of runtime order O(n). Therefore u cannot iterate
* over the table and call this method for every row. That would result in O(n*n)
* which won't work for tables of moderate and larger size.
*
*
* @return If your
* code is running at the client side and you have a constructed UI this
* method returns the SWT row index of this row. Otherwise -1 is returned.
*/
public int getUIRowIndex () {
checkUnchanged();
TableUIDelegateClient uiDel = getUIDelegate();
int modelIndex = index_;
if (index_ == -1) modelIndex = table_.indexOf(key_);
if (uiDel != null) return uiDel.modelIndex2SwtIndex(modelIndex);
return -1;
}
/**
*
* @return the ITableWM where this row belongs to.
* @since 2.0.4
* @author s3460
*/
public ITableWM getTableWM() {
return table_;
}
/**
* Returns the UIDelegate of the widget model or null, if we are executing at
* the server or the UI is not constructed yet.
*/
private TableUIDelegateClient getUIDelegate () {
if (!table_.getPage().isAtServer()) {
TableUIDelegateClient uiDel = (TableUIDelegateClient) ((TableWMClient)table_).getUIDelegate();
if (uiDel.isUIAttached()) return uiDel;
}
return null;
}
// must be called at the begin of every method of this to ensure that
// the table did not change
private void checkUnchanged () {
if (updateCount_ != table_.updateCount_) throw new IllegalStateException();
}
// must be called after a modification of the table
private void tableChanged () {
updateCount_ = table_.updateCount_;
}
/**
* Used by the table to construct a row
*/
TableRow () {
}
/**
* Constructs an Atom array that will be stored in the table.
*
* @return newly created Atom array
* @exception IllegalArgumentException if cells has too few columns.
*/
private Atom[] newAtomArray (TableWM table, Object [] cells, int imageId) {
if (cells.length < table.columnCount_) throw new IllegalArgumentException();
Atom[] atoms = new Atom[table.columnCount_+1];
for (int i=0; i 0) atoms[table.columnCount_] = new Atom (imageId);
return atoms;
}
/**
* Sets a particular atom in an Atom array from a provided object.
*
* @param atoms arrays of atoms
* @param colIndex must be less than columnCount of the table.
* @param value value to set; uses Atom.newInstance or
* AtomTransferServer.newInstance.
* @exception IllegalArgumentException if colIndex invalid.
*/
private void setCellInAtomArray (Atom[] atoms, int colIndex, Object value) {
if (colIndex >= table_.columnCount_) throw new IllegalArgumentException();
Atom a = null;
if (value != null) {
byte type = table_.getColumnType(colIndex);
if(value instanceof Date&&(type==Types.T_DATE||type==Types.T_TIMESTAMP)) {
a = new Atom (type, (Date)value);
} else {
if (table_.isAtServer_) a = AtomTransferServer.newInstance(value);
else a = Atom.newInstance(value);
}
}
atoms[colIndex] = a;
}
TableWM table_;
// the key of the row
String key_;
// row index; -1 if unknown
int index_ = -1;
// drawn from table to detect illegal updates
int updateCount_;
// redundantly stored here; this is a copy of the row in the table;
// modifying it does not modify the table.
Atom[] atoms_;
}