1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.panels.testcase;
14
15 import java.awt.BorderLayout;
16 import java.awt.Component;
17 import java.awt.Toolkit;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.KeyAdapter;
20 import java.awt.event.KeyEvent;
21 import java.awt.event.MouseAdapter;
22 import java.awt.event.MouseEvent;
23 import java.beans.PropertyChangeEvent;
24 import java.beans.PropertyChangeListener;
25
26 import javax.swing.AbstractAction;
27 import javax.swing.AbstractListModel;
28 import javax.swing.Action;
29 import javax.swing.BorderFactory;
30 import javax.swing.JLabel;
31 import javax.swing.JList;
32 import javax.swing.JMenu;
33 import javax.swing.JPanel;
34 import javax.swing.JPopupMenu;
35 import javax.swing.ListCellRenderer;
36 import javax.swing.ListSelectionModel;
37 import javax.swing.event.PopupMenuEvent;
38 import javax.swing.event.PopupMenuListener;
39
40 import com.eviware.soapui.SoapUI;
41 import com.eviware.soapui.config.TestStepConfig;
42 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
43 import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
44 import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestStepFactory;
45 import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestStepRegistry;
46 import com.eviware.soapui.model.ModelItem;
47 import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
48 import com.eviware.soapui.model.testsuite.TestStep;
49 import com.eviware.soapui.support.UISupport;
50 import com.eviware.soapui.support.action.ActionList;
51 import com.eviware.soapui.support.action.ActionSupport;
52
53 /***
54 * Panel for displaying and editing a list of TestSteps
55 *
56 * @author Ole.Matzura
57 */
58
59 public class TestStepList extends JPanel
60 {
61 private TestStepListModel testStepListModel;
62 private JList testStepList;
63 private JPopupMenu testListPopup;
64 private JMenu appendStepMenu;
65 private MoveStepDownAction moveStepDownAction;
66 private MoveStepUpAction moveStepUpAction;
67 private final WsdlTestCase testCase;
68
69 public TestStepList(WsdlTestCase testCase)
70 {
71 super(new BorderLayout());
72 this.testCase = testCase;
73
74 buildUI();
75 }
76
77 private void buildUI()
78 {
79 testStepListModel = new TestStepListModel();
80 testStepList = new JList(testStepListModel);
81 testStepList.setCellRenderer(new TestStepCellRenderer());
82 testStepList.setFixedCellHeight( 22 );
83 testStepList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
84 testStepList.addKeyListener(new TestStepListKeyHandler());
85
86 testStepList.addMouseListener(new StepListMouseListener());
87 testStepList.addMouseListener(new MouseAdapter()
88 {
89 private int newIndex;
90 private int prevIndex;
91
92 public void mouseClicked(MouseEvent e)
93 {
94 if( e.getButton() != MouseEvent.BUTTON1 )
95 return;
96
97 newIndex = testStepList.locationToIndex(e.getPoint());
98
99 if((prevIndex == newIndex) & ( prevIndex != -1))
100 {
101 testStepList.clearSelection();
102 newIndex = -1;
103 }
104
105 prevIndex = newIndex;
106 }
107 } );
108
109 testListPopup = new JPopupMenu();
110 testListPopup.addSeparator();
111
112 appendStepMenu = new JMenu("Append Step");
113
114 WsdlTestStepRegistry registry = WsdlTestStepRegistry.getInstance();
115 WsdlTestStepFactory[] factories = (WsdlTestStepFactory[]) registry.getFactories();
116
117 for (int c = 0; c < factories.length; c++)
118 {
119 if (factories[c].canCreate())
120 appendStepMenu.add(new InsertTestStepAction(factories[c]));
121 }
122
123 testListPopup.add(appendStepMenu);
124
125 testListPopup.addSeparator();
126 moveStepDownAction = new MoveStepDownAction();
127 testListPopup.add(moveStepDownAction);
128 moveStepUpAction = new MoveStepUpAction();
129 testListPopup.add(moveStepUpAction);
130
131 testListPopup.addPopupMenuListener(new StepListPopupMenuListener(testCase));
132 testStepList.setComponentPopupMenu(testListPopup);
133
134 add( testStepList, BorderLayout.CENTER );
135 }
136
137 public void setEnabled(boolean enabled)
138 {
139 testStepList.setEnabled( enabled );
140
141 super.setEnabled(enabled);
142 }
143
144 private final class TestStepListKeyHandler extends KeyAdapter
145 {
146 public void keyPressed(KeyEvent e)
147 {
148 int ctrlKey = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
149 if ((e.getModifiers() & ctrlKey) == ctrlKey && (e.getKeyCode() == KeyEvent.VK_DOWN))
150 {
151 moveStepDownAction.actionPerformed(new ActionEvent(testStepList, 0, "DOWN"));
152 e.consume();
153 }
154 else if ((e.getModifiers() & ctrlKey) == ctrlKey && (e.getKeyCode() == KeyEvent.VK_UP))
155 {
156 moveStepUpAction.actionPerformed(new ActionEvent(testStepList, 0, "UP"));
157 e.consume();
158 }
159 else if (e.getKeyChar() == KeyEvent.VK_ENTER)
160 {
161 int ix = testStepList.getSelectedIndex();
162 if (ix != -1)
163 {
164 TestStep testStep = (TestStep) testCase.getTestStepAt(ix);
165 UISupport.selectAndShow(testStep);
166 e.consume();
167 }
168 }
169 else
170 {
171 int ix = testStepList.getSelectedIndex();
172 if (ix != -1)
173 {
174 TestStep testStep = (TestStep) testCase.getTestStepAt(ix);
175 ActionList actions = testStep.getActions();
176 if( actions != null )
177 actions.dispatchKeyEvent( e );
178 }
179 }
180 }
181 }
182
183 private final class StepListPopupMenuListener implements PopupMenuListener
184 {
185 private StepListPopupMenuListener(WsdlTestCase case1)
186 {
187 super();
188 }
189
190 public void popupMenuWillBecomeVisible(PopupMenuEvent e)
191 {
192 while (testListPopup.getComponentCount() > 4)
193 testListPopup.remove(0);
194
195 int ix = testStepList.getSelectedIndex();
196
197 if( SoapUI.getTestMonitor().hasRunningLoadTest( testCase ))
198 {
199 appendStepMenu.setEnabled( false );
200 moveStepDownAction.setEnabled(false);
201 moveStepUpAction.setEnabled(false);
202
203 return;
204 }
205
206 appendStepMenu.setEnabled(true);
207 moveStepUpAction.setEnabled(ix > 0);
208 moveStepDownAction.setEnabled(ix > -1 && ix < testCase.getTestStepCount() - 1);
209
210 if( ix >= 0 )
211 {
212 TestStep testStep = (TestStep) testCase.getTestStepAt(ix);
213 testListPopup.add( new JPopupMenu.Separator(), 0 );
214 ActionSupport.insertActions(testStep.getActions(), testListPopup, 0);
215 }
216 }
217
218 public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
219 {
220 }
221
222 public void popupMenuCanceled(PopupMenuEvent e)
223 {
224 }
225 }
226
227 private final class StepListMouseListener extends MouseAdapter
228 {
229 public void mouseClicked(MouseEvent e)
230 {
231 if (e.getClickCount() < 2)
232 {
233 return;
234 }
235
236 ModelItem modelItem = (ModelItem) testStepList.getSelectedValue();
237 if (modelItem == null)
238 return;
239
240 Action defaultAction = modelItem.getActions().getDefaultAction();
241 if( defaultAction != null )
242 defaultAction.actionPerformed( new ActionEvent( TestStepList.this, 0, null ));
243 }
244 }
245
246 /***
247 * Renderer which sets icon and wider border for teststeps
248 *
249 * @author Ole.Matzura
250 */
251
252 private final static class TestStepCellRenderer extends JLabel implements ListCellRenderer
253 {
254 public TestStepCellRenderer()
255 {
256 setOpaque(true);
257 setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
258 }
259
260 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
261 boolean cellHasFocus)
262 {
263 WsdlTestStep testStep = (WsdlTestStep) value;
264 setText(testStep.getName());
265 setIcon(testStep.getIcon());
266
267 if (isSelected)
268 {
269 setBackground(list.getSelectionBackground());
270 setForeground(list.getSelectionForeground());
271 }
272 else
273 {
274 setBackground(list.getBackground());
275 setForeground(list.getForeground());
276 }
277
278 setEnabled(list.isEnabled());
279
280 return this;
281 }
282 }
283
284 private class TestStepListModel extends AbstractListModel implements PropertyChangeListener
285 {
286 private TestStepListTestSuiteListener testStepListTestSuiteListener = new TestStepListTestSuiteListener();
287
288 public TestStepListModel()
289 {
290 for (int c = 0; c < getSize(); c++)
291 testCase.getTestStepAt(c).addPropertyChangeListener(this);
292
293 testCase.getTestSuite().addTestSuiteListener(testStepListTestSuiteListener);
294 }
295
296 public int getSize()
297 {
298 return testCase.getTestStepCount();
299 }
300
301 public Object getElementAt(int index)
302 {
303 return testCase.getTestStepAt(index);
304 }
305
306 public void propertyChange(PropertyChangeEvent arg0)
307 {
308 int ix = testCase.getIndexOfTestStep((TestStep) arg0.getSource());
309 if (ix == -1)
310 return;
311
312 fireContentsChanged(this, ix, ix);
313 }
314
315 public void release()
316 {
317 testCase.getTestSuite().removeTestSuiteListener(testStepListTestSuiteListener);
318
319 for (int c = 0; c < getSize(); c++)
320 testCase.getTestStepAt(c).removePropertyChangeListener(this);
321 }
322
323 private class TestStepListTestSuiteListener extends TestSuiteListenerAdapter
324 {
325 public void testStepAdded(TestStep testStep, int ix)
326 {
327 if( testStep.getTestCase() == testCase )
328 {
329 testStep.addPropertyChangeListener(TestStepListModel.this);
330 fireIntervalAdded(TestStepListModel.this, ix, ix);
331 }
332 }
333
334 public void testStepRemoved(TestStep testStep, int ix)
335 {
336 if( testStep.getTestCase() == testCase )
337 {
338 testStep.removePropertyChangeListener(TestStepListModel.this);
339 fireIntervalRemoved(TestStepListModel.this, ix, ix);
340 }
341 }
342 }
343 }
344
345 private class MoveStepDownAction extends AbstractAction
346 {
347 public MoveStepDownAction()
348 {
349 super("Move down");
350 putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu DOWN" ));
351 }
352
353 public void actionPerformed(ActionEvent e)
354 {
355 int ix = testStepList.getSelectedIndex();
356 if (ix == -1 || ix >= testCase.getTestStepCount() - 1)
357 return;
358
359 testCase.moveTestStep(ix, 1);
360 testStepList.setSelectedIndex(ix+1);
361 }
362 }
363
364 private class MoveStepUpAction extends AbstractAction
365 {
366 public MoveStepUpAction()
367 {
368 super("Move up");
369 putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu UP" ));
370 }
371
372 public void actionPerformed(ActionEvent e)
373 {
374 int ix = testStepList.getSelectedIndex();
375 if (ix < 1)
376 return;
377
378 testCase.moveTestStep(ix, -1);
379 testStepList.setSelectedIndex(ix-1);
380 }
381 }
382
383 public class InsertTestStepAction extends AbstractAction
384 {
385 private final WsdlTestStepFactory factory;
386
387 public InsertTestStepAction(WsdlTestStepFactory factory)
388 {
389 super(factory.getTestStepName());
390 putValue( Action.SHORT_DESCRIPTION, factory.getTestStepDescription());
391 putValue( Action.SMALL_ICON, factory.getTestStepIcon() );
392 this.factory = factory;
393 }
394
395 public void actionPerformed(ActionEvent e)
396 {
397 String name = UISupport.prompt( "Specify name for new step", "Insert Step", factory.getTestStepName());
398 if( name != null )
399 {
400 TestStepConfig newTestStepConfig = factory.createNewTestStep(testCase, name);
401 if( newTestStepConfig != null )
402 {
403 WsdlTestStep testStep = testCase.addTestStep(newTestStepConfig);
404 UISupport.selectAndShow( testStep );
405 }
406 }
407 }
408 }
409
410 public void setSelectedIndex(int i)
411 {
412 testStepList.setSelectedIndex(i);
413 }
414
415 public void setSelectedValue(TestStep testStep, boolean b)
416 {
417 testStepList.setSelectedValue(testStep, true);
418 }
419
420 public void release()
421 {
422 testStepListModel.release();
423 }
424 }