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