1
2
3
4
5
6
7
8
9
10
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.Dimension;
18 import java.awt.event.ActionEvent;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.swing.AbstractAction;
23 import javax.swing.Action;
24 import javax.swing.DefaultCellEditor;
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.JTable;
31 import javax.swing.ListSelectionModel;
32 import javax.swing.SwingUtilities;
33 import javax.swing.event.ListSelectionEvent;
34 import javax.swing.event.ListSelectionListener;
35 import javax.xml.namespace.QName;
36
37 import org.apache.xmlbeans.SchemaType;
38 import org.apache.xmlbeans.XmlBeans;
39
40 import com.eviware.soapui.impl.rest.support.RestParamProperty;
41 import com.eviware.soapui.impl.rest.support.RestParamsPropertyHolder;
42 import com.eviware.soapui.impl.rest.support.RestUtils;
43 import com.eviware.soapui.impl.rest.support.RestParamsPropertyHolder.ParameterStyle;
44 import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
45 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
46 import com.eviware.soapui.model.testsuite.TestProperty;
47 import com.eviware.soapui.support.StringUtils;
48 import com.eviware.soapui.support.UISupport;
49 import com.eviware.soapui.support.components.JXToolBar;
50 import com.eviware.soapui.support.components.SimpleBindingForm;
51 import com.eviware.soapui.support.components.StringListFormComponent;
52 import com.jgoodies.binding.PresentationModel;
53
54 public class RestParamsTable extends JPanel
55 {
56 protected RestParamsPropertyHolder params;
57 protected RestParamsTableModel paramsTableModel;
58 protected JTable paramsTable;
59 protected AddParamAction addParamAction = new AddParamAction();
60 protected RemoveParamAction removeParamAction = new RemoveParamAction();
61 protected ClearParamsAction clearParamsAction = new ClearParamsAction();
62 protected UseDefaultParamsAction defaultParamsAction = new UseDefaultParamsAction();
63 protected MovePropertyDownAction movePropertyDownAction = new MovePropertyDownAction();
64 protected MovePropertyUpAction movePropertyUpAction = new MovePropertyUpAction();
65 protected UpdateParamsAction updateParamsAction = new UpdateParamsAction();
66 private PresentationModel<RestParamProperty> paramDetailsModel;
67 private StringListFormComponent optionsFormComponent;
68 private SimpleBindingForm detailsForm;
69
70 public RestParamsTable( RestParamsPropertyHolder params, boolean showInspector )
71 {
72 super( new BorderLayout() );
73 this.params = params;
74 init( params, showInspector );
75 }
76
77 protected RestParamsTableModel createTableModel( RestParamsPropertyHolder params )
78 {
79 return new RestParamsTableModel( params );
80 }
81
82 protected void init( RestParamsPropertyHolder params, boolean showInspector )
83 {
84 paramsTableModel = createTableModel( params );
85 paramsTable = new JTable( paramsTableModel );
86 paramsTable.setRowHeight( 19 );
87 paramsTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
88 paramsTable.setDefaultEditor( ParameterStyle.class, new DefaultCellEditor( new JComboBox( new Object[] {
89 ParameterStyle.QUERY, ParameterStyle.TEMPLATE, ParameterStyle.HEADER, ParameterStyle.MATRIX,
90 ParameterStyle.PLAIN } ) ) );
91
92 paramsTable.getSelectionModel().addListSelectionListener( new ListSelectionListener()
93 {
94
95 public void valueChanged( ListSelectionEvent e )
96 {
97 int selectedRow = paramsTable.getSelectedRow();
98 removeParamAction.setEnabled( selectedRow != -1 );
99 movePropertyDownAction.setEnabled( selectedRow < paramsTable.getRowCount() - 1 );
100 movePropertyUpAction.setEnabled( selectedRow > 0 );
101
102 if( selectedRow != -1 )
103 {
104 RestParamProperty selectedParameter = getSelectedParameter();
105 if( paramDetailsModel != null )
106 {
107 paramDetailsModel.setBean( selectedParameter );
108 detailsForm.setEnabled( true );
109 }
110 }
111 else
112 {
113
114 if( paramDetailsModel != null )
115 {
116 detailsForm.setEnabled( false );
117 paramDetailsModel.setBean( null );
118 }
119 }
120
121
122
123
124
125
126
127
128
129
130 }
131 } );
132
133 add( buildToolbar(), BorderLayout.NORTH );
134
135 if( showInspector )
136 {
137 final JSplitPane splitPane = UISupport.createVerticalSplit( new JScrollPane( paramsTable ), buildDetails() );
138 add( splitPane, BorderLayout.CENTER );
139
140 SwingUtilities.invokeLater( new Runnable()
141 {
142 public void run()
143 {
144 splitPane.setDividerLocation( 0.5 );
145 }
146 } );
147 }
148 else
149 {
150 add( new JScrollPane( paramsTable ), BorderLayout.CENTER );
151 }
152 }
153
154 private JComponent buildDetails()
155 {
156 paramDetailsModel = new PresentationModel<RestParamProperty>( null );
157 detailsForm = new SimpleBindingForm( paramDetailsModel );
158
159 detailsForm.addSpace( 5 );
160 detailsForm.appendCheckBox( "required", "Required", "Sets if parameter is required" );
161
162
163
164 List<QName> types = new ArrayList<QName>();
165 for( SchemaType type : XmlBeans.getBuiltinTypeSystem().globalTypes() )
166 {
167 types.add( type.getName() );
168 }
169
170 detailsForm.appendComboBox( "type", "Type", types.toArray(), "The type of the parameter" );
171 optionsFormComponent = new StringListFormComponent( "Available values for this Parameter" );
172 optionsFormComponent.setPreferredSize( new Dimension( 350, 80 ) );
173 detailsForm.appendComponent( "options", "Options", optionsFormComponent );
174 detailsForm.appendTextField( "description", "Description", "A short description of the parameter" );
175 detailsForm.appendCheckBox( "disableUrlEncoding", "Disable Encoding",
176 "Disables URL-Encoding of the parameter value" );
177
178 detailsForm.addSpace( 5 );
179
180 detailsForm.setEnabled( false );
181
182 return new JScrollPane( detailsForm.getPanel() );
183 }
184
185 protected RestParamProperty getSelectedParameter()
186 {
187 return paramsTable.getSelectedRow() == -1 ? null : paramsTableModel.getParameterAt( paramsTable.getSelectedRow() );
188 }
189
190 public JTable getParamsTable()
191 {
192 return paramsTable;
193 }
194
195 public void release()
196 {
197 paramsTableModel.release();
198 if( paramDetailsModel != null )
199 paramDetailsModel.setBean( null );
200 }
201
202 protected Component buildToolbar()
203 {
204 JXToolBar toolbar = UISupport.createToolbar();
205
206 toolbar.add( UISupport.createToolbarButton( addParamAction ) );
207 toolbar.add( UISupport.createToolbarButton( removeParamAction, false ) );
208 toolbar.add( UISupport.createToolbarButton( clearParamsAction, paramsTable.getRowCount() > 0 ) );
209 toolbar.addSeparator();
210 toolbar.add( UISupport.createToolbarButton( movePropertyDownAction, false ) );
211 toolbar.add( UISupport.createToolbarButton( movePropertyUpAction, false ) );
212 toolbar.addSeparator();
213 toolbar.add( UISupport.createToolbarButton( updateParamsAction ) );
214 toolbar.addSeparator();
215
216 insertAdditionalButtons( toolbar );
217
218 toolbar.addGlue();
219
220 toolbar.add( UISupport.createToolbarButton( new ShowOnlineHelpAction( HelpUrls.WADL_PARAMS_HELP_URL ) ) );
221
222 return toolbar;
223 }
224
225 protected void insertAdditionalButtons( JXToolBar toolbar )
226 {
227 }
228
229 private class AddParamAction extends AbstractAction
230 {
231 public AddParamAction()
232 {
233 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ) );
234 putValue( Action.SHORT_DESCRIPTION, "Adds a parameter to the parameter table" );
235 }
236
237 public void actionPerformed( ActionEvent e )
238 {
239 String name = UISupport.prompt( "Specify parameter name", "Add Parameter", "" );
240 if( StringUtils.hasContent( name ) )
241 {
242 params.addProperty( name );
243 final int row = params.getPropertyNames().length - 1;
244 SwingUtilities.invokeLater( new Runnable()
245 {
246 public void run()
247 {
248 requestFocusInWindow();
249 scrollRectToVisible( paramsTable.getCellRect( row, 1, true ) );
250 SwingUtilities.invokeLater( new Runnable()
251 {
252 public void run()
253 {
254 paramsTable.editCellAt( row, 1 );
255 paramsTable.getEditorComponent().requestFocusInWindow();
256 }
257 } );
258 }
259 } );
260
261 clearParamsAction.setEnabled( true );
262 }
263 }
264 }
265
266 private class UpdateParamsAction extends AbstractAction
267 {
268 private UpdateParamsAction()
269 {
270 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ) );
271 putValue( Action.SHORT_DESCRIPTION, "Updates params from a specified URL" );
272 }
273
274 public void actionPerformed( ActionEvent e )
275 {
276 String str = UISupport.prompt( "Enter new url below", "Extract Params", "" );
277 if( str == null )
278 return;
279
280 try
281 {
282 params.resetValues();
283 RestUtils.extractParams( str, params, false );
284 }
285 catch( Exception e1 )
286 {
287 UISupport.showErrorMessage( e1 );
288 }
289 }
290 }
291
292 private class RemoveParamAction extends AbstractAction
293 {
294 public RemoveParamAction()
295 {
296 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/remove_property.gif" ) );
297 putValue( Action.SHORT_DESCRIPTION, "Removes the selected parameter" );
298 setEnabled( false );
299 }
300
301 public void actionPerformed( ActionEvent e )
302 {
303 int row = paramsTable.getSelectedRow();
304 if( row == -1 )
305 return;
306
307 UISupport.stopCellEditing( paramsTable );
308
309 String propertyName = paramsTableModel.getValueAt( row, 0 ).toString();
310 if( UISupport.confirm( "Remove parameter [" + propertyName + "]?", "Remove Parameter" ) )
311 {
312 paramsTable.clearSelection();
313 params.removeProperty( propertyName );
314 clearParamsAction.setEnabled( params.getPropertyCount() > 0 );
315 }
316 }
317 }
318
319 private class ClearParamsAction extends AbstractAction
320 {
321 public ClearParamsAction()
322 {
323 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/clear_properties.gif" ) );
324 putValue( Action.SHORT_DESCRIPTION, "Clears all current parameter values" );
325 }
326
327 public void actionPerformed( ActionEvent e )
328 {
329 if( UISupport.confirm( "Clear all parameter values?", "Clear Parameters" ) )
330 {
331 params.clear();
332 }
333 }
334 }
335
336 private class UseDefaultParamsAction extends AbstractAction
337 {
338 public UseDefaultParamsAction()
339 {
340 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/default_properties.gif" ) );
341 putValue( Action.SHORT_DESCRIPTION, "Reverts all current parameters to default values" );
342 }
343
344 public void actionPerformed( ActionEvent e )
345 {
346 if( UISupport.confirm( "Revert all parameters to default values?", "Use Defaults" ) )
347 {
348 for( TestProperty property : params.getProperties().values() )
349 {
350 property.setValue( null );
351 }
352 }
353 }
354 }
355
356 private class MovePropertyUpAction extends AbstractAction
357 {
358 public MovePropertyUpAction()
359 {
360 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/up_arrow.gif" ) );
361 putValue( Action.SHORT_DESCRIPTION, "Moves selected parameter up one row" );
362 setEnabled( false );
363 }
364
365 public void actionPerformed( ActionEvent e )
366 {
367 int ix = paramsTable.getSelectedRow();
368 if( ix != -1 )
369 {
370 params.moveProperty( params.getPropertyAt( ix ).getName(), ix - 1 );
371 paramsTable.setRowSelectionInterval( ix - 1, ix - 1 );
372 }
373 }
374 }
375
376 private class MovePropertyDownAction extends AbstractAction
377 {
378 public MovePropertyDownAction()
379 {
380 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/down_arrow.gif" ) );
381 putValue( Action.SHORT_DESCRIPTION, "Moves selected parameter down one row" );
382 setEnabled( false );
383 }
384
385 public void actionPerformed( ActionEvent e )
386 {
387 int ix = paramsTable.getSelectedRow();
388 if( ix != -1 )
389 {
390 params.moveProperty( params.getPropertyAt( ix ).getName(), ix + 1 );
391 paramsTable.setRowSelectionInterval( ix + 1, ix + 1 );
392 }
393 }
394 }
395
396 public void setParams( RestParamsPropertyHolder params )
397 {
398 this.params = params;
399 paramsTableModel.setParams( params );
400 }
401
402 public void refresh()
403 {
404 paramsTableModel.fireTableDataChanged();
405 }
406 }