package net.xoetrope.builder.helper;
import net.xoetrope.xui.XProjectManager;
import net.xoetrope.xui.data.XBaseModel;
import net.xoetrope.xui.data.XModel;
/**
* A utility class to help construct a HTML like table structure
* <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
* @version $Revision: 2.3 $
*/
public class XTableModelHelper
{
public XTableModelHelper()
{
}
/**
* Creates a new dataset within the model with the specified name. The new
* XModel is appended to the root and returned
* @param name The name to be given to the new XModel
* @return The newly created XModel
*/
public static XModel createDataSet( String name )
{
return createDataSet( name, XProjectManager.getModel() );
}
public static XModel createDataSet( String name, XModel model )
{
XModel dset = (XModel)model.get( name );
if ( dset == null ) {
dset = new XBaseModel();
dset.setId( name );
dset.setTagName( "dataset" );
model.append( dset );
}
return dset;
}
public static XModel createDataItem( String name, String value, XModel model )
{
XBaseModel child = new XBaseModel();
child.setTagName( "data" );
child.setId( name );
child.setAttribValue( XBaseModel.VALUE_ATTRIBUTE, value );
model.append( child );
return child;
}
/**
* Checks that the base Datasets node exists within the model and if not
* creates one. The newly created XModel is returned.
* @return The newly created XModel
*/
private static XModel getDatasetsNode()
{
XModel dsets = null;
XModel root = XProjectManager.getModel();
if( root.getNumChildren()==0 ){
dsets = (XModel)root.get( "base" );
root.setId("Datasets");
root.setTagName("Datasets");
}
return dsets;
}
/**
* Adds a new table XModel node to the model node
* The XModel which will have the newly created table XModel
* appended to it
* @param model The XModel which will have the newly created model appended
* to it
* @param name The name of the new XModel
* @return The newly created table XModel
*/
public static XModel createTable( XModel model, String name )
{
XBaseModel table = new XBaseModel();
table.setTagName( "table" );
table.setId( "items" );
model.append( table );
return table;
}
/**
* Creates a new 'th' node and returns it
* @param model The model which will have the newly created XModel appended to
* it
* @return The newly created th node
*/
public static XModel addHeader( XModel model )
{
XBaseModel heading = new XBaseModel();
heading.setTagName( "th" );
model.append( heading );
return heading;
}
/**
* Add a new 'tr' node to the passed XModel
* @param model The model which will have the newly created XModel appended to
* it
* @return The newly created th node
*/
public static XModel addRow( XModel model )
{
XBaseModel row = new XBaseModel();
row.setTagName( "tr" );
model.append( row );
return row;
}
/**
* Add data to the specified model.
* @param model The node which we are adding the data to
* @param name The name of the new data item
* @param value The value of the new data item
* @return The newly created data item
*/
public static XModel addData( XModel model, String name, String value )
{
XBaseModel item = new XBaseModel();
item.setTagName( "td" );
item.setId( name );
item.set( value );
model.append( item );
return item;
}
}