View Javadoc

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