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  import org.jdesktop.swingx.JXTable;
15  
16  import javax.swing.*;
17  import javax.swing.event.ListSelectionEvent;
18  import javax.swing.event.ListSelectionListener;
19  import javax.xml.namespace.QName;
20  import java.awt.*;
21  import java.awt.event.ActionEvent;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  public class RestParamsTable extends JPanel
26  {
27  	private final XmlBeansRestParamsTestPropertyHolder params;
28  	private RestParamsTableModel paramsTableModel;
29  	private JXTable paramsTable;
30  	private AddParamAction addParamAction = new AddParamAction();
31  	private RemoveParamAction removeParamAction = new RemoveParamAction();
32  	private ClearParamsAction clearParamsAction = new ClearParamsAction();
33  	private MovePropertyDownAction movePropertyDownAction = new MovePropertyDownAction();
34  	private MovePropertyUpAction movePropertyUpAction = new MovePropertyUpAction();
35  	private PresentationModel<RestParamProperty> paramDetailsModel;
36  	private JComponentInspector<JComponent> detailsInspector;
37  	private JInspectorPanel inspectorPanel;
38  	private StringListFormComponent optionsFormComponent;
39  
40  	public RestParamsTable(XmlBeansRestParamsTestPropertyHolder params, boolean showInspector)
41  	{
42  		super( new BorderLayout() );
43  		this.params = params;
44  		
45  		paramsTableModel = new RestParamsTableModel( params );
46  		paramsTable = new JXTable( paramsTableModel );
47  		paramsTable.setHorizontalScrollEnabled(true);
48  		paramsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
49  		paramsTable.setDefaultEditor(ParameterStyle.class, new DefaultCellEditor(new JComboBox( 
50  				new Object[] {ParameterStyle.QUERY, ParameterStyle.TEMPLATE, ParameterStyle.HEADER, ParameterStyle.MATRIX, ParameterStyle.PLAIN })));
51  		
52  		paramsTable.getSelectionModel().addListSelectionListener( new ListSelectionListener() {
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 inspectorPanel.deactivate();
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 		
112 		form.addSpace( 5 );
113 
114 		return new JScrollPane( form.getPanel() );
115 	}
116 
117 	protected RestParamProperty getSelectedParameter()
118 	{
119 		return paramsTable.getSelectedRow() == -1 ? null : paramsTableModel.getParameterAt(paramsTable.getSelectedRow());
120 	}
121 
122 	private Component buildToolbar()
123 	{
124 		JXToolBar toolbar = UISupport.createToolbar();
125 		
126 		toolbar.add( UISupport.createToolbarButton( addParamAction ));
127 		toolbar.add( UISupport.createToolbarButton( removeParamAction, false ));
128 		toolbar.add( UISupport.createToolbarButton( clearParamsAction, paramsTable.getRowCount() > 0 ));
129 		toolbar.addSeparator();
130 		toolbar.add( UISupport.createToolbarButton( movePropertyDownAction, false ));
131 		toolbar.add( UISupport.createToolbarButton( movePropertyUpAction, false ));
132 		toolbar.addSeparator();
133 
134       insertAdditionalButtons( toolbar );
135 
136       toolbar.addGlue();
137 
138       toolbar.add(UISupport.createToolbarButton(new ShowOnlineHelpAction( HelpUrls.WADL_PARAMS_HELP_URL )));
139 
140       return toolbar;
141 	}
142 
143    protected void insertAdditionalButtons( JXToolBar toolbar )
144    {
145    }
146 
147    private class AddParamAction extends AbstractAction
148 	{
149 		public AddParamAction()
150 		{
151 			putValue(Action.SMALL_ICON, UISupport.createImageIcon("/add_property.gif"));
152 			putValue(Action.SHORT_DESCRIPTION, "Adds a parameter to the parameter table");
153 		}
154 
155 		public void actionPerformed(ActionEvent e)
156 		{
157 			String name = UISupport.prompt("Specify unique parameter name", "Add Parameter", "");
158 			if ( StringUtils.hasContent( name ))
159 			{
160 				if( params.hasProperty( name ))
161 				{
162 					UISupport.showErrorMessage( "Param name [" + name + "] already exists.." );
163 					return;
164 				}
165 				
166 				params.addProperty(name);
167 				final int row = params.getPropertyNames().length - 1;
168 				paramsTableModel.fireTableRowsInserted(row, row);
169 				SwingUtilities.invokeLater( new Runnable()
170 				{
171 					public void run()
172 					{
173 						requestFocusInWindow();
174 						scrollRectToVisible( paramsTable.getCellRect( row,1,true ) );
175 						SwingUtilities.invokeLater( new Runnable()
176 						{
177 							public void run()
178 							{
179 								paramsTable.editCellAt(row, 1);
180 								paramsTable.getEditorComponent().requestFocusInWindow();
181 							}
182 						} );
183 					}
184 				} );
185 				
186 				clearParamsAction.setEnabled( true );
187 			}
188 		}
189 	}
190 
191 	private class RemoveParamAction extends AbstractAction
192 	{
193 		public RemoveParamAction()
194 		{
195 			putValue(Action.SMALL_ICON, UISupport.createImageIcon("/remove_property.gif"));
196 			putValue(Action.SHORT_DESCRIPTION, "Removes the selected parameter");
197 			setEnabled(false);
198 		}
199 
200 		public void actionPerformed(ActionEvent e)
201 		{
202 			int row = paramsTable.getSelectedRow();
203 			if (row == -1)
204 				return;
205 
206 			UISupport.stopCellEditing(paramsTable);
207 
208 			String propertyName = paramsTableModel.getValueAt(row, 0).toString();
209 			if (UISupport.confirm("Remove parameter [" + propertyName + "]?", "Remove Parameter"))
210 			{
211 				params.removeProperty( propertyName );
212 				paramsTableModel.fireTableRowsDeleted(row, row);
213 				clearParamsAction.setEnabled(params.getPropertyCount() > 0 );
214 			}
215 		}
216 	}
217 	
218 	private class ClearParamsAction extends AbstractAction
219 	{
220 		public ClearParamsAction()
221 		{
222 			putValue(Action.SMALL_ICON, UISupport.createImageIcon("/clear_properties.gif"));
223 			putValue(Action.SHORT_DESCRIPTION, "Clears all current parameter values");
224 		}
225 
226 		public void actionPerformed(ActionEvent e)
227 		{
228 			if( UISupport.confirm("Clear all parameter values?", "Clear Parameters"))
229 			{
230 				for( String name : params.getPropertyNames() )
231 				{
232 					params.getProperty( name ).setValue( null );
233 				}
234 			}
235 		}
236 	}
237 
238 	private class MovePropertyUpAction extends AbstractAction
239 	{
240 		public MovePropertyUpAction()
241 		{
242 			putValue(Action.SMALL_ICON, UISupport.createImageIcon("/up_arrow.gif"));
243 			putValue(Action.SHORT_DESCRIPTION, "Moves selected parameter up one row");
244 			setEnabled(false);
245 		}
246 		
247 		public void actionPerformed(ActionEvent e)
248 		{
249 			int ix = paramsTable.getSelectedRow();
250 			if( ix != -1 )
251 			{
252 				params.moveProperty(	params.getPropertyAt(ix).getName(), ix-1 );
253 				paramsTable.setRowSelectionInterval(ix-1,ix-1);
254 			}
255 		}
256 	}
257 	
258 	private class MovePropertyDownAction extends AbstractAction
259 	{
260 		public MovePropertyDownAction()
261 		{
262 			putValue(Action.SMALL_ICON, UISupport.createImageIcon("/down_arrow.gif"));
263 			putValue(Action.SHORT_DESCRIPTION, "Moves selected parameter down one row");
264 			setEnabled(false);
265 		}
266 		
267 		public void actionPerformed(ActionEvent e)
268 		{
269 			int ix = paramsTable.getSelectedRow();
270 			if( ix != -1 )
271 			{
272 				params.moveProperty(	params.getPropertyAt(ix).getName(), ix+1 );
273 				paramsTable.setRowSelectionInterval(ix+1, ix+1);
274 			}
275 		}
276 	}
277 
278 	public void refresh()
279 	{
280 		paramsTableModel.fireTableDataChanged();
281 	}
282 }