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 mainPanel.add( buildToolbar(), BorderLayout.NORTH );
91 mainPanel.add( new JScrollPane( representationsTable ), BorderLayout.CENTER );
92
93 representationsTable.getSelectionModel().addListSelectionListener( new ListSelectionListener()
94 {
95 public void valueChanged( ListSelectionEvent e )
96 {
97 removeRepresentationAction.setEnabled( representationsTable.getSelectedRow() != -1 );
98 }
99 } );
100 }
101
102 protected JXToolBar buildToolbar()
103 {
104 JXToolBar toolbar = UISupport.createToolbar();
105
106 addRepresentationAction = new AbstractRestRepresentationsInspector.AddRepresentationAction();
107 toolbar.addFixed( UISupport.createToolbarButton( addRepresentationAction ) );
108 removeRepresentationAction = new AbstractRestRepresentationsInspector.RemoveRepresentationAction();
109 toolbar.addFixed( UISupport.createToolbarButton( removeRepresentationAction ) );
110
111 return toolbar;
112 }
113
114 public RestRequest getRequest()
115 {
116 return request;
117 }
118
119 @Override
120 public boolean isEnabledFor( EditorView<XmlDocument> view )
121 {
122 return !view.getViewId().equals( RawXmlEditorFactory.VIEW_ID );
123 }
124
125 public boolean beforeSubmit( Submit submit, SubmitContext context )
126 {
127 return true;
128 }
129
130 public class RepresentationsTableModel extends AbstractTableModel implements PropertyChangeListener
131 {
132 private List<RestRepresentation> data = new ArrayList<RestRepresentation>();
133
134 public RepresentationsTableModel()
135 {
136 initData();
137 }
138
139 private void initData()
140 {
141 if( !data.isEmpty() )
142 {
143 release();
144 data.clear();
145 }
146
147 for( RestRepresentation representation : request.getRepresentations( null, null ) )
148 {
149 if( types.contains( representation.getType() ) )
150 {
151 representation.addPropertyChangeListener( this );
152 data.add( representation );
153 }
154 }
155 }
156
157 public int getColumnCount()
158 {
159 return 4;
160 }
161
162 public int getRowCount()
163 {
164 return data.size();
165 }
166
167 public Object getValueAt( int rowIndex, int columnIndex )
168 {
169 RestRepresentation representation = data.get( rowIndex );
170
171 switch( columnIndex )
172 {
173 case 0:
174 return representation.getType().toString();
175 case 1:
176 return representation.getMediaType();
177 case 2:
178 return representation.getType().equals(RestRepresentation.Type.REQUEST ) ? "n/a" :
179 representation.getStatus().toString();
180 case 3:
181 return representation.getElement() == null ? null : representation.getElement().toString();
182 }
183
184 return null;
185 }
186
187 @Override
188 public boolean isCellEditable( int rowIndex, int columnIndex )
189 {
190 return columnIndex > 0 && columnIndex < 3 &&
191 !(data.get( rowIndex ).getType().equals( RestRepresentation.Type.REQUEST ) && columnIndex == 2 );
192 }
193
194 @Override
195 public void setValueAt( Object value, int rowIndex, int columnIndex )
196 {
197 RestRepresentation representation = data.get( rowIndex );
198
199 switch( columnIndex )
200 {
201 case 1:
202 representation.setMediaType( value == null ? "" : value.toString() );
203 break;
204 case 2:
205 {
206 if( value == null )
207 value = "";
208
209 String[] items = value.toString().split( " " );
210 List<Integer> status = new ArrayList<Integer>();
211
212 for( String item : items )
213 {
214 try
215 {
216 if( StringUtils.hasContent( item ) )
217 status.add( Integer.parseInt( item.trim() ) );
218 }
219 catch( NumberFormatException e )
220 {
221 }
222 }
223
224 representation.setStatus( status );
225 break;
226 }
227 }
228 }
229
230 @Override
231 public String getColumnName( int column )
232 {
233 switch( column )
234 {
235 case 0:
236 return "Type";
237 case 1:
238 return "Media-Type";
239 case 2:
240 return "Status Codes";
241 case 3:
242 return "QName";
243 }
244
245 return null;
246 }
247
248 public void refresh()
249 {
250 initData();
251 fireTableDataChanged();
252 }
253
254 public void propertyChange( PropertyChangeEvent evt )
255 {
256 fireTableDataChanged();
257 }
258
259 public void release()
260 {
261 for( RestRepresentation representation : data )
262 {
263 representation.removePropertyChangeListener( this );
264 }
265 }
266
267 public RestRepresentation getRepresentationAtRow( int rowIndex )
268 {
269 return data.get( rowIndex );
270 }
271 }
272
273 @Override
274 public void release()
275 {
276 tableModel.release();
277 request.removePropertyChangeListener( "representations", this );
278 }
279
280 public void propertyChange( PropertyChangeEvent evt )
281 {
282 tableModel.refresh();
283 updateLabel();
284 }
285
286 private void updateLabel()
287 {
288 int cnt = 0;
289 for( RestRepresentation representation : request.getRepresentations( ))
290 {
291 if( types.contains( representation.getType() ))
292 cnt++;
293 }
294
295 setTitle( "Representations (" + cnt + ")" );
296 }
297
298 private class AddRepresentationAction extends AbstractAction
299 {
300 private AddRepresentationAction()
301 {
302 putValue( SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ) );
303 putValue( SHORT_DESCRIPTION, "Adds a new Response Representation to this Method" );
304 }
305
306 public void actionPerformed( ActionEvent e )
307 {
308 String type = types.size() == 1 ? types.get( 0 ).toString() :
309 UISupport.prompt( "Specify type of Representation to add", "Add Representation",
310 new StringList( types ).toStringArray());
311
312 if( type != null )
313 {
314 request.addNewRepresentation( RestRepresentation.Type.valueOf( type ) );
315 }
316 }
317 }
318
319 private class RemoveRepresentationAction extends AbstractAction
320 {
321 private RemoveRepresentationAction()
322 {
323 putValue( SMALL_ICON, UISupport.createImageIcon( "/remove_property.gif" ) );
324 putValue( SHORT_DESCRIPTION, "Removes selected Representation from this Method" );
325 }
326
327 public void actionPerformed( ActionEvent e )
328 {
329 if( UISupport.confirm( "Remove selected Representation?", "Remove Representation" ) )
330 {
331 request.removeRepresentation( tableModel.getRepresentationAtRow( representationsTable.getSelectedRow() ) );
332 }
333 }
334 }
335
336 }