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.Component;
    17  import java.awt.Dimension;
    18  import java.awt.event.ActionEvent;
    19  import java.io.File;
    20  import java.io.IOException;
    21
    22  import javax.swing.AbstractAction;
    23  import javax.swing.Action;
    24  import javax.swing.Box;
    25  import javax.swing.Icon;
    26  import javax.swing.JButton;
    27  import javax.swing.JComponent;
    28  import javax.swing.JLabel;
    29  import javax.swing.JPanel;
    30  import javax.swing.JScrollPane;
    31  import javax.swing.JTable;
    32  import javax.swing.JTextField;
    33  import javax.swing.event.ListSelectionEvent;
    34  import javax.swing.event.ListSelectionListener;
    35  import javax.swing.table.AbstractTableModel;
    36  import javax.swing.text.Document;
    37
    38  import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
    39  import com.eviware.soapui.impl.wsdl.panels.support.TestRunComponentEnabler;
    40  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
    41  import com.eviware.soapui.impl.wsdl.teststeps.WsdlPropertiesTestStep;
    42  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStepListenerAdapter;
    43  import com.eviware.soapui.impl.wsdl.teststeps.WsdlPropertiesTestStep.StepProperty;
    44  import com.eviware.soapui.model.ModelItem;
    45  import com.eviware.soapui.model.testsuite.TestStepProperty;
    46  import com.eviware.soapui.support.DocumentListenerAdapter;
    47  import com.eviware.soapui.support.UISupport;
    48  import com.eviware.soapui.support.components.JXToolBar;
    49  import com.eviware.soapui.ui.desktop.DesktopPanel;
    50
    51  /***
    52   * DesktopPanel for WsdlPropertiesTestSteps
    53   *
    54   * @author Ole.Matzura
    55   */
    56
    57  public class PropertiesStepDesktopPanel extends JPanel implements DesktopPanel
    58  {
    59  	private final WsdlPropertiesTestStep testStep;
    60  	private JTextField sourceField;
    61  	private JTextField targetField;
    62  	private PropertiesModel propertiesModel;
    63  	private JTable propertiesTable;
    64  	private RemovePropertyAction removePropertyAction;
    65  	private TestRunComponentEnabler componentEnabler;
    66  	private InternalWsdlTestStepListener wsdlTestStepListener;
    67
    68  	public PropertiesStepDesktopPanel(WsdlPropertiesTestStep testStep)
    69  	{
    70  		super(new BorderLayout());
    71  		this.testStep = testStep;
    72  		componentEnabler = new TestRunComponentEnabler(testStep.getTestCase());
    73  		buildUI();
    74
    75  		wsdlTestStepListener = new InternalWsdlTestStepListener();
    76  		testStep.addTestStepListener(wsdlTestStepListener);
    77  	}
    78
    79  	private void buildUI()
    80  	{
    81  		add(buildToolbar(), BorderLayout.NORTH);
    82  		add(buildPropertiesTable(), BorderLayout.CENTER);
    83
    84  		setPreferredSize(new Dimension(600, 400));
    85  	}
    86
    87  	private Component buildPropertiesTable()
    88  	{
    89  		propertiesModel = new PropertiesModel();
    90  		propertiesTable = new JTable(propertiesModel);
    91
    92  		propertiesTable.setDragEnabled(true);
    93
    94  //		propertiesTable.setTransferHandler(new PropertiesTransferHandler( propertiesTable ));
    95
    96  		propertiesTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
    97  		propertiesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener()
    98  		{
    99
    100 			public void valueChanged(ListSelectionEvent e)
    101 			{
    102 				removePropertyAction.setEnabled(propertiesTable.getSelectedRow() != -1);
    103 			}
    104 		});
    105
    106 		componentEnabler.add(propertiesTable);
    107
    108 		return new JScrollPane(propertiesTable);
    109 	}
    110
    111 	private JComponent buildToolbar()
    112 	{
    113 		JXToolBar toolbar = UISupport.createToolbar();
    114
    115 		JButton addPropertyButton = UISupport.createToolbarButton(new AddPropertyAction());
    116 		toolbar.add(addPropertyButton);
    117 		removePropertyAction = new RemovePropertyAction();
    118 		JButton removePropertyButton = UISupport.createToolbarButton(removePropertyAction);
    119 		toolbar.add(removePropertyButton);
    120
    121 		toolbar.addSeparator();
    122 		toolbar.add(new JLabel("Load from:"));
    123 		sourceField = new JTextField(testStep.getSource(), 15);
    124 		sourceField.setToolTipText("The filename/url or referring system-property to load properties from");
    125 		sourceField.getDocument().addDocumentListener(new DocumentListenerAdapter()
    126 		{
    127 			public void update(Document document)
    128 			{
    129 				testStep.setSource(sourceField.getText());
    130
    131 			}
    132 		});
    133
    134 		toolbar.addFixed(sourceField);
    135 		JButton setSourceButton = UISupport.createToolbarButton(new SetPropertiesSourceAction());
    136 		toolbar.add(setSourceButton);
    137
    138 		toolbar.addSeparator();
    139 		toolbar.add(new JLabel("Save to:"));
    140 		targetField = new JTextField(testStep.getTarget(), 15);
    141 		targetField.setToolTipText("The filename/url or referring system-property to save properties to");
    142 		targetField.getDocument().addDocumentListener(new DocumentListenerAdapter()
    143 		{
    144 			public void update(Document document)
    145 			{
    146 				testStep.setTarget(targetField.getText());
    147
    148 			}
    149 		});
    150
    151 		toolbar.addFixed(targetField);
    152 		JButton setTargetButton = UISupport.createToolbarButton(new SetPropertiesTargetAction());
    153 		toolbar.add(setTargetButton);
    154
    155 		toolbar.add(Box.createHorizontalGlue());
    156 		toolbar.addSeparator();
    157 		toolbar.add(UISupport.createToolbarButton(new ShowOnlineHelpAction(HelpUrls.PROPERTIESSTEPEDITOR_HELP_URL)));
    158
    159 		componentEnabler.add(sourceField);
    160 		componentEnabler.add(targetField);
    161 		componentEnabler.add(setTargetButton);
    162 		componentEnabler.add(setSourceButton);
    163 		componentEnabler.add(addPropertyButton);
    164 		componentEnabler.add(removePropertyButton);
    165
    166 		return toolbar;
    167 	}
    168
    169 	public Icon getIcon()
    170 	{
    171 		return getModelItem().getIcon();
    172 	}
    173
    174 	public ModelItem getModelItem()
    175 	{
    176 		return testStep;
    177 	}
    178
    179 	public boolean onClose(boolean canCancel)
    180 	{
    181 		if( propertiesTable.isEditing() )
    182 			propertiesTable.getCellEditor().stopCellEditing();
    183
    184 		componentEnabler.release();
    185 		testStep.removeTestStepListener(wsdlTestStepListener);
    186 		return true;
    187 	}
    188
    189 	public JComponent getComponent()
    190 	{
    191 		return this;
    192 	}
    193
    194 	public boolean dependsOn(ModelItem modelItem)
    195 	{
    196 		return modelItem == testStep || modelItem == testStep.getTestCase()
    197 				|| modelItem == testStep.getTestCase().getTestSuite()
    198 				|| modelItem == testStep.getTestCase().getTestSuite().getProject();
    199 	}
    200
    201 	public String getTitle()
    202 	{
    203 		return testStep.getTestCase().getName() + " - " + testStep.getName();
    204 	}
    205
    206 	public String getDescription()
    207 	{
    208 		return "Properties: [" + testStep.getName() + "] - " + testStep.getTestStepTitle();
    209 	}
    210
    211 	private final class InternalWsdlTestStepListener extends WsdlTestStepListenerAdapter
    212 	{
    213 		private boolean enabled = true;
    214
    215 		public boolean isEnabled()
    216 		{
    217 			return enabled;
    218 		}
    219
    220 		public void setEnabled(boolean enabled)
    221 		{
    222 			this.enabled = enabled;
    223 		}
    224
    225 		public void propertyAdded(String name)
    226 		{
    227 			if (enabled)
    228 				propertiesModel.fireTableDataChanged();
    229 		}
    230
    231 		public void propertyRemoved(String name)
    232 		{
    233 			if (enabled)
    234 				propertiesModel.fireTableDataChanged();
    235 		}
    236
    237 		public void propertyRenamed(String oldName, String newName)
    238 		{
    239 			if (enabled)
    240 				propertiesModel.fireTableDataChanged();
    241 		}
    242
    243 		public void propertyValueChanged(String name, String oldValue, String newValue)
    244 		{
    245 			if (enabled)
    246 				propertiesModel.fireTableDataChanged();
    247 		}
    248 	}
    249
    250 	private class PropertiesModel extends AbstractTableModel
    251 	{
    252 		public int getRowCount()
    253 		{
    254 			return testStep.getStepPropertyCount();
    255 		}
    256
    257 		public int getColumnCount()
    258 		{
    259 			return 2;
    260 		}
    261
    262 		public String getColumnName(int columnIndex)
    263 		{
    264 			switch (columnIndex)
    265 			{
    266 			case 0:
    267 				return "Name";
    268 			case 1:
    269 				return "Value";
    270 			}
    271
    272 			return null;
    273 		}
    274
    275 		public boolean isCellEditable(int rowIndex, int columnIndex)
    276 		{
    277 			return true;
    278 		}
    279
    280 		public void setValueAt(Object aValue, int rowIndex, int columnIndex)
    281 		{
    282 			StepProperty property = testStep.getTestStepPropertyAt(rowIndex);
    283 			switch (columnIndex)
    284 			{
    285 			case 0:
    286 			{
    287 				TestStepProperty prop = testStep.getProperty((String) aValue);
    288 				if (prop != null && prop != property)
    289 				{
    290 					UISupport.showErrorMessage("Property name exists!");
    291 					return;
    292 				}
    293 				wsdlTestStepListener.setEnabled(false);
    294 				property.setName(aValue.toString());
    295 				wsdlTestStepListener.setEnabled(true);
    296 				break;
    297 			}
    298 			case 1:
    299 			{
    300 				wsdlTestStepListener.setEnabled(false);
    301 				property.setValue(aValue.toString());
    302 				wsdlTestStepListener.setEnabled(true);
    303 				break;
    304 			}
    305 			}
    306 		}
    307
    308 		public Object getValueAt(int rowIndex, int columnIndex)
    309 		{
    310 			TestStepProperty property = testStep.getTestStepPropertyAt(rowIndex);
    311
    312 			switch (columnIndex)
    313 			{
    314 			case 0:
    315 				return property.getName();
    316 			case 1:
    317 				return property.getValue();
    318 			}
    319
    320 			return null;
    321 		}
    322 	}
    323
    324 	private class AddPropertyAction extends AbstractAction
    325 	{
    326 		public AddPropertyAction()
    327 		{
    328 			putValue(Action.SMALL_ICON, UISupport.createImageIcon("/add_property.gif"));
    329 			putValue(Action.SHORT_DESCRIPTION, "Adds a property to the property list");
    330 		}
    331
    332 		public void actionPerformed(ActionEvent e)
    333 		{
    334 			String name = UISupport.prompt("Specify property name", "Add Property", "");
    335 			if (name != null)
    336 			{
    337 				wsdlTestStepListener.setEnabled(false);
    338 				testStep.addProperty(name);
    339 				wsdlTestStepListener.setEnabled(true);
    340 				int row = testStep.getStepPropertyCount() - 1;
    341 				propertiesModel.fireTableRowsInserted(row, row);
    342 				propertiesTable.editCellAt(row, 1);
    343 			}
    344 		}
    345 	}
    346
    347 	private class RemovePropertyAction extends AbstractAction
    348 	{
    349 		public RemovePropertyAction()
    350 		{
    351 			putValue(Action.SMALL_ICON, UISupport.createImageIcon("/remove_property.gif"));
    352 			putValue(Action.SHORT_DESCRIPTION, "Removes the selected property from the property list");
    353 			setEnabled(false);
    354 		}
    355
    356 		public void actionPerformed(ActionEvent e)
    357 		{
    358 			int row = propertiesTable.getSelectedRow();
    359 			if (row == -1)
    360 				return;
    361
    362 			UISupport.stopCellEditing(propertiesTable);
    363
    364 			if (UISupport.confirm("Remove property [" + propertiesModel.getValueAt(row, 0) + "]?", "Remove Property"))
    365 			{
    366 				wsdlTestStepListener.setEnabled(false);
    367 				testStep.removePropertyAt(row);
    368 				wsdlTestStepListener.setEnabled(true);
    369 				propertiesModel.fireTableRowsDeleted(row, row);
    370 			}
    371 		}
    372 	}
    373
    374 	private class SetPropertiesSourceAction extends AbstractAction
    375 	{
    376 		public SetPropertiesSourceAction()
    377 		{
    378 			putValue(Action.SMALL_ICON, UISupport.createImageIcon("/set_properties_source.gif"));
    379 			putValue(Action.SHORT_DESCRIPTION, "Selects the properties source file");
    380 		}
    381
    382 		public void actionPerformed(ActionEvent e)
    383 		{
    384 			File file = UISupport.getFileDialogs().open(this, "Set properties source", null, null, null);
    385 			if (file != null)
    386 			{
    387 				testStep.setSource(file.getAbsolutePath());
    388 				sourceField.setText(testStep.getSource());
    389 				try
    390 				{
    391 					boolean createMissing = UISupport.confirm("Create missing properties?", "Set Properties Source");
    392 					int cnt = testStep.loadProperties(createMissing);
    393 					UISupport.showInfoMessage("Loaded " + cnt + " properties from [" + testStep.getSource() + "]");
    394 				}
    395 				catch (IOException e1)
    396 				{
    397 					UISupport.showErrorMessage("Failed to load properties from [" + testStep.getSource() + "]; " + e1);
    398 				}
    399 			}
    400 		}
    401 	}
    402
    403 	private class SetPropertiesTargetAction extends AbstractAction
    404 	{
    405 		public SetPropertiesTargetAction()
    406 		{
    407 			putValue(Action.SMALL_ICON, UISupport.createImageIcon("/set_properties_target.gif"));
    408 			putValue(Action.SHORT_DESCRIPTION, "Selects the properties target file");
    409 		}
    410
    411 		public void actionPerformed(ActionEvent e)
    412 		{
    413 			File file = UISupport.getFileDialogs().saveAs(this, "Set properties target");
    414 			if (file != null)
    415 			{
    416 				testStep.setTarget(file.getAbsolutePath());
    417 				targetField.setText(testStep.getTarget());
    418
    419 				try
    420 				{
    421 					int cnt = testStep.saveProperties();
    422 					UISupport.showInfoMessage("Saved " + cnt + " properties to [" + testStep.getTarget() + "]");
    423 				}
    424 				catch (IOException e1)
    425 				{
    426 					UISupport.showErrorMessage("Failed to save properties to [" + testStep.getTarget() + "]; " + e1);
    427 				}
    428 			}
    429 		}
    430 	}
    431
    432 	/*
    433 	public class PropertiesTransferHandler extends AbstractPropertiesTransferHandler
    434 	{
    435 		public PropertiesTransferHandler(JComponent component)
    436 		{
    437 			super(component);
    438 		}
    439
    440 		protected StepProperty getSelectedProperty(JComponent c)
    441 		{
    442 			int rowIndex = propertiesTable.getSelectedRow();
    443 			if (rowIndex == -1)
    444 				return null;
    445
    446 			return testStep.getTestStepPropertyAt(rowIndex);
    447 		}
    448 	}*/
    449 }