View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.wsdl.panels.attachments;
14  
15  import java.beans.PropertyChangeEvent;
16  import java.beans.PropertyChangeListener;
17  import java.io.File;
18  import java.io.IOException;
19  import java.util.Arrays;
20  
21  import javax.swing.table.AbstractTableModel;
22  
23  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
24  import com.eviware.soapui.impl.wsdl.support.WsdlAttachment;
25  import com.eviware.soapui.model.iface.Attachment;
26  
27  /***
28   *
29   * @author emibre
30   */
31  public class MockAttachmentTableModel extends AbstractTableModel implements PropertyChangeListener, AttachmentTableModel {
32  	
33  	private boolean isResponse = false;
34  	private WsdlMockResponse mockResponse;
35  	
36  	/*** Creates a new instance of AttachmentTableModel */
37  	public MockAttachmentTableModel(WsdlMockResponse mockOperation2, boolean isResponse) {
38  		this.mockResponse = mockOperation2;
39  		this.isResponse = isResponse;
40  		
41  		mockResponse.addPropertyChangeListener( WsdlMockResponse.MOCKRESULT_PROPERTY, this );
42  		mockResponse.addPropertyChangeListener( WsdlMockResponse.ATTACHMENTS_PROPERTY, this );
43  	}
44  	
45  	public void release()
46  	{
47  		mockResponse.removePropertyChangeListener( this );
48  	}
49  	
50  	public void addFile( File file, boolean cacheInRequest) throws IOException 
51  	{
52  		if (isResponse)
53  		{
54  			Attachment attachment = mockResponse.attachFile( file, cacheInRequest );
55  			attachment.setContentType( ContentTypeHandler.getContentTypeFromFilename(file.getName()));
56  			this.fireTableRowsInserted(mockResponse.getAttachmentCount(), mockResponse.getAttachmentCount());
57  		}
58  	}
59  		
60  	public void removeAttachment(int[] rowIndexes) {
61  		Arrays.sort(rowIndexes);
62  		for (int i = rowIndexes.length-1; i >=0; i--)
63  			removeAttachment(rowIndexes[i]);
64  	}
65  	
66  	public void removeAttachment(int rowIndex) {
67  		if (isResponse)
68  		{
69  			mockResponse.removeAttachment(mockResponse.getAttachmentAt(rowIndex));
70  			this.fireTableRowsDeleted(rowIndex, rowIndex);
71  		}
72  	}
73  	
74  	public int getRowCount() {
75  		if (isResponse)
76  			return mockResponse.getAttachmentCount();
77  		else {
78  			try {
79  				// Response may not exist yet and attachments may be null
80  				return mockResponse.getMockResult().getMockRequest().getRequestAttachments().length;
81  			} catch (Exception e) {
82  				return 0;
83  			}
84  		}
85  	}
86  	
87  	public int getColumnCount() {
88  		return isResponse ? 6 : 5;
89  	}
90  	
91  	public Attachment getAttachmentAt( int rowIndex )
92  	{
93  		if (isResponse)
94  			return mockResponse.getAttachmentAt(rowIndex);
95  		else
96  			return mockResponse.getMockResult().getMockRequest().getRequestAttachments()[rowIndex];
97  	}
98  	
99  	public Object getValueAt(int rowIndex, int columnIndex) {
100 		if (rowIndex > getRowCount())
101 			return null;
102 		
103 		Attachment att = null;
104 		if (isResponse)
105 			att = mockResponse.getAttachmentAt(rowIndex);
106 		else
107 			att = mockResponse.getMockResult().getMockRequest().getRequestAttachments()[rowIndex];
108 		
109 		switch (columnIndex) {
110 			case 0:
111 				return att.getName();
112 			case 1:
113 				return att.getContentType();
114 			case 2:
115 				return att.getSize();
116 			case 3:
117 				return att.getPart();
118 			case 4:
119 				return att.getAttachmentType();
120 			case 5:
121 				return att.getContentID();
122 			default:
123 				return null;
124 		}
125 	}
126 	
127 	public int findColumn(String columnName) {
128 		if (columnName.equals("Name"))
129 			return 0;
130 		else if (columnName.equals("Content type"))
131 			return 1;
132 		else if (columnName.equals("Size"))
133 			return 2;
134 		else if (columnName.equals("Part"))
135 			return 3;
136 		else if (columnName.equals("Type"))
137 			return 4;
138 		
139 		return -1;
140 	}
141 	
142 	public String getColumnName(int column) {
143 		if (column == 0)
144 			return "Name";
145 		else if (column == 1)
146 			return "Content type";
147 		else if (column == 2)
148 			return "Size";
149 		else if (column == 3)
150 			return "Part";
151 		else if (column == 4)
152 			return "Type";
153 		else if (column == 5)
154 			return "ContentID";
155 		else
156 			return null;
157 	}
158 	
159 	public boolean isCellEditable(int rowIndex, int columnIndex) {
160 		return isResponse && (columnIndex == 1 || columnIndex == 3 || columnIndex == 5 );
161 	}
162 	
163 	public void setValueAt(Object aValue, int rowIndex, int columnIndex)
164 	{
165 		if( !isResponse )
166 			return;
167 		
168 		WsdlAttachment att = ( WsdlAttachment ) mockResponse.getAttachmentAt(rowIndex);
169 		if( columnIndex == 1 )
170 			att.setContentType( (String)aValue );
171 		else if( columnIndex == 3 )
172 			att.setPart( (String)aValue );
173 		else if( columnIndex == 5 )
174 			att.setContentID( (String)aValue );
175 		
176 		fireTableRowsUpdated( rowIndex, rowIndex );
177 	}
178 
179 	/***
180 	 * Update table when attachments or response changes
181 	 */
182 	
183 	public void propertyChange(PropertyChangeEvent evt)
184 	{
185 		fireTableDataChanged();
186 	}
187 	
188 }