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