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