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.ArrayList;
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.teststeps.PropertyTransfer;
29  import com.eviware.soapui.impl.wsdl.teststeps.PropertyTransfersTestStep;
30  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
31  import com.eviware.soapui.model.TestModelItem;
32  import com.eviware.soapui.model.TestPropertyHolder;
33  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
34  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
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 ChooseAnotherPropertyTargetResolver implements Resolver
42  {
43  
44  	private boolean resolved;
45  	private PropertyTransfer badTransfer = null;
46  	private PropertyTransfersTestStep parent = null;
47  	private ArrayList<Object> sources = new ArrayList<Object>();
48  	private ArrayList<String[]> properties = new ArrayList<String[]>();
49  
50  	public ChooseAnotherPropertyTargetResolver( PropertyTransfer propertyTransfer, PropertyTransfersTestStep parent )
51  	{
52  		this.badTransfer = propertyTransfer;
53  		this.parent = parent;
54  
55  		sources.add( PropertyExpansionUtils.getGlobalProperties() );
56  		properties.add( PropertyExpansionUtils.getGlobalProperties().getPropertyNames() );
57  		sources.add( parent.getTestCase().getTestSuite().getProject() );
58  		properties.add( parent.getTestCase().getTestSuite().getProject().getPropertyNames() );
59  		sources.add( parent.getTestCase().getTestSuite() );
60  		properties.add( parent.getTestCase().getTestSuite().getPropertyNames() );
61  
62  		sources.add( parent.getTestCase() );
63  		properties.add( parent.getTestCase().getPropertyNames() );
64  
65  		for( int c = 0; c < parent.getTestCase().getTestStepCount(); c++ )
66  		{
67  			WsdlTestStep testStep = parent.getTestCase().getTestStepAt( c );
68  			if( testStep == parent )
69  				continue;
70  
71  			sources.add( testStep );
72  			properties.add( testStep.getPropertyNames() );
73  		}
74  
75  	}
76  
77  	public String getDescription()
78  	{
79  		return "Choose new target property";
80  	}
81  
82  	@Override
83  	public String toString()
84  	{
85  		return getDescription();
86  	}
87  
88  	public String getResolvedPath()
89  	{
90  		return null;
91  	}
92  
93  	public boolean isResolved()
94  	{
95  		return resolved;
96  	}
97  
98  	public boolean resolve()
99  	{
100 		PropertyChangeDialog propertyChangeDialog = new PropertyChangeDialog( "Choose another property" );
101 		propertyChangeDialog.showAndChoose();
102 
103 		return resolved;
104 	}
105 
106 	@SuppressWarnings( "serial" )
107 	private class PropertyChangeDialog extends JDialog
108 	{
109 
110 		private JComboBox sourceStepCombo;
111 		private JComboBox propertiesCombo;
112 		private JButton okBtn = new JButton( " Ok " );
113 		private JButton cancelBtn = new JButton( " Cancel " );
114 
115 		public PropertyChangeDialog( String title )
116 		{
117 			super( UISupport.getMainFrame(), title, true );
118 			init();
119 		}
120 
121 		private void init()
122 		{
123 			FormLayout layout = new FormLayout( "min,right:pref, 4dlu, 40dlu, 5dlu, 40dlu, min ",
124 					"min, pref, 4dlu, pref, 4dlu, pref, min" );
125 			CellConstraints cc = new CellConstraints();
126 			PanelBuilder panel = new PanelBuilder( layout );
127 			panel.addLabel( "Source:", cc.xy( 2, 2 ) );
128 			DefaultComboBoxModel sourceStepComboModel = new DefaultComboBoxModel();
129 			sourceStepCombo = new JComboBox( sourceStepComboModel );
130 			sourceStepCombo.setRenderer( new StepComboRenderer() );
131 			for( Object element : sources )
132 				sourceStepComboModel.addElement( element );
133 
134 			sourceStepCombo.setSelectedIndex( 0 );
135 			panel.add( sourceStepCombo, cc.xyw( 4, 2, 3 ) );
136 
137 			int index = sourceStepCombo.getSelectedIndex();
138 
139 			propertiesCombo = new JComboBox( properties.get( index ) );
140 			panel.addLabel( "Property:", cc.xy( 2, 4 ) );
141 			panel.add( propertiesCombo, cc.xyw( 4, 4, 3 ) );
142 
143 			panel.add( okBtn, cc.xy( 4, 6 ) );
144 			panel.add( cancelBtn, cc.xy( 6, 6 ) );
145 
146 			sourceStepCombo.addActionListener( new ActionListener()
147 			{
148 
149 				public void actionPerformed( ActionEvent e )
150 				{
151 					int index = sourceStepCombo.getSelectedIndex();
152 					propertiesCombo.removeAllItems();
153 					if( properties.get( index ).length > 0 )
154 					{
155 						propertiesCombo.setEnabled( true );
156 						for( String str : properties.get( index ) )
157 							propertiesCombo.addItem( str );
158 					}
159 					else
160 					{
161 						propertiesCombo.setEnabled( false );
162 					}
163 
164 				}
165 
166 			} );
167 
168 			okBtn.addActionListener( new ActionListener()
169 			{
170 
171 				public void actionPerformed( ActionEvent e )
172 				{
173 
174 					String name;
175 					TestPropertyHolder sourceStep = ( TestPropertyHolder )sourceStepCombo.getSelectedItem();
176 					if( sourceStep == PropertyExpansionUtils.getGlobalProperties() )
177 						name = PropertyExpansion.GLOBAL_REFERENCE;
178 					else if( sourceStep == parent.getTestCase().getTestSuite().getProject() )
179 						name = PropertyExpansion.PROJECT_REFERENCE;
180 					else if( sourceStep == parent.getTestCase().getTestSuite() )
181 						name = PropertyExpansion.TESTSUITE_REFERENCE;
182 					else if( sourceStep == parent.getTestCase() )
183 						name = PropertyExpansion.TESTCASE_REFERENCE;
184 					else
185 						name = sourceStep.getModelItem().getName();
186 
187 					badTransfer.setTargetStepName( name );
188 
189 					badTransfer.setTargetPropertyName( ( String )propertiesCombo.getSelectedItem() );
190 
191 					resolved = true;
192 
193 					setVisible( false );
194 				}
195 
196 			} );
197 
198 			cancelBtn.addActionListener( new ActionListener()
199 			{
200 
201 				public void actionPerformed( ActionEvent e )
202 				{
203 					resolved = false;
204 
205 					setVisible( false );
206 				}
207 
208 			} );
209 
210 			setLocationRelativeTo( UISupport.getParentFrame( this ) );
211 			panel.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
212 			this.add( panel.getPanel() );
213 		}
214 
215 		public void showAndChoose()
216 		{
217 			this.pack();
218 			this.setVisible( true );
219 		}
220 	}
221 
222 	@SuppressWarnings( "serial" )
223 	private class StepComboRenderer extends DefaultListCellRenderer
224 	{
225 		@SuppressWarnings( "finally" )
226 		@Override
227 		public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected,
228 				boolean cellHasFocus )
229 		{
230 			Component result = super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
231 
232 			try
233 			{
234 				if( value instanceof TestModelItem )
235 				{
236 					TestModelItem item = ( TestModelItem )value;
237 					setIcon( item.getIcon() );
238 					setText( item.getName() );
239 				}
240 				else if( value == PropertyExpansionUtils.getGlobalProperties() )
241 				{
242 					setText( "Global" );
243 				}
244 
245 			}
246 			catch( Exception e )
247 			{
248 				setText( "Removed element" );
249 			}
250 			finally
251 			{
252 				return result;
253 			}
254 		}
255 	}
256 }