View Javadoc

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