View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.method;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Component;
17  import java.awt.event.ItemEvent;
18  import java.awt.event.ItemListener;
19  import java.beans.PropertyChangeEvent;
20  
21  import javax.swing.JComboBox;
22  import javax.swing.JTabbedPane;
23  
24  import com.eviware.soapui.impl.rest.RestMethod;
25  import com.eviware.soapui.impl.rest.RestRepresentation;
26  import com.eviware.soapui.impl.rest.RestRequestInterface;
27  import com.eviware.soapui.impl.rest.actions.method.NewRestRequestAction;
28  import com.eviware.soapui.impl.rest.panels.resource.RestParamsTable;
29  import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
30  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
31  import com.eviware.soapui.model.ModelItem;
32  import com.eviware.soapui.support.UISupport;
33  import com.eviware.soapui.support.action.swing.SwingActionDelegate;
34  import com.eviware.soapui.support.components.JXToolBar;
35  import com.eviware.soapui.ui.support.ModelItemDesktopPanel;
36  
37  public class RestMethodDesktopPanel extends ModelItemDesktopPanel<RestMethod>
38  {
39  	private RestParamsTable paramsTable;
40  	private boolean updatingRequest;
41  	private JComboBox methodCombo;
42  
43  	public RestMethodDesktopPanel( RestMethod modelItem )
44  	{
45  		super( modelItem );
46  
47  		add( buildToolbar(), BorderLayout.NORTH );
48  		add( buildContent(), BorderLayout.CENTER );
49  	}
50  
51  	private Component buildContent()
52  	{
53  		JTabbedPane tabs = new JTabbedPane();
54  
55  		paramsTable = new RestParamsTable( getModelItem().getParams(), true );
56  		tabs.addTab( "Method Parameters", paramsTable );
57  
58  		tabs
59  				.addTab( "Representations", new RestRepresentationsTable( getModelItem(), new RestRepresentation.Type[] {
60  						RestRepresentation.Type.REQUEST, RestRepresentation.Type.RESPONSE, RestRepresentation.Type.FAULT },
61  						false ) );
62  
63  		/*
64  		 * tabs.addTab("Response Representations", new RestRepresentationsTable(
65  		 * getModelItem(), new RestRepresentation.Type[] {
66  		 * RestRepresentation.Type.RESPONSE, RestRepresentation.Type.FAULT },
67  		 * false));
68  		 */
69  
70  		return UISupport.createTabPanel( tabs, false );
71  	}
72  
73  	@Override
74  	public String getTitle()
75  	{
76  		return getName( getModelItem() );
77  	}
78  
79  	public RestParamsTable getParamsTable()
80  	{
81  		return paramsTable;
82  	}
83  
84  	@Override
85  	protected boolean release()
86  	{
87  		paramsTable.release();
88  		return super.release();
89  	}
90  
91  	private String getName( RestMethod modelItem )
92  	{
93  		return modelItem.getName();
94  	}
95  
96  	private Component buildToolbar()
97  	{
98  		JXToolBar toolbar = UISupport.createToolbar();
99  
100 		methodCombo = new JComboBox( RestRequestInterface.RequestMethod.getMethods() );
101 
102 		methodCombo.setSelectedItem( getModelItem().getMethod() );
103 		methodCombo.setToolTipText( "Set desired HTTP method" );
104 		methodCombo.addItemListener( new ItemListener()
105 		{
106 			public void itemStateChanged( ItemEvent e )
107 			{
108 				updatingRequest = true;
109 				getModelItem().setMethod( ( RestRequestInterface.RequestMethod )methodCombo.getSelectedItem() );
110 				updatingRequest = false;
111 			}
112 		} );
113 
114 		toolbar.addLabeledFixed( "HTTP method", methodCombo );
115 		toolbar.addSeparator();
116 
117 		toolbar.addFixed( createActionButton( SwingActionDelegate.createDelegate( NewRestRequestAction.SOAPUI_ACTION_ID,
118 				getModelItem(), null, "/create_empty_request.gif" ), true ) );
119 
120 		toolbar.addSeparator();
121 
122 		toolbar.addGlue();
123 		toolbar.add( UISupport.createToolbarButton( new ShowOnlineHelpAction( HelpUrls.RESTMETHODEDITOR_HELP_URL ) ) );
124 
125 		return toolbar;
126 	}
127 
128 	@Override
129 	public boolean dependsOn( ModelItem modelItem )
130 	{
131 		return getModelItem().dependsOn( modelItem );
132 	}
133 
134 	public boolean onClose( boolean canCancel )
135 	{
136 		return true;
137 	}
138 
139 	@Override
140 	public void propertyChange( PropertyChangeEvent evt )
141 	{
142 		super.propertyChange( evt );
143 
144 		if( evt.getPropertyName().equals( "method" ) && !updatingRequest )
145 		{
146 			methodCombo.setSelectedItem( evt.getNewValue() );
147 		}
148 	}
149 
150 }