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