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