001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.panels.attachments;
014:
015: import java.beans.PropertyChangeEvent;
016: import java.beans.PropertyChangeListener;
017: import java.io.File;
018: import java.io.IOException;
019: import java.util.Arrays;
020:
021: import javax.swing.table.AbstractTableModel;
022:
023: import com.eviware.soapui.impl.wsdl.WsdlRequest;
024: import com.eviware.soapui.impl.wsdl.support.WsdlAttachment;
025: import com.eviware.soapui.model.iface.Attachment;
026:
027: /**
028: * TableModel for Request Attachments
029: *
030: * @author emibre
031: */
032:
033: public class RequestAttachmentTableModel extends AbstractTableModel
034: implements PropertyChangeListener, AttachmentTableModel {
035:
036: WsdlRequest request;
037: boolean isRequest = false;
038:
039: /** Creates a new instance of AttachmentTableModel */
040: public RequestAttachmentTableModel(WsdlRequest request,
041: boolean isRequest) {
042: this .request = request;
043: this .isRequest = isRequest;
044:
045: this .request.addPropertyChangeListener(this );
046: }
047:
048: public void release() {
049: request.removePropertyChangeListener(this );
050: }
051:
052: /*
053: * (non-Javadoc)
054: *
055: * @see com.eviware.soapui.impl.wsdl.panels.attachments.AttachmentTableModel#addFile(java.io.File,
056: * boolean)
057: */
058: public void addFile(File file, boolean cacheInRequest)
059: throws IOException {
060: if (isRequest) {
061: Attachment attachment = request.attachFile(file,
062: cacheInRequest);
063: attachment.setContentType(ContentTypeHandler
064: .getContentTypeFromFilename(file.getName()));
065: }
066:
067: this .fireTableRowsInserted(request.getAttachmentCount(),
068: request.getAttachmentCount());
069: }
070:
071: public void removeAttachment(int[] rowIndexes) {
072: Arrays.sort(rowIndexes);
073: for (int i = rowIndexes.length - 1; i >= 0; i--)
074: removeAttachment(rowIndexes[i]);
075: }
076:
077: public void removeAttachment(int rowIndex) {
078: if (isRequest) {
079: request.removeAttachment(request.getAttachmentAt(rowIndex));
080: this .fireTableRowsDeleted(rowIndex, rowIndex);
081: }
082: }
083:
084: public int getRowCount() {
085: if (isRequest)
086: return request.getAttachmentCount();
087: else {
088: try {
089: // Response may not exist yet and attachments may be null
090: return request.getResponse().getAttachments().length;
091: } catch (Exception e) {
092: return 0;
093: }
094: }
095: }
096:
097: public int getColumnCount() {
098: return isRequest ? 6 : 5;
099: }
100:
101: public Attachment getAttachmentAt(int rowIndex) {
102: if (isRequest)
103: return request.getAttachmentAt(rowIndex);
104: else
105: return request.getResponse().getAttachments()[rowIndex];
106: }
107:
108: public Object getValueAt(int rowIndex, int columnIndex) {
109: if (rowIndex > getRowCount())
110: return null;
111:
112: Attachment att = null;
113: if (isRequest)
114: att = request.getAttachmentAt(rowIndex);
115: else
116: att = request.getResponse().getAttachments()[rowIndex];
117:
118: switch (columnIndex) {
119: case 0:
120: return att.getName();
121: case 1:
122: return att.getContentType();
123: case 2:
124: return att.getSize();
125: case 3:
126: return att.getPart();
127: case 4:
128: return att.getAttachmentType();
129: case 5:
130: return att.getContentID();
131: default:
132: return null;
133: }
134: }
135:
136: public int findColumn(String columnName) {
137: if (columnName.equals("Name"))
138: return 0;
139: else if (columnName.equals("Content type"))
140: return 1;
141: else if (columnName.equals("Size"))
142: return 2;
143: else if (columnName.equals("Part"))
144: return 3;
145: else if (columnName.equals("Type"))
146: return 4;
147:
148: return -1;
149: }
150:
151: public String getColumnName(int column) {
152: if (column == 0)
153: return "Name";
154: else if (column == 1)
155: return "Content type";
156: else if (column == 2)
157: return "Size";
158: else if (column == 3)
159: return "Part";
160: else if (column == 4)
161: return "Type";
162: else if (column == 5)
163: return "ContentID";
164: else
165: return null;
166: }
167:
168: public boolean isCellEditable(int rowIndex, int columnIndex) {
169: return isRequest
170: && (columnIndex == 1 || columnIndex == 3 || columnIndex == 5);
171: }
172:
173: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
174: if (!isRequest)
175: return;
176:
177: WsdlAttachment att = (WsdlAttachment) request
178: .getAttachmentAt(rowIndex);
179: if (columnIndex == 1)
180: att.setContentType((String) aValue);
181: else if (columnIndex == 3)
182: att.setPart((String) aValue);
183: else if (columnIndex == 5)
184: att.setContentID((String) aValue);
185:
186: fireTableRowsUpdated(rowIndex, rowIndex);
187: }
188:
189: /**
190: * Update table when attachments or response changes
191: */
192:
193: public void propertyChange(PropertyChangeEvent evt) {
194: fireTableDataChanged();
195: }
196: }
|