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  
65  	protected JXToolBar buildToolbar()
66  	{
67  		JXToolBar toolbar = UISupport.createToolbar();
68  		if( !readOnly )
69  		{
70  			addRepresentationAction = new AddRepresentationAction();
71  			toolbar.addFixed( UISupport.createToolbarButton( addRepresentationAction ) );
72  
73  			removeRepresentationAction = new RemoveRepresentationAction();
74  			removeRepresentationAction.setEnabled( false );
75  			representationsTable.getSelectionModel().addListSelectionListener( new ListSelectionListener()
76  			{
77  				public void valueChanged( ListSelectionEvent e )
78  				{
79  					removeRepresentationAction.setEnabled( representationsTable.getSelectedRow() != -1 );
80  				}
81  			} );
82  			toolbar.addFixed( UISupport.createToolbarButton( removeRepresentationAction ) );
83  		}
84  
85  		return toolbar;
86  	}
87  
88  	public class RepresentationsTableModel extends AbstractTableModel implements PropertyChangeListener
89  	{
90  		private List<RestRepresentation> data = new ArrayList<RestRepresentation>();
91  
92  		public RepresentationsTableModel()
93  		{
94  			initData();
95  		}
96  
97  		private void initData()
98  		{
99  			if( !data.isEmpty() )
100 			{
101 				release();
102 				data.clear();
103 			}
104 
105 			for( RestRepresentation representation : restMethod.getRepresentations() )
106 			{
107 				if( types.contains( representation.getType() ) )
108 				{
109 					representation.addPropertyChangeListener( this );
110 					data.add( representation );
111 				}
112 			}
113 		}
114 
115 		public int getColumnCount()
116 		{
117 			return 4;
118 		}
119 
120 		public int getRowCount()
121 		{
122 			return data.size();
123 		}
124 
125 		public Object getValueAt( int rowIndex, int columnIndex )
126 		{
127 			RestRepresentation representation = data.get( rowIndex );
128 
129 			switch( columnIndex )
130 			{
131 			case 0 :
132 				return representation.getType().toString();
133 			case 1 :
134 				return representation.getMediaType();
135 			case 2 :
136 				return representation.getType().equals( RestRepresentation.Type.REQUEST ) ? "n/a" : representation
137 						.getStatus().toString();
138 			case 3 :
139 				return representation.getElement() == null ? null : representation.getElement().toString();
140 			}
141 
142 			return null;
143 		}
144 
145 		@Override
146 		public boolean isCellEditable( int rowIndex, int columnIndex )
147 		{
148 			return !readOnly && columnIndex > 0 && columnIndex < 3
149 					&& !( data.get( rowIndex ).getType().equals( RestRepresentation.Type.REQUEST ) && columnIndex == 2 );
150 		}
151 
152 		@Override
153 		public void setValueAt( Object value, int rowIndex, int columnIndex )
154 		{
155 			if( readOnly )
156 				return;
157 			RestRepresentation representation = data.get( rowIndex );
158 
159 			switch( columnIndex )
160 			{
161 			case 1 :
162 				representation.setMediaType( value == null ? "" : value.toString() );
163 				break;
164 			case 2 :
165 			{
166 				if( value == null )
167 					value = "";
168 
169 				String[] items = value.toString().split( " " );
170 				List<Integer> status = new ArrayList<Integer>();
171 
172 				for( String item : items )
173 				{
174 					try
175 					{
176 						if( StringUtils.hasContent( item ) )
177 							status.add( Integer.parseInt( item.trim() ) );
178 					}
179 					catch( NumberFormatException e )
180 					{
181 					}
182 				}
183 
184 				representation.setStatus( status );
185 				break;
186 			}
187 			}
188 		}
189 
190 		@Override
191 		public String getColumnName( int column )
192 		{
193 			switch( column )
194 			{
195 			case 0 :
196 				return "Type";
197 			case 1 :
198 				return "Media-Type";
199 			case 2 :
200 				return "Status Codes";
201 			case 3 :
202 				return "QName";
203 			}
204 
205 			return null;
206 		}
207 
208 		public void refresh()
209 		{
210 			initData();
211 			fireTableDataChanged();
212 		}
213 
214 		public void propertyChange( PropertyChangeEvent evt )
215 		{
216 			fireTableDataChanged();
217 		}
218 
219 		public void release()
220 		{
221 			for( RestRepresentation representation : data )
222 			{
223 				representation.removePropertyChangeListener( this );
224 			}
225 		}
226 
227 		public RestRepresentation getRepresentationAtRow( int rowIndex )
228 		{
229 			return data.get( rowIndex );
230 		}
231 	}
232 
233 	public RestRepresentation getRepresentationAtRow( int rowIndex )
234 	{
235 		return tableModel.getRepresentationAtRow( rowIndex );
236 	}
237 
238 	private class AddRepresentationAction extends AbstractAction
239 	{
240 		private AddRepresentationAction()
241 		{
242 			putValue( SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ) );
243 			putValue( SHORT_DESCRIPTION, "Adds a new Response Representation to this Method" );
244 		}
245 
246 		public void actionPerformed( ActionEvent e )
247 		{
248 			String type = types.size() == 1 ? types.get( 0 ).toString() : UISupport.prompt(
249 					"Specify type of Representation to add", "Add Representation", new StringList( types ).toStringArray() );
250 
251 			if( type != null )
252 			{
253 				restMethod.addNewRepresentation( RestRepresentation.Type.valueOf( type ) );
254 			}
255 		}
256 	}
257 
258 	private class RemoveRepresentationAction extends AbstractAction
259 	{
260 		private RemoveRepresentationAction()
261 		{
262 			putValue( SMALL_ICON, UISupport.createImageIcon( "/remove_property.gif" ) );
263 			putValue( SHORT_DESCRIPTION, "Removes selected Representation from this Method" );
264 		}
265 
266 		public void actionPerformed( ActionEvent e )
267 		{
268 			if( UISupport.confirm( "Remove selected Representation?", "Remove Representation" ) )
269 			{
270 				restMethod
271 						.removeRepresentation( tableModel.getRepresentationAtRow( representationsTable.getSelectedRow() ) );
272 			}
273 		}
274 	}
275 
276 	public void propertyChange( PropertyChangeEvent arg0 )
277 	{
278 		tableModel.refresh();
279 	}
280 
281 	public void release()
282 	{
283 		tableModel.release();
284 	}
285 
286 	public void refresh()
287 	{
288 		tableModel.refresh();
289 	}
290 
291 	public int getSelectedRow()
292 	{
293 		return representationsTable.getSelectedRow();
294 	}
295 }