1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.support;
14
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.Map;
18
19 import javax.swing.Action;
20 import javax.swing.JButton;
21 import javax.swing.JCheckBox;
22 import javax.swing.JComboBox;
23 import javax.swing.JComponent;
24 import javax.swing.JLabel;
25 import javax.swing.JList;
26 import javax.swing.JPanel;
27 import javax.swing.JSeparator;
28 import javax.swing.JTextField;
29 import javax.swing.text.JTextComponent;
30
31 import com.jgoodies.forms.layout.CellConstraints;
32 import com.jgoodies.forms.layout.FormLayout;
33 import com.jgoodies.forms.layout.RowSpec;
34
35 /***
36 * Utility-class for creating forms
37 */
38
39 public class SimpleForm
40 {
41 private JPanel panel;
42 private CellConstraints cc = new CellConstraints();
43 private FormLayout layout;
44 private RowSpec rowSpec;
45 private int rowSpacing = 5;
46 private Map<String,JComponent> components = new HashMap<String,JComponent>();
47 private Map<JComboBox,Object[]> comboBoxMaps = new HashMap<JComboBox,Object[]>();
48 private String rowAlignment = "top";
49 private Map<String,String> hiddenValues;
50
51 public SimpleForm()
52 {
53 this( new FormLayout( "10px,right:pref,10px,pref:grow,5px" ) );
54 }
55
56 public SimpleForm( FormLayout layout )
57 {
58 this.layout = layout;
59 panel = new JPanel( layout );
60 rowSpec = new RowSpec( rowAlignment + ":pref" );
61 }
62
63 public JPanel getPanel()
64 {
65 return panel;
66 }
67
68 public String getRowAlignment()
69 {
70 return rowAlignment;
71 }
72
73 public void setRowAlignment( String rowAlignment )
74 {
75 this.rowAlignment = rowAlignment;
76 }
77
78 public int getRowSpacing()
79 {
80 return rowSpacing;
81 }
82
83 public void setRowSpacing( int rowSpacing )
84 {
85 this.rowSpacing = rowSpacing;
86 }
87
88 public void addHiddenValue( String name, String value )
89 {
90 if( hiddenValues == null )
91 hiddenValues = new HashMap<String,String>();
92
93 hiddenValues.put( name, value );
94 }
95
96 public JButton addRightButton( Action action )
97 {
98 if( rowSpacing > 0 && !components.isEmpty() )
99 addSpace( rowSpacing );
100
101 layout.appendRow( rowSpec );
102 int row = layout.getRowCount();
103
104 JButton button = new JButton( action );
105 panel.add( button, cc.xy( 4, row, "right,bottom" ) );
106 return button;
107 }
108
109 public void addSpace( int size )
110 {
111 if( size > 0 )
112 layout.appendRow( new RowSpec( size + "px" ) );
113 }
114
115 public void addRightComponent( JComponent component )
116 {
117 if( rowSpacing > 0 && !components.isEmpty() )
118 addSpace( rowSpacing );
119
120 layout.appendRow( rowSpec );
121 int row = layout.getRowCount();
122
123 panel.add( component, cc.xy( 4, row, "right,bottom" ) );
124 }
125
126 public JCheckBox appendCheckBox( String caption, String label, boolean selected )
127 {
128 JCheckBox checkBox = new JCheckBox( label, selected );
129 components.put( caption, checkBox );
130 append( caption, checkBox );
131 return checkBox;
132 }
133
134 public void append( String label, JComponent component )
135 {
136 append( label, component, null );
137 }
138
139 public JComboBox appendComboBox( String label, Map values )
140 {
141 Object[] valueArray = new Object[values.size()];
142 Object[] keyArray = new Object[values.size()];
143
144 int ix = 0;
145 for( Iterator i = values.keySet().iterator(); i.hasNext(); ix++ )
146 {
147 keyArray[ix] = i.next();
148 valueArray[ix] = values.get( keyArray[ix] );
149 }
150
151 JComboBox comboBox = new JComboBox( valueArray );
152
153 comboBoxMaps.put( comboBox, keyArray );
154
155 append( label, comboBox );
156 return comboBox;
157 }
158
159 public JComboBox appendComboBox( String label, String[] values )
160 {
161 JComboBox comboBox = new JComboBox( values );
162 append( label, comboBox );
163 return comboBox;
164 }
165
166 public void appendFixed( String label, JComponent component )
167 {
168 append( label, component, "left:pref" );
169 }
170
171 public void append( String label, JComponent component, String alignments )
172 {
173 if( rowSpacing > 0 && !components.isEmpty() )
174 addSpace( rowSpacing );
175
176 layout.appendRow( rowSpec );
177 int row = layout.getRowCount();
178
179 if( label != null )
180 panel.add( new JLabel( label ), cc.xy( 2, row ) );
181
182 if( alignments == null )
183 panel.add( component, cc.xy( 4, row ) );
184 else
185 panel.add( component, cc.xy( 4, row, alignments ) );
186
187 components.put( label, component );
188 }
189
190 public void appendSeparator()
191 {
192 addSpace( rowSpacing );
193
194 layout.appendRow( rowSpec );
195 int row = layout.getRowCount();
196
197 panel.add( new JSeparator(), cc.xywh( 2, row, 3, 1 ) );
198 }
199
200 public void appendTextField( String label )
201 {
202 append( label, new JTextField() );
203 }
204
205 public String getComponentValue( String label )
206 {
207 JComponent component = getComponent( label );
208 if( component == null )
209 {
210 return (String) (hiddenValues == null ? null : hiddenValues.get( label ));
211 }
212
213 if( component instanceof JTextComponent )
214 {
215 return ((JTextComponent) component).getText();
216 }
217
218 if( component instanceof JComboBox )
219 {
220 JComboBox comboBox = ((JComboBox) component);
221 if( comboBoxMaps.containsKey( component ) )
222 {
223 Object[] keys = (Object[]) comboBoxMaps.get( comboBox );
224 int selectedItem = comboBox.getSelectedIndex();
225
226 if( selectedItem != -1 ) return (String) keys[selectedItem];
227 }
228 else
229 return comboBox.getSelectedItem().toString();
230 }
231
232 if( component instanceof JList )
233 {
234 return (String) ((JList) component).getSelectedValue();
235 }
236
237 if( component instanceof JCheckBox )
238 {
239 return String.valueOf( ((JCheckBox) component).isSelected() );
240 }
241
242 return null;
243 }
244
245 public JComponent getComponent( String label )
246 {
247 return (JComponent) components.get( label );
248 }
249 }
250