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