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