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