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