package net.xoetrope.swt;
import net.xoetrope.xui.XAttributedComponent;
import net.xoetrope.xui.XModelHolder;
import net.xoetrope.xui.data.XModel;
import net.xoetrope.xui.style.XStyleComponent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
/**
* @todo add listeners
* @todo add data bindings
* @todo add simple path/node selection interfaces
*/
/**
* A tree control, wraps JTree
* <p>
* Copyright (c) Xoetrope Ltd., 1998-2004<br>
* License: see license.txt $Revision: 2.4 $
*/
public class XTree extends Tree implements XAttributedComponent, XStyleComponent, XModelHolder
{
private String styleName;
protected XModel model;
protected boolean updateModelSelection = false;
protected boolean usesDatabase = false;
protected boolean showAttrib = false;
/**
* Create a new Tree component
*
* @param parent
* parent object
*/
public XTree( Object parent )
{
super( (Composite)parent, SWT.VIRTUAL | SWT.BORDER );
setHeaderVisible( false );
new TreeColumn( this, SWT.CENTER );
}
/**
* Suppress the subclassing exception
*/
protected void checkSubclass()
{
}
/**
* Set one or more attributes of the component.
*
* @param attribName
* the name of the attribute
* @param attribValue
* the value of the attribute
* @return 0 for success, non zero for failure or to require some further
* action
*/
public int setAttribute( String attribName, Object attribValue )
{
String attribNameLwr = attribName.toLowerCase();
String attribValueStr = (String)attribValue;
String attribValueLwr = null;
if ( attribValue != null )
attribValueLwr = attribValueStr.toLowerCase();
if ( attribNameLwr.equals( "showattrib" ) )
showAttrib = attribValueStr.equals( "true" ) || attribValueStr.equals( "1" );
else if ( attribNameLwr.equals( "tooltip" ) )
setToolTipText( attribValueStr );
else if ( attribNameLwr.equals( "visible" ) )
setVisible( attribValueLwr.equals( "true" ) || attribValueLwr.equals( "1" ) );
else
return -1;
return 0;
}
/**
* Set the tree style name
*
* @param style
* the style name
*/
public void setStyle( String style )
{
styleName = style;
}
/**
* Get the style name
*
* @return the style name
*/
public String getStyleName()
{
return styleName;
}
/**
* Set the XModel which we will be generating the table from
*
* @param xmodel
* the XModel of data
*/
public void setModel( XModel xmodel )
{
model = xmodel;
if ( model != null ) {
usesDatabase = ( model.getClass().getName().indexOf( "DatabaseTableModel" ) > 0 );
model.get();
}
update();
}
/**
* Get the model
*/
public XModel getModel()
{
return model;
}
/**
* Update the tree
*/
public void update()
{
removeAll();
for ( int i = getColumnCount() - 1; i >= 0; i-- )
getColumn( i ).dispose();
if ( usesDatabase )
databaseRefill();
else
staticRefill();
}
/**
* Refill from the database
*/
public void databaseRefill()
{
int nbItems = model.getNumChildren();
int nbColumns = model.getNumAttributes();
System.out.println( "Number items : " + nbItems );
System.out.println( "Number columns : " + nbColumns );
if ( nbItems > 0 && nbColumns > 0 ) {
for ( int i = 0; i < nbItems; i++ ) {
TreeItem ti = new TreeItem( this, SWT.NONE );
ti.setText( getValueAt( i, 0 ) );
for ( int j = 1; j < nbColumns; j++ ) {
TreeItem ti2 = new TreeItem( ti, SWT.NONE );
String s = getValueAt( i, j );
ti2.setText( showAttrib ? model.getAttribName( j ) + " : " + s : s );
}
}
}
}
/**
* Refill from static elements
*/
public void staticRefill()
{
int nbItems = model.getNumChildren();
for ( int i = 0; i < nbItems; i++ ) {
String text = getText( model, i );
System.out.println( text );
TreeItem ti = new TreeItem( this, SWT.NONE );
ti.setText( text );
staticRefill( model.get( i ), ti );
}
}
/**
* Refill from static elements
*
* @param xmodel
* parent model
* @param ti
* parent tree item
*/
private void staticRefill( XModel xmodel, TreeItem ti )
{
int nbItems = xmodel.getNumChildren();
for ( int i = 0; i < nbItems; i++ ) {
String text = getText( xmodel, i );
TreeItem treeItem = new TreeItem( ti, SWT.NONE );
treeItem.setText( text );
staticRefill( xmodel.get( i ), treeItem );
}
}
/**
* Get the value of the indexes
*
* @param rowIndex
* row index
* @param columnIndex
* column index
* @return the value
*/
public String getValueAt( int rowIndex, int columnIndex )
{
return (String)model.get( rowIndex ).get( columnIndex ).get();
}
/**
* Get the appropriate text inside the model
*
* @param xmodel
* parent model
* @param i
* index of the children
* @return the text
*/
public String getText( XModel xmodel, int i )
{
XModel xm = xmodel.get( i );
String value = xm.getAttribValueAsString( xm.getAttribute( "value" ) );
if ( value != null )
return value;
return xm.getAttribValueAsString( xm.getAttribute( "id" ) );
}
}