package net.xoetrope.optional.data;
import java.util.Hashtable;
import net.xoetrope.debug.DebugLogger;
import net.xoetrope.optional.data.sql.DatabaseFieldModel;
import net.xoetrope.optional.data.sql.DatabaseRowModel;
import net.xoetrope.optional.data.sql.DatabaseTableModel;
import net.xoetrope.xui.PageSupport;
import net.xoetrope.xui.XTextHolder;
import net.xoetrope.xui.data.XDataBinding;
import net.xoetrope.xui.data.XDataBindingFactory;
import net.xoetrope.xui.data.XModel;
import net.xoetrope.xui.XModelHolder;
import net.xoetrope.xui.XProject;
import net.xoetrope.xui.build.BuildProperties;
/**
* A factory for bindings from this package. This binding factory is implicitly
* registered as part of loading the XOptionalDataSource. The XuiBuilder class
* then uses the factory to create bindings for the supported component types.
* <p>Copyright Xoetrope Ltd. 2003-2004</p>
* $Revision: 2.8 $
* License: see license.txt
*/
public class XOptionalBindingFactory extends XDataBindingFactory
{
/**
* Creates a new instance of XOptionalBindingFactory
* @param project the owner project
*/
private XOptionalBindingFactory( XProject project )
{
super( project );
if ( BuildProperties.DEBUG ) {
String temp = currentProject.getStartupParam( "XDataSourceClass" );
if ( temp == null ) {
DebugLogger.logError( "You must define a value for XDataSourceClass in the startup properties to use XUI's optional data bindings" );
currentProject.fixError( "DataSourceClassMissing", this, null );
}
}
}
/**
* Registe an instance of this binding factory.
*/
public static void register( XProject project )
{
Object instance = project.getObject( "XOptionalBindingFactory" );
if ( instance == null ) {
instance = new XOptionalBindingFactory( project );
project.setObject( "XOptionalBindingFactory", instance );
}
project.registerBindingFactory( (XOptionalBindingFactory)instance );
}
/**
* Try to get a binding factory to construct the binding
* @param page the page that will own the binding
* @param comp the target component
* @param instanceConfig the attributes of the binding instance
* @return the new binding if one could be constructed
*/
public XDataBinding getBinding( PageSupport page, Object comp, Hashtable instanceConfig )
{
Class clazz = comp.getClass();
String className = clazz.getName();
// Split the class name from the package name
// String clsName = className;
// int pos = className.lastIndexOf( '.' );
// if ( pos > 0 )
// clsName = className.substring( pos + 1 );
XModel model = (XModel)currentProject.getModel().get( (String)instanceConfig.get( "source" ));
XDataBinding binding = null;
if ( ( className.indexOf( "XComboBox" ) > 0 ) && ( model instanceof DatabaseTableModel ) )
binding = new XListTableBinding();
else if (( className.indexOf( "XTable" ) > 0 ) || ( comp instanceof XModelHolder ))
binding = new XTableTableBinding();
else if ( ( comp instanceof XTextHolder ) && (( model instanceof DatabaseTableModel ) || ( model instanceof DatabaseRowModel ) || ( model instanceof DatabaseFieldModel )))
binding = new XTextTableBinding();
if ( binding != null )
binding.setup( currentProject, comp, null, instanceConfig );
return binding;
}
}