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.util.HashMap;
16  import java.util.Iterator;
17  import java.util.Map;
18  
19  import javax.swing.BorderFactory;
20  import javax.swing.JLabel;
21  import javax.swing.JPanel;
22  import javax.swing.JSeparator;
23  import javax.swing.border.Border;
24  
25  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.NamespaceTable;
26  import com.eviware.soapui.model.iface.Interface;
27  import com.eviware.soapui.support.types.StringToStringMap;
28  import com.eviware.x.form.XForm;
29  import com.eviware.x.form.XFormField;
30  import com.eviware.x.form.XFormOptionsField;
31  import com.eviware.x.form.XFormTextField;
32  import com.jgoodies.forms.layout.CellConstraints;
33  import com.jgoodies.forms.layout.FormLayout;
34  import com.jgoodies.forms.layout.RowSpec;
35  
36  public class SwingXFormImpl implements XForm
37  {
38     private JPanel panel;
39     private CellConstraints cc = new CellConstraints();
40     private FormLayout layout;
41     private RowSpec rowSpec;
42     private int rowSpacing = 5;
43     private Map<String,XFormField> components = new HashMap<String,XFormField>();
44     private String rowAlignment = "top";
45  	private String name;
46  	
47  	public SwingXFormImpl(String name)
48  	{
49  		this.name = name;
50  		layout = new FormLayout( "5px,right:pref,5px,left:default,5px:grow(1.0)" );
51  		panel = new JPanel( layout );
52        rowSpec = new RowSpec( rowAlignment + ":pref" );
53  	}
54     
55     public String getName()
56  	{
57  		return name;
58  	}
59     
60     public void setName(String name)
61     {
62        this.name = name;
63     }
64  
65  	public JPanel getPanel()
66     {
67        return panel;
68     }
69  
70     public void addSpace( int size )
71     {
72        if( size > 0 )
73           layout.appendRow( new RowSpec( size + "px" ) );
74     }
75  
76     public XFormField addCheckBox( String name, String description )
77     {
78        JCheckBoxFormField checkBox = new JCheckBoxFormField( description == null ? name : description );
79        addComponent( name, checkBox );
80        return checkBox;
81     }
82  
83     public XFormField addComponent(String label, XFormField formComponent)
84  	{
85     	components.put( label, formComponent );
86     	
87     	if( rowSpacing > 0 && !components.isEmpty() )
88           addSpace( rowSpacing );
89  
90        layout.appendRow( rowSpec );
91     	
92        int row = layout.getRowCount();
93  
94        AbstractSwingXFormField swingFormComponent = (AbstractSwingXFormField) formComponent;
95        
96        if( label != null )
97        {
98           JLabel jlabel = new JLabel( label );
99           jlabel.setBorder( BorderFactory.createEmptyBorder( 2, 0, 0, 0) );
100 			panel.add( jlabel, cc.xy( 2, row ) );
101       }
102       
103       panel.add( swingFormComponent.getComponent(), cc.xy( 4, row ) );
104       components.put( label, formComponent );		
105       
106       return formComponent;
107 	}
108 
109 	public XFormOptionsField addComboBox( String name, Object [] values, String description )
110    {
111       JComboBoxFormField comboBox = new JComboBoxFormField( values );
112       comboBox.setToolTip( description );
113       addComponent( name, comboBox );
114       return comboBox;
115    }
116 
117    public void addSeparator()
118    {
119    	addSeparator( null );
120    }   
121    
122    public void addSeparator( String label )
123    {
124       addSpace( rowSpacing );
125 
126       layout.appendRow( rowSpec );
127       int row = layout.getRowCount();
128 
129       if( label == null )
130       	panel.add( new JSeparator(), cc.xywh( 2, row, 3, 1 ) );
131       else
132       	panel.add( new JLabel( label ), cc.xywh( 2, row, 3, 1 ) );
133    }
134 
135    public XFormTextField addTextField( String name, String description, FieldType type )
136    {
137    	if( type == FieldType.FOLDER || type == FieldType.FILE ||
138    			type == FieldType.PROJECT_FOLDER || type == FieldType.PROJECT_FILE)
139    	{   		
140 			return (XFormTextField) addComponent( name, new FileFormField( description, type ) );
141    	}
142    	else if( type == FieldType.PASSWORD )
143    	{
144 	      JPasswordFieldFormField pwdField = new JPasswordFieldFormField();
145 	      pwdField.getComponent().setColumns( 30 );
146 	      pwdField.setToolTip( description );
147 	      addComponent( name, pwdField );
148 			return pwdField;
149    	}
150    	else if( type == FieldType.TEXTAREA )
151    	{
152 	      JTextAreaFormField field = new JTextAreaFormField();
153 	      field.getTextArea().setColumns( 40 );
154 	      field.getTextArea().setRows( 5 );
155 	      field.setToolTip( description );
156 	      addComponent( name, field );
157 			return field;
158    	}
159    	else
160    	{
161 	      JTextFieldFormField textField = new JTextFieldFormField();
162 	      textField.getComponent().setColumns( 40 );
163 	      textField.setToolTip( description );
164 	      addComponent( name, textField );
165 			return textField;
166    	}
167    }
168 
169    public void setComponentValue( String label, String value )
170    {
171       XFormField component = getComponent( label );
172 		if( component != null ) component.setValue( value );
173    }
174    
175    public String getComponentValue( String name )
176    {
177       XFormField component = getComponent( name );
178       return component == null ? null : component.getValue();
179    }
180 
181    public XFormField getComponent( String label )
182    {
183       return components.get( label );
184    }
185 
186 	public void setBorder(Border border)
187 	{
188 		panel.setBorder( border );
189 	}
190 
191 	public XFormField addComponent(XFormField component)
192 	{
193 		if( rowSpacing > 0 && !components.isEmpty() )
194          addSpace( rowSpacing );
195 	
196 		layout.appendRow( rowSpec );
197       int row = layout.getRowCount();
198 		
199       AbstractSwingXFormField swingFormComponent = (AbstractSwingXFormField) component;
200 		panel.add( swingFormComponent.getComponent(), cc.xyw( 1, row, 4 ) );
201 		
202 		return component;
203 	}
204 
205 	public void setValues(StringToStringMap values)
206 	{
207 		for( Iterator<String> i = values.keySet().iterator(); i.hasNext(); )
208 		{
209 			String key = i.next();
210 			setComponentValue( key, values.get( key ));
211 		}
212 	}
213 
214 	public StringToStringMap getValues()
215 	{
216 		StringToStringMap values = new StringToStringMap();
217 		
218 		for( Iterator<String> i = components.keySet().iterator(); i.hasNext(); )
219 		{
220 			String key = i.next();
221 			values.put( key, getComponentValue( key ));
222 		}
223 		
224 		return values;
225 	}
226 
227    public XFormField addNameSpaceTable(String label, Interface modelItem)
228    {
229       return addComponent( label, new NamespaceTable( modelItem ) );
230    }
231 
232 	public void setOptions(String name, Object[] values)
233 	{
234 		XFormOptionsField combo = (XFormOptionsField) getComponent( name );
235 		if( combo != null )
236 			combo.setOptions( values );
237 	}
238 
239 	public void addLabel(String name, String label)
240 	{
241 		addComponent( name, new JLabelFormField( label ));
242 	}
243 
244 	public XFormField[] getFormFields()
245 	{
246 		return components.values().toArray( new XFormField[components.size()] );
247 	}
248 
249 	public void setFormFieldProperty(String name, Object value)
250 	{
251 		for( XFormField field : components.values() )
252 		{
253 			field.setProperty( name, value );
254 		}
255 	}
256 
257 	public String[] getOptions( String name )
258 	{
259 		XFormField combo = getComponent( name );
260 		if( combo instanceof XFormOptionsField  )
261 			return ((XFormOptionsField)combo).getOptions();
262 		
263 		return null;
264 	}
265 
266 	public XFormField getFormField( String name )
267 	{
268 		return components.get( name );
269 	}
270 }