package net.xoetrope.optional.data;
import net.xoetrope.xui.data.table.XTableModel;
import net.xoetrope.xui.data.XModel;
import net.xoetrope.xui.data.XModelAdapter;
import net.xoetrope.xui.data.table.XRowModel;
/**
* <p>Adapts an XModel to provide access to child nodes as a list. This class is
* intended to facilitate uses of tables and combo boxes or list components </p>
* <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
* License: see license.txt
* $Revision: 2.2 $
* License: see license.txt
*/
public class XTableModelAdapter implements XModelAdapter
{
private XTableModel model;
private int outputFieldIdx = 0;
/**
* Create a new adapter for a model node
* @param src the node to adapt
*/
public XTableModelAdapter( XModel src )
{
model = (XTableModel)src;
}
/**
* Create a new adapter for a model node
*/
public XTableModelAdapter()
{
}
/**
* Get the number of children that the model node has
* @return the number of children
*/
public int getNumChildren()
{
return model.getNumChildren();
}
/**
* Gets the individual list item value
* @param i The index of the listitem
* @return The value of the listitem
*/
public Object get( int i )
{
XRowModel row = (XRowModel)model.get( i );
return row.getFieldValue( outputFieldIdx );
}
/**
* Gets the individual list item value
* @param i The index of the listitem
* @param fieldIdx the field index
* @return The value of the listitem
*/
public Object get( int i, int fieldIdx )
{
XRowModel row = (XRowModel)model.get( i );
return row.getFieldValue( fieldIdx );
}
/**
* Set the value of the listitem
* @param o The new value
*/
public void set( Object o )
{
model.set( o );
}
/**
* Gets the value of the selected item from the list.
* @return the field value from the current row/record
*/
public Object getSelected()
{
return model.get();
}
/**
* Gets the value of the selected item from the list.
* @return the field value
* @param fieldIdx the index (zero based) of the database table's field or column
*/
public Object getSelected( int fieldIdx )
{
return model.getFieldValue( fieldIdx );
}
/**
* Set the field to return for lists;
* @param fieldIdx the field index (zero based)
*/
public void setOutputField( int fieldIdx )
{
outputFieldIdx = fieldIdx;
}
/**
* Get the adapter source
* @return the model node that holds the table (the DatabaseTableMode node)
*/
public XModel getModel()
{
return model;
}
/**
* Set the adapter source
* @param src the model
*/
public void setModel( XModel src )
{
model = (XTableModel)src;
}
/**
* Locate a key value in the underlying data source
* @param key the key to locate
* @param keyColumnIdx the index of the key column
* @return the row/record index taht contains the first instance of the key,
* or -1 if the key is not found
*/
public int find( String key, int keyColumnIdx )
{
model.sync();
int numChildren = model.getNumChildren();
for ( int i = 0; i < numChildren; i++ ) {
String s = model.get( i ).get( keyColumnIdx ).toString();
if ( s.equals( key ) )
return i;
}
return -1;
}
/**
* Force a sync/update of the table
*/
public void sync()
{
model.sync();
}
}