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