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.resource;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Component;
17  import java.awt.event.FocusEvent;
18  import java.awt.event.FocusListener;
19  import java.beans.PropertyChangeEvent;
20  
21  import javax.swing.JTabbedPane;
22  import javax.swing.text.Document;
23  
24  import com.eviware.soapui.impl.rest.RestResource;
25  import com.eviware.soapui.impl.rest.actions.resource.NewRestMethodAction;
26  import com.eviware.soapui.impl.rest.support.RestParamProperty;
27  import com.eviware.soapui.impl.rest.support.RestUtils;
28  import com.eviware.soapui.impl.rest.support.RestParamsPropertyHolder.ParameterStyle;
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.DocumentListenerAdapter;
33  import com.eviware.soapui.support.UISupport;
34  import com.eviware.soapui.support.action.swing.SwingActionDelegate;
35  import com.eviware.soapui.support.components.JUndoableTextField;
36  import com.eviware.soapui.support.components.JXToolBar;
37  import com.eviware.soapui.ui.support.ModelItemDesktopPanel;
38  
39  public class RestResourceDesktopPanel extends ModelItemDesktopPanel<RestResource>
40  {
41  	private JUndoableTextField pathTextField;
42  	private boolean updating;
43  	private RestParamsTable paramsTable;
44  
45  	public RestResourceDesktopPanel( RestResource modelItem )
46  	{
47  		super( modelItem );
48  
49  		add( buildToolbar(), BorderLayout.NORTH );
50  		add( buildContent(), BorderLayout.CENTER );
51  	}
52  
53  	private Component buildContent()
54  	{
55  		JTabbedPane tabs = new JTabbedPane();
56  		paramsTable = new RestParamsTable( getModelItem().getParams(), true );
57  		tabs.addTab( "Resource Parameters", paramsTable );
58  		return UISupport.createTabPanel( tabs, false );
59  	}
60  
61  	@Override
62  	public String getTitle()
63  	{
64  		return getName( getModelItem() );
65  	}
66  
67  	public RestParamsTable getParamsTable()
68  	{
69  		return paramsTable;
70  	}
71  
72  	@Override
73  	protected boolean release()
74  	{
75  		paramsTable.release();
76  		return super.release();
77  	}
78  
79  	private String getName( RestResource modelItem )
80  	{
81  		if( modelItem.getParentResource() != null )
82  			return getName( modelItem.getParentResource() ) + "/" + modelItem.getName();
83  		else
84  			return modelItem.getName();
85  	}
86  
87  	private Component buildToolbar()
88  	{
89  		JXToolBar toolbar = UISupport.createToolbar();
90  
91  		toolbar.addFixed( createActionButton( SwingActionDelegate.createDelegate( NewRestMethodAction.SOAPUI_ACTION_ID,
92  				getModelItem(), null, "/create_empty_method.gif" ), true ) );
93  
94  		toolbar.addSeparator();
95  
96  		pathTextField = new JUndoableTextField( getModelItem().getPath(), 20 );
97  		pathTextField.getDocument().addDocumentListener( new DocumentListenerAdapter()
98  		{
99  			public void update( Document document )
100 			{
101 				if( !updating )
102 				{
103 					updating = true;
104 					getModelItem().setPath( getText( document ) );
105 					updating = false;
106 				}
107 			}
108 		} );
109 		pathTextField.addFocusListener( new FocusListener()
110 		{
111 			public void focusLost( FocusEvent e )
112 			{
113 				for( String p : RestUtils.extractTemplateParams( getModelItem().getPath() ))
114 				{
115 					if( !getModelItem().hasProperty( p ))
116 					{
117 						if( UISupport.confirm( "Add template parameter [" + p + "] to resource?", "Add Parameter" ))
118 						{
119 							RestParamProperty property = getModelItem().addProperty( p );
120 							property.setStyle( ParameterStyle.TEMPLATE );
121 							String value = UISupport.prompt( "Specify default value for parameter [" + p + "]", "Add Parameter", "" );
122 							if( value != null )
123 								property.setDefaultValue( value );
124 						}
125 					}
126 				}
127 			}
128 			
129 			public void focusGained( FocusEvent e )
130 			{
131 			}
132 		});
133 		
134 		toolbar.addLabeledFixed( "Resource Path", pathTextField );
135 
136 		toolbar.addGlue();
137 		toolbar.add( UISupport.createToolbarButton( new ShowOnlineHelpAction( HelpUrls.RESTRESOURCEEDITOR_HELPURL ) ) );
138 
139 		return toolbar;
140 	}
141 
142 	@Override
143 	public boolean dependsOn( ModelItem modelItem )
144 	{
145 		return getModelItem().dependsOn( modelItem );
146 	}
147 
148 	public boolean onClose( boolean canCancel )
149 	{
150 		return true;
151 	}
152 
153 	@Override
154 	public void propertyChange( PropertyChangeEvent evt )
155 	{
156 		if( evt.getPropertyName().equals( "path" ) )
157 		{
158 			if( !updating )
159 			{
160 				updating = true;
161 				pathTextField.setText( String.valueOf( evt.getNewValue() ) );
162 				updating = false;
163 			}
164 		}
165 
166 		super.propertyChange( evt );
167 	}
168 }