1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.support.components;
14
15 import java.awt.Font;
16 import java.awt.event.ComponentAdapter;
17 import java.awt.event.ComponentEvent;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.Map;
21
22 import javax.swing.Action;
23 import javax.swing.BorderFactory;
24 import javax.swing.ComboBoxModel;
25 import javax.swing.JButton;
26 import javax.swing.JCheckBox;
27 import javax.swing.JComboBox;
28 import javax.swing.JComponent;
29 import javax.swing.JLabel;
30 import javax.swing.JList;
31 import javax.swing.JPanel;
32 import javax.swing.JPasswordField;
33 import javax.swing.JSeparator;
34 import javax.swing.JTextField;
35 import javax.swing.border.Border;
36 import javax.swing.text.JTextComponent;
37
38 import com.jgoodies.forms.layout.CellConstraints;
39 import com.jgoodies.forms.layout.FormLayout;
40 import com.jgoodies.forms.layout.RowSpec;
41
42 /***
43 * Utility-class for creating forms
44 */
45
46 public class SimpleForm
47 {
48 private JPanel panel;
49 private CellConstraints cc = new CellConstraints();
50 private FormLayout layout;
51 private RowSpec rowSpec;
52 private int rowSpacing = 5;
53 private Map<String,JComponent> components = new HashMap<String,JComponent>();
54 private Map<JComboBox,Object[]> comboBoxMaps = new HashMap<JComboBox,Object[]>();
55 private String rowAlignment = "top";
56 private Map<String,String> hiddenValues;
57 private boolean appended;
58 private Font labelFont;
59
60 public SimpleForm()
61 {
62 this( 5 );
63 }
64
65 public SimpleForm( String layout )
66 {
67 this( new FormLayout( layout ) );
68 }
69
70 public SimpleForm( FormLayout layout )
71 {
72 this.layout = layout;
73 panel = new JPanel( layout );
74 rowSpec = new RowSpec( rowAlignment + ":pref" );
75 }
76
77 public SimpleForm( int indent )
78 {
79 this( indent + "px:none,right:pref,10px,left:default,5px:grow(1.0)" );
80 }
81
82 public JPanel getPanel()
83 {
84 return panel;
85 }
86
87 public String getRowAlignment()
88 {
89 return rowAlignment;
90 }
91
92 public Font getLabelFont()
93 {
94 return labelFont;
95 }
96
97 public void setLabelFont( Font labelFont )
98 {
99 this.labelFont = labelFont;
100 }
101
102 public void setRowAlignment( String rowAlignment )
103 {
104 this.rowAlignment = rowAlignment;
105 rowSpec = new RowSpec( rowAlignment + ":pref" );
106 }
107
108 public int getRowSpacing()
109 {
110 return rowSpacing;
111 }
112
113 public void setRowSpacing( int rowSpacing )
114 {
115 this.rowSpacing = rowSpacing;
116 }
117
118 public void addHiddenValue( String name, String value )
119 {
120 if( hiddenValues == null )
121 hiddenValues = new HashMap<String,String>();
122
123 hiddenValues.put( name, value );
124 }
125
126 public JButton addRightButton( Action action )
127 {
128 if( rowSpacing > 0 && !components.isEmpty() )
129 addSpace( rowSpacing );
130
131 layout.appendRow( rowSpec );
132 int row = layout.getRowCount();
133
134 JButton button = new JButton( action );
135 panel.add( button, cc.xy( 4, row, "right,bottom" ) );
136 return button;
137 }
138
139 public void addSpace()
140 {
141 addSpace( rowSpacing );
142 }
143
144 public void addSpace( int size )
145 {
146 if( size > 0 )
147 layout.appendRow( new RowSpec( size + "px" ) );
148 }
149
150 public void addRightComponent( JComponent component )
151 {
152 if( rowSpacing > 0 && !components.isEmpty() )
153 addSpace( rowSpacing );
154
155 layout.appendRow( rowSpec );
156 int row = layout.getRowCount();
157
158 panel.add( component, cc.xy( 4, row, "right,bottom" ) );
159 }
160
161 public JCheckBox appendCheckBox( String caption, String label, boolean selected )
162 {
163 JCheckBox checkBox = new JCheckBox( label, selected );
164 components.put( caption, checkBox );
165 append( caption, checkBox );
166 return checkBox;
167 }
168
169 public void append( String label, JComponent component )
170 {
171 append( label, component, null );
172 }
173
174 public JComboBox appendComboBox( String label, Map values )
175 {
176 Object[] valueArray = new Object[values.size()];
177 Object[] keyArray = new Object[values.size()];
178
179 int ix = 0;
180 for( Iterator i = values.keySet().iterator(); i.hasNext(); ix++ )
181 {
182 keyArray[ix] = i.next();
183 valueArray[ix] = values.get( keyArray[ix] );
184 }
185
186 JComboBox comboBox = new JComboBox( valueArray );
187
188 comboBoxMaps.put( comboBox, keyArray );
189
190 append( label, comboBox );
191 return comboBox;
192 }
193
194 public JComboBox appendComboBox( String label, Object [] values, String tooltip )
195 {
196 JComboBox comboBox = new JComboBox( values );
197 comboBox.setToolTipText( tooltip );
198 append( label, comboBox );
199 return comboBox;
200 }
201
202 public JComboBox appendComboBox( String label, ComboBoxModel model, String tooltip )
203 {
204 JComboBox comboBox = new JComboBox( model );
205 comboBox.setToolTipText( tooltip );
206 append( label, comboBox );
207 return comboBox;
208 }
209
210 public void appendFixed( String label, JComponent component )
211 {
212 append( label, component, "left:pref" );
213 }
214
215 public void append( String label, JComponent component, String alignments )
216 {
217 int spaceRowIndex = -1;
218
219 if( rowSpacing > 0 && appended )
220 {
221 addSpace( rowSpacing );
222 spaceRowIndex = layout.getRowCount();
223 }
224
225 layout.appendRow( rowSpec );
226 int row = layout.getRowCount();
227
228 if( label != null )
229 {
230 JLabel jlabel = new JLabel( label );
231 jlabel.setBorder( BorderFactory.createEmptyBorder( 3, 0, 0, 0 ) );
232 if( labelFont != null )
233 jlabel.setFont( labelFont );
234
235 panel.add( jlabel, cc.xy( 2, row ) );
236
237 component.addComponentListener( new LabelHider( jlabel, spaceRowIndex ) );
238 }
239 else
240 component.addComponentListener( new LabelHider( null, spaceRowIndex ) );
241
242 if( alignments == null )
243 panel.add( component, cc.xy( 4, row ) );
244 else
245 panel.add( component, cc.xy( 4, row, alignments ) );
246
247 components.put( label, component );
248 appended = true;
249 }
250
251 public void appendSeparator()
252 {
253 if( appended && rowSpacing > 0 )
254 addSpace( rowSpacing );
255
256 layout.appendRow( rowSpec );
257 int row = layout.getRowCount();
258
259 panel.add( new JSeparator(), cc.xywh( 2, row, 3, 1 ) );
260 appended = true;
261 }
262
263 public JTextField appendTextField( String label, String tooltip )
264 {
265 JTextField textField = new JTextField();
266 textField.setColumns( 30 );
267 textField.setToolTipText( tooltip );
268 append( label, textField );
269 return textField;
270 }
271
272 public JTextField appendPasswordField( String label, String tooltip )
273 {
274 JPasswordField textField = new JPasswordField();
275 textField.setColumns( 30 );
276 textField.setToolTipText( tooltip );
277 append( label, textField );
278 return textField;
279 }
280
281 public void setComponentValue( String label, String value )
282 {
283 JComponent component = getComponent( label );
284
285 if( component instanceof JTextComponent )
286 {
287 ((JTextComponent) component).setText( value );
288 }
289 else if( component instanceof JComboBox )
290 {
291 JComboBox comboBox = ((JComboBox) component);
292 comboBox.setSelectedItem( value );
293 }
294 else if( component instanceof JList )
295 {
296 ((JList) component).setSelectedValue( value, true );
297 }
298 else if( component instanceof JCheckBox )
299 {
300 ((JCheckBox) component).setSelected( Boolean.valueOf( value ));
301 }
302 else if( component instanceof JFormComponent )
303 {
304 ((JFormComponent) component).setValue( value );
305 }
306 }
307
308 public String getComponentValue( String label )
309 {
310 JComponent component = getComponent( label );
311 if( component == null )
312 {
313 return (String) (hiddenValues == null ? null : hiddenValues.get( label ));
314 }
315
316 if( component instanceof JTextComponent )
317 {
318 return ((JTextComponent) component).getText();
319 }
320
321 if( component instanceof JComboBox )
322 {
323 JComboBox comboBox = ((JComboBox) component);
324 int selectedIndex = comboBox.getSelectedIndex();
325 if( selectedIndex != -1 )
326 {
327 if( comboBoxMaps.containsKey( component ) )
328 {
329 Object[] keys = (Object[]) comboBoxMaps.get( comboBox );
330 Object value = keys[selectedIndex];
331 return (String) value == null ? null : value.toString();
332 }
333 else
334 {
335 Object value = comboBox.getSelectedItem();
336 return (String) value == null ? null : value.toString();
337 }
338 }
339 }
340
341 if( component instanceof JList )
342 {
343 return (String) ((JList) component).getSelectedValue();
344 }
345
346 if( component instanceof JCheckBox )
347 {
348 return String.valueOf( ((JCheckBox) component).isSelected() );
349 }
350
351 else if( component instanceof JFormComponent )
352 {
353 return ((JFormComponent) component).getValue();
354 }
355
356 return null;
357 }
358
359 public JComponent getComponent( String label )
360 {
361 return (JComponent) components.get( label );
362 }
363
364 public void setBorder(Border border)
365 {
366 panel.setBorder( border );
367 }
368
369 public int getRowCount()
370 {
371 return layout.getRowCount();
372 }
373
374 public void addComponent(JComponent component)
375 {
376 layout.appendRow( rowSpec );
377 int row = layout.getRowCount();
378
379 panel.add( component, cc.xyw( 2, row, 4 ) );
380 }
381
382 public void setValues(Map<String, String> values)
383 {
384 for( Iterator<String> i = values.keySet().iterator(); i.hasNext(); )
385 {
386 String key = i.next();
387 setComponentValue( key, values.get( key ));
388 }
389 }
390
391 public void getValues(Map<String, String> values)
392 {
393 for( Iterator<String> i = components.keySet().iterator(); i.hasNext(); )
394 {
395 String key = i.next();
396 values.put( key, getComponentValue( key ));
397 }
398 }
399
400 public void append( JComponent component )
401 {
402 int spaceRowIndex = -1;
403
404 if( rowSpacing > 0 && appended )
405 {
406 addSpace( rowSpacing );
407 spaceRowIndex = layout.getRowCount();
408 }
409
410 layout.appendRow( rowSpec );
411 int row = layout.getRowCount();
412
413 panel.add( component, cc.xyw( 2, row, 4 ) );
414
415 component.addComponentListener( new LabelHider( null, spaceRowIndex ) );
416
417 appended = true;
418 }
419
420 private final class LabelHider extends ComponentAdapter
421 {
422 private final JLabel jlabel;
423 private final int rowIndex;
424
425 public LabelHider( JLabel jlabel, int i )
426 {
427 this.jlabel = jlabel;
428 this.rowIndex = i;
429 }
430
431 public void componentHidden( ComponentEvent e )
432 {
433 if( jlabel != null )
434 jlabel.setVisible( false );
435
436 if( rowIndex >= 0 && rowIndex < layout.getRowCount() )
437 layout.setRowSpec( rowIndex, new RowSpec( "0px" ));
438 }
439
440 public void componentShown( ComponentEvent e )
441 {
442 if( jlabel != null )
443 jlabel.setVisible( true );
444
445 if( rowIndex >= 0 && rowIndex < layout.getRowCount() )
446 layout.setRowSpec( rowIndex, new RowSpec( rowSpacing + "px" ));
447 }
448 }
449 }
450