View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.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  		Dimension size = dialog.getSize();
88  		if( size.getHeight() < 270 )
89  		{
90  			dialog.setSize( new Dimension( ( int ) size.getWidth(), 270 ) );
91  		}
92  		
93  		UISupport.centerDialog( dialog );
94  		
95  		dialog.setVisible( visible );
96  	}
97  
98  	public boolean validate()
99  	{
100 		XFormField[] formFields = form.getFormFields();
101 		for( int c = 0; c < formFields.length; c++ )
102 		{
103 			ValidationMessage [] messages = formFields[c].validate();
104 			if( messages != null && messages.length > 0 )
105 			{
106 				((AbstractSwingXFormField)messages[0].getFormField()).getComponent().requestFocus();
107 				UISupport.showErrorMessage( messages[0].getMessage() );
108 				return false;
109 			}
110 		}
111 		
112 		return true;
113 	}
114 
115 	public void setFormFieldProperty(String name, Object value)
116 	{
117 		form.setFormFieldProperty( name, value );
118 	}
119 
120 	public String getValue( String field )
121 	{
122 		return form.getComponentValue( field );
123 	}
124 
125 	public void setValue( String field, String value )
126 	{
127 		form.setComponentValue( field, value );
128 	}
129 
130 	public int getValueIndex( String name )
131 	{
132 		String [] options = form.getOptions( name );
133 		if( options == null )
134 			return -1;
135 		
136 		return Arrays.asList( options ).indexOf( form.getComponentValue( name ) );
137 	}
138 
139 	public boolean show()
140 	{
141 		show( new StringToStringMap() );
142 		return getReturnValue() == XFormDialog.OK_OPTION;
143 	}
144 
145 	public XFormField getFormField( String name )
146 	{
147 		return form.getFormField( name );
148 	}
149 }