package net.xoetrope.swt;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import org.eclipse.swt.widgets.Layout;
import java.util.Hashtable;
import net.xoetrope.xui.LayoutHelper;
import net.xoetrope.xui.WidgetAdapter;
import net.xoetrope.xui.XPage;
import net.xoetrope.xui.helper.ReflectionHelper;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.layout.GridLayout;
/**
* A helper class for working with layout managers. This class provides mappings
* between the names of layout styles and constraints and the corresponding Java
* constants.
* <p>
* Copyright (c) Xoetrope Ltd., 2002-2003
* </p>
* <p>
* License: see license.txt
* </p>
* $Revision: 2.21 $
*/
public class XLayoutHelper implements LayoutHelper
{
public static final int LEFT = 0;
public static final int RIGHT = 1;
public static final int TOP = 2;
public static final int BOTTOM = 3;
public static final String[] flowLayoutFields = {
"type", "fill", "wrap", "justify", "pack", "spacing", "marginHeight|hgap", "marginWidth|vgap", "marginTop|mtop", "marginBottom|mbottom",
"marginLeft|mleft", "marginRight|mright"
};
public static final String[] gridLayoutFields = {
"numColumns|cols", "makeColumnsEqualWidth|stdWidth", "horizontalSpacing|hspacing", "verticalSpacing|vspacing", "marginHeight|hgap",
"marginWidth|vgap", "marginTop|mtop", "marginBottom|mbottom", "marginLeft|mleft", "marginRight|mright"
};
/**
* Sets a LayoutManager for the panel
*
* @param cont
* the container whose layout manager is being set or null to set the
* parent panel's layout manager
* @param type
* the layout manager as defined in the XLayoutHelper class
* @return the new layout
*/
public Object addLayout( Object cont, int type )
{
try {
Layout lm = null;
switch ( type ) {
case XPage.BORDER_LAYOUT:
lm = new BorderLayout();
break;
case XPage.FLOW_LAYOUT:
lm = new RowLayout();
break;
case XPage.GRID_LAYOUT:
lm = new GridLayout();
break;
default:
break;
}
WidgetAdapter.getInstance().setLayout( cont, lm );
return lm;
}
catch ( Exception e ) {
return null;
}
}
/**
* Sets a LayoutManager for the panel
*
* @param cont
* the container whose layout manager is being set or null to set the
* parent panel's layout manager
* @param type
* the layout manager as defined in the XLayoutHelper class
* @param attribs
* the attributes of the layout
* @return the new layout
*/
public Object addLayout( Object cont, int type, Hashtable attribs )
{
Layout lm = null;
if ( type != XPage.BOX_LAYOUT )
lm = (Layout)addLayout( cont, type );
if ( attribs != null ) {
switch ( type ) {
case XPage.BORDER_LAYOUT:
break;
case XPage.FLOW_LAYOUT:
setAttributes( lm, attribs, flowLayoutFields );
break;
case XPage.GRID_LAYOUT:
setAttributes( lm, attribs, gridLayoutFields );
break;
case XPage.NULL_LAYOUT:
break;
default:
break;
}
}
return lm;
}
/**
* Gets a constraint object corresponding to a constraint name
*
* @param name
* the constraint name
* @return the constraint object
*/
public int getAlignment( String name )
{
return 0;
}
/**
* Get the layout type enumerated in XPage
*
* @param ls
* the layout style
* @return the type id
*/
public int getLayoutType( String ls )
{
ls = ls.toUpperCase();
if ( ( ls == null ) || ( ls.length() == 0 ) || ls.equals( "NULL" ) )
return XPage.NULL_LAYOUT;
else if ( ls.equals( "FLOW" ) )
return XPage.FLOW_LAYOUT;
else if ( ls.equals( "BORDER" ) )
return XPage.BORDER_LAYOUT;
else if ( ls.equals( "GRID" ) )
return XPage.GRID_LAYOUT;
return XPage.NULL_LAYOUT;
}
/**
* Get the layout type
*
* @param layout
* the layout manager instance
* @return the type name e.g. FLOW or BORDER
*/
public String getLayoutClass( Object layout )
{
if ( layout == null )
return null;
else {
// String className = layout.getClass().getName();
if ( layout instanceof RowLayout )
return "Flow";
else if ( layout instanceof BorderLayout )
return "Border";
else if ( layout instanceof GridLayout )
return "Grid";
}
return null;
}
/**
* Does the layout manager use constraints for its children?
*
* @param layout
* the layout manager instance
* @return true if the layout uses constraints
*/
public boolean getUsesConstraints( Object layout )
{
if ( layout == null )
return false;
else {
// String className = layout.getClass().getName();
if ( layout instanceof RowLayout )
return false;
else if ( layout instanceof BorderLayout )
return true;
else if ( layout instanceof GridLayout )
return true;
}
return false;
}
/**
* Does this layout manager used the absoulte dimensions?
*
* @param layout
* the layout manager instance
* @return true if the X,Y,W,H dimensions are used
*/
public boolean getUsesDimensions( Object layout )
{
if ( layout == null )
return true;
else {
String className = layout.getClass().getName();
if ( className.indexOf( "SpringLayout" ) > 0 )
return true;
else if ( className.indexOf( "GuideLayout" ) > 0 )
return true;
}
return false;
}
/**
* Gets a constraint object corresponding to a constraint name
*
* @param name
* the quoted constraint name e.g. "WEST"
* <ul>
* <li>WEST=BorderLayout.WEST</li>
* <li>EAST=BorderLayout.EAST</li>
* <li>NORTH=BorderLayout.NORTH</li>
* <li>SOUTH=BorderLayout.SOUTH</li>
* <li>CENTER=BorderLayout.CENTER</li>
* <li>AFTER_LAST_LINE=BorderLayout.AFTER_LAST_LINE</li>
* <li>AFTER_LINE_ENDS=BorderLayout.AFTER_LINE_ENDS</li>
* <li>BEFORE_FIRST_LINE=BorderLayout.BEFORE_FIRST_LINE</li>
* <li>BEFORE_LINE_BEGINS=BorderLayout.BEFORE_LINE_BEGINS</li>
* <li>GridBagConstraints=14 GridBagConstraint parameters separated
* by commas
* <OL>
* <li> gridx - The initial gridx value</li>
* <li> gridy - The initial gridy value.</li>
* <li> gridwidth - The initial gridwidth value.</li>
* <li> gridheight - The initial gridheight value.</li>
* <li> weightx - The initial weightx value.</li>
* <li> weighty - The initial weighty value.</li>
* <li> anchorstr - The anchor string value
* (EAST|WEST|NORTH|SOUTH|NORTHEAST|NORTHWEST|SOUTHEAST|SOUTHWEST)</li>
* <li> fillstr - The fill string value (HORIZONTAL|VERTICAL)</li>
* <li> insets top - The initial inset</li>
* <li> insets left - The initial inset</li>
* <li> insets right - The initial inset</li>
* <li> insets bottom - The initial inset</li>
* <li> ipadx - The initial ipadx value</li>
* <li> ipady - The initial ipady value</li>
* </OL>
* </li>
* </ul>
* @return the constraint object
*/
public Object getConstraint( String name )
{
if ( name == null )
return null;
String uname = name.toUpperCase();
if ( uname.equals( "WEST" ) )
return BorderLayout.WEST;
else if ( uname.equals( "EAST" ) )
return BorderLayout.EAST;
else if ( uname.equals( "NORTH" ) )
return BorderLayout.NORTH;
else if ( uname.equals( "SOUTH" ) )
return BorderLayout.SOUTH;
else if ( uname.equals( "CENTER" ) )
return BorderLayout.CENTER;
else if ( uname.equals( "AFTER_LAST_LINE" ) )
return "Last";
else if ( uname.equals( "AFTER_LINE_ENDS" ) )
return "After";
else if ( uname.equals( "BEFORE_FIRST_LINE" ) )
return "First";
else if ( uname.equals( "BEFORE_LINE_BEGINS" ) )
return "Before";
else {
String[] params = uname.split( "," );
if ( params.length == 14 ) {
// Grid Bag Constraints
String anchorstr = params[ 6 ].toUpperCase();
String fillstr = params[ 7 ].toUpperCase();
int anchor = GridBagConstraints.CENTER;
int fill = GridBagConstraints.NONE;
/* Anchor determination */
if ( anchorstr.equals( "EAST" ) )
anchor = GridBagConstraints.EAST;
else if ( anchorstr.equals( "WEST" ) )
anchor = GridBagConstraints.WEST;
else if ( anchorstr.equals( "NORTH" ) )
anchor = GridBagConstraints.NORTH;
else if ( anchorstr.equals( "SOUTH" ) )
anchor = GridBagConstraints.SOUTH;
else if ( anchorstr.equals( "NORTHEAST" ) )
anchor = GridBagConstraints.NORTHEAST;
else if ( anchorstr.equals( "NORTHWEST" ) )
anchor = GridBagConstraints.NORTHWEST;
else if ( anchorstr.equals( "SOUTHEAST" ) )
anchor = GridBagConstraints.SOUTHEAST;
else if ( anchorstr.equals( "SOUTHWEST" ) )
anchor = GridBagConstraints.SOUTHWEST;
/* fill determination */
if ( fillstr.equals( "HORIZONTAL" ) )
fill = GridBagConstraints.HORIZONTAL;
else if ( fillstr.equals( "VERTICAL" ) )
fill = GridBagConstraints.VERTICAL;
return new GridBagConstraints( new Integer( params[ 0 ] ).intValue(), // gridx
// -
// The
// initial
// gridx
// value.
new Integer( params[ 1 ] ).intValue(), // gridy - The initial gridy
// value.
new Integer( params[ 2 ] ).intValue(), // gridwidth - The initial
// gridwidth value.
new Integer( params[ 3 ] ).intValue(), // gridheight - The initial
// gridheight value.
new Double( params[ 4 ] ).doubleValue(), // weightx - The initial
// weightx value.
new Double( params[ 5 ] ).doubleValue(), // weighty - The initial
// weighty value.
anchor, // anchor - The initial anchor value.
fill, // fill - The initial fill value.
new Insets( new Integer( params[ 8 ] ).intValue(), new Integer( params[ 9 ] ).intValue(), new Integer( params[ 10 ] ).intValue(),
new Integer( params[ 11 ] ).intValue() ), // insets - The
// initial insets
// value.
new Integer( params[ 12 ] ).intValue(), // ipadx - The initial ipadx
// value.
new Integer( params[ 13 ] ).intValue() // ipady - The initial ipady
// value.
);
}
}
return name;
}
/**
* Set the attributes of the layout via reflection.
*
* @param lm
* the layout manager instance
* @param attribs
* the table of attributes
* @param fieldNames
* the array of names of the property being set. If the attribute
* name differs from the name of the property being set then the
* field name - attribute name pair can be specified as
* <code>fieldName|attributeName</>
* @param attribute the attribute name
*/
protected void setAttributes( Object lm, Hashtable attribs, String[] fieldNames )
{
for ( int i = 0; i < fieldNames.length; i++ ) {
String attributeName = fieldNames[ i ];
String fieldName;
int pos = attributeName.indexOf( '|' );
if ( pos > 0 ) {
fieldName = attributeName.substring( 0, pos );
attributeName = attributeName.substring( ++pos );
}
else
fieldName = attributeName;
Object value = attribs.get( attributeName );
if ( value != null )
ReflectionHelper.setFieldViaReflection( lm, fieldName, (String)value );
}
}
/**
* Set the attributes for a layout manager
*
* @param cont
* the container
* @param lm
* the layout manager
* @param attrib
* the attribute name
* @param value
* the value of the attribute
*/
public void setAttrib( Object cont, Object lm, String attrib, Object value )
{
String methodName = attrib.substring( 0, 1 ).toUpperCase() + attrib.substring( 1 );
ReflectionHelper.setViaReflection( methodName, lm, value, value.getClass() );
}
/**
* Convert an attribute value to the equivalent BoxLayout constant, defaults
* to x-axis if not found.
*
* @param value
* the attribute value specifying the alignment
* @return the equivalent BoxLayout integer constant
*/
protected Integer getBoxAlignment( String value )
{
if ( value != null ) {
String uvalue = value.toUpperCase();
if ( uvalue.equals( "X" ) || uvalue.equals( "0" ) )
return new Integer( 0 );// BoxLayout.X_AXIS;
else if ( uvalue.equals( "Y" ) || uvalue.equals( "1" ) )
return new Integer( 1 );// BoxLayout.Y_AXIS;
else if ( uvalue.equals( "LINE" ) )
return new Integer( 2 );// BoxLayout.LINE_AXIS;
else if ( uvalue.equals( "PAGE" ) )
return new Integer( 3 );// BoxLayout.PAGE_AXIS;
}
return new Integer( 0 );
}
}