View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.impl.wsdl.panels.teststeps;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Dimension;
17  import java.awt.event.ActionEvent;
18  import java.awt.event.MouseEvent;
19  import java.beans.PropertyChangeEvent;
20  import java.beans.PropertyChangeListener;
21  import java.io.File;
22  import java.io.IOException;
23  
24  import javax.swing.AbstractAction;
25  import javax.swing.Action;
26  import javax.swing.Box;
27  import javax.swing.JButton;
28  import javax.swing.JComponent;
29  import javax.swing.JLabel;
30  import javax.swing.JTextField;
31  import javax.swing.text.Document;
32  
33  import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
34  import com.eviware.soapui.impl.wsdl.panels.support.TestRunComponentEnabler;
35  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.PropertyHolderTable;
36  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
37  import com.eviware.soapui.impl.wsdl.teststeps.WsdlPropertiesTestStep;
38  import com.eviware.soapui.model.ModelItem;
39  import com.eviware.soapui.model.support.ModelSupport;
40  import com.eviware.soapui.support.DocumentListenerAdapter;
41  import com.eviware.soapui.support.StringUtils;
42  import com.eviware.soapui.support.UISupport;
43  import com.eviware.soapui.support.components.JXToolBar;
44  import com.eviware.soapui.ui.support.ModelItemDesktopPanel;
45  
46  /***
47   * DesktopPanel for WsdlPropertiesTestSteps
48   * 
49   * @author Ole.Matzura
50   */
51  
52  public class PropertiesStepDesktopPanel extends ModelItemDesktopPanel<WsdlPropertiesTestStep> implements
53  		PropertyChangeListener
54  {
55  	private final WsdlPropertiesTestStep testStep;
56  	private JTextField sourceField;
57  	private JTextField targetField;
58  	private PropertyHolderTable propertiesTable;
59  	private TestRunComponentEnabler componentEnabler;
60  	protected boolean updatingSource;
61  	protected boolean updatingTarget;
62  
63  	public PropertiesStepDesktopPanel( WsdlPropertiesTestStep testStep )
64  	{
65  		super( testStep );
66  		this.testStep = testStep;
67  		componentEnabler = new TestRunComponentEnabler( testStep.getTestCase() );
68  		buildUI();
69  
70  		testStep.addPropertyChangeListener( this );
71  	}
72  
73  	private void buildUI()
74  	{
75  		propertiesTable = createPropertyHolderTable();
76  		add( propertiesTable, BorderLayout.CENTER );
77  
78  		JXToolBar toolbar = propertiesTable.getToolbar();
79  
80  		toolbar.addRelatedGap();
81  		JButton reloadButton = UISupport.createToolbarButton( new ReloadPropertiesFromSourceAction() );
82  		toolbar.add( reloadButton );
83  
84  		toolbar.addSeparator();
85  		toolbar.add( new JLabel( "Load from:" ) );
86  		sourceField = new JTextField( testStep.getSource(), 20 )
87  		{
88  			@Override
89  			public String getToolTipText( MouseEvent event )
90  			{
91  				return testStep.getSource( true );
92  			}
93  		};
94  		sourceField.setToolTipText( "The filename/url or referring system-property to load properties from" );
95  		sourceField.getDocument().addDocumentListener( new DocumentListenerAdapter()
96  		{
97  			public void update( Document document )
98  			{
99  				if( updatingSource )
100 					return;
101 
102 				updatingSource = true;
103 				testStep.setSource( sourceField.getText() );
104 				updatingSource = false;
105 			}
106 		} );
107 
108 		toolbar.addRelatedGap();
109 		toolbar.addFixed( sourceField );
110 		JButton setSourceButton = UISupport.createToolbarButton( new SetPropertiesSourceAction() );
111 		toolbar.addRelatedGap();
112 		toolbar.add( setSourceButton );
113 
114 		toolbar.addSeparator();
115 		toolbar.add( new JLabel( "Save to:" ) );
116 		targetField = new JTextField( testStep.getTarget(), 20 )
117 		{
118 			@Override
119 			public String getToolTipText( MouseEvent event )
120 			{
121 				return testStep.getTarget( true );
122 			}
123 		};
124 
125 		targetField.setToolTipText( "The filename/url or referring system-property to save properties to" );
126 		targetField.getDocument().addDocumentListener( new DocumentListenerAdapter()
127 		{
128 			public void update( Document document )
129 			{
130 				if( updatingTarget )
131 					return;
132 
133 				updatingTarget = true;
134 				testStep.setTarget( targetField.getText() );
135 				updatingTarget = false;
136 			}
137 		} );
138 
139 		toolbar.addRelatedGap();
140 		toolbar.addFixed( targetField );
141 		JButton setTargetButton = UISupport.createToolbarButton( new SetPropertiesTargetAction() );
142 		toolbar.addRelatedGap();
143 		toolbar.add( setTargetButton );
144 
145 		toolbar.add( Box.createHorizontalGlue() );
146 		toolbar.addSeparator();
147 		toolbar.add( UISupport.createToolbarButton( new ShowOnlineHelpAction( HelpUrls.PROPERTIESSTEPEDITOR_HELP_URL ) ) );
148 
149 		componentEnabler.add( sourceField );
150 		componentEnabler.add( targetField );
151 		componentEnabler.add( setTargetButton );
152 		componentEnabler.add( setSourceButton );
153 		componentEnabler.add( propertiesTable );
154 
155 		setPreferredSize( new Dimension( 600, 400 ) );
156 	}
157 
158 	protected PropertyHolderTable createPropertyHolderTable()
159 	{
160 		return new PropertyHolderTable( getModelItem() );
161 	}
162 
163 	public boolean onClose( boolean canCancel )
164 	{
165 		componentEnabler.release();
166 		propertiesTable.release();
167 		super.release();
168 		return true;
169 	}
170 
171 	public JComponent getComponent()
172 	{
173 		return this;
174 	}
175 
176 	public boolean dependsOn( ModelItem modelItem )
177 	{
178 		return modelItem == testStep || modelItem == testStep.getTestCase()
179 				|| modelItem == testStep.getTestCase().getTestSuite()
180 				|| modelItem == testStep.getTestCase().getTestSuite().getProject();
181 	}
182 
183 	private class SetPropertiesSourceAction extends AbstractAction
184 	{
185 		public SetPropertiesSourceAction()
186 		{
187 			putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/set_properties_source.gif" ) );
188 			putValue( Action.SHORT_DESCRIPTION, "Selects the properties source file" );
189 		}
190 
191 		public void actionPerformed( ActionEvent e )
192 		{
193 			String root = ModelSupport.getResourceRoot( testStep );
194 			File file = UISupport.getFileDialogs().open( this, "Set properties source", "properties",
195 					"Properties Files (*.properties)", root );
196 			if( file != null )
197 			{
198 				updatingSource = true;
199 				testStep.setSource( file.getAbsolutePath() );
200 				sourceField.setText( testStep.getSource() );
201 				updatingSource = false;
202 				try
203 				{
204 					boolean createMissing = UISupport.confirm( "Create missing properties?", "Set Properties Source" );
205 					int cnt = testStep.loadProperties( createMissing );
206 					UISupport.showInfoMessage( "Loaded " + cnt + " properties from [" + testStep.getSource() + "]" );
207 				}
208 				catch( IOException e1 )
209 				{
210 					UISupport.showErrorMessage( "Failed to load properties from [" + testStep.getSource() + "]; " + e1 );
211 				}
212 			}
213 		}
214 	}
215 
216 	private class ReloadPropertiesFromSourceAction extends AbstractAction
217 	{
218 		public ReloadPropertiesFromSourceAction()
219 		{
220 			putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/reload_properties.gif" ) );
221 			putValue( Action.SHORT_DESCRIPTION, "Reloads the current properties from the selected file" );
222 		}
223 
224 		public void actionPerformed( ActionEvent e )
225 		{
226 			if( StringUtils.isNullOrEmpty( testStep.getSource() ) )
227 			{
228 				UISupport.showErrorMessage( "Missing source-file to load from" );
229 				return;
230 			}
231 
232 			try
233 			{
234 				boolean createMissing = UISupport.confirm( "Create missing properties?", "Reload Properties" );
235 				int cnt = testStep.loadProperties( createMissing );
236 				UISupport.showInfoMessage( "Loaded " + cnt + " properties from [" + testStep.getSource() + "]" );
237 			}
238 			catch( Exception e1 )
239 			{
240 				UISupport.showErrorMessage( "Failed to load properties from [" + testStep.getSource() + "]; " + e1 );
241 			}
242 		}
243 	}
244 
245 	private class SetPropertiesTargetAction extends AbstractAction
246 	{
247 		public SetPropertiesTargetAction()
248 		{
249 			putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/set_properties_target.gif" ) );
250 			putValue( Action.SHORT_DESCRIPTION, "Selects the properties target file" );
251 		}
252 
253 		public void actionPerformed( ActionEvent e )
254 		{
255 			String root = ModelSupport.getResourceRoot( testStep );
256 			File file = UISupport.getFileDialogs().saveAs( this, "Set properties target", "properties",
257 					"Properties Files (*.properties)", new File( root ) );
258 			if( file != null )
259 			{
260 				updatingTarget = true;
261 				testStep.setTarget( file.getAbsolutePath() );
262 				targetField.setText( testStep.getTarget() );
263 				updatingTarget = false;
264 
265 				try
266 				{
267 					int cnt = testStep.saveProperties();
268 					UISupport.showInfoMessage( "Saved " + cnt + " properties to [" + testStep.getTarget() + "]" );
269 				}
270 				catch( IOException e1 )
271 				{
272 					UISupport.showErrorMessage( "Failed to save properties to [" + testStep.getTarget() + "]; " + e1 );
273 				}
274 			}
275 		}
276 	}
277 
278 	public void propertyChange( PropertyChangeEvent evt )
279 	{
280 		if( !updatingSource && evt.getPropertyName().equals( WsdlPropertiesTestStep.SOURCE_PROPERTY ) )
281 			sourceField.setText( evt.getNewValue().toString() );
282 		else if( !updatingTarget && evt.getPropertyName().equals( WsdlPropertiesTestStep.TARGET_PROPERTY ) )
283 			targetField.setText( evt.getNewValue().toString() );
284 	}
285 
286 	@Override
287 	protected boolean release()
288 	{
289 		testStep.removePropertyChangeListener( this );
290 
291 		return super.release();
292 	}
293 
294 	/*
295 	 * public class PropertiesTransferHandler extends
296 	 * AbstractPropertiesTransferHandler { public
297 	 * PropertiesTransferHandler(JComponent component) { super(component); }
298 	 * 
299 	 * protected StepProperty getSelectedProperty(JComponent c) { int rowIndex =
300 	 * propertiesTable.getSelectedRow(); if (rowIndex == -1) return null;
301 	 * 
302 	 * return testStep.getTestStepPropertyAt(rowIndex); } }
303 	 */
304 }