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.support.http;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Component;
17  import java.awt.Dimension;
18  import java.awt.event.ItemEvent;
19  import java.awt.event.ItemListener;
20  import java.beans.PropertyChangeEvent;
21  import java.beans.PropertyChangeListener;
22  
23  import javax.swing.DefaultComboBoxModel;
24  import javax.swing.JCheckBox;
25  import javax.swing.JComboBox;
26  import javax.swing.JComponent;
27  import javax.swing.JPanel;
28  import javax.swing.JScrollPane;
29  import javax.swing.JSplitPane;
30  import javax.swing.SwingUtilities;
31  import javax.swing.text.Document;
32  
33  import com.eviware.soapui.impl.rest.panels.resource.RestParamsTable;
34  import com.eviware.soapui.impl.rest.panels.resource.RestParamsTableModel;
35  import com.eviware.soapui.impl.rest.support.RestParamProperty;
36  import com.eviware.soapui.impl.rest.support.RestParamsPropertyHolder;
37  import com.eviware.soapui.impl.support.AbstractHttpRequest;
38  import com.eviware.soapui.impl.support.panels.AbstractHttpXmlRequestDesktopPanel.HttpRequestDocument;
39  import com.eviware.soapui.impl.support.panels.AbstractHttpXmlRequestDesktopPanel.HttpRequestMessageEditor;
40  import com.eviware.soapui.support.DocumentListenerAdapter;
41  import com.eviware.soapui.support.UISupport;
42  import com.eviware.soapui.support.components.JXToolBar;
43  import com.eviware.soapui.support.editor.views.AbstractXmlEditorView;
44  import com.eviware.soapui.support.propertyexpansion.PropertyExpansionPopupListener;
45  import com.eviware.soapui.support.xml.JXEditTextArea;
46  import com.eviware.soapui.support.xml.XmlUtils;
47  
48  public class HttpRequestContentView extends AbstractXmlEditorView<HttpRequestDocument> implements
49  		PropertyChangeListener
50  {
51  	private final HttpRequestInterface<?> httpRequest;
52  	private JPanel contentPanel;
53  	private JXEditTextArea contentEditor;
54  	private boolean updatingRequest;
55  	private JComponent panel;
56  	private JComboBox mediaTypeCombo;
57  	private JSplitPane split;
58  	private RestParamsTable paramsTable;
59  	private JCheckBox postQueryCheckBox;
60  
61  	public HttpRequestContentView( HttpRequestMessageEditor httpRequestMessageEditor, HttpRequestInterface<?> httpRequest )
62  	{
63  		super( "Request", httpRequestMessageEditor, HttpRequestContentViewFactory.VIEW_ID );
64  		this.httpRequest = httpRequest;
65  
66  		httpRequest.addPropertyChangeListener( this );
67  	}
68  
69  	public JComponent getComponent()
70  	{
71  		if( panel == null )
72  		{
73  			buildComponent();
74  		}
75  
76  		return panel;
77  	}
78  
79  	protected void buildComponent()
80  	{
81  		JPanel p = new JPanel( new BorderLayout() );
82  
83  		p.add( buildToolbar(), BorderLayout.NORTH );
84  		p.add( buildContent(), BorderLayout.CENTER );
85  
86  		paramsTable = buildParamsTable();
87  
88  		split = UISupport.createVerticalSplit( paramsTable, p );
89  
90  		panel = new JPanel( new BorderLayout() );
91  		panel.add( split );
92  
93  		fixRequestPanel();
94  	}
95  
96  	protected RestParamsTable buildParamsTable()
97  	{
98  		paramsTable = new RestParamsTable( httpRequest.getParams(), false )
99  		{
100 			protected RestParamsTableModel createTableModel( RestParamsPropertyHolder params )
101 			{
102 				return new RestParamsTableModel( params )
103 				{
104 					@Override
105 					public String getColumnName( int column )
106 					{
107 						return column == 0 ? "Name" : "Value";
108 					}
109 
110 					public int getColumnCount()
111 					{
112 						return 2;
113 					}
114 
115 					public Object getValueAt( int rowIndex, int columnIndex )
116 					{
117 						RestParamProperty prop = params.getPropertyAt( rowIndex );
118 						return columnIndex == 0 ? prop.getName() : prop.getValue();
119 					}
120 
121 					@Override
122 					public void setValueAt( Object value, int rowIndex, int columnIndex )
123 					{
124 						RestParamProperty prop = params.getPropertyAt( rowIndex );
125 						if( columnIndex == 0 )
126 							prop.setName( value.toString() );
127 						else
128 							prop.setValue( value.toString() );
129 					}
130 				};
131 			}
132 		};
133 		return paramsTable;
134 	}
135 
136 	@Override
137 	public void release()
138 	{
139 		super.release();
140 		httpRequest.removePropertyChangeListener( this );
141 		paramsTable.release();
142 	}
143 
144 	public HttpRequestInterface<?> getRestRequest()
145 	{
146 		return httpRequest;
147 	}
148 
149 	protected Component buildContent()
150 	{
151 		contentPanel = new JPanel( new BorderLayout() );
152 
153 		contentEditor = JXEditTextArea.createXmlEditor( true );
154 		contentEditor.setText( XmlUtils.prettyPrintXml( httpRequest.getRequestContent() ) );
155 
156 		contentEditor.getDocument().addDocumentListener( new DocumentListenerAdapter()
157 		{
158 
159 			@Override
160 			public void update( Document document )
161 			{
162 				if( !updatingRequest )
163 				{
164 					updatingRequest = true;
165 					httpRequest.setRequestContent( getText( document ) );
166 					updatingRequest = false;
167 				}
168 			}
169 		} );
170 
171 		contentPanel.add( new JScrollPane( contentEditor ) );
172 
173 		PropertyExpansionPopupListener.enable( contentEditor, httpRequest );
174 
175 		return contentPanel;
176 	}
177 
178 	private void enableBodyComponents()
179 	{
180 		httpRequest.setPostQueryString( httpRequest.hasRequestBody() && httpRequest.isPostQueryString() );
181 		postQueryCheckBox.setSelected( httpRequest.isPostQueryString() );
182 		mediaTypeCombo.setEnabled( httpRequest.hasRequestBody() && !httpRequest.isPostQueryString() );
183 		contentEditor.setEnabledAndEditable( httpRequest.hasRequestBody() && !httpRequest.isPostQueryString() );
184 		postQueryCheckBox.setEnabled( httpRequest.hasRequestBody() );
185 	}
186 
187 	protected Component buildToolbar()
188 	{
189 		JXToolBar toolbar = UISupport.createToolbar();
190 
191 		addMediaTypeCombo( toolbar );
192 		toolbar.addSeparator();
193 
194 		addPostQueryCheckBox( toolbar );
195 
196 		toolbar.setMinimumSize( new Dimension( 50, 20 ) );
197 
198 		return toolbar;
199 	}
200 
201 	protected void addPostQueryCheckBox( JXToolBar toolbar )
202 	{
203 		postQueryCheckBox = new JCheckBox( "Post QueryString", httpRequest.isPostQueryString() );
204 		postQueryCheckBox.setToolTipText( "Controls if Query-parameters should be put in message body" );
205 		postQueryCheckBox.setOpaque( false );
206 		postQueryCheckBox.addItemListener( new ItemListener()
207 		{
208 			public void itemStateChanged( ItemEvent e )
209 			{
210 				httpRequest.setPostQueryString( postQueryCheckBox.isSelected() );
211 				enableBodyComponents();
212 			}
213 		} );
214 
215 		postQueryCheckBox.setPreferredSize( new Dimension( 130, 20 ) );
216 		toolbar.addFixed( postQueryCheckBox );
217 	}
218 
219 	protected void addMediaTypeCombo( JXToolBar toolbar )
220 	{
221 		mediaTypeCombo = new JComboBox( getRequestMediaTypes() );
222 		mediaTypeCombo.setPreferredSize( new Dimension( 120, 20 ) );
223 		mediaTypeCombo.setEnabled( httpRequest.hasRequestBody() );
224 		mediaTypeCombo.setEditable( true );
225 		if( httpRequest.getMediaType() != null )
226 			mediaTypeCombo.setSelectedItem( httpRequest.getMediaType() );
227 
228 		mediaTypeCombo.addItemListener( new ItemListener()
229 		{
230 			public void itemStateChanged( ItemEvent e )
231 			{
232 				httpRequest.setMediaType( String.valueOf( mediaTypeCombo.getSelectedItem() ) );
233 			}
234 		} );
235 
236 		toolbar.addLabeledFixed( "Media Type", mediaTypeCombo );
237 	}
238 
239 	protected Object[] getRequestMediaTypes()
240 	{
241 		return new String[] { "application/xml", "text/xml", "multipart/form-data" };
242 	}
243 
244 	public void propertyChange( PropertyChangeEvent evt )
245 	{
246 		if( evt.getPropertyName().equals( "request" ) && !updatingRequest )
247 		{
248 			updatingRequest = true;
249 			contentEditor.setText( ( String )evt.getNewValue() );
250 			updatingRequest = false;
251 		}
252 		else if( evt.getPropertyName().equals( "method" ) )
253 		{
254 			fixRequestPanel();
255 		}
256 		else if( evt.getPropertyName().equals( "mediaType" ) )
257 		{
258 			mediaTypeCombo.setSelectedItem( evt.getNewValue() );
259 		}
260 		else if( evt.getPropertyName().equals( AbstractHttpRequest.ATTACHMENTS_PROPERTY ) )
261 		{
262 			mediaTypeCombo.setModel( new DefaultComboBoxModel( getRequestMediaTypes() ) );
263 			mediaTypeCombo.setSelectedItem( httpRequest.getMediaType() );
264 		}
265 	}
266 
267 	private void fixRequestPanel()
268 	{
269 		if( httpRequest.hasRequestBody() )
270 		{
271 			panel.remove( paramsTable );
272 			split.setLeftComponent( paramsTable );
273 			panel.add( split );
274 			enableBodyComponents();
275 			SwingUtilities.invokeLater( new Runnable()
276 			{
277 				public void run()
278 				{
279 					// wait for panel to get shown..
280 					if( panel.getHeight() == 0 )
281 					{
282 						SwingUtilities.invokeLater( this );
283 					}
284 					else
285 					{
286 						split.setDividerLocation( 0.5F );
287 					}
288 				}
289 			} );
290 		}
291 		else
292 		{
293 			panel.remove( split );
294 			panel.add( paramsTable );
295 		}
296 	}
297 
298 	@Override
299 	public void setXml( String xml )
300 	{
301 	}
302 
303 	public boolean saveDocument( boolean validate )
304 	{
305 		return false;
306 	}
307 
308 	public void setEditable( boolean enabled )
309 	{
310 		contentEditor.setEnabledAndEditable( enabled ? httpRequest.hasRequestBody() : false );
311 		mediaTypeCombo.setEnabled( enabled && !httpRequest.isPostQueryString() );
312 		postQueryCheckBox.setEnabled( enabled );
313 	}
314 
315 	public RestParamsTable getParamsTable()
316 	{
317 		return paramsTable;
318 	}
319 
320 }