View Javadoc

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