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.types.StringToStringMap;
29 import com.eviware.x.form.XForm;
30 import com.eviware.x.form.XFormField;
31 import com.eviware.x.form.XFormOptionsField;
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,left: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 if( name != null && description != null )
81 checkBox.setToolTip( description );
82
83 addComponent( name, checkBox );
84 return checkBox;
85 }
86
87 public XFormField addComponent(String label, XFormField formComponent)
88 {
89 components.put( label, formComponent );
90
91 if( rowSpacing > 0 && !components.isEmpty() )
92 addSpace( rowSpacing );
93
94 layout.appendRow( rowSpec );
95
96 int row = layout.getRowCount();
97
98 AbstractSwingXFormField<?> swingFormComponent = (AbstractSwingXFormField<?>) formComponent;
99
100 if( label != null )
101 {
102 JLabel jlabel = new JLabel( label.endsWith(":") ? label : label+":" );
103 jlabel.setBorder( BorderFactory.createEmptyBorder( 2, 0, 0, 0) );
104 panel.add( jlabel, cc.xy( 2, row ) );
105
106 jlabel.setLabelFor( swingFormComponent.getComponent() );
107 int ix = label.indexOf( '&' );
108 if( ix >= 0 )
109 {
110 jlabel.setText( label.substring( 0, ix ) + label.substring( ix+1 ) );
111 jlabel.setDisplayedMnemonicIndex( ix );
112 jlabel.setDisplayedMnemonic( label.charAt( ix+1 ) );
113 }
114
115 swingFormComponent.getComponent().getAccessibleContext().setAccessibleDescription( label );
116 }
117
118 panel.add( swingFormComponent.getComponent(), cc.xy( 4, row ) );
119 components.put( label, formComponent );
120
121 return formComponent;
122 }
123
124 public XFormOptionsField addComboBox( String name, Object [] values, String description )
125 {
126 JComboBoxFormField comboBox = new JComboBoxFormField( values );
127 comboBox.setToolTip( description );
128 addComponent( name, comboBox );
129 return comboBox;
130 }
131
132 public void addSeparator()
133 {
134 addSeparator( null );
135 }
136
137 public void addSeparator( String label )
138 {
139 addSpace( rowSpacing );
140
141 layout.appendRow( rowSpec );
142 int row = layout.getRowCount();
143
144 if( label == null )
145 panel.add( new JSeparator(), cc.xywh( 2, row, 3, 1 ) );
146 else
147 panel.add( new JLabel( label ), cc.xywh( 2, row, 3, 1 ) );
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 }