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.awt.BorderLayout;
    16  import java.awt.Dimension;
    17  import java.util.ArrayList;
    18  import java.util.Arrays;
    19  import java.util.List;
    20
    21  import javax.swing.BorderFactory;
    22  import javax.swing.ImageIcon;
    23  import javax.swing.JDialog;
    24  import javax.swing.JPanel;
    25  import javax.swing.JTabbedPane;
    26
    27  import com.eviware.soapui.support.UISupport;
    28  import com.eviware.soapui.support.action.swing.ActionList;
    29  import com.eviware.soapui.support.components.JButtonBar;
    30  import com.eviware.soapui.support.types.StringToStringMap;
    31  import com.eviware.x.form.ValidationMessage;
    32  import com.eviware.x.form.XForm;
    33  import com.eviware.x.form.XFormDialog;
    34  import com.eviware.x.form.XFormField;
    35
    36  public class JTabbedFormDialog extends SwingXFormDialog
    37  {
    38  	private JDialog dialog;
    39  	private List<SwingXFormImpl> forms = new ArrayList<SwingXFormImpl>();
    40  	private JTabbedPane tabs;
    41
    42  	public JTabbedFormDialog(String name, XForm[] forms, ActionList actions, String description, ImageIcon icon)
    43  	{
    44  		dialog = new JDialog( UISupport.getMainFrame(), name, true );
    45  		tabs = new JTabbedPane();
    46  		for(XForm form : forms)
    47  		{
    48  			SwingXFormImpl swingFormImpl = ((SwingXFormImpl)form);
    49  			this.forms.add( swingFormImpl );
    50
    51  			JPanel panel = swingFormImpl.getPanel();
    52  			panel.setBorder( BorderFactory.createEmptyBorder( 0, 0, 5, 0 ));
    53
    54  			tabs.addTab( form.getName(), panel );
    55  		}
    56
    57  		JButtonBar buttons = UISupport.initDialogActions( actions, dialog );
    58
    59  		if( description != null || icon != null )
    60  			dialog.getContentPane().add( UISupport.buildDescription( name, description, icon ), BorderLayout.NORTH );
    61
    62  		dialog.getContentPane().add( UISupport.createTabPanel( tabs, false ), BorderLayout.CENTER );
    63
    64  		buttons.setBorder( BorderFactory.createEmptyBorder( 3, 5, 3, 5 ));
    65  		dialog.getContentPane().add( buttons, BorderLayout.SOUTH );
    66  		dialog.pack();
    67  		Dimension size = dialog.getSize();
    68  		if( size.getHeight() < 300 )
    69  		{
    70  			dialog.setSize( new Dimension( ( int ) size.getWidth(), 300 ) );
    71  		}
    72  	}
    73
    74  	public void setValues(StringToStringMap values)
    75  	{
    76  		for( XForm form : forms )
    77  			form.setValues( values );
    78  	}
    79
    80  	public void setOptions(String field, Object[] options)
    81  	{
    82  		for( XForm form : forms )
    83  			form.setOptions( field, options );
    84  	}
    85
    86  	public XFormField getFormField( String name )
    87  	{
    88  		for( XForm form : forms )
    89  		{
    90  			XFormField formField = form.getFormField( name );
    91  			if( formField != null )
    92  				return formField;
    93  		}
    94
    95  		return null;
    96  	}
    97
    98  	public StringToStringMap getValues()
    99  	{
    100 		StringToStringMap result = new StringToStringMap();
    101
    102 		for( XForm form : forms )
    103 			result.putAll( form.getValues());
    104
    105 		return result;
    106 	}
    107
    108 	public void setVisible(boolean visible)
    109 	{
    110 		if( visible )
    111 			tabs.setSelectedIndex( 0 );
    112
    113 		UISupport.centerDialog( dialog );
    114
    115 		dialog.setVisible( visible );
    116 	}
    117
    118 	public boolean validate()
    119 	{
    120 		for( int i = 0; i < forms.size(); i++ )
    121 		{
    122 			XFormField[] formFields = forms.get( i ).getFormFields();
    123 			for( int c = 0; c < formFields.length; c++ )
    124 			{
    125 				ValidationMessage [] messages = formFields[c].validate();
    126 				if( messages != null && messages.length > 0 )
    127 				{
    128 					tabs.setSelectedIndex( i );
    129 					((AbstractSwingXFormField)messages[0].getFormField()).getComponent().requestFocus();
    130 					UISupport.showErrorMessage( messages[0].getMessage() );
    131 					return false;
    132 				}
    133 			}
    134 		}
    135
    136 		return true;
    137 	}
    138
    139 	public void setFormFieldProperty(String name, Object value)
    140 	{
    141 		for(XForm form : forms)
    142 			form.setFormFieldProperty( name, value );
    143 	}
    144
    145 	public String getValue( String field )
    146 	{
    147 		for(XForm form : forms)
    148       {
    149 			if( form.getComponent( field ) != null )
    150 				return form.getComponent( field ).getValue();
    151       }
    152
    153 		return null;
    154 	}
    155
    156 	public void setValue( String field, String value )
    157 	{
    158 		for(XForm form : forms)
    159       {
    160 			if( form.getComponent( field ) != null )
    161 				form.getComponent( field ).setValue( value );
    162       }
    163 	}
    164
    165 	public int getValueIndex( String name )
    166 	{
    167 		for(SwingXFormImpl form : forms)
    168 		{
    169 			if( form.getComponent( name ) != null )
    170 			{
    171 				String [] options = form.getOptions( name );
    172 				if( options == null )
    173 					return -1;
    174
    175 				return Arrays.asList( options ).indexOf( form.getComponentValue( name ) );
    176 			}
    177 		}
    178
    179 		return -1;
    180 	}
    181
    182 	public boolean show()
    183 	{
    184 		setReturnValue( XFormDialog.CANCEL_OPTION );
    185 		show( new StringToStringMap() );
    186 		return getReturnValue() == XFormDialog.OK_OPTION;
    187 	}
    188
    189 	public void setWidth( int i )
    190 	{
    191 		dialog.setPreferredSize( new Dimension( i, ( int ) dialog.getPreferredSize().getHeight()) );
    192 	}
    193 }