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 java.awt.BorderLayout;
16  import java.awt.Dimension;
17  import java.awt.event.ActionEvent;
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.swing.AbstractAction;
22  import javax.swing.BorderFactory;
23  import javax.swing.DefaultComboBoxModel;
24  import javax.swing.ImageIcon;
25  import javax.swing.JComboBox;
26  import javax.swing.JComponent;
27  import javax.swing.JDialog;
28  import javax.swing.JList;
29  import javax.swing.JPanel;
30  import javax.swing.JSeparator;
31  
32  import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
33  import com.eviware.soapui.support.UISupport;
34  import com.eviware.soapui.support.action.swing.ActionList;
35  import com.eviware.soapui.support.action.swing.DefaultActionList;
36  import com.jgoodies.forms.layout.FormLayout;
37  
38  /***
39   * Utility for creating simple configuration dialogs
40   * 
41   * @author Ole.Matzura
42   */
43  
44  public class SwingConfigurationDialogImpl implements ConfigurationDialog
45  {
46  	private JDialog dialog;
47  	private SimpleForm form;
48  	private boolean result;
49  	private Map<String,String> values;
50  	private Map<String,FieldType> fieldTypes = new HashMap<String,FieldType>();
51  	private final String title;
52  	private Dimension size;
53  	private JComponent content;
54  	private ActionList actions;
55  	private String description;
56  	private ImageIcon icon;
57  	
58  	public SwingConfigurationDialogImpl( String title, String helpUrl, String description, ImageIcon icon )
59  	{
60  		this.title = title;
61  		this.description = description;
62  		this.icon = icon;
63  		form = new SimpleForm( new FormLayout( "10px,left:pref,10px,left:pref,5px" ) );
64  		
65  		actions = new DefaultActionList( "Actions" );
66  		if( helpUrl != null )
67  		{
68  			actions.addAction( new ShowOnlineHelpAction( helpUrl ));
69  			actions.addSeparator();
70  		}
71  		
72  		OkAction okAction = new OkAction();
73  		actions.addAction( okAction );
74  		actions.addAction( new CancelAction() );
75  		actions.setDefaultAction( okAction );
76  	}
77  	
78  	public boolean show( Map<String,String> values )
79  	{
80  		if( dialog == null )
81  			buildDialog();
82  		
83  		this.values = values;
84  		
85  		result = false;
86  		
87  		form.setValues( values );
88  		
89  		if( size == null )
90  			dialog.pack();
91  		else
92  			dialog.setSize( size );
93  		
94  		UISupport.showDialog( dialog );
95  		return result;
96  	}
97  
98  	public Dimension getSize()
99  	{
100 		return size;
101 	}
102 
103 	public void setSize(Dimension preferredSize)
104 	{
105 		this.size = preferredSize;
106 	}
107 
108 	public void addTextField( String name, String tooltip )
109 	{
110 	   addTextField( name, tooltip, FieldType.TEXT );
111 	}
112 	
113 	public void addTextField( String name, String tooltip, FieldType type )
114 	{
115 		if( type == FieldType.DIRECTORY )
116 			form.append( name, new DirectoryFormComponent( tooltip ) );
117 		else
118 			form.appendTextField( name, tooltip );
119 		
120 		fieldTypes.put( name, type );
121 	}
122 	
123    public void addCheckBox( String caption, String label, boolean selected )
124    {
125    	form.appendCheckBox( caption, label, selected );
126    }
127 	
128    public void setContent( JComponent content )
129    {
130 		this.content = content;
131 	}
132    
133 	private void buildDialog()
134 	{
135 		dialog = new JDialog( UISupport.getMainFrame(), title, true );
136 		
137 		form.getPanel().setBorder( BorderFactory.createEmptyBorder( 5, 0, 5, 0 ));
138 		
139 		JPanel panel = new JPanel( new BorderLayout() );
140 		JComponent contentPanel = content == null ? form.getPanel() : content;
141 		panel.add( contentPanel, BorderLayout.CENTER );
142 		
143 		JButtonBar buttons = UISupport.initDialogActions( actions, dialog );
144 		buttons.setBorder( BorderFactory.createEmptyBorder( 3, 5, 3, 5 ) );
145 		
146 		if( content == null )
147 		{
148 			JPanel p = new JPanel( new BorderLayout() );
149 			p.add( new JSeparator(), BorderLayout.NORTH );
150 			p.add( buttons, BorderLayout.CENTER );
151 		
152 			panel.add( p, BorderLayout.SOUTH );
153 		}
154 		else
155 		{
156 			panel.add( buttons, BorderLayout.SOUTH );
157 		}
158 		
159 		if( description != null || icon != null )
160 			dialog.getContentPane().add( UISupport.buildDescription( title, description, icon ), BorderLayout.NORTH );
161 		
162 		dialog.getContentPane().add( panel );
163 		dialog.pack();
164 	}
165 	
166 	private class OkAction extends AbstractAction
167 	{
168 		public OkAction()
169 		{
170 			super( "Ok" );
171 		}
172 		
173 		public void actionPerformed(ActionEvent e)
174 		{
175 			result = true;
176 			
177 			form.getValues( values );
178 			dialog.setVisible( false );
179 		}
180 	}
181 
182 	private class CancelAction extends AbstractAction
183 	{
184 		public CancelAction()
185 		{
186 			super( "Cancel" );
187 		}
188 
189 		public void actionPerformed(ActionEvent e)
190 		{
191 			dialog.setVisible( false );
192 		}
193 	}
194 
195 	public void addComboBox( String label, Object[] objects, String tooltip)
196 	{
197 		form.appendComboBox( label, objects, tooltip );
198 	}
199 
200 	public void setValues(String id, String[] values)
201 	{
202 		JComponent component = form.getComponent( id );
203 		if( component instanceof JComboBox )
204 		{
205 			((JComboBox)component).setModel( new DefaultComboBoxModel( values ));
206 		}
207 		else if( component instanceof JList )
208 		{
209 			((JList)component).setModel( new DefaultComboBoxModel( values ));
210 		}
211 		else throw new RuntimeException( "Could not set values on [" + component + "]" );
212 	}
213 
214 	public void addComboBox(String label, String tooltip)
215 	{
216 		form.appendComboBox( label, new String[] {}, tooltip );
217 	}
218 
219 	public void addComponent(JComponent component)
220 	{
221 		form.addComponent( component );
222 	}
223 
224 	public void getValues(Map<String, String> values)
225 	{
226 		form.getValues( values );
227 	}
228 
229 	public ActionList getActions()
230 	{
231 		return actions;
232 	}
233 
234 	public void hide()
235 	{
236 		dialog.setVisible( false );
237 	}
238 }