View Javadoc

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