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