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       else
222       	component.addComponentListener( new LabelHider( null, spaceRowIndex ) );
223 
224       if( alignments == null )
225          panel.add( component, cc.xy( 4, row ) );
226       else
227          panel.add( component, cc.xy( 4, row, alignments ) );
228 
229       components.put( label, component );
230       appended = true;
231    }
232 
233    public void appendSeparator()
234    {
235    	if( appended && rowSpacing > 0 )
236    		addSpace( rowSpacing );
237 
238       layout.appendRow( rowSpec );
239       int row = layout.getRowCount();
240 
241       panel.add( new JSeparator(), cc.xywh( 2, row, 3, 1 ) );
242       appended = true;
243    }
244 
245    public JTextField appendTextField( String label, String tooltip )
246    {
247       JTextField textField = new JTextField();
248       textField.setColumns( 30 );
249       textField.setToolTipText( tooltip );
250 		append( label, textField );
251 		return textField;
252    }
253    
254    public JTextField appendPasswordField( String label, String tooltip )
255    {
256       JPasswordField textField = new JPasswordField();
257       textField.setColumns( 30 );
258       textField.setToolTipText( tooltip );
259 		append( label, textField );
260 		return textField;
261    }
262 
263    public void setComponentValue( String label, String value )
264    {
265       JComponent component = getComponent( label );
266 
267       if( component instanceof JTextComponent )
268       {
269          ((JTextComponent) component).setText( value );
270       }
271       else if( component instanceof JComboBox )
272       {
273          JComboBox comboBox = ((JComboBox) component);
274          comboBox.setSelectedItem( value );
275       }
276       else if( component instanceof JList )
277       {
278          ((JList) component).setSelectedValue( value, true );
279       }
280       else if( component instanceof JCheckBox )
281       {
282          ((JCheckBox) component).setSelected( Boolean.valueOf( value ));
283       }
284       else if( component instanceof JFormComponent )
285       {
286       	((JFormComponent) component).setValue( value );
287       }      	
288    }
289    
290    public String getComponentValue( String label )
291    {
292       JComponent component = getComponent( label );
293       if( component == null )
294       {
295          return (String) (hiddenValues == null ? null : hiddenValues.get( label ));
296       }
297       
298       if( component instanceof JTextComponent )
299       {
300          return ((JTextComponent) component).getText();
301       }
302 
303       if( component instanceof JComboBox )
304       {
305          JComboBox comboBox = ((JComboBox) component);
306          int selectedIndex = comboBox.getSelectedIndex();
307          if( selectedIndex != -1 )
308          {
309 	         if( comboBoxMaps.containsKey( component ) )
310 	         {
311 	            Object[] keys = (Object[]) comboBoxMaps.get( comboBox );
312 	            Object value = keys[selectedIndex];
313 					return (String) value == null ? null : value.toString(); // Added support for enums
314 	         }
315 	         else
316 	         {
317 	            Object value = comboBox.getSelectedItem();
318 	            return (String) value == null ? null : value.toString(); // Added support for enums
319 	         }
320          }
321       }
322 
323       if( component instanceof JList )
324       {
325          return (String) ((JList) component).getSelectedValue();
326       }
327 
328       if( component instanceof JCheckBox )
329       {
330          return String.valueOf( ((JCheckBox) component).isSelected() );
331       }
332 
333       else if( component instanceof JFormComponent )
334       {
335       	return ((JFormComponent) component).getValue();
336       }      	
337       
338       return null;
339    }
340 
341    public JComponent getComponent( String label )
342    {
343       return (JComponent) components.get( label );
344    }
345 
346 	public void setBorder(Border border)
347 	{
348 		panel.setBorder( border );
349 	}
350 
351 	public int getRowCount()
352 	{
353 		return layout.getRowCount();
354 	}
355 	
356 	public void addComponent(JComponent component)
357 	{
358 		layout.appendRow( rowSpec );
359       int row = layout.getRowCount();
360 		
361 		panel.add( component, cc.xyw( 2, row, 4 ) );
362 	}
363 
364 	public void setValues(Map<String, String> values)
365 	{
366 		for( Iterator<String> i = values.keySet().iterator(); i.hasNext(); )
367 		{
368 			String key = i.next();
369 			setComponentValue( key, values.get( key ));
370 		}
371 	}
372 
373 	public void getValues(Map<String, String> values)
374 	{
375 		for( Iterator<String> i = components.keySet().iterator(); i.hasNext(); )
376 		{
377 			String key = i.next();
378 			values.put( key, getComponentValue( key ));
379 		}
380 	}
381 
382 	public void append( JComponent component )
383 	{
384 		int spaceRowIndex = -1;
385    	
386       if( rowSpacing > 0 && appended )
387       {
388          addSpace( rowSpacing );
389          spaceRowIndex = layout.getRowCount();
390       }
391 
392       layout.appendRow( rowSpec );
393       int row = layout.getRowCount();
394 
395       panel.add( component, cc.xyw( 2, row, 4 ) );
396       
397       component.addComponentListener( new LabelHider( null, spaceRowIndex ) );
398       
399       appended = true;
400 	}
401 	
402 	private final class LabelHider extends ComponentAdapter
403 	{
404 		private final JLabel jlabel;
405 		private final int rowIndex;
406 
407 		public LabelHider( JLabel jlabel, int i )
408 		{
409 			this.jlabel = jlabel;
410 			this.rowIndex = i;
411 		}
412 
413 		public void componentHidden( ComponentEvent e )
414 		{
415 			if( jlabel != null )
416 				jlabel.setVisible( false );
417 			
418 			if( rowIndex >= 0 && rowIndex < layout.getRowCount() )
419 				layout.setRowSpec( rowIndex, new RowSpec( "0px" ));
420 		}
421 
422 		public void componentShown( ComponentEvent e )
423 		{
424 			if( jlabel != null )
425 				jlabel.setVisible( true );
426 			
427 			if( rowIndex >= 0 && rowIndex < layout.getRowCount() )
428 				layout.setRowSpec( rowIndex, new RowSpec( rowSpacing + "px" ));
429 		}
430 	}
431 }
432