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.mock.WsdlMockResponse;
024: import com.eviware.soapui.impl.wsdl.support.WsdlAttachment;
025: import com.eviware.soapui.model.iface.Attachment;
026:
027: /**
028: * TableModel for MockAttachments in a WsdlMockResponse
029: *
030: * @author ole.matzura
031: */
032:
033: public class MockAttachmentTableModel extends AbstractTableModel
034: implements PropertyChangeListener, AttachmentTableModel {
035:
036: private boolean isResponse = false;
037: private WsdlMockResponse mockResponse;
038:
039: /** Creates a new instance of AttachmentTableModel */
040: public MockAttachmentTableModel(WsdlMockResponse mockOperation2,
041: boolean isResponse) {
042: this .mockResponse = mockOperation2;
043: this .isResponse = isResponse;
044:
045: mockResponse.addPropertyChangeListener(this );
046: }
047:
048: public void release() {
049: mockResponse.removePropertyChangeListener(this );
050: }
051:
052: public void addFile(File file, boolean cacheInRequest)
053: throws IOException {
054: if (isResponse) {
055: Attachment attachment = mockResponse.attachFile(file,
056: cacheInRequest);
057: attachment.setContentType(ContentTypeHandler
058: .getContentTypeFromFilename(file.getName()));
059: this .fireTableRowsInserted(mockResponse
060: .getAttachmentCount(), mockResponse
061: .getAttachmentCount());
062: }
063: }
064:
065: public void removeAttachment(int[] rowIndexes) {
066: Arrays.sort(rowIndexes);
067: for (int i = rowIndexes.length - 1; i >= 0; i--)
068: removeAttachment(rowIndexes[i]);
069: }
070:
071: public void removeAttachment(int rowIndex) {
072: if (isResponse) {
073: mockResponse.removeAttachment(mockResponse
074: .getAttachmentAt(rowIndex));
075: this .fireTableRowsDeleted(rowIndex, rowIndex);
076: }
077: }
078:
079: public int getRowCount() {
080: if (isResponse)
081: return mockResponse.getAttachmentCount();
082: else {
083: try {
084: // Response may not exist yet and attachments may be null
085: return mockResponse.getMockResult().getMockRequest()
086: .getRequestAttachments().length;
087: } catch (Exception e) {
088: return 0;
089: }
090: }
091: }
092:
093: public int getColumnCount() {
094: return isResponse ? 6 : 5;
095: }
096:
097: public Attachment getAttachmentAt(int rowIndex) {
098: if (isResponse)
099: return mockResponse.getAttachmentAt(rowIndex);
100: else
101: return mockResponse.getMockResult().getMockRequest()
102: .getRequestAttachments()[rowIndex];
103: }
104:
105: public Object getValueAt(int rowIndex, int columnIndex) {
106: if (rowIndex > getRowCount())
107: return null;
108:
109: Attachment att = null;
110: if (isResponse)
111: att = mockResponse.getAttachmentAt(rowIndex);
112: else
113: att = mockResponse.getMockResult().getMockRequest()
114: .getRequestAttachments()[rowIndex];
115:
116: switch (columnIndex) {
117: case 0:
118: return att.getName();
119: case 1:
120: return att.getContentType();
121: case 2:
122: return att.getSize();
123: case 3:
124: return att.getPart();
125: case 4:
126: return att.getAttachmentType();
127: case 5:
128: return att.getContentID();
129: default:
130: return null;
131: }
132: }
133:
134: public int findColumn(String columnName) {
135: if (columnName.equals("Name"))
136: return 0;
137: else if (columnName.equals("Content type"))
138: return 1;
139: else if (columnName.equals("Size"))
140: return 2;
141: else if (columnName.equals("Part"))
142: return 3;
143: else if (columnName.equals("Type"))
144: return 4;
145:
146: return -1;
147: }
148:
149: public String getColumnName(int column) {
150: if (column == 0)
151: return "Name";
152: else if (column == 1)
153: return "Content type";
154: else if (column == 2)
155: return "Size";
156: else if (column == 3)
157: return "Part";
158: else if (column == 4)
159: return "Type";
160: else if (column == 5)
161: return "ContentID";
162: else
163: return null;
164: }
165:
166: public boolean isCellEditable(int rowIndex, int columnIndex) {
167: return isResponse
168: && (columnIndex == 1 || columnIndex == 3 || columnIndex == 5);
169: }
170:
171: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
172: if (!isResponse)
173: return;
174:
175: WsdlAttachment att = (WsdlAttachment) mockResponse
176: .getAttachmentAt(rowIndex);
177: if (columnIndex == 1)
178: att.setContentType((String) aValue);
179: else if (columnIndex == 3)
180: att.setPart((String) aValue);
181: else if (columnIndex == 5)
182: att.setContentID((String) aValue);
183:
184: fireTableRowsUpdated(rowIndex, rowIndex);
185: }
186:
187: /**
188: * Update table when attachments or response changes
189: */
190:
191: public void propertyChange(PropertyChangeEvent evt) {
192: fireTableDataChanged();
193: }
194:
195: }
|