View Javadoc

1   package com.eviware.soapui.impl.rest.panels.resource;
2   
3   import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder;
4   import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder.ParameterStyle;
5   import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder.RestParamProperty;
6   import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
7   import com.eviware.soapui.impl.wsdl.support.HelpUrls;
8   import com.eviware.soapui.support.StringUtils;
9   import com.eviware.soapui.support.UISupport;
10  import com.eviware.soapui.support.components.*;
11  import com.jgoodies.binding.PresentationModel;
12  import org.apache.xmlbeans.SchemaType;
13  import org.apache.xmlbeans.XmlBeans;
14  
15  import javax.swing.*;
16  import javax.swing.event.ListSelectionEvent;
17  import javax.swing.event.ListSelectionListener;
18  import javax.xml.namespace.QName;
19  import java.awt.*;
20  import java.awt.event.ActionEvent;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  public class RestParamsTable extends JPanel
25  {
26     private final XmlBeansRestParamsTestPropertyHolder params;
27     private RestParamsTableModel paramsTableModel;
28     private JTable paramsTable;
29     private AddParamAction addParamAction = new AddParamAction();
30     private RemoveParamAction removeParamAction = new RemoveParamAction();
31     private ClearParamsAction clearParamsAction = new ClearParamsAction();
32     private MovePropertyDownAction movePropertyDownAction = new MovePropertyDownAction();
33     private MovePropertyUpAction movePropertyUpAction = new MovePropertyUpAction();
34     private PresentationModel<RestParamProperty> paramDetailsModel;
35     private JComponentInspector<JComponent> detailsInspector;
36     private JInspectorPanel inspectorPanel;
37     private StringListFormComponent optionsFormComponent;
38  
39     public RestParamsTable( XmlBeansRestParamsTestPropertyHolder params, boolean showInspector )
40     {
41        super( new BorderLayout() );
42        this.params = params;
43  
44        paramsTableModel = new RestParamsTableModel( params );
45        paramsTable = new JTable( paramsTableModel );
46        paramsTable.setRowHeight( 19 );
47        paramsTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
48        paramsTable.setDefaultEditor( ParameterStyle.class, new DefaultCellEditor( new JComboBox(
49                new Object[]{ParameterStyle.QUERY, ParameterStyle.TEMPLATE, ParameterStyle.HEADER, ParameterStyle.MATRIX, ParameterStyle.PLAIN} ) ) );
50  
51        paramsTable.getSelectionModel().addListSelectionListener( new ListSelectionListener()
52        {
53  
54           public void valueChanged( ListSelectionEvent e )
55           {
56              int selectedRow = paramsTable.getSelectedRow();
57              removeParamAction.setEnabled( selectedRow != -1 );
58              movePropertyDownAction.setEnabled( selectedRow < paramsTable.getRowCount() - 1 );
59              movePropertyUpAction.setEnabled( selectedRow > 0 );
60  
61              if( detailsInspector != null )
62              {
63                 detailsInspector.setEnabled( selectedRow != -1 );
64  
65                 if( selectedRow != -1 )
66                 {
67                    RestParamProperty selectedParameter = getSelectedParameter();
68                    paramDetailsModel.setBean( selectedParameter );
69                 }
70                 else
71                 {
72                    inspectorPanel.deactivate();
73                    paramDetailsModel.setBean( null );
74                 }
75              }
76           }
77        } );
78  
79        add( buildToolbar(), BorderLayout.NORTH );
80  
81        if( showInspector )
82        {
83           inspectorPanel = JInspectorPanelFactory.build( new JScrollPane( paramsTable ) );
84           detailsInspector = new JComponentInspector<JComponent>( buildDetails(), "Parameter Details",
85                   "Details for the selected Parameter", false );
86           inspectorPanel.addInspector( detailsInspector );
87  
88           add( inspectorPanel.getComponent(), BorderLayout.CENTER );
89        }
90        else
91        {
92           add( new JScrollPane( paramsTable ), BorderLayout.CENTER );
93        }
94     }
95  
96     private JComponent buildDetails()
97     {
98        paramDetailsModel = new PresentationModel<RestParamProperty>( null );
99        SimpleBindingForm form = new SimpleBindingForm( paramDetailsModel );
100 
101       form.addSpace( 5 );
102       form.appendCheckBox( "required", "Required", "Sets if parameter is required" );
103       form.appendTextField( "defaultValue", "Default", "The default value for this parameter" );
104 
105       List<QName> types = new ArrayList<QName>();
106       for( SchemaType type : XmlBeans.getBuiltinTypeSystem().globalTypes() )
107       {
108          types.add( type.getName() );
109       }
110 
111       form.appendComboBox( "type", "Type", types.toArray(), "The type of the parameter" );
112       optionsFormComponent = new StringListFormComponent( "Available values for this Parameter" );
113       optionsFormComponent.setPreferredSize( new Dimension( 350, 80 ) );
114       form.appendComponent( "options", "Options", optionsFormComponent );
115       form.appendTextField( "description", "Description", "A short description of the parameter" );
116       form.appendCheckBox( "disableUrlEncoding", "Disable Encoding", "Disables URL-Encoding of the parameter value" );
117 
118       form.addSpace( 5 );
119 
120       return new JScrollPane( form.getPanel() );
121    }
122 
123    protected RestParamProperty getSelectedParameter()
124    {
125       return paramsTable.getSelectedRow() == -1 ? null : paramsTableModel.getParameterAt( paramsTable.getSelectedRow() );
126    }
127 
128    public JTable getParamsTable()
129    {
130       return paramsTable;
131    }
132 
133    private Component buildToolbar()
134    {
135       JXToolBar toolbar = UISupport.createToolbar();
136 
137       toolbar.add( UISupport.createToolbarButton( addParamAction ) );
138       toolbar.add( UISupport.createToolbarButton( removeParamAction, false ) );
139       toolbar.add( UISupport.createToolbarButton( clearParamsAction, paramsTable.getRowCount() > 0 ) );
140       toolbar.addSeparator();
141       toolbar.add( UISupport.createToolbarButton( movePropertyDownAction, false ) );
142       toolbar.add( UISupport.createToolbarButton( movePropertyUpAction, false ) );
143       toolbar.addSeparator();
144 
145       insertAdditionalButtons( toolbar );
146 
147       toolbar.addGlue();
148 
149       toolbar.add( UISupport.createToolbarButton( new ShowOnlineHelpAction( HelpUrls.WADL_PARAMS_HELP_URL ) ) );
150 
151       return toolbar;
152    }
153 
154    protected void insertAdditionalButtons( JXToolBar toolbar )
155    {
156    }
157 
158    private class AddParamAction extends AbstractAction
159    {
160       public AddParamAction()
161       {
162          putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ) );
163          putValue( Action.SHORT_DESCRIPTION, "Adds a parameter to the parameter table" );
164       }
165 
166       public void actionPerformed( ActionEvent e )
167       {
168          String name = UISupport.prompt( "Specify unique parameter name", "Add Parameter", "" );
169          if( StringUtils.hasContent( name ) )
170          {
171             if( params.hasProperty( name ) )
172             {
173                UISupport.showErrorMessage( "Param name [" + name + "] already exists.." );
174                return;
175             }
176 
177             params.addProperty( name );
178             final int row = params.getPropertyNames().length - 1;
179             SwingUtilities.invokeLater( new Runnable()
180             {
181                public void run()
182                {
183                   requestFocusInWindow();
184                   scrollRectToVisible( paramsTable.getCellRect( row, 1, true ) );
185                   SwingUtilities.invokeLater( new Runnable()
186                   {
187                      public void run()
188                      {
189                         paramsTable.editCellAt( row, 1 );
190                         paramsTable.getEditorComponent().requestFocusInWindow();
191                      }
192                   } );
193                }
194             } );
195 
196             clearParamsAction.setEnabled( true );
197          }
198       }
199    }
200 
201    private class RemoveParamAction extends AbstractAction
202    {
203       public RemoveParamAction()
204       {
205          putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/remove_property.gif" ) );
206          putValue( Action.SHORT_DESCRIPTION, "Removes the selected parameter" );
207          setEnabled( false );
208       }
209 
210       public void actionPerformed( ActionEvent e )
211       {
212          int row = paramsTable.getSelectedRow();
213          if( row == -1 )
214             return;
215 
216          UISupport.stopCellEditing( paramsTable );
217 
218          String propertyName = paramsTableModel.getValueAt( row, 0 ).toString();
219          if( UISupport.confirm( "Remove parameter [" + propertyName + "]?", "Remove Parameter" ) )
220          {
221             paramsTable.clearSelection();
222             params.removeProperty( propertyName );
223             clearParamsAction.setEnabled( params.getPropertyCount() > 0 );
224          }
225       }
226    }
227 
228    private class ClearParamsAction extends AbstractAction
229    {
230       public ClearParamsAction()
231       {
232          putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/clear_properties.gif" ) );
233          putValue( Action.SHORT_DESCRIPTION, "Clears all current parameter values" );
234       }
235 
236       public void actionPerformed( ActionEvent e )
237       {
238          if( UISupport.confirm( "Clear all parameter values?", "Clear Parameters" ) )
239          {
240             for( String name : params.getPropertyNames() )
241             {
242                params.getProperty( name ).setValue( null );
243             }
244          }
245       }
246    }
247 
248    private class MovePropertyUpAction extends AbstractAction
249    {
250       public MovePropertyUpAction()
251       {
252          putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/up_arrow.gif" ) );
253          putValue( Action.SHORT_DESCRIPTION, "Moves selected parameter up one row" );
254          setEnabled( false );
255       }
256 
257       public void actionPerformed( ActionEvent e )
258       {
259          int ix = paramsTable.getSelectedRow();
260          if( ix != -1 )
261          {
262             params.moveProperty( params.getPropertyAt( ix ).getName(), ix - 1 );
263             paramsTable.setRowSelectionInterval( ix - 1, ix - 1 );
264          }
265       }
266    }
267 
268    private class MovePropertyDownAction extends AbstractAction
269    {
270       public MovePropertyDownAction()
271       {
272          putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/down_arrow.gif" ) );
273          putValue( Action.SHORT_DESCRIPTION, "Moves selected parameter down one row" );
274          setEnabled( false );
275       }
276 
277       public void actionPerformed( ActionEvent e )
278       {
279          int ix = paramsTable.getSelectedRow();
280          if( ix != -1 )
281          {
282             params.moveProperty( params.getPropertyAt( ix ).getName(), ix + 1 );
283             paramsTable.setRowSelectionInterval( ix + 1, ix + 1 );
284          }
285       }
286    }
287 
288    public void refresh()
289    {
290       paramsTableModel.fireTableDataChanged();
291    }
292 }