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