View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  package com.eviware.soapui.impl.rest.panels.method;
13  
14  import java.awt.BorderLayout;
15  import java.awt.event.ActionEvent;
16  import java.beans.PropertyChangeEvent;
17  import java.beans.PropertyChangeListener;
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import javax.swing.AbstractAction;
23  import javax.swing.JPanel;
24  import javax.swing.JScrollPane;
25  import javax.swing.JTable;
26  import javax.swing.event.ListSelectionEvent;
27  import javax.swing.event.ListSelectionListener;
28  import javax.swing.table.AbstractTableModel;
29  
30  import com.eviware.soapui.impl.rest.RestMethod;
31  import com.eviware.soapui.impl.rest.RestRepresentation;
32  import com.eviware.soapui.support.StringUtils;
33  import com.eviware.soapui.support.UISupport;
34  import com.eviware.soapui.support.components.JXToolBar;
35  import com.eviware.soapui.support.types.StringList;
36  
37  public class RestRepresentationsTable extends JPanel implements PropertyChangeListener
38  {
39  	private RestMethod restMethod;
40  	private List<RestRepresentation.Type> types;
41  	private JTable representationsTable;
42  	private RepresentationsTableModel tableModel;
43  	private AddRepresentationAction addRepresentationAction;
44  	private RemoveRepresentationAction removeRepresentationAction;
45  	private boolean readOnly;
46  
47  	public RestRepresentationsTable( RestMethod restMethod, RestRepresentation.Type[] types, boolean readOnly )
48  	{
49  		super( new BorderLayout() );
50  		this.restMethod = restMethod;
51  		this.types = Arrays.asList( types );
52  		this.readOnly = readOnly;
53  
54  		tableModel = new RepresentationsTableModel();
55  		representationsTable = new JTable( tableModel );
56  		representationsTable.setRowHeight( 18 );
57  
58  		add( buildToolbar(), BorderLayout.NORTH );
59  		add( new JScrollPane( representationsTable ), BorderLayout.CENTER );
60  
61  		restMethod.addPropertyChangeListener( "representations", this );
62  	}
63  
64  	protected JXToolBar buildToolbar()
65  	{
66  		JXToolBar toolbar = UISupport.createToolbar();
67  		if( !readOnly )
68  		{
69  			addRepresentationAction = new AddRepresentationAction();
70  			toolbar.addFixed( UISupport.createToolbarButton( addRepresentationAction ) );
71  
72  			removeRepresentationAction = new RemoveRepresentationAction();
73  			removeRepresentationAction.setEnabled( false );
74  			representationsTable.getSelectionModel().addListSelectionListener( new ListSelectionListener()
75  			{
76  				public void valueChanged( ListSelectionEvent e )
77  				{
78  					removeRepresentationAction.setEnabled( representationsTable.getSelectedRow() != -1 );
79  				}
80  			} );
81  			toolbar.addFixed( UISupport.createToolbarButton( removeRepresentationAction ) );
82  		}
83  
84  		return toolbar;
85  	}
86  
87  	public class RepresentationsTableModel extends AbstractTableModel implements PropertyChangeListener
88  	{
89  		private List<RestRepresentation> data = new ArrayList<RestRepresentation>();
90  
91  		public RepresentationsTableModel()
92  		{
93  			initData();
94  		}
95  
96  		private void initData()
97  		{
98  			if( !data.isEmpty() )
99  			{
100 				release();
101 				data.clear();
102 			}
103 
104 			for( RestRepresentation representation : restMethod.getRepresentations() )
105 			{
106 				if( types.contains( representation.getType() ) )
107 				{
108 					representation.addPropertyChangeListener( this );
109 					data.add( representation );
110 				}
111 			}
112 		}
113 
114 		public int getColumnCount()
115 		{
116 			return 4;
117 		}
118 
119 		public int getRowCount()
120 		{
121 			return data.size();
122 		}
123 
124 		public Object getValueAt( int rowIndex, int columnIndex )
125 		{
126 			RestRepresentation representation = data.get( rowIndex );
127 
128 			switch( columnIndex )
129 			{
130 			case 0 :
131 				return representation.getType().toString();
132 			case 1 :
133 				return representation.getMediaType();
134 			case 2 :
135 				return representation.getType().equals( RestRepresentation.Type.REQUEST ) ? "n/a" : representation
136 						.getStatus().toString();
137 			case 3 :
138 				return representation.getElement() == null ? null : representation.getElement().toString();
139 			}
140 
141 			return null;
142 		}
143 
144 		@Override
145 		public boolean isCellEditable( int rowIndex, int columnIndex )
146 		{
147 			return !readOnly && columnIndex > 0 && columnIndex < 3
148 					&& !( data.get( rowIndex ).getType().equals( RestRepresentation.Type.REQUEST ) && columnIndex == 2 );
149 		}
150 
151 		@Override
152 		public void setValueAt( Object value, int rowIndex, int columnIndex )
153 		{
154 			if( readOnly )
155 				return;
156 			RestRepresentation representation = data.get( rowIndex );
157 
158 			switch( columnIndex )
159 			{
160 			case 1 :
161 				representation.setMediaType( value == null ? "" : value.toString() );
162 				break;
163 			case 2 :
164 			{
165 				if( value == null )
166 					value = "";
167 
168 				String[] items = value.toString().split( " " );
169 				List<Integer> status = new ArrayList<Integer>();
170 
171 				for( String item : items )
172 				{
173 					try
174 					{
175 						if( StringUtils.hasContent( item ) )
176 							status.add( Integer.parseInt( item.trim() ) );
177 					}
178 					catch( NumberFormatException e )
179 					{
180 					}
181 				}
182 
183 				representation.setStatus( status );
184 				break;
185 			}
186 			}
187 		}
188 
189 		@Override
190 		public String getColumnName( int column )
191 		{
192 			switch( column )
193 			{
194 			case 0 :
195 				return "Type";
196 			case 1 :
197 				return "Media-Type";
198 			case 2 :
199 				return "Status Codes";
200 			case 3 :
201 				return "QName";
202 			}
203 
204 			return null;
205 		}
206 
207 		public void refresh()
208 		{
209 			initData();
210 			fireTableDataChanged();
211 		}
212 
213 		public void propertyChange( PropertyChangeEvent evt )
214 		{
215 			fireTableDataChanged();
216 		}
217 
218 		public void release()
219 		{
220 			for( RestRepresentation representation : data )
221 			{
222 				representation.removePropertyChangeListener( this );
223 			}
224 		}
225 
226 		public RestRepresentation getRepresentationAtRow( int rowIndex )
227 		{
228 			return data.get( rowIndex );
229 		}
230 	}
231 
232 	public RestRepresentation getRepresentationAtRow( int rowIndex )
233 	{
234 		return tableModel.getRepresentationAtRow( rowIndex );
235 	}
236 
237 	private class AddRepresentationAction extends AbstractAction
238 	{
239 		private AddRepresentationAction()
240 		{
241 			putValue( SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ) );
242 			putValue( SHORT_DESCRIPTION, "Adds a new Response Representation to this Method" );
243 		}
244 
245 		public void actionPerformed( ActionEvent e )
246 		{
247 			String type = types.size() == 1 ? types.get( 0 ).toString() : UISupport.prompt(
248 					"Specify type of Representation to add", "Add Representation", new StringList( types ).toStringArray() );
249 
250 			if( type != null )
251 			{
252 				restMethod.addNewRepresentation( RestRepresentation.Type.valueOf( type ) );
253 			}
254 		}
255 	}
256 
257 	private class RemoveRepresentationAction extends AbstractAction
258 	{
259 		private RemoveRepresentationAction()
260 		{
261 			putValue( SMALL_ICON, UISupport.createImageIcon( "/remove_property.gif" ) );
262 			putValue( SHORT_DESCRIPTION, "Removes selected Representation from this Method" );
263 		}
264 
265 		public void actionPerformed( ActionEvent e )
266 		{
267 			if( UISupport.confirm( "Remove selected Representation?", "Remove Representation" ) )
268 			{
269 				restMethod
270 						.removeRepresentation( tableModel.getRepresentationAtRow( representationsTable.getSelectedRow() ) );
271 			}
272 		}
273 	}
274 
275 	public void propertyChange( PropertyChangeEvent arg0 )
276 	{
277 		tableModel.refresh();
278 	}
279 
280 	public void release()
281 	{
282 		tableModel.release();
283 		restMethod.removePropertyChangeListener( "representations", this );
284 	}
285 
286 	public void refresh()
287 	{
288 		tableModel.refresh();
289 	}
290 
291 	public int getSelectedRow()
292 	{
293 		return representationsTable.getSelectedRow();
294 	}
295 }