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.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.WsdlProject;
29  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
30  import com.eviware.soapui.model.iface.Interface;
31  import com.eviware.soapui.model.iface.Operation;
32  import com.eviware.soapui.support.UISupport;
33  import com.eviware.soapui.support.resolver.ResolveContext.Resolver;
34  import com.jgoodies.forms.builder.PanelBuilder;
35  import com.jgoodies.forms.layout.CellConstraints;
36  import com.jgoodies.forms.layout.FormLayout;
37  
38  public abstract class ChangeOperationResolver implements Resolver
39  {
40  
41  	private boolean resolved = false;
42  	private WsdlProject project;
43  	private Operation pickedOperation;
44  
45  	public ChangeOperationResolver(WsdlTestStep testStep)
46  	{
47  		this.project = testStep.getTestCase().getTestSuite().getProject();
48  
49  	}
50  
51  	public String getResolvedPath()
52  	{
53  		return "";
54  	}
55  
56  	public boolean isResolved()
57  	{
58  		return resolved;
59  	}
60  
61  	public boolean resolve()
62  	{
63  
64  		PropertyChangeDialog pDialog = new PropertyChangeDialog("Choose operation");
65  		pDialog.showAndChoose();
66  		resolved = update();
67  		return resolved;
68  	}
69  
70  	public abstract boolean update();
71  
72  	public String getDescription()
73  	{
74  		return "Resolve: Choose another operation";
75  	}
76  
77  	@Override
78  	public String toString()
79  	{
80  		return getDescription();
81  	}
82  
83  	@SuppressWarnings("serial")
84  	private class PropertyChangeDialog extends JDialog
85  	{
86  
87  		private JComboBox sourceStepCombo;
88  		private JComboBox propertiesCombo;
89  		private JButton okBtn = new JButton(" Ok ");
90  		private JButton cancelBtn = new JButton(" Cancel ");
91  
92  		public PropertyChangeDialog(String title)
93  		{
94  			super(UISupport.getMainFrame(), title, true);
95  			init();
96  		}
97  
98  		private void init()
99  		{
100 			FormLayout layout = new FormLayout("min,right:pref, 4dlu, 40dlu, 5dlu, 40dlu, min ",
101 			"min, pref, 4dlu, pref, 4dlu, pref, min");
102 			CellConstraints cc = new CellConstraints();
103 			PanelBuilder panel = new PanelBuilder(layout);
104 			panel.addLabel("Interface:", cc.xy(2, 2));
105 
106 			List<Interface> ifaces = project.getInterfaceList();
107 			DefaultComboBoxModel sourceStepComboModel = new DefaultComboBoxModel();
108 			sourceStepCombo = new JComboBox(sourceStepComboModel);
109 			sourceStepCombo.setRenderer(new InterfaceComboRenderer());
110 			for (Interface element : ifaces)
111 				sourceStepComboModel.addElement(element);
112 
113 			sourceStepCombo.setSelectedIndex(0);
114 			panel.add(sourceStepCombo, cc.xyw(4, 2, 3));
115 
116 
117 			propertiesCombo = new JComboBox(((Interface) sourceStepCombo.getSelectedItem()).getOperationList().toArray());
118 			propertiesCombo.setRenderer(new OperationComboRender());
119 
120 			panel.addLabel("Operation:", cc.xy(2, 4));
121 			panel.add(propertiesCombo, cc.xyw(4, 4, 3));
122 
123 			panel.add(okBtn, cc.xy(4, 6));
124 			panel.add(cancelBtn, cc.xy(6, 6));
125 
126 			sourceStepCombo.addActionListener(new ActionListener()
127 			{
128 
129 				public void actionPerformed(ActionEvent e)
130 				{
131 					Interface iface = project.getInterfaceByName(((Interface) sourceStepCombo.getSelectedItem()).getName());
132 					propertiesCombo.removeAllItems();
133 					if (iface != null)
134 					{
135 						propertiesCombo.setEnabled(true);
136 						for (Operation op : iface.getOperationList())
137 							propertiesCombo.addItem(op);
138 					}
139 					else
140 					{
141 						propertiesCombo.setEnabled(false);
142 					}
143 
144 				}
145 
146 			});
147 
148 			okBtn.addActionListener(new ActionListener()
149 			{
150 
151 				public void actionPerformed(ActionEvent e)
152 				{
153 
154 					pickedOperation = (Operation) propertiesCombo.getSelectedItem();
155 
156 					setVisible(false);
157 				}
158 
159 			});
160 
161 			cancelBtn.addActionListener(new ActionListener()
162 			{
163 
164 				public void actionPerformed(ActionEvent e)
165 				{
166 					setVisible(false);
167 				}
168 
169 			});
170 
171 			setLocationRelativeTo(UISupport.getParentFrame(this));
172 			panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
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 }