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