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.impl.rest.panels.request;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Dimension;
17  import java.awt.event.ItemEvent;
18  import java.awt.event.ItemListener;
19  import java.beans.PropertyChangeEvent;
20  import java.beans.PropertyChangeListener;
21  
22  import javax.swing.AbstractListModel;
23  import javax.swing.ComboBoxModel;
24  import javax.swing.DefaultComboBoxModel;
25  import javax.swing.JComboBox;
26  import javax.swing.JComponent;
27  import javax.swing.JLabel;
28  import javax.swing.JPanel;
29  import javax.swing.text.Document;
30  
31  import com.eviware.soapui.impl.rest.RestRequestInterface;
32  import com.eviware.soapui.impl.rest.RestResource;
33  import com.eviware.soapui.impl.rest.support.RestParamProperty;
34  import com.eviware.soapui.impl.rest.support.RestUtils;
35  import com.eviware.soapui.impl.support.panels.AbstractHttpXmlRequestDesktopPanel;
36  import com.eviware.soapui.impl.wsdl.WsdlSubmitContext;
37  import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestInterface;
38  import com.eviware.soapui.model.ModelItem;
39  import com.eviware.soapui.model.iface.Submit;
40  import com.eviware.soapui.model.iface.Request.SubmitException;
41  import com.eviware.soapui.model.support.TestPropertyListenerAdapter;
42  import com.eviware.soapui.model.testsuite.TestProperty;
43  import com.eviware.soapui.support.DocumentListenerAdapter;
44  import com.eviware.soapui.support.UISupport;
45  import com.eviware.soapui.support.components.JUndoableTextField;
46  import com.eviware.soapui.support.components.JXToolBar;
47  import com.eviware.soapui.support.propertyexpansion.PropertyExpansionPopupListener;
48  import com.eviware.soapui.support.swing.ModelItemListCellRenderer;
49  
50  public abstract class AbstractRestRequestDesktopPanel<T extends ModelItem, T2 extends RestRequestInterface> extends
51  		AbstractHttpXmlRequestDesktopPanel<T, T2>
52  {
53  	private boolean updatingRequest;
54  	private JUndoableTextField pathTextField;
55  	private JComboBox acceptCombo;
56  	private JLabel pathLabel;
57  	private boolean updating;
58  	private InternalTestPropertyListener testPropertyListener = new AbstractRestRequestDesktopPanel.InternalTestPropertyListener();
59  	private RestParamPropertyChangeListener restParamPropertyChangeListener = new AbstractRestRequestDesktopPanel.RestParamPropertyChangeListener();
60  	private JComboBox pathCombo;
61  
62  	public AbstractRestRequestDesktopPanel( T modelItem, T2 requestItem )
63  	{
64  		super( modelItem, requestItem );
65  
66  		if( requestItem.getResource() != null )
67  		{
68  			requestItem.getResource().addPropertyChangeListener( this );
69  		}
70  
71  		requestItem.addTestPropertyListener( testPropertyListener );
72  
73  		for( TestProperty param : requestItem.getParams().getProperties().values() )
74  		{
75  			( ( RestParamProperty )param ).addPropertyChangeListener( restParamPropertyChangeListener );
76  		}
77  	}
78  
79  	public void propertyChange( PropertyChangeEvent evt )
80  	{
81  		updateFullPathLabel();
82  
83  		if( evt.getPropertyName().equals( "accept" ) && !updatingRequest )
84  		{
85  			acceptCombo.setSelectedItem( evt.getNewValue() );
86  		}
87  		else if( evt.getPropertyName().equals( "responseMediaTypes" ) && !updatingRequest )
88  		{
89  			Object item = acceptCombo.getSelectedItem();
90  			acceptCombo.setModel( new DefaultComboBoxModel( ( Object[] )evt.getNewValue() ) );
91  			acceptCombo.setSelectedItem( item );
92  		}
93  		else if( evt.getPropertyName().equals( "path" )
94  				&& ( getRequest().getResource() == null || getRequest().getResource() == evt.getSource() ) )
95  		{
96  			if( pathLabel != null )
97  			{
98  				updateFullPathLabel();
99  			}
100 
101 			if( !updating && pathTextField != null )
102 			{
103 				updating = true;
104 				pathTextField.setText( ( String )evt.getNewValue() );
105 				pathTextField.setToolTipText( pathTextField.getText() );
106 				updating = false;
107 			}
108 		}
109 
110 		super.propertyChange( evt );
111 	}
112 
113 	@Override
114 	protected Submit doSubmit() throws SubmitException
115 	{
116 		return getRequest().submit( new WsdlSubmitContext( getModelItem() ), true );
117 	}
118 
119 	@Override
120 	protected String getHelpUrl()
121 	{
122 		return null;
123 	}
124 
125 	@Override
126 	protected void insertButtons( JXToolBar toolbar )
127 	{
128 		if( getRequest().getResource() == null )
129 		{
130 			addToolbarComponents( toolbar );
131 		}
132 	}
133 
134 	@Override
135 	protected JComponent buildToolbar()
136 	{
137 		if( getRequest().getResource() != null )
138 		{
139 			JPanel panel = new JPanel( new BorderLayout() );
140 			panel.add( super.buildToolbar(), BorderLayout.NORTH );
141 
142 			JXToolBar toolbar = UISupport.createToolbar();
143 			addToolbarComponents( toolbar );
144 
145 			panel.add( toolbar, BorderLayout.SOUTH );
146 			return panel;
147 		}
148 		else
149 		{
150 			return super.buildToolbar();
151 		}
152 	}
153 
154 	protected void addToolbarComponents( JXToolBar toolbar )
155 	{
156 		toolbar.addSeparator();
157 
158 		if( getRequest().getResource() != null )
159 		{
160 			acceptCombo = new JComboBox( getRequest().getResponseMediaTypes() );
161 			acceptCombo.setEditable( true );
162 			acceptCombo.setToolTipText( "Sets accepted encoding(s) for response" );
163 			acceptCombo.setSelectedItem( getRequest().getAccept() );
164 			acceptCombo.addItemListener( new ItemListener()
165 			{
166 				public void itemStateChanged( ItemEvent e )
167 				{
168 					updatingRequest = true;
169 					getRequest().setAccept( String.valueOf( acceptCombo.getSelectedItem() ) );
170 					updatingRequest = false;
171 				}
172 			} );
173 
174 			toolbar.addLabeledFixed( "Accept", acceptCombo );
175 			toolbar.addSeparator();
176 
177 			if( getRequest() instanceof RestTestRequestInterface )
178 			{
179 				pathCombo = new JComboBox( new PathComboBoxModel() );
180 				pathCombo.setRenderer( new ModelItemListCellRenderer() );
181 				pathCombo.setPreferredSize( new Dimension( 200, 20 ) );
182 				pathCombo.setSelectedItem( getRequest().getResource().getPath() );
183 				pathCombo.addItemListener( new ItemListener()
184 				{
185 					public void itemStateChanged( ItemEvent e )
186 					{
187 						if( updating )
188 							return;
189 
190 						updating = true;
191 						getRequest().getResource().setPath( String.valueOf( pathCombo.getSelectedItem() ) );
192 						updating = false;
193 					}
194 				} );
195 
196 				toolbar.addLabeledFixed( "Resource:", pathCombo );
197 				toolbar.addSeparator();
198 			}
199 			else
200 			{
201 				toolbar.add( new JLabel( "Full Path: " ) );
202 			}
203 
204 			pathLabel = new JLabel();
205 			updateFullPathLabel();
206 
207 			toolbar.add( pathLabel );
208 		}
209 		else
210 		{
211 			pathTextField = new JUndoableTextField();
212 			pathTextField.setPreferredSize( new Dimension( 300, 20 ) );
213 			pathTextField.setText( getRequest().getPath() );
214 			pathTextField.setToolTipText( pathTextField.getText() );
215 			pathTextField.getDocument().addDocumentListener( new DocumentListenerAdapter()
216 			{
217 				@Override
218 				public void update( Document document )
219 				{
220 					if( updating )
221 						return;
222 
223 					updating = true;
224 					getRequest().setPath( pathTextField.getText() );
225 					updating = false;
226 				}
227 			} );
228 			PropertyExpansionPopupListener.enable( pathTextField, getModelItem() );
229 
230 			toolbar.addLabeledFixed( "Request URL:", pathTextField );
231 		}
232 
233 		toolbar.addSeparator();
234 	}
235 
236 	protected boolean release()
237 	{
238 		if( getRequest().getResource() != null )
239 		{
240 			getRequest().getResource().removePropertyChangeListener( this );
241 		}
242 
243 		getRequest().removeTestPropertyListener( testPropertyListener );
244 
245 		for( TestProperty param : getRequest().getParams().getProperties().values() )
246 		{
247 			( ( RestParamProperty )param ).removePropertyChangeListener( restParamPropertyChangeListener );
248 		}
249 
250 		return super.release();
251 	}
252 
253 	private class InternalTestPropertyListener extends TestPropertyListenerAdapter
254 	{
255 		@Override
256 		public void propertyValueChanged( String name, String oldValue, String newValue )
257 		{
258 			updateFullPathLabel();
259 		}
260 
261 		@Override
262 		public void propertyAdded( String name )
263 		{
264 			updateFullPathLabel();
265 
266 			getRequest().getParams().getProperty( name ).addPropertyChangeListener( restParamPropertyChangeListener );
267 		}
268 
269 		@Override
270 		public void propertyRemoved( String name )
271 		{
272 			updateFullPathLabel();
273 		}
274 
275 		@Override
276 		public void propertyRenamed( String oldName, String newName )
277 		{
278 			updateFullPathLabel();
279 		}
280 	}
281 
282 	private void updateFullPathLabel()
283 	{
284 		if( pathLabel != null && getRequest().getResource() != null )
285 		{
286 			String text = RestUtils.expandPath( getRequest().getResource().getFullPath(), getRequest().getParams(),
287 					getRequest() );
288 			pathLabel.setText( "[" + text + "]" );
289 			pathLabel.setToolTipText( text );
290 		}
291 	}
292 
293 	private class RestParamPropertyChangeListener implements PropertyChangeListener
294 	{
295 		public void propertyChange( PropertyChangeEvent evt )
296 		{
297 			updateFullPathLabel();
298 		}
299 	}
300 
301 	private class PathComboBoxModel extends AbstractListModel implements ComboBoxModel
302 	{
303 		public int getSize()
304 		{
305 			return getRequest().getResource().getService().getAllResources().size();
306 		}
307 
308 		public Object getElementAt( int index )
309 		{
310 			return getRequest().getResource().getService().getAllResources().get( index );
311 		}
312 
313 		public void setSelectedItem( Object anItem )
314 		{
315 			( ( RestTestRequestInterface )getRequest() ).setResource( ( RestResource )anItem );
316 		}
317 
318 		public Object getSelectedItem()
319 		{
320 			return getRequest().getResource();
321 		}
322 	}
323 
324 }