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.JScrollPane;
34 import javax.swing.JSeparator;
35 import javax.swing.JTextArea;
36 import javax.swing.JTextField;
37 import javax.swing.border.Border;
38 import javax.swing.text.JTextComponent;
39
40 import com.eviware.soapui.support.swing.JTextComponentPopupMenu;
41 import com.eviware.soapui.support.xml.JXEditTextArea;
42 import com.jgoodies.forms.layout.CellConstraints;
43 import com.jgoodies.forms.layout.FormLayout;
44 import com.jgoodies.forms.layout.RowSpec;
45
46 /***
47 * Utility-class for creating forms
48 */
49
50 public class SimpleForm
51 {
52 private JPanel panel;
53 private CellConstraints cc = new CellConstraints();
54 private FormLayout layout;
55 private RowSpec rowSpec;
56 private int rowSpacing = 5;
57 private Map<String, JComponent> components = new HashMap<String, JComponent>();
58 private Map<JComboBox, Object[]> comboBoxMaps = new HashMap<JComboBox, Object[]>();
59 private String rowAlignment = "top";
60 private Map<String, String> hiddenValues;
61 private boolean appended;
62 private Font labelFont;
63 private int defaultTextAreaColumns = 30;
64 private int defaultTextAreaRows = 3;
65 private int defaultTextFieldColumns = 30;
66
67 public SimpleForm()
68 {
69 this( 5 );
70 }
71
72 public SimpleForm( String layout )
73 {
74 this( new FormLayout( layout ) );
75 }
76
77 public SimpleForm( FormLayout layout )
78 {
79 this.layout = layout;
80 panel = new JPanel( layout );
81 rowSpec = new RowSpec( rowAlignment + ":pref" );
82 }
83
84 public SimpleForm( int indent )
85 {
86 this( indent + "px:none,left:pref,10px,left:default,5px:grow(1.0)" );
87 }
88
89 public JPanel getPanel()
90 {
91 return panel;
92 }
93
94 public String getRowAlignment()
95 {
96 return rowAlignment;
97 }
98
99 public Font getLabelFont()
100 {
101 return labelFont;
102 }
103
104 public void setLabelFont( Font labelFont )
105 {
106 this.labelFont = labelFont;
107 }
108
109 public void setRowAlignment( String rowAlignment )
110 {
111 this.rowAlignment = rowAlignment;
112 rowSpec = new RowSpec( rowAlignment + ":pref" );
113 }
114
115 public int getRowSpacing()
116 {
117 return rowSpacing;
118 }
119
120 public void setRowSpacing( int rowSpacing )
121 {
122 this.rowSpacing = rowSpacing;
123 }
124
125 public void addHiddenValue( String name, String value )
126 {
127 if( hiddenValues == null )
128 hiddenValues = new HashMap<String, String>();
129
130 hiddenValues.put( name, value );
131 }
132
133 public JButton addRightButton( Action action )
134 {
135 if( rowSpacing > 0 && !components.isEmpty() )
136 addSpace( rowSpacing );
137
138 layout.appendRow( rowSpec );
139 int row = layout.getRowCount();
140
141 JButton button = new JButton( action );
142 panel.add( button, cc.xy( 4, row, "right,bottom" ) );
143 return button;
144 }
145
146 public void addSpace()
147 {
148 addSpace( rowSpacing );
149 }
150
151 public void addSpace( int size )
152 {
153 if( size > 0 )
154 layout.appendRow( new RowSpec( size + "px" ) );
155 }
156
157 public void addRightComponent( JComponent component )
158 {
159 if( rowSpacing > 0 && !components.isEmpty() )
160 addSpace( rowSpacing );
161
162 layout.appendRow( rowSpec );
163
164 int row = layout.getRowCount();
165 panel.add( component, cc.xy( 4, row, "right,bottom" ) );
166 }
167
168 public JCheckBox appendCheckBox( String caption, String label, boolean selected )
169 {
170 JCheckBox checkBox = new JCheckBox( label, selected );
171 checkBox.getAccessibleContext().setAccessibleDescription( caption );
172 components.put( caption, checkBox );
173 append( caption, checkBox );
174 return checkBox;
175 }
176
177
178
179 public void append( String label, JComponent component )
180 {
181 append( label, component, null );
182 }
183
184 public JComboBox appendComboBox( String label, Map<?, ?> values )
185 {
186 Object[] valueArray = new Object[values.size()];
187 Object[] keyArray = new Object[values.size()];
188
189 int ix = 0;
190 for( Iterator<?> i = values.keySet().iterator(); i.hasNext(); ix++ )
191 {
192 keyArray[ix] = i.next();
193 valueArray[ix] = values.get( keyArray[ix] );
194 }
195
196 JComboBox comboBox = new JComboBox( valueArray );
197
198 comboBoxMaps.put( comboBox, keyArray );
199
200 append( label, comboBox );
201 return comboBox;
202 }
203
204 public JComboBox appendComboBox( String label, Object[] values, String tooltip )
205 {
206 JComboBox comboBox = new JComboBox( values );
207 comboBox.setToolTipText( tooltip );
208 comboBox.getAccessibleContext().setAccessibleDescription( tooltip );
209 append( label, comboBox );
210 return comboBox;
211 }
212
213 public JComboBox appendComboBox( String label, ComboBoxModel model, String tooltip )
214 {
215 JComboBox comboBox = new JComboBox( model );
216 comboBox.setToolTipText( tooltip );
217 comboBox.getAccessibleContext().setAccessibleDescription( tooltip );
218 append( label, comboBox );
219 return comboBox;
220 }
221
222 public JButton appendButton( String label, String tooltip )
223 {
224 JButton button = new JButton();
225 button.setToolTipText( tooltip );
226 button.getAccessibleContext().setAccessibleDescription( tooltip );
227 append( label, button );
228 return button;
229 }
230
231 public void appendFixed( String label, JComponent component )
232 {
233 append( label, component, "left:pref" );
234 }
235
236 public <T extends JComponent> T append( String label, T component, String alignments )
237 {
238 JLabel jlabel = null;
239 if( label != null )
240 {
241 jlabel = new JLabel( label.endsWith( ":" ) ? label : label + ":" );
242 jlabel.setBorder( BorderFactory.createEmptyBorder( 3, 0, 0, 0 ) );
243 if( labelFont != null )
244 jlabel.setFont( labelFont );
245 }
246
247 return append( label, jlabel, component, alignments );
248 }
249
250 public <T extends JComponent> T append( String name, JComponent label, T component, String alignments )
251 {
252 int spaceRowIndex = -1;
253
254 if( rowSpacing > 0 && appended )
255 {
256 addSpace( rowSpacing );
257 spaceRowIndex = layout.getRowCount();
258 }
259
260 layout.appendRow( rowSpec );
261 int row = layout.getRowCount();
262
263 if( label != null )
264 {
265 panel.add( label, cc.xy( 2, row ) );
266 component.addComponentListener( new LabelHider( label, spaceRowIndex ) );
267
268 if( label instanceof JLabel )
269 {
270 JLabel jl = ( ( JLabel )label );
271 jl.setLabelFor( component );
272 String text = jl.getText();
273 int ix = text.indexOf( '&' );
274 if( ix >= 0 )
275 {
276 jl.setText( text.substring( 0, ix ) + text.substring( ix + 1 ) );
277 jl.setDisplayedMnemonicIndex( ix );
278 jl.setDisplayedMnemonic( text.charAt( ix + 1 ) );
279 }
280
281 if( component.getAccessibleContext() != null )
282 component.getAccessibleContext().setAccessibleName( text );
283 }
284 }
285 else
286 component.addComponentListener( new LabelHider( null, spaceRowIndex ) );
287
288 if( alignments == null )
289 panel.add( component, cc.xy( 4, row ) );
290 else
291 panel.add( component, cc.xy( 4, row, alignments ) );
292
293 components.put( name, component );
294 appended = true;
295
296 return component;
297 }
298
299 public boolean hasComponents()
300 {
301 return !components.isEmpty();
302 }
303
304 public void appendSeparator()
305 {
306 if( appended && rowSpacing > 0 )
307 addSpace( rowSpacing );
308
309 layout.appendRow( rowSpec );
310 int row = layout.getRowCount();
311
312 panel.add( new JSeparator(), cc.xywh( 2, row, 3, 1 ) );
313 appended = true;
314 }
315
316 public JTextField appendTextField( String label, String tooltip )
317 {
318 JTextField textField = new JUndoableTextField();
319 textField.setColumns( defaultTextFieldColumns );
320 textField.setToolTipText( tooltip );
321 textField.getAccessibleContext().setAccessibleDescription( tooltip );
322 JTextComponentPopupMenu.add( textField );
323 append( label, textField );
324 return textField;
325 }
326
327 public JTextArea appendTextArea( String label, String tooltip )
328 {
329 JTextArea textArea = new JUndoableTextArea();
330 textArea.setColumns( defaultTextAreaColumns );
331 textArea.setRows( defaultTextAreaRows );
332 textArea.setAutoscrolls( true );
333 textArea.add( new JScrollPane() );
334 textArea.setToolTipText( tooltip );
335 textArea.getAccessibleContext().setAccessibleDescription( tooltip );
336 JTextComponentPopupMenu.add( textArea );
337 append( label, new JScrollPane( textArea ) );
338 return textArea;
339 }
340
341 public int getDefaultTextAreaColumns()
342 {
343 return defaultTextAreaColumns;
344 }
345
346 public void setDefaultTextAreaColumns( int defaultTextAreaColumns )
347 {
348 this.defaultTextAreaColumns = defaultTextAreaColumns;
349 }
350
351 public int getDefaultTextAreaRows()
352 {
353 return defaultTextAreaRows;
354 }
355
356 public void setDefaultTextAreaRows( int defaultTextAreaRows )
357 {
358 this.defaultTextAreaRows = defaultTextAreaRows;
359 }
360
361 public JPasswordField appendPasswordField( String label, String tooltip )
362 {
363 JPasswordField textField = new JPasswordField();
364 textField.setColumns( defaultTextFieldColumns );
365 textField.setToolTipText( tooltip );
366 textField.getAccessibleContext().setAccessibleDescription( tooltip );
367 append( label, textField );
368 return textField;
369 }
370
371 public void setComponentValue( String label, String value )
372 {
373 JComponent component = getComponent( label );
374
375 if( component instanceof JScrollPane )
376 component = ( JComponent )( ( JScrollPane )component ).getViewport().getComponent( 0 );
377
378 if( component instanceof JTextComponent )
379 {
380 ( ( JTextComponent )component ).setText( value );
381 }
382 else if( component instanceof JComboBox )
383 {
384 JComboBox comboBox = ( ( JComboBox )component );
385 comboBox.setSelectedItem( value );
386 }
387 else if( component instanceof JList )
388 {
389 ( ( JList )component ).setSelectedValue( value, true );
390 }
391 else if( component instanceof JCheckBox )
392 {
393 ( ( JCheckBox )component ).setSelected( Boolean.valueOf( value ) );
394 }
395 else if( component instanceof JFormComponent )
396 {
397 ( ( JFormComponent )component ).setValue( value );
398 }
399 else if( component instanceof JXEditTextArea )
400 {
401 ( ( JXEditTextArea )component ).setText( value );
402 }
403 }
404
405 public String getComponentValue( String label )
406 {
407 JComponent component = getComponent( label );
408 if( component == null )
409 {
410 return ( String )( hiddenValues == null ? null : hiddenValues.get( label ) );
411 }
412
413 if( component instanceof JTextComponent )
414 {
415 return ( ( JTextComponent )component ).getText();
416 }
417
418 if( component instanceof JComboBox )
419 {
420 JComboBox comboBox = ( ( JComboBox )component );
421 int selectedIndex = comboBox.getSelectedIndex();
422 if( selectedIndex != -1 )
423 {
424 if( comboBoxMaps.containsKey( component ) )
425 {
426 Object[] keys = ( Object[] )comboBoxMaps.get( comboBox );
427 Object value = keys[selectedIndex];
428 return ( String )value == null ? null : value.toString();
429
430
431
432 }
433 else
434 {
435 Object value = comboBox.getSelectedItem();
436 return ( String )value == null ? null : value.toString();
437
438
439
440 }
441 }
442 }
443
444 if( component instanceof JList )
445 {
446 return ( String )( ( JList )component ).getSelectedValue();
447 }
448
449 if( component instanceof JCheckBox )
450 {
451 return String.valueOf( ( ( JCheckBox )component ).isSelected() );
452 }
453
454 else if( component instanceof JFormComponent )
455 {
456 return ( ( JFormComponent )component ).getValue();
457 }
458
459 return null;
460 }
461
462 public JComponent getComponent( String label )
463 {
464 return ( JComponent )components.get( label );
465 }
466
467 public void setBorder( Border border )
468 {
469 panel.setBorder( border );
470 }
471
472 public int getRowCount()
473 {
474 return layout.getRowCount();
475 }
476
477 public void addComponent( JComponent component )
478 {
479 layout.appendRow( rowSpec );
480 int row = layout.getRowCount();
481
482 panel.add( component, cc.xyw( 2, row, 4 ) );
483 }
484
485 public void setValues( Map<String, String> values )
486 {
487 for( Iterator<String> i = values.keySet().iterator(); i.hasNext(); )
488 {
489 String key = i.next();
490 setComponentValue( key, values.get( key ) );
491 }
492 }
493
494 public void getValues( Map<String, String> values )
495 {
496 for( Iterator<String> i = components.keySet().iterator(); i.hasNext(); )
497 {
498 String key = i.next();
499 values.put( key, getComponentValue( key ) );
500 }
501 }
502
503 public void append( JComponent component )
504 {
505 int spaceRowIndex = -1;
506
507 if( rowSpacing > 0 && appended )
508 {
509 addSpace( rowSpacing );
510 spaceRowIndex = layout.getRowCount();
511 }
512
513 layout.appendRow( rowSpec );
514 int row = layout.getRowCount();
515
516 panel.add( component, cc.xyw( 2, row, 4 ) );
517
518 component.addComponentListener( new LabelHider( null, spaceRowIndex ) );
519
520 appended = true;
521 }
522
523 public void setDefaultTextFieldColumns( int defaultTextFieldColumns )
524 {
525 this.defaultTextFieldColumns = defaultTextFieldColumns;
526 }
527
528 private final class LabelHider extends ComponentAdapter
529 {
530 private final JComponent jlabel;
531 private final int rowIndex;
532
533 public LabelHider( JComponent label, int i )
534 {
535 this.jlabel = label;
536 this.rowIndex = i;
537 }
538
539 public void componentHidden( ComponentEvent e )
540 {
541 if( jlabel != null )
542 jlabel.setVisible( false );
543
544 if( rowIndex >= 0 && rowIndex < layout.getRowCount() )
545 layout.setRowSpec( rowIndex, new RowSpec( "0px" ) );
546 }
547
548 public void componentShown( ComponentEvent e )
549 {
550 if( jlabel != null )
551 jlabel.setVisible( true );
552
553 if( rowIndex >= 0 && rowIndex < layout.getRowCount() )
554 layout.setRowSpec( rowIndex, new RowSpec( rowSpacing + "px" ) );
555 }
556 }
557
558 public <T extends JComponent> T append( String name, JLabel label, T field )
559 {
560 return append( name, label, field, null );
561 }
562
563 public void setEnabled( boolean b )
564 {
565 for( JComponent component : components.values() )
566 {
567 if( component instanceof JScrollPane )
568 ( ( JScrollPane )component ).getViewport().getView().setEnabled( b );
569
570 component.setEnabled( b );
571 }
572 }
573
574 public <T extends JComponent> T addLeftComponent( T component )
575 {
576 if( rowSpacing > 0 && !components.isEmpty() )
577 addSpace( rowSpacing );
578
579 layout.appendRow( rowSpec );
580
581 int row = layout.getRowCount();
582 panel.add( component, cc.xy( 4, row ) );
583
584 return component;
585 }
586 }