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