1
2
3
4
5
6
7
8
9
10
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 * TableModel for MockAttachments in a WsdlMockResponse
29 *
30 * @author ole.matzura
31 */
32
33 public class MockAttachmentTableModel extends AbstractTableModel implements PropertyChangeListener, AttachmentTableModel {
34
35 private boolean isResponse = false;
36 private WsdlMockResponse mockResponse;
37
38 /*** Creates a new instance of AttachmentTableModel */
39 public MockAttachmentTableModel(WsdlMockResponse mockOperation2, boolean isResponse) {
40 this.mockResponse = mockOperation2;
41 this.isResponse = isResponse;
42
43 mockResponse.addPropertyChangeListener( this );
44 }
45
46 public void release()
47 {
48 mockResponse.removePropertyChangeListener( this );
49 }
50
51 public void addFile( File file, boolean cacheInRequest) throws IOException
52 {
53 if (isResponse)
54 {
55 Attachment attachment = mockResponse.attachFile( file, cacheInRequest );
56 attachment.setContentType( ContentTypeHandler.getContentTypeFromFilename(file.getName()));
57 this.fireTableRowsInserted(mockResponse.getAttachmentCount(), mockResponse.getAttachmentCount());
58 }
59 }
60
61 public void removeAttachment(int[] rowIndexes) {
62 Arrays.sort(rowIndexes);
63 for (int i = rowIndexes.length-1; i >=0; i--)
64 removeAttachment(rowIndexes[i]);
65 }
66
67 public void removeAttachment(int rowIndex) {
68 if (isResponse)
69 {
70 mockResponse.removeAttachment(mockResponse.getAttachmentAt(rowIndex));
71 this.fireTableRowsDeleted(rowIndex, rowIndex);
72 }
73 }
74
75 public int getRowCount() {
76 if (isResponse)
77 return mockResponse.getAttachmentCount();
78 else {
79 try {
80
81 return mockResponse.getMockResult().getMockRequest().getRequestAttachments().length;
82 } catch (Exception e) {
83 return 0;
84 }
85 }
86 }
87
88 public int getColumnCount() {
89 return isResponse ? 6 : 5;
90 }
91
92 public Attachment getAttachmentAt( int rowIndex )
93 {
94 if (isResponse)
95 return mockResponse.getAttachmentAt(rowIndex);
96 else
97 return mockResponse.getMockResult().getMockRequest().getRequestAttachments()[rowIndex];
98 }
99
100 public Object getValueAt(int rowIndex, int columnIndex) {
101 if (rowIndex > getRowCount())
102 return null;
103
104 Attachment att = null;
105 if (isResponse)
106 att = mockResponse.getAttachmentAt(rowIndex);
107 else
108 att = mockResponse.getMockResult().getMockRequest().getRequestAttachments()[rowIndex];
109
110 switch (columnIndex) {
111 case 0:
112 return att.getName();
113 case 1:
114 return att.getContentType();
115 case 2:
116 return att.getSize();
117 case 3:
118 return att.getPart();
119 case 4:
120 return att.getAttachmentType();
121 case 5:
122 return att.getContentID();
123 default:
124 return null;
125 }
126 }
127
128 public int findColumn(String columnName) {
129 if (columnName.equals("Name"))
130 return 0;
131 else if (columnName.equals("Content type"))
132 return 1;
133 else if (columnName.equals("Size"))
134 return 2;
135 else if (columnName.equals("Part"))
136 return 3;
137 else if (columnName.equals("Type"))
138 return 4;
139
140 return -1;
141 }
142
143 public String getColumnName(int column) {
144 if (column == 0)
145 return "Name";
146 else if (column == 1)
147 return "Content type";
148 else if (column == 2)
149 return "Size";
150 else if (column == 3)
151 return "Part";
152 else if (column == 4)
153 return "Type";
154 else if (column == 5)
155 return "ContentID";
156 else
157 return null;
158 }
159
160 public boolean isCellEditable(int rowIndex, int columnIndex) {
161 return isResponse && (columnIndex == 1 || columnIndex == 3 || columnIndex == 5 );
162 }
163
164 public void setValueAt(Object aValue, int rowIndex, int columnIndex)
165 {
166 if( !isResponse )
167 return;
168
169 WsdlAttachment att = ( WsdlAttachment ) mockResponse.getAttachmentAt(rowIndex);
170 if( columnIndex == 1 )
171 att.setContentType( (String)aValue );
172 else if( columnIndex == 3 )
173 att.setPart( (String)aValue );
174 else if( columnIndex == 5 )
175 att.setContentID( (String)aValue );
176
177 fireTableRowsUpdated( rowIndex, rowIndex );
178 }
179
180 /***
181 * Update table when attachments or response changes
182 */
183
184 public void propertyChange(PropertyChangeEvent evt)
185 {
186 fireTableDataChanged();
187 }
188
189 }