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