View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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  
19  import javax.swing.DefaultComboBoxModel;
20  import javax.swing.JComboBox;
21  
22  import com.eviware.soapui.impl.wsdl.WsdlProject;
23  import com.eviware.soapui.impl.wsdl.actions.project.SimpleDialog;
24  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
25  import com.eviware.soapui.model.iface.Interface;
26  import com.eviware.soapui.model.iface.Operation;
27  import com.eviware.soapui.support.components.SimpleForm;
28  import com.eviware.soapui.support.resolver.ResolveContext.Resolver;
29  import com.eviware.soapui.support.swing.ModelItemListCellRenderer;
30  
31  public abstract class ChangeOperationResolver implements Resolver
32  {
33  	private boolean resolved = false;
34  	private WsdlProject project;
35  	private Operation selectedOperation;
36  	private String operationType;
37  
38  	public ChangeOperationResolver( WsdlTestStep testStep, String operationType )
39  	{
40  		this.project = testStep.getTestCase().getTestSuite().getProject();
41  
42  		this.operationType = operationType;
43  	}
44  
45  	public String getResolvedPath()
46  	{
47  		return "";
48  	}
49  
50  	public boolean isResolved()
51  	{
52  		return resolved;
53  	}
54  
55  	public boolean resolve()
56  	{
57  		PropertyChangeDialog pDialog = new PropertyChangeDialog( "Resolve " + operationType );
58  		pDialog.setVisible( true );
59  		resolved = update();
60  		return resolved;
61  	}
62  
63  	public abstract boolean update();
64  
65  	protected abstract Interface[] getInterfaces( WsdlProject project );
66  
67  	public String getDescription()
68  	{
69  		return "Resolve: Select another " + operationType;
70  	}
71  
72  	@Override
73  	public String toString()
74  	{
75  		return getDescription();
76  	}
77  
78  	@SuppressWarnings( "serial" )
79  	private class PropertyChangeDialog extends SimpleDialog
80  	{
81  		private JComboBox sourceStepCombo;
82  		private JComboBox propertiesCombo;
83  
84  		public PropertyChangeDialog( String title )
85  		{
86  			super( title, getDescription(), null );
87  		}
88  
89  		protected Component buildContent()
90  		{
91  			SimpleForm form = new SimpleForm();
92  
93  			form.addSpace( 5 );
94  			Interface[] ifaces = getInterfaces( project );
95  			DefaultComboBoxModel sourceStepComboModel = new DefaultComboBoxModel();
96  			sourceStepCombo = form.appendComboBox( "Interfaces", sourceStepComboModel, "Target Interface" );
97  			sourceStepCombo.setRenderer( new ModelItemListCellRenderer() );
98  			for( Interface element : ifaces )
99  				sourceStepComboModel.addElement( element );
100 
101 			propertiesCombo = form.appendComboBox( operationType, ( ( Interface )sourceStepCombo.getSelectedItem() )
102 					.getOperationList().toArray(), "Target " + operationType );
103 			propertiesCombo.setRenderer( new ModelItemListCellRenderer() );
104 
105 			sourceStepCombo.addActionListener( new ActionListener()
106 			{
107 				public void actionPerformed( ActionEvent e )
108 				{
109 					Interface iface = project.getInterfaceByName( ( ( Interface )sourceStepCombo.getSelectedItem() )
110 							.getName() );
111 					propertiesCombo.removeAllItems();
112 					if( iface != null )
113 					{
114 						propertiesCombo.setEnabled( true );
115 						for( Operation op : iface.getOperationList() )
116 							propertiesCombo.addItem( op );
117 					}
118 					else
119 					{
120 						propertiesCombo.setEnabled( false );
121 					}
122 				}
123 			} );
124 
125 			form.addSpace( 5 );
126 			return form.getPanel();
127 		}
128 
129 		protected boolean handleOk()
130 		{
131 			selectedOperation = ( Operation )propertiesCombo.getSelectedItem();
132 			return true;
133 		}
134 	}
135 
136 	public Operation getSelectedOperation()
137 	{
138 		return selectedOperation;
139 	}
140 
141 }