package net.xoetrope.xui.data.table;
import java.util.List;
import net.xoetrope.xui.data.XModel;
import net.xoetrope.xui.helper.NumberFormatter;
/**
* <p>Provides a model for table rows. The attributes are equivalent to the row
* fields. The row will have no children. As a Java object the fields and rows
* indices are zero based in contrast to the JDBC indexing setup.</p>
* <p>The class is not intended to be used directly except in rare circumstances,
* instead most access will be via the model and bindings or via the
* XTableModel class</p>
* <p>Copyright (c) Xoetrope Ltd. 2001-2003</p>
* $Revision: 2.7 $
* License: see license.txt
*/
public class XRowModel extends XModel
{
protected int rowIdx;
protected XTableModel sourceData;
/**
* Default constructor, no table or row is referenced
*/
public XRowModel()
{
}
/**
* Construct a model node for a table
* @param table the table to reference
* @param row the row to reference
*/
public XRowModel( XTableModel table, int row )
{
sourceData = table;
rowIdx = row;
}
/**
* Get the referenced table
* @return the XTableModel holding the data
*/
public XTableModel getTable()
{
return sourceData;
}
/**
* Set the row to reference
* @param table the table to reference
* @param row the row to reference
*/
public void setRowReference( XTableModel table, int row )
{
sourceData = table;
rowIdx = row;
}
/**
* Get a field as a double value
* <p>This method is required by the XModel interface but in this case it is
* not implemented as it is inappropriate in the context of the class</p>
* <p>Try getDouble instead</p>
* @param elementName the field name
* @return the field value
*/
public double getValueAsDouble( String elementName )
{
/**This method is inappropriate for this node type*/
throw new java.lang.UnsupportedOperationException(
"Method getValueAsDouble() not yet implemented.");
}
/**
* Get a field value
*
*
* @param attribName the field name
* @return the field (XFieldModel) value
*/
public Object get( String attribName )
{
return new XFieldModel( sourceData, rowIdx, sourceData.getAttribute( attribName ));
}
/**
* Gets the model element tag name, e.g. 'Component' from the XML fragment
* <Component ....
* @return the model element name
*/
public String getTagName()
{
return "";
}
/**
* Append a node. Not implemented in this instance as it is not logical
* @param newObject the object to append
*/
public void append( XModel newObject )
{
/**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
}
/**
* Get a field value as an int
* @param attribName the fieldName
* @return the value
*/
public int getInt( String attribName )
{
return new Integer( sourceData.getFieldValue( rowIdx, sourceData.getAttribute( attribName ))).intValue();
}
/**
* Get a field value as an double
* @param attribName the fieldName
* @return the value
*/
public double getDouble( String attribName )
{
return new Double( sourceData.getFieldValue( rowIdx, sourceData.getAttribute( attribName ) ) ).doubleValue();
}
/**
* Get a field value as an String
* @param attribName the fieldName
* @return the value
*/
public String getString( String attribName )
{
return sourceData.getFieldValue( rowIdx, sourceData.getAttribute( attribName ) );
}
/**
* Get the value of the first field in this row
* @return the field value
*/
public String getValue()
{
return sourceData.getFieldValue( rowIdx, 0 );
}
/**
* Gets an individual field value
* @param fieldIdx the field index
* @return the value
*/
public String getFieldValue( int fieldIdx )
{
return sourceData.getFieldValue( rowIdx, fieldIdx );
}
/**
* Get the value of an attribute
* @param i the index of the attribute
* @return the attribute value
*/
public Object getAttribValue( int i )
{
return sourceData.getFieldValue( rowIdx, i );
}
/**
* Get an attribute as a string
* @param i the index of the attribute
* @return the attribute value
*/
public String getAttribValueAsString(int i)
{
return sourceData.getFieldValue( rowIdx, i );
}
/**
* Gets the value attribute of the specified node as a string.
* @param elementName the child element name
* @return the value as a string
*/
public String getValueAsString( String elementName )
{
return sourceData.getAttribValueAsString( sourceData.getAttribute( elementName ));
}
/**
* Get the ID of this row
* @return the row index as a string value
*/
public String getId()
{
return new Integer( rowIdx ).toString();
}
/**
* Get the name of an attribute
* @param i the index of the attribute
* @return the attribute name
*/
public String getAttribName(int i)
{
return sourceData.getAttribName( i );
}
/**
* returns the index of the attribiteNames array whose value is the same
* as the attribName
* @param attribName The name of the attribute we are trying to locate
* @return The index of the attributeNames array containg the name
*/
public int getAttribute( String attribName )
{
return sourceData.getAttribute( attribName );
}
/**
* Set the value of the XModel located at the elementName
* @param attribName The path to the XModel in the format 'base/foo
* @param newObject The new value of the XModel
*/
public void set( String attribName, Object newObject )
{
/**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
}
/**
* Sets the model value
* @param s the new value
*/
public void set( Object s )
{
/**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
fireModelUpdated();
}
/**
* Gets the value attribute - a field value
* @param i the field index
* @return the value of the model
*/
public XModel get( int i )
{
XModel databaseFieldModel = new XFieldModel( sourceData, rowIdx, i );
databaseFieldModel.setParent( this );
return databaseFieldModel;
}
/**
* Gets the value attribute
* @return the value of the model
*/
public Object get()
{
return new XFieldModel( sourceData, rowIdx, 0 );
}
/**
* This method does not nothing it is provided merely as an implementation of the
* XModel interface. A child or attribute can be logically appended to a table node.
* @param id the node id
* @return null as nothing is appended
*/
public Object append( String id )
{
return null;
}
/**
* Get an attribute as an int value
* <p>This method is required by the XModel interface but in this case it is
* not implemented as it is inappropriate in the context of the class</p>
* @param elementName The name of the element whose value we want
* @return The string value of the attributeValues array at position i
*/
public int getValueAsInt( String elementName )
{
/**This method is inappropriate for this node type*/
throw new java.lang.UnsupportedOperationException(
"Method getValueAsInt() not yet implemented.");
}
/**
* Get the value of a field as a model node
* @param i the field index
* @return the new field model node
*/
public XModel getValue( int i )
{
return new XFieldModel( sourceData, rowIdx, i );
}
/**
* Sets the attribute (field) value
* @param i The index of the attributeValues array whose value we want
* @param value the value object
*/
public void setAttribValue( int i, Object value )
{
sourceData.setFieldValue( rowIdx, i, value.toString());
}
/**
* Sets the attribute (field) value
* @param i The index of the attributeValues array whose value we want
* @param attribName - ignored
* @param value the value object
*/
public void setAttribValue( int i, String attribName, Object value )
{
sourceData.setFieldValue( rowIdx, i, value.toString());
}
/**
* Set the value of a field
* @param colIdx the field index
* @param newValue the new field value
*/
public void setFieldValue( int colIdx, String newValue )
{
sourceData.setFieldValue( rowIdx, colIdx, newValue );
}
/**
* Get the hashcode for this row
* @return the row id
*/
public int hashCode()
{
return rowIdx;
}
/**
* Gets the number of fields for this row
* @return the number of fields
*/
public int getNumChildren()
{
return sourceData.getNumAttributes();
}
/**
* Get an attribute value as a double
* @param i The index of the attributeValues array whose value we want
* @return The double value of the attributeValues array at position i
*
* @deprecated use getAttribValueAsDouble( i, decimalSeparator, groupingSeparator )
* instead, if the locale is different from the locale used to write the values
* to the model, then the parsed value may be incorrect.
*/
public double getAttribValueAsDouble( int i )
{
return new Double( sourceData.getFieldValue( rowIdx, i ) ).doubleValue();
}
/**
* Get an attribute value as a double
* @param i The index of the attributeValues array whose value we want
* @param decimalSeparator the decimal separator
* @param groupingSeparator the grouping (thousands) separator
* @return The double value of the attributeValues array at position i
*/
public double getAttribValueAsDouble( int i, char decimalSeparator, char groupSeparator )
{
String v = sourceData.getFieldValue( rowIdx, i );
return NumberFormatter.parseDouble( v, decimalSeparator, groupSeparator );
}
/**
* Get an attribute value as a string
* @param i The index of the attributeValues array whose value we want
* @return The int value of the attributeValues array at position i
*/
public int getAttribValueAsInt( int i )
{
return new Integer( sourceData.getFieldValue( rowIdx, i ) ).intValue();
}
/**
* Remove a node
* <p>This method is required by the XModel interface but in this case it is
* not implemented as it is inappropriate in the context of the class</p>
* @param node - ignored
*/
public void remove( XModel node )
{
/**@todo Implement this abstract method*/
throw new java.lang.UnsupportedOperationException(
"Method remove() not yet implemented." );
}
}