View Javadoc

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