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