package net.xoetrope.swing;
import net.xoetrope.xui.WidgetAdapter;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import net.xoetrope.xui.style.XStyle;
import net.xoetrope.xui.XProjectManager;
import net.xoetrope.xui.style.XStyleConstants;
/**
* Adapts the abstract component definition used by the common XUI
* (net.xoetrope.xui) package to the concrete definition of an Swing component.
* <p>Copyright (c) Xoetrope Ltd., 1998-2005</p>
* License: see license.txt
* @version $Revision: 1.5 $
*/
public class SwingWidgetAdapter extends WidgetAdapter
{
protected SwingWidgetAdapter()
{
}
/**
* Get an instance of this adapter. The adapter is itself stateless so only a single
* instance of the class is required.
* @return The adapter instance
*/
public static WidgetAdapter getInstance()
{
if ( instance == null )
instance = new SwingWidgetAdapter();
return instance;
}
/**
* Get the name of the object
* @param component The component instance
* @return The name
*/
public String getName( Object component )
{
return ((Component)component).getName();
}
/**
* Get the number of child components owned by this component, assuming that
* the component is a container.
* @param container the container instance
* @return the number of children
*/
public int getComponentCount( Object container )
{
return ((Container)container).getComponentCount();
}
/**
* Get the children. Assumes that the object is a container
* @param container the container instance
* @return an array of the child components
*/
public Object[] getComponents( Object container )
{
return ((Container)container).getComponents();
}
/**
* Get the child component at a specific index within the container. Assumes
* that the object is a container
* @param container the container instance
* @param i the component index
* @return an array of the child components
*/
public Object getComponent( Object container, int i )
{
return ((Container)container).getComponent( i );
}
/**
* Is the component visible?
* @param component the component instance
* @return true if the component is visible
*/
public boolean isVisible( Object component )
{
return ((Component)component).isVisible();
}
/**
* Set the component visiblity
* @param component the component instance
* @param state true if the component is to be made visible
*/
public void setVisible( Object component, boolean state )
{
((Component)component).setVisible( state );
}
/**
* Remove a component from the container
* @param component the container instance
* @param obj the component to remove
*/
public void remove( Object component, Object obj )
{
((Container)component).remove( (Component)obj );
}
/**
* Remove all components from the container
* @param component the container instance
*/
public void removeAll( Object component )
{
((Container)component).removeAll();
}
/**
* Add a component to the container
* @param container the container instance
* @param comp the component to add
*/
public void add( Object container, Object comp )
{
((Container)container).add( (Component)comp );
}
/**
* Add a component to the container using a layout manager constraint
* @param container the container instance
* @param comp the component to add
* @param constraint the layout constraint
*/
public void add( Object container, Object comp, Object constraint )
{
((Container)container).add( (Component)comp, constraint );
}
/**
* Set the layout manager for the container
* @param component the container instance
* @param lm the LayoutManager instance
*/
public void setLayout( Object component, Object lm )
{
((Container)component).setLayout( (LayoutManager)lm );
}
/**
* Get the layout manager used by the container
* @param component the container instance
* @return the LayoutManager instance
*/
public Object getLayout( Object component )
{
return ((Container)component).getLayout();
}
/**
* Request that the container updates its layout
* @param component the container instance
*/
public void doLayout( Object component )
{
((Component)component).doLayout();
}
/**
* Set the foreground color
* @param component the container instance
* @param c the color
*/
public void setForeground( Object component, Object c )
{
((Component)component).setForeground( (Color)c );
}
/**
* Set the background color
* @param component the container instance
* @param c the color
*/
public void setBackground( Object component, Object c )
{
((Component)component).setBackground( (Color)c );
}
/**
* Set the font
* @param component the container instance
* @param f the font
*/
public void setFont( Object component, Object f )
{
((Component)component).setFont( (Font)f );
}
/**
* Get the size of the component
* @param component the container instance
* @return the size (an instance of java.awt.Dimension) of the object
*/
public Object getSize( Object component )
{
return ((Component)component).getSize();
}
/**
* Get the parent/owner object
* @param component the container instance
* @return the parent Component
*/
public Object getParent( Object component )
{
return ((Component)component).getParent();
}
/**
* Set the enabled state of the component
* @param component the container instance
* @param b true to enable
*/
public void setEnabled( Object component, boolean b )
{
((Component)component).setEnabled( b );
}
/**
* Set the current cursor for the component
* @param component the container instance
* @param cursor the cursor object
*/
public void setCursor( Object component, Object cursor )
{
((Component)component).setCursor( (Cursor)cursor );
}
/**
* Set the current bounds for the component
* @param component the container instance
* @param x the left/x edge coordinates
* @param y the top/y edge coordinates
* @param w the width dimension
* @param h the height dimension
*/
public void setBounds( Object component, int x, int y, int w, int h )
{
((Component)component).setBounds( x, y, w, h );
}
/**
* Set the current location of the component
* @param component the container instance
* @param x the left/x edge coordinates
* @param y the top/y edge coordinates
*/
public void setLocation( Object component, int x, int y )
{
((Component)component).setLocation( x, y );
}
/**
* Set the size of the component
* @param component the container instance
* @param w the width dimension
* @param h the height dimension
*/
public void setSize( Object component, int w, int h )
{
((Component)component).setSize( w, h );
}
/**
* Set the size of the component
* @param component the container instance
* @param w the width dimension
* @param h the height dimension
*/
public void setPreferredSize( Object component, int w, int h )
{
if ( component instanceof JComponent )
((JComponent)component).setPreferredSize( new Dimension( w, h ));
}
/**
* Set the minimum size of the component
* @param component the container instance
* @param w the width dimension
* @param h the height dimension
*/
public void setMinimumSize( Object component, int w, int h )
{
if ( component instanceof JComponent )
((JComponent)component).setMinimumSize( new Dimension( w, h ));
}
/**
* Set the maximum size of the component
* @param component the container instance
* @param w the width dimension
* @param h the height dimension
*/
public void setMaximumSize( Object component, int w, int h )
{
if ( component instanceof JComponent )
((JComponent)component).setMaximumSize( new Dimension( w, h ));
}
/**
* Get the location of the component
* @param component the container instance
* @return the AWT Point object indicating the location
*/
public Object getLocation( Object component )
{
return ((Component)component).getLocation();
}
/**
* Get the location of the component's left (X) edge
* @param component the container instance
* @return the x coordinate
*/
public int getX( Object component )
{
return ((Component)component).getX();
}
/**
* Get the location of the component's top (Y) edge
* @param component the container instance
* @return the y coordinate
*/
public int getY( Object component )
{
return ((Component)component).getY();
}
/**
* Get the component width
* @param component the container instance
* @return the width
*/
public int getWidth( Object component )
{
return ((Component)component).getWidth();
}
/**
* Get the component height
* @param component the container instance
* @return the height
*/
public int getHeight( Object component )
{
return ((Component)component).getHeight();
}
/**
* Set the name of the component
* @param component the container instance
* @param name the new component name
*/
public void setName( Object component, String name )
{
((Component)component).setName( name );
}
/**
* Request that the component repaints itself
* @param component the container instance
*/
public void repaint( Object component )
{
((Component)component).repaint();
}
/**
* Check if the component is a container object, that is that it is capable of
* containing children
* @param container the container instance
* @return true if the object is a container
*/
public boolean isContainer( Object container )
{
return ( container instanceof Container );
}
/**
* Check whether or not constructing an object requires the parent or if an
* argumentless constructor can be used.
* @return true if the parent is needed, otherwise false
*/
public boolean requiresParent()
{
return false;
}
/**
* Sets the text which will be shown if the cursor rests over the component
* @param component the container instance
* @param tooltip the text to show
*/
public void setTooltip( Object component, String tooltip )
{
if ( component instanceof JComponent )
((JComponent)component).setToolTipText( tooltip );
}
/**
* Sets the opaque-property
* @param component the container instance
* @param opaque true or false
*/
public void setOpaque( Object component, boolean opaque )
{
if ( component instanceof JComponent )
((JComponent) component).setOpaque( opaque );
}
public void setHorizontalAlignment( Object component, String value )
{
int align = XAlignmentHelper.getAlignmentOption( value );
if ( component instanceof JLabel )
((JLabel)component).setHorizontalAlignment( align );
else if (component instanceof JTextField)
((JTextField)component).setHorizontalAlignment( align );
else if (component instanceof AbstractButton)
((AbstractButton)component).setHorizontalAlignment( align );
else if (component instanceof XImageButton)
((XImageButton)component).setHorizontalAlignment( align );
}
public void setVerticalAlignment( Object component, String value )
{
int align = XAlignmentHelper.getVerticalAlignmentOption( value );
if ( component instanceof JLabel )
((JLabel)component).setVerticalAlignment( align );
else if ( component instanceof AbstractButton )
((AbstractButton) component).setVerticalAlignment(align);
else if ( component instanceof XImageButton )
((XImageButton) component).setVerticalAlignment(align);
}
public void setBorderType( Object component, String border )
{
if ( component instanceof JComponent ) {
JComponent comp = (JComponent) component;
if ( border.equalsIgnoreCase( "none" ))
comp.setBorder( null );
else if ( border.equalsIgnoreCase( "empty" ))
comp.setBorder( new EmptyBorder( 3, 3, 3, 3 ));
else if (border.equalsIgnoreCase( "thinline" ))
comp.setBorder( new LineBorder(comp.getBackground().brighter(), 1, false));
else if (border.equalsIgnoreCase( "line" ))
comp.setBorder( new LineBorder(comp.getBackground().brighter(), 3, false));
else if (border.equalsIgnoreCase( "thickline" ))
comp.setBorder( new LineBorder(comp.getBackground().brighter(), 5, false));
else if (border.equalsIgnoreCase( "rounded" ))
comp.setBorder( new LineBorder(comp.getBackground().brighter(), 3, true));
else if (border.equalsIgnoreCase( "bevel" ))
comp.setBorder( new BevelBorder(BevelBorder.LOWERED));
}
}
public void setBorderStyle( Object component, String borderStyle )
{
if ( component instanceof JComponent ) {
JComponent comp = (JComponent) component;
XStyle style = XProjectManager.getCurrentProject().getStyleManager().getStyle( borderStyle );
String border = style.getStyleAsString( style.getStyleIndex( XStyleConstants.BORDER_TYPE ));
if ( border == null )
border = "none";
Color color = style.getStyleAsColor( XStyle.COLOR_FORE );
int margint = 0;
int marginl = 0;
int marginb = 0;
int marginr = 0;
boolean margin = false;
String sValue = style.getStyleAsString( style.getStyleIndex( XStyleConstants.MARGIN_SIZE ));
if ( sValue != null ) {
String[] vals = sValue.toString().split( "," );
margin = true;
if ( vals.length == 1 )
margint = marginl = marginb = marginr = Integer.parseInt( vals[ 0 ] );
else {
margint = Integer.parseInt( vals[ 0 ] );
marginl = Integer.parseInt( vals[ 1 ] );
marginb = Integer.parseInt( vals[ 2 ] );
marginr = Integer.parseInt( vals[ 3 ] );
}
}
int paddingt = 0;
int paddingl = 0;
int paddingb = 0;
int paddingr = 0;
boolean padding = false;
sValue = style.getStyleAsString( style.getStyleIndex( XStyleConstants.PADDING_SIZE ));
if ( sValue != null ) {
String[] vals = sValue.toString().split( "," );
padding = true;
if ( vals.length == 1 )
paddingt = paddingl = paddingb = paddingr = Integer.parseInt( vals[ 0 ] );
else {
paddingt = Integer.parseInt( vals[ 0 ] );
paddingl = Integer.parseInt( vals[ 1 ] );
paddingb = Integer.parseInt( vals[ 2 ] );
paddingr = Integer.parseInt( vals[ 3 ] );
}
}
Border b = null;
if ( border.equalsIgnoreCase( "none" ))
b = null;
else if (border.equalsIgnoreCase( "empty" ))
b = new EmptyBorder( 3, 3, 3, 3 );
else if (border.equalsIgnoreCase( "thinline" ))
b = new LineBorder( color, 1, false );
else if (border.equalsIgnoreCase("line"))
b = new LineBorder( color, 3, false );
else if (border.equalsIgnoreCase("thickline"))
b = new LineBorder( color, 10, false );
else if (border.equalsIgnoreCase("rounded"))
b = new LineBorder( color, 3, true );
else if (border.equalsIgnoreCase( "bevel" ))
b = new BevelBorder( BevelBorder.LOWERED );
if (( margin == false ) && ( padding == false ))
comp.setBorder( b );
else if ( b == null )
comp.setBorder( new EmptyBorder( margint + paddingt, marginl
+ paddingl, marginb + paddingb, marginr + paddingr ));
else if ( margin == false )
comp.setBorder( new CompoundBorder( b, new EmptyBorder( paddingt,
paddingl, paddingb, paddingr )));
else if ( padding == false )
comp.setBorder( new CompoundBorder( new EmptyBorder( margint,
marginl, marginb, marginr ), b ));
else
comp.setBorder( new CompoundBorder( new EmptyBorder( margint,
marginl, marginb, marginr ),
new CompoundBorder(b, new EmptyBorder( paddingt,
paddingl, paddingb, paddingr ))));
}
}
}