View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.x.impl.swing;
    14
    15  import java.util.HashMap;
    16  import java.util.Iterator;
    17  import java.util.Map;
    18
    19  import javax.swing.BorderFactory;
    20  import javax.swing.JLabel;
    21  import javax.swing.JPanel;
    22  import javax.swing.JSeparator;
    23  import javax.swing.border.Border;
    24
    25  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.NamespaceTable;
    26  import com.eviware.soapui.model.iface.Interface;
    27  import com.eviware.soapui.support.types.StringToStringMap;
    28  import com.eviware.x.form.XForm;
    29  import com.eviware.x.form.XFormField;
    30  import com.eviware.x.form.XFormOptionsField;
    31  import com.eviware.x.form.XFormTextField;
    32  import com.jgoodies.forms.layout.CellConstraints;
    33  import com.jgoodies.forms.layout.FormLayout;
    34  import com.jgoodies.forms.layout.RowSpec;
    35
    36  public class SwingXFormImpl implements XForm
    37  {
    38     private JPanel panel;
    39     private CellConstraints cc = new CellConstraints();
    40     private FormLayout layout;
    41     private RowSpec rowSpec;
    42     private int rowSpacing = 5;
    43     private Map<String,XFormField> components = new HashMap<String,XFormField>();
    44     private String rowAlignment = "top";
    45  	private String name;
    46
    47  	public SwingXFormImpl(String name)
    48  	{
    49  		this.name = name;
    50  		layout = new FormLayout( "5px,right:pref,5px,left:default,5px:grow(1.0)" );
    51  		panel = new JPanel( layout );
    52        rowSpec = new RowSpec( rowAlignment + ":pref" );
    53  	}
    54
    55     public String getName()
    56  	{
    57  		return name;
    58  	}
    59
    60     public void setName(String name)
    61     {
    62        this.name = name;
    63     }
    64
    65  	public JPanel getPanel()
    66     {
    67        return panel;
    68     }
    69
    70     public void addSpace( int size )
    71     {
    72        if( size > 0 )
    73           layout.appendRow( new RowSpec( size + "px" ) );
    74     }
    75
    76     public XFormField addCheckBox( String name, String description )
    77     {
    78        JCheckBoxFormField checkBox = new JCheckBoxFormField( description == null ? name : description );
    79        addComponent( name, checkBox );
    80        return checkBox;
    81     }
    82
    83     public XFormField addComponent(String label, XFormField formComponent)
    84  	{
    85     	components.put( label, formComponent );
    86
    87     	if( rowSpacing > 0 && !components.isEmpty() )
    88           addSpace( rowSpacing );
    89
    90        layout.appendRow( rowSpec );
    91
    92        int row = layout.getRowCount();
    93
    94        AbstractSwingXFormField swingFormComponent = (AbstractSwingXFormField) formComponent;
    95
    96        if( label != null )
    97        {
    98           JLabel jlabel = new JLabel( label );
    99           jlabel.setBorder( BorderFactory.createEmptyBorder( 2, 0, 0, 0) );
    100 			panel.add( jlabel, cc.xy( 2, row ) );
    101       }
    102
    103       panel.add( swingFormComponent.getComponent(), cc.xy( 4, row ) );
    104       components.put( label, formComponent );
    105
    106       return formComponent;
    107 	}
    108
    109 	public XFormOptionsField addComboBox( String name, Object [] values, String description )
    110    {
    111       JComboBoxFormField comboBox = new JComboBoxFormField( values );
    112       comboBox.setToolTip( description );
    113       addComponent( name, comboBox );
    114       return comboBox;
    115    }
    116
    117 //   public <T extends ModelItem> XFormOptionsField addComboBox( String name, Object [] values, String description,
    118 //         final T target, final SoapUIAction<T> action  )
    119 //   {
    120 //      // TODO Not implemented for Swing
    121 //      return null;
    122 //   }
    123
    124    public void addSeparator()
    125    {
    126    	addSeparator( null );
    127    }
    128
    129    public void addSeparator( String label )
    130    {
    131       addSpace( rowSpacing );
    132
    133       layout.appendRow( rowSpec );
    134       int row = layout.getRowCount();
    135
    136       if( label == null )
    137       	panel.add( new JSeparator(), cc.xywh( 2, row, 3, 1 ) );
    138       else
    139       	panel.add( new JLabel( label ), cc.xywh( 2, row, 3, 1 ) );
    140    }
    141
    142    public XFormTextField addTextField( String name, String description, FieldType type )
    143    {
    144    	if( type == FieldType.FOLDER || type == FieldType.FILE ||
    145    			type == FieldType.PROJECT_FOLDER || type == FieldType.PROJECT_FILE)
    146    	{
    147 			return (XFormTextField) addComponent( name, new FileFormField( description, type ) );
    148    	}
    149    	else if( type == FieldType.PASSWORD )
    150    	{
    151 	      JPasswordFieldFormField pwdField = new JPasswordFieldFormField();
    152 	      pwdField.getComponent().setColumns( 30 );
    153 	      pwdField.setToolTip( description );
    154 	      addComponent( name, pwdField );
    155 			return pwdField;
    156    	}
    157    	else if( type == FieldType.TEXTAREA )
    158    	{
    159 	      JTextAreaFormField field = new JTextAreaFormField();
    160 	      field.getTextArea().setColumns( 40 );
    161 	      field.getTextArea().setRows( 5 );
    162 	      field.setToolTip( description );
    163 	      addComponent( name, field );
    164 			return field;
    165    	}
    166    	else
    167    	{
    168 	      JTextFieldFormField textField = new JTextFieldFormField();
    169 	      textField.getComponent().setColumns( 40 );
    170 	      textField.setToolTip( description );
    171 	      addComponent( name, textField );
    172 			return textField;
    173    	}
    174    }
    175
    176    public void setComponentValue( String label, String value )
    177    {
    178       XFormField component = getComponent( label );
    179 		if( component != null ) component.setValue( value );
    180    }
    181
    182    public String getComponentValue( String name )
    183    {
    184       XFormField component = getComponent( name );
    185       return component == null ? null : component.getValue();
    186    }
    187
    188    public XFormField getComponent( String label )
    189    {
    190       return components.get( label );
    191    }
    192
    193 	public void setBorder(Border border)
    194 	{
    195 		panel.setBorder( border );
    196 	}
    197
    198 	public XFormField addComponent(XFormField component)
    199 	{
    200 		if( rowSpacing > 0 && !components.isEmpty() )
    201          addSpace( rowSpacing );
    202
    203 		layout.appendRow( rowSpec );
    204       int row = layout.getRowCount();
    205
    206       AbstractSwingXFormField swingFormComponent = (AbstractSwingXFormField) component;
    207 		panel.add( swingFormComponent.getComponent(), cc.xyw( 1, row, 4 ) );
    208
    209 		return component;
    210 	}
    211
    212 	public void setValues(StringToStringMap values)
    213 	{
    214 		for( Iterator<String> i = values.keySet().iterator(); i.hasNext(); )
    215 		{
    216 			String key = i.next();
    217 			setComponentValue( key, values.get( key ));
    218 		}
    219 	}
    220
    221 	public StringToStringMap getValues()
    222 	{
    223 		StringToStringMap values = new StringToStringMap();
    224
    225 		for( Iterator<String> i = components.keySet().iterator(); i.hasNext(); )
    226 		{
    227 			String key = i.next();
    228 			values.put( key, getComponentValue( key ));
    229 		}
    230
    231 		return values;
    232 	}
    233
    234    public XFormField addNameSpaceTable(String label, Interface modelItem)
    235    {
    236       return addComponent( label, new NamespaceTable( modelItem ) );
    237    }
    238
    239 	public void setOptions(String name, Object[] values)
    240 	{
    241 		XFormOptionsField combo = (XFormOptionsField) getComponent( name );
    242 		if( combo != null )
    243 			combo.setOptions( values );
    244 	}
    245
    246 	public void addLabel(String name, String label)
    247 	{
    248 		addComponent( name, new JLabelFormField( label ));
    249 	}
    250
    251 	public XFormField[] getFormFields()
    252 	{
    253 		return components.values().toArray( new XFormField[components.size()] );
    254 	}
    255
    256 	public void setFormFieldProperty(String name, Object value)
    257 	{
    258 		for( XFormField field : components.values() )
    259 		{
    260 			field.setProperty( name, value );
    261 		}
    262 	}
    263
    264 	public String[] getOptions( String name )
    265 	{
    266 		XFormField combo = getComponent( name );
    267 		if( combo instanceof XFormOptionsField  )
    268 			return ((XFormOptionsField)combo).getOptions();
    269
    270 		return null;
    271 	}
    272
    273 	public XFormField getFormField( String name )
    274 	{
    275 		return components.get( name );
    276 	}
    277 }