1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package com.eviware.soapui.impl.rest.panels.request.inspectors.representations;
26
27 import com.eviware.soapui.impl.rest.RestRepresentation;
28 import com.eviware.soapui.impl.rest.RestRequest;
29 import com.eviware.soapui.model.iface.Submit;
30 import com.eviware.soapui.model.iface.SubmitContext;
31 import com.eviware.soapui.support.StringUtils;
32 import com.eviware.soapui.support.UISupport;
33 import com.eviware.soapui.support.components.JXToolBar;
34 import com.eviware.soapui.support.editor.EditorView;
35 import com.eviware.soapui.support.editor.inspectors.AbstractXmlInspector;
36 import com.eviware.soapui.support.editor.views.xml.raw.RawXmlEditorFactory;
37 import com.eviware.soapui.support.editor.xml.XmlDocument;
38 import com.eviware.soapui.support.types.StringList;
39
40 import javax.swing.*;
41 import javax.swing.event.ListSelectionEvent;
42 import javax.swing.event.ListSelectionListener;
43 import javax.swing.table.AbstractTableModel;
44 import java.awt.*;
45 import java.awt.event.ActionEvent;
46 import java.beans.PropertyChangeEvent;
47 import java.beans.PropertyChangeListener;
48 import java.util.ArrayList;
49 import java.util.Arrays;
50 import java.util.List;
51
52 public abstract class AbstractRestRepresentationsInspector extends AbstractXmlInspector implements PropertyChangeListener
53 {
54 private JPanel mainPanel;
55 private final RestRequest request;
56 private JTable representationsTable;
57 private RepresentationsTableModel tableModel;
58 private AbstractRestRepresentationsInspector.AddRepresentationAction addRepresentationAction;
59 private AbstractRestRepresentationsInspector.RemoveRepresentationAction removeRepresentationAction;
60 private List<RestRepresentation.Type> types;
61
62 protected AbstractRestRepresentationsInspector(
63 RestRequest request, String name, String description,
64 RestRepresentation.Type[] types
65 )
66 {
67 super( name, description, true, RestRepresentationsInspectorFactory.INSPECTOR_ID );
68 this.request = request;
69 this.types = Arrays.asList( types );
70
71 request.addPropertyChangeListener( "representations", this );
72 updateLabel();
73 }
74
75 public JComponent getComponent()
76 {
77 if( mainPanel == null )
78 {
79 buildUI();
80 }
81
82 return mainPanel;
83 }
84
85 protected void buildUI()
86 {
87 mainPanel = new JPanel( new BorderLayout() );
88 tableModel = new RepresentationsTableModel();
89 representationsTable = new JTable( tableModel );
90 representationsTable.setRowHeight( 18 );
91 mainPanel.add( buildToolbar(), BorderLayout.NORTH );
92 mainPanel.add( new JScrollPane( representationsTable ), BorderLayout.CENTER );
93
94 representationsTable.getSelectionModel().addListSelectionListener( new ListSelectionListener()
95 {
96 public void valueChanged( ListSelectionEvent e )
97 {
98 removeRepresentationAction.setEnabled( representationsTable.getSelectedRow() != -1 );
99 }
100 } );
101 }
102
103 protected JXToolBar buildToolbar()
104 {
105 JXToolBar toolbar = UISupport.createToolbar();
106
107 addRepresentationAction = new AbstractRestRepresentationsInspector.AddRepresentationAction();
108 toolbar.addFixed( UISupport.createToolbarButton( addRepresentationAction ) );
109 removeRepresentationAction = new AbstractRestRepresentationsInspector.RemoveRepresentationAction();
110 toolbar.addFixed( UISupport.createToolbarButton( removeRepresentationAction ) );
111
112 return toolbar;
113 }
114
115 public RestRequest getRequest()
116 {
117 return request;
118 }
119
120 @Override
121 public boolean isEnabledFor( EditorView<XmlDocument> view )
122 {
123 return !view.getViewId().equals( RawXmlEditorFactory.VIEW_ID );
124 }
125
126 public boolean beforeSubmit( Submit submit, SubmitContext context )
127 {
128 return true;
129 }
130
131 public class RepresentationsTableModel extends AbstractTableModel implements PropertyChangeListener
132 {
133 private List<RestRepresentation> data = new ArrayList<RestRepresentation>();
134
135 public RepresentationsTableModel()
136 {
137 initData();
138 }
139
140 private void initData()
141 {
142 if( !data.isEmpty() )
143 {
144 release();
145 data.clear();
146 }
147
148 for( RestRepresentation representation : request.getRepresentations( null, null ) )
149 {
150 if( types.contains( representation.getType() ) )
151 {
152 representation.addPropertyChangeListener( this );
153 data.add( representation );
154 }
155 }
156 }
157
158 public int getColumnCount()
159 {
160 return 4;
161 }
162
163 public int getRowCount()
164 {
165 return data.size();
166 }
167
168 public Object getValueAt( int rowIndex, int columnIndex )
169 {
170 RestRepresentation representation = data.get( rowIndex );
171
172 switch( columnIndex )
173 {
174 case 0:
175 return representation.getType().toString();
176 case 1:
177 return representation.getMediaType();
178 case 2:
179 return representation.getType().equals(RestRepresentation.Type.REQUEST ) ? "n/a" :
180 representation.getStatus().toString();
181 case 3:
182 return representation.getElement() == null ? null : representation.getElement().toString();
183 }
184
185 return null;
186 }
187
188 @Override
189 public boolean isCellEditable( int rowIndex, int columnIndex )
190 {
191 return columnIndex > 0 && columnIndex < 3 &&
192 !(data.get( rowIndex ).getType().equals( RestRepresentation.Type.REQUEST ) && columnIndex == 2 );
193 }
194
195 @Override
196 public void setValueAt( Object value, int rowIndex, int columnIndex )
197 {
198 RestRepresentation representation = data.get( rowIndex );
199
200 switch( columnIndex )
201 {
202 case 1:
203 representation.setMediaType( value == null ? "" : value.toString() );
204 break;
205 case 2:
206 {
207 if( value == null )
208 value = "";
209
210 String[] items = value.toString().split( " " );
211 List<Integer> status = new ArrayList<Integer>();
212
213 for( String item : items )
214 {
215 try
216 {
217 if( StringUtils.hasContent( item ) )
218 status.add( Integer.parseInt( item.trim() ) );
219 }
220 catch( NumberFormatException e )
221 {
222 }
223 }
224
225 representation.setStatus( status );
226 break;
227 }
228 }
229 }
230
231 @Override
232 public String getColumnName( int column )
233 {
234 switch( column )
235 {
236 case 0:
237 return "Type";
238 case 1:
239 return "Media-Type";
240 case 2:
241 return "Status Codes";
242 case 3:
243 return "QName";
244 }
245
246 return null;
247 }
248
249 public void refresh()
250 {
251 initData();
252 fireTableDataChanged();
253 }
254
255 public void propertyChange( PropertyChangeEvent evt )
256 {
257 fireTableDataChanged();
258 }
259
260 public void release()
261 {
262 for( RestRepresentation representation : data )
263 {
264 representation.removePropertyChangeListener( this );
265 }
266 }
267
268 public RestRepresentation getRepresentationAtRow( int rowIndex )
269 {
270 return data.get( rowIndex );
271 }
272 }
273
274 @Override
275 public void release()
276 {
277 tableModel.release();
278 request.removePropertyChangeListener( "representations", this );
279 }
280
281 public void propertyChange( PropertyChangeEvent evt )
282 {
283 tableModel.refresh();
284 updateLabel();
285 }
286
287 private void updateLabel()
288 {
289 int cnt = 0;
290 for( RestRepresentation representation : request.getRepresentations( ))
291 {
292 if( types.contains( representation.getType() ))
293 cnt++;
294 }
295
296 setTitle( "Representations (" + cnt + ")" );
297 }
298
299 private class AddRepresentationAction extends AbstractAction
300 {
301 private AddRepresentationAction()
302 {
303 putValue( SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ) );
304 putValue( SHORT_DESCRIPTION, "Adds a new Response Representation to this Method" );
305 }
306
307 public void actionPerformed( ActionEvent e )
308 {
309 String type = types.size() == 1 ? types.get( 0 ).toString() :
310 UISupport.prompt( "Specify type of Representation to add", "Add Representation",
311 new StringList( types ).toStringArray());
312
313 if( type != null )
314 {
315 request.addNewRepresentation( RestRepresentation.Type.valueOf( type ) );
316 }
317 }
318 }
319
320 private class RemoveRepresentationAction extends AbstractAction
321 {
322 private RemoveRepresentationAction()
323 {
324 putValue( SMALL_ICON, UISupport.createImageIcon( "/remove_property.gif" ) );
325 putValue( SHORT_DESCRIPTION, "Removes selected Representation from this Method" );
326 }
327
328 public void actionPerformed( ActionEvent e )
329 {
330 if( UISupport.confirm( "Remove selected Representation?", "Remove Representation" ) )
331 {
332 request.removeRepresentation( tableModel.getRepresentationAtRow( representationsTable.getSelectedRow() ) );
333 }
334 }
335 }
336
337 }