package net.xoetrope.swing.tree;
import java.util.Hashtable;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import net.xoetrope.optional.data.pojo.XPojoContext;
import net.xoetrope.optional.data.pojo.XPojoModel;
import net.xoetrope.xui.data.XBaseModel;
import net.xoetrope.xui.data.XModel;
import net.xoetrope.xui.data.XModelAdapter;
import net.xoetrope.xui.data.XModelAdapterConfigurable;
/**
* An adapter to allow the XPojoModel to be used with tree controls.
*/
public class XTreePojoModelAdapter extends XTreeModelAdapter
implements XModelAdapterConfigurable
{
/**
* The model node
*/
protected XPojoModel model;
/**
* The name of the property defining
* child nodes
*/
protected String children;
/**
* The name of the property whose value
* will appear as the tree node
*/
protected String dispAttr;
/**
* Configuration of this adapter
*/
protected Hashtable configuration;
/**
* Constructs a new adapter
*/
public XTreePojoModelAdapter()
{
super();
}
/**
* Configures this adapter
* @param instanceConfiguratin Hashtable containing
* configuration details.
*/
public void configure( Hashtable bindingConfiguration, Hashtable instanceConfiguration )
{
configuration = instanceConfiguration;
if ( instanceConfiguration != null ) {
children = (String)configuration.get( "children" );
dispAttr = (String)configuration.get( "attrib" );
}
}
/**
* Get the number of children belong to the model node that this object adapts
* @return the number of children
*/
public int getNumChildren()
{
XPojoModel mdl = getChildrenNode();
return ( mdl != null ? mdl.getNumChildren() : 0 );
}
/**
* Returns the model node wrapping children
* of this tree node.
* @return XPojoModel pojo model node
*/
private XPojoModel getChildrenNode()
{
if ( model.isCollection() || ( children == null ))
return model;
else
return (XPojoModel)model.get( children );
}
/**
* Gets the individual list item value
* @param i The index of the listitem
* @return The value of the listitem
*/
public Object get( int i )
{
return ( getChildrenNode().get( i ));
}
/**
* 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 selected list item/node
*/
public Object getSelected()
{
return model.get();
}
/**
* Set the adapter source
* @param src the model
*/
public void setModel( XModel src )
{
model = (XPojoModel)src;
setUserObject( model.get() );
populateTreeModel();
}
/**
* Get the model being used by this adapter
* @return the model node
*/
public XModel getModel()
{
return model;
}
/**
* Gets the name of the model node
* @return the name
*/
public String getTagName()
{
return model.getAttribValueAsString( XBaseModel.ID_ATTRIBUTE );
}
/**
* Add the children of this node to the tree model
* @param src the source model node
*/
protected void populateTreeModel()
{
removeAllChildren();
XPojoModel mdl = getChildrenNode();
int numChildren = mdl.getNumChildren();
// add the children
for ( int i = 0; i < numChildren; i++ ) {
XTreePojoModelAdapter childAdapter = new XTreePojoModelAdapter();
childAdapter.configure( null, configuration );
childAdapter.setModel( mdl.get( i ));
add( childAdapter );
}
}
/**
* Returns the String representation of this tree node
*/
public String toString()
{
if ( model.isCollection() )
return "";
XModel dispModel = ( dispAttr != null ?
(XModel)model.get( dispAttr ) : model );
Object o = dispModel.get();
return ( o != null ? o.toString() : "null" );
}
}