View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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  
13  package com.eviware.soapui.impl.rest.panels.request.inspectors.representations;
14  
15  import com.eviware.soapui.impl.rest.RestRepresentation;
16  import com.eviware.soapui.impl.rest.RestRequest;
17  import com.eviware.soapui.support.StringUtils;
18  import com.eviware.soapui.support.editor.EditorView;
19  import com.eviware.soapui.support.editor.inspectors.AbstractXmlInspector;
20  import com.eviware.soapui.support.editor.views.xml.raw.RawXmlEditorFactory;
21  import com.eviware.soapui.support.editor.xml.XmlDocument;
22  import org.jdesktop.swingx.JXTable;
23  
24  import javax.swing.*;
25  import javax.swing.table.AbstractTableModel;
26  import java.awt.*;
27  import java.beans.PropertyChangeEvent;
28  import java.beans.PropertyChangeListener;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  public class RestResponseRepresentationsInspector extends AbstractXmlInspector implements PropertyChangeListener
33  {
34  	private JPanel mainPanel;
35  	private final RestRequest request;
36  	private JXTable representationsTable;
37  	private ResponseRepresentationsTableModel tableModel;
38  
39  	protected RestResponseRepresentationsInspector( RestRequest request )
40  	{
41  		super( "Rep", "Response Representations", true, RestRepresentationsInspectorFactory.INSPECTOR_ID );
42  		this.request = request;
43  		
44  		request.addPropertyChangeListener("representations", this);
45  	}
46  
47  	public JComponent getComponent()
48  	{
49  		if( mainPanel == null )
50  		{
51  			mainPanel = new JPanel( new BorderLayout() );
52  			tableModel = new ResponseRepresentationsTableModel();
53  			representationsTable = new JXTable( tableModel );
54  			mainPanel.add( new JScrollPane( representationsTable ), BorderLayout.CENTER );
55  		}
56  
57  		return mainPanel;
58  	}
59  	
60  	@Override
61  	public boolean isEnabledFor( EditorView<XmlDocument> view )
62  	{
63  		return !view.getViewId().equals( RawXmlEditorFactory.VIEW_ID );
64  	}
65  	
66  	public class ResponseRepresentationsTableModel extends AbstractTableModel implements PropertyChangeListener
67     {
68  		List<RestRepresentation> data = new ArrayList<RestRepresentation>();
69  		
70  		public ResponseRepresentationsTableModel()
71  		{
72  			initData();
73  		}
74  		
75  		private void initData()
76  		{
77           if( !data.isEmpty())
78           {
79              release();
80  			   data.clear();
81           }
82  			
83  			for( RestRepresentation representation : request.getRepresentations( null, null ))
84  			{
85  				if( representation.getType() != RestRepresentation.Type.REQUEST )
86              {
87                 representation.addPropertyChangeListener( this );
88  					data.add( representation );
89              }
90  			}
91  		}
92  
93  		public int getColumnCount()
94  		{
95  			return 4;
96  		}
97  
98  		public int getRowCount()
99  		{
100 			return data.size();
101 		}
102 
103 		public Object getValueAt(int rowIndex, int columnIndex)
104 		{
105 			RestRepresentation representation = data.get( rowIndex );
106 			
107 			switch( columnIndex )
108 			{
109 			case 0 : return representation.getType().toString();
110 			case 1 : return representation.getMediaType();
111 			case 2 : return representation.getStatus().toString();
112          case 3 : return representation.getElement() == null ? null : representation.getElement().toString();
113 			}
114 			
115 			return null;
116 		}
117 
118 		@Override
119 		public boolean isCellEditable(int rowIndex, int columnIndex)
120 		{
121 			return columnIndex > 0 && columnIndex < 3;
122 		}
123 
124 		@Override
125 		public void setValueAt(Object value, int rowIndex, int columnIndex)
126 		{
127 			RestRepresentation representation = data.get( rowIndex );
128 			
129 			switch( columnIndex )
130 			{
131 			case 1 : representation.setMediaType(value == null ? "" : value.toString()); break;
132 			case 2 : 
133 				{
134 					if( value == null )
135 						value = "";
136 					
137 					String[] items = value.toString().split( "," );
138 					List<Integer> status = new ArrayList<Integer>();
139 					
140 					for( String item : items )
141 					{
142 						try
143 						{
144 							if( StringUtils.hasContent(item))
145 								status.add( Integer.parseInt( item.trim() ));
146 						}
147 						catch (NumberFormatException e)
148 						{
149 						}
150 					}
151 					
152 					representation.setStatus( status );
153 					break;
154 				}
155 			}
156 		}
157 
158 		@Override
159 		public String getColumnName(int column)
160 		{
161 			switch( column )
162 			{
163 			case 0 : return "Type";
164 			case 1 : return "Media-Type";
165 			case 2 : return "Status Codes";
166          case 3 : return "QName";
167          }
168 			
169 			return null;
170 		}
171 
172 		public void refresh()
173 		{
174 			initData();
175 			fireTableDataChanged();
176 		}
177 
178       public void propertyChange( PropertyChangeEvent evt )
179       {
180          fireTableDataChanged();
181       }
182 
183       public void release()
184       {
185          for( RestRepresentation representation : data )
186          {
187             representation.removePropertyChangeListener( this );
188          }
189       }
190    }
191 
192    @Override
193    public void release()
194    {
195       tableModel.release();
196    }
197 
198    public void propertyChange(PropertyChangeEvent evt)
199 	{
200 		tableModel.refresh();
201 	}
202 }