1 package com.eviware.soapui.support.resolver;
2
3 import java.awt.Component;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.util.List;
7
8 import javax.swing.DefaultComboBoxModel;
9 import javax.swing.DefaultListCellRenderer;
10 import javax.swing.JButton;
11 import javax.swing.JComboBox;
12 import javax.swing.JDialog;
13 import javax.swing.JList;
14
15 import com.eviware.soapui.impl.wsdl.WsdlProject;
16 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
17 import com.eviware.soapui.impl.wsdl.teststeps.WsdlRunTestCaseTestStep;
18 import com.eviware.soapui.model.iface.Interface;
19 import com.eviware.soapui.model.iface.Operation;
20 import com.eviware.soapui.model.testsuite.TestCase;
21 import com.eviware.soapui.model.testsuite.TestSuite;
22 import com.eviware.soapui.support.UISupport;
23 import com.eviware.soapui.support.resolver.ResolveContext.Resolver;
24 import com.jgoodies.forms.builder.PanelBuilder;
25 import com.jgoodies.forms.layout.CellConstraints;
26 import com.jgoodies.forms.layout.FormLayout;
27
28 public class ChooseAnotherTestCase implements Resolver
29 {
30
31 private boolean resolved;
32 private WsdlRunTestCaseTestStep runTestStep;
33 private WsdlProject project;
34 private WsdlTestCase pickedTestCase;
35
36 public ChooseAnotherTestCase(WsdlRunTestCaseTestStep wsdlRunTestCaseTestStep)
37 {
38 runTestStep = wsdlRunTestCaseTestStep;
39 project = runTestStep.getTestCase().getTestSuite().getProject();
40 }
41
42 public String getDescription()
43 {
44 return "Choose another test step";
45 }
46
47 @Override
48 public String toString()
49 {
50 return getDescription();
51 }
52
53 public String getResolvedPath()
54 {
55
56 return null;
57 }
58
59 public boolean isResolved()
60 {
61 return resolved;
62 }
63
64 public boolean resolve()
65 {
66 TestCaseChangeDialog dialog = new TestCaseChangeDialog("Choose another test case");
67 dialog.showAndChoose();
68
69 return resolved;
70 }
71
72 @SuppressWarnings("serial")
73 private class TestCaseChangeDialog extends JDialog
74 {
75
76 private JComboBox tSuiteStepCombo;
77 private JComboBox tCaseCombo;
78 private JButton okBtn = new JButton(" Ok ");
79 private JButton cancelBtn = new JButton(" Cancel ");
80
81 public TestCaseChangeDialog(String title)
82 {
83 super(UISupport.getMainFrame(), title, true);
84 init();
85 }
86
87 private void init()
88 {
89 FormLayout layout = new FormLayout("right:pref, 4dlu, 30dlu, 5dlu, 30dlu, min ",
90 "min, pref, 4dlu, pref, 4dlu, pref, min");
91 CellConstraints cc = new CellConstraints();
92 PanelBuilder panel = new PanelBuilder(layout);
93 panel.addLabel("Interface:", cc.xy(1, 2));
94
95 List<TestSuite> tSuites = project.getTestSuiteList();
96 DefaultComboBoxModel sourceStepComboModel = new DefaultComboBoxModel();
97 tSuiteStepCombo = new JComboBox(sourceStepComboModel);
98 tSuiteStepCombo.setRenderer(new TestSuiteComboRenderer());
99 for (TestSuite element : tSuites)
100 sourceStepComboModel.addElement(element);
101
102 tSuiteStepCombo.setSelectedIndex(0);
103 panel.add(tSuiteStepCombo, cc.xyw(3, 2, 3));
104
105 tCaseCombo = new JComboBox(((TestSuite) tSuiteStepCombo.getSelectedItem()).getTestCaseList().toArray());
106 tCaseCombo.setRenderer(new TestCaseComboRender());
107
108 panel.addLabel("Operation:", cc.xy(1, 4));
109 panel.add(tCaseCombo, cc.xyw(3, 4, 3));
110
111 panel.add(okBtn, cc.xy(3, 6));
112 panel.add(cancelBtn, cc.xy(5, 6));
113
114 tSuiteStepCombo.addActionListener(new ActionListener()
115 {
116
117 public void actionPerformed(ActionEvent e)
118 {
119 Interface iface = project.getInterfaceByName(((TestSuite) tSuiteStepCombo.getSelectedItem()).getName());
120 tCaseCombo.removeAllItems();
121 if (iface != null)
122 {
123 tCaseCombo.setEnabled(true);
124 for (Operation op : iface.getOperationList())
125 tCaseCombo.addItem(op);
126 }
127 else
128 {
129 tCaseCombo.setEnabled(false);
130 }
131
132 }
133
134 });
135
136 okBtn.addActionListener(new ActionListener()
137 {
138
139 public void actionPerformed(ActionEvent e)
140 {
141
142 pickedTestCase = (WsdlTestCase) tCaseCombo.getSelectedItem();
143 runTestStep.setTargetTestCase(pickedTestCase);
144
145 setVisible(false);
146 }
147
148 });
149
150 cancelBtn.addActionListener(new ActionListener()
151 {
152
153 public void actionPerformed(ActionEvent e)
154 {
155 setVisible(false);
156 }
157
158 });
159
160 setLocationRelativeTo(UISupport.getParentFrame(this));
161 this.add(panel.getPanel());
162 }
163
164 public void showAndChoose()
165 {
166 this.pack();
167 this.setVisible(true);
168 }
169 }
170
171 @SuppressWarnings("serial")
172 private class TestSuiteComboRenderer extends DefaultListCellRenderer
173 {
174 @Override
175 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
176 boolean cellHasFocus)
177 {
178 Component result = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
179
180 if (value instanceof TestSuite)
181 {
182 TestSuite item = (TestSuite) value;
183 setIcon(item.getIcon());
184 setText(item.getName());
185 }
186
187 return result;
188 }
189 }
190
191 @SuppressWarnings("serial")
192 private class TestCaseComboRender extends DefaultListCellRenderer
193 {
194
195 @Override
196 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
197 boolean cellHasFocus)
198 {
199 Component result = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
200
201 if (value instanceof TestCase)
202 {
203 TestCase item = (TestCase) value;
204 setIcon(item.getIcon());
205 setText(item.getName());
206 }
207
208 return result;
209 }
210
211 }
212
213 public WsdlTestCase getPickedTestCase()
214 {
215 return pickedTestCase;
216 }
217
218 }