View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.awt.Dialog.ModalityType;
19  import java.util.concurrent.CountDownLatch;
20  
21  import javax.swing.Action;
22  import javax.swing.BorderFactory;
23  import javax.swing.ImageIcon;
24  import javax.swing.JDialog;
25  import javax.swing.JPanel;
26  
27  import com.eviware.soapui.support.StringUtils;
28  import com.eviware.soapui.support.UISupport;
29  import com.eviware.soapui.support.action.swing.ActionList;
30  import com.eviware.soapui.support.action.swing.DefaultActionList;
31  import com.eviware.soapui.support.components.JButtonBar;
32  import com.eviware.soapui.support.types.StringToStringMap;
33  import com.eviware.x.form.ValidationMessage;
34  import com.eviware.x.form.XForm;
35  import com.eviware.x.form.XFormDialog;
36  import com.eviware.x.form.XFormField;
37  
38  public class JFormDialog extends SwingXFormDialog
39  {
40  	private JDialog dialog;
41  	private SwingXFormImpl form;
42  	private JButtonBar buttons;
43  	private boolean resized;
44  
45  	public JFormDialog( String name, SwingXFormImpl form, ActionList actions, String description, ImageIcon icon )
46  	{
47  		dialog = new JDialog( UISupport.getMainFrame(), name, true );
48  
49  		buttons = UISupport.initDialogActions( actions, dialog );
50  		buttons.setBorder( BorderFactory.createEmptyBorder( 5, 0, 0, 0 ) );
51  
52  		JPanel panel = new JPanel( new BorderLayout() );
53  		this.form = ( SwingXFormImpl )form;
54  		panel.add( ( this.form.getPanel() ), BorderLayout.CENTER );
55  		panel.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) );
56  
57  		if( description != null || icon != null )
58  			dialog.getContentPane().add( UISupport.buildDescription( name, description, icon ), BorderLayout.NORTH );
59  
60  		dialog.getContentPane().add( panel, BorderLayout.CENTER );
61  
62  		buttons.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory
63  				.createMatteBorder( 1, 0, 0, 0, Color.GRAY ), BorderFactory.createMatteBorder( 1, 0, 0, 0, Color.WHITE ) ),
64  				BorderFactory.createEmptyBorder( 3, 5, 3, 5 ) ) );
65  
66  		dialog.getContentPane().add( buttons, BorderLayout.SOUTH );
67  	}
68  
69  	public void setValues( StringToStringMap values )
70  	{
71  		form.setValues( values );
72  	}
73  
74  	public JDialog getDialog()
75  	{
76  		return dialog;
77  	}
78  
79  	public void setSize( int i, int j )
80  	{
81  		dialog.setSize( i, j );
82  		resized = true;
83  	}
84  
85  	public XForm[] getForms()
86  	{
87  		return new XForm[] { form };
88  	}
89  
90  	public StringToStringMap getValues()
91  	{
92  		StringToStringMap result = new StringToStringMap();
93  		result.putAll( form.getValues() );
94  
95  		return result;
96  	}
97  
98  	public void setOptions( String field, Object[] options )
99  	{
100 		form.setOptions( field, options );
101 	}
102 
103 	public void setVisible( boolean visible )
104 	{
105 		if( !resized && visible )
106 		{
107 			dialog.pack();
108 			if( dialog.getHeight() < 270 )
109 			{
110 				dialog.setSize( new Dimension( dialog.getWidth(), 270 ) );
111 			}
112 
113 			if( dialog.getWidth() < 450 )
114 			{
115 				dialog.setSize( new Dimension( 450, dialog.getHeight() ) );
116 			}
117 		}
118 
119 		if( visible )
120 		{
121 			UISupport.centerDialog( dialog );
122 		}
123 		dialog.setVisible( visible );
124 
125 		if( startSignal != null )
126 			startSignal.countDown();
127 	}
128 
129 	public void addAction( Action action )
130 	{
131 		DefaultActionList actions = new DefaultActionList();
132 		actions.addAction( action );
133 		buttons.addActions( actions );
134 	}
135 
136 	public boolean validate()
137 	{
138 		XFormField[] formFields = form.getFormFields();
139 		for( int c = 0; c < formFields.length; c++ )
140 		{
141 			ValidationMessage[] messages = formFields[c].validate();
142 			if( messages != null && messages.length > 0 )
143 			{
144 				( ( AbstractSwingXFormField<?> )messages[0].getFormField() ).getComponent().requestFocus();
145 				UISupport.showErrorMessage( messages[0].getMessage() );
146 				return false;
147 			}
148 		}
149 
150 		return true;
151 	}
152 
153 	public void setFormFieldProperty( String name, Object value )
154 	{
155 		form.setFormFieldProperty( name, value );
156 	}
157 
158 	public String getValue( String field )
159 	{
160 		return form.getComponentValue( field );
161 	}
162 
163 	public void setValue( String field, String value )
164 	{
165 		form.setComponentValue( field, value );
166 	}
167 
168 	public int getValueIndex( String name )
169 	{
170 		Object[] options = form.getOptions( name );
171 		if( options == null )
172 			return -1;
173 
174 		return StringUtils.toStringList( options ).indexOf( form.getComponentValue( name ) );
175 	}
176 
177 	private CountDownLatch startSignal;
178 
179 	public boolean show()
180 	{
181 		setReturnValue( XFormDialog.CANCEL_OPTION );
182 		show( new StringToStringMap() );
183 		if( dialog.getModalityType() == ModalityType.MODELESS )
184 		{
185 			startSignal = new CountDownLatch( 1 );
186 			try
187 			{
188 				startSignal.await();
189 			}
190 			catch( InterruptedException e )
191 			{
192 				e.printStackTrace();
193 			}
194 
195 			startSignal = null;
196 		}
197 
198 		return getReturnValue() == XFormDialog.OK_OPTION;
199 	}
200 
201 	public XFormField getFormField( String name )
202 	{
203 		return form.getFormField( name );
204 	}
205 
206 	public void setWidth( int i )
207 	{
208 		dialog.setPreferredSize( new Dimension( i, ( int )dialog.getPreferredSize().getHeight() ) );
209 	}
210 
211 	public void release()
212 	{
213 		dialog.dispose();
214 	}
215 }