View Javadoc

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