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