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.awt.Component;
016: import java.awt.Toolkit;
017: import java.awt.datatransfer.DataFlavor;
018: import java.awt.datatransfer.Transferable;
019: import java.awt.dnd.DnDConstants;
020: import java.awt.dnd.DropTarget;
021: import java.awt.dnd.DropTargetDragEvent;
022: import java.awt.dnd.DropTargetDropEvent;
023: import java.awt.dnd.DropTargetEvent;
024: import java.awt.dnd.DropTargetListener;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.MouseAdapter;
027: import java.awt.event.MouseEvent;
028: import java.beans.PropertyChangeEvent;
029: import java.beans.PropertyChangeListener;
030: import java.io.File;
031: import java.io.FileOutputStream;
032: import java.io.IOException;
033: import java.util.List;
034:
035: import javax.swing.AbstractListModel;
036: import javax.swing.ComboBoxModel;
037: import javax.swing.DefaultCellEditor;
038: import javax.swing.JButton;
039: import javax.swing.JComboBox;
040: import javax.swing.JFileChooser;
041: import javax.swing.JTable;
042: import javax.swing.event.ListSelectionEvent;
043: import javax.swing.event.ListSelectionListener;
044:
045: import com.eviware.soapui.SoapUI;
046: import com.eviware.soapui.impl.wsdl.WsdlAttachmentPart;
047: import com.eviware.soapui.impl.wsdl.WsdlRequest;
048: import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
049: import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
050: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
051: import com.eviware.soapui.model.iface.Attachment;
052: import com.eviware.soapui.support.Tools;
053: import com.eviware.soapui.support.UISupport;
054:
055: /**
056: * AttachmentPanel for WsdlMockResponse
057: *
058: * @author ole.matzura
059: */
060:
061: public class MockAttachmentPanel extends javax.swing.JPanel {
062: private DropTarget dropTarget;
063: private FileTransferHandler fileTransferHandler;
064: private MockAttachmentTableModel tableModel;
065: private JFileChooser fc;
066: private final WsdlMockResponse mockOperation;
067: boolean allowChange = false;
068: boolean isResponse = false;
069: private JButton exportBtn;
070:
071: /** Creates new form FileTableList */
072: public MockAttachmentPanel(WsdlMockResponse response,
073: boolean isResponse) {
074: this .mockOperation = response;
075: this .allowChange = isResponse;
076: this .isResponse = isResponse;
077: initComponents();
078: initFileTransfer();
079: }
080:
081: public void release() {
082: tableModel.release();
083: if (attachmentPartCellEditor != null)
084: attachmentPartCellEditor.release();
085: }
086:
087: private void initFileTransfer() {
088: if (allowChange) {
089: fileTransferHandler = new FileTransferHandler(tableModel);
090: fileTable.setDragEnabled(true);
091: fileTable.setTransferHandler(fileTransferHandler);
092:
093: dropTarget = new DropTarget();
094: dropTarget.setActive(true);
095: try {
096: dropTarget
097: .addDropTargetListener(new DropTargetListener() {
098: public void dragEnter(
099: DropTargetDragEvent dtde) {
100: }
101:
102: public void dragExit(DropTargetEvent dte) {
103: }
104:
105: public void dragOver(
106: DropTargetDragEvent dtde) {
107: }
108:
109: @SuppressWarnings("unchecked")
110: public void drop(DropTargetDropEvent dtde) {
111: try {
112: dtde
113: .acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
114: Transferable trans = dtde
115: .getTransferable();
116: List<File> files = (List<File>) trans
117: .getTransferData(DataFlavor.javaFileListFlavor);
118: for (File f : files) {
119: System.out
120: .println("Dropping file: "
121: + f.getName());
122:
123: Boolean retval = UISupport
124: .confirmOrCancel(
125: "Cache attachment in response?",
126: "Att Attachment");
127: if (retval == null)
128: return;
129:
130: tableModel.addFile(f, retval);
131: }
132:
133: } catch (Exception e) {
134: SoapUI.logError(e);
135: }
136: }
137:
138: public void dropActionChanged(
139: DropTargetDragEvent dtde) {
140: }
141: });
142: } catch (Exception e) {
143: SoapUI.logError(e);
144: }
145:
146: jScrollPane1.getViewport().setDropTarget(dropTarget);
147: }
148: }
149:
150: private void initComponents() {
151: jScrollPane1 = new javax.swing.JScrollPane();
152: tableModel = new MockAttachmentTableModel(mockOperation,
153: isResponse);
154: fileTable = new JTable(tableModel);
155:
156: if (isResponse) {
157: attachmentPartCellEditor = new AttachmentPartCellEditor();
158: fileTable.getColumnModel().getColumn(3).setCellEditor(
159: attachmentPartCellEditor);
160: }
161:
162: setLayout(new java.awt.BorderLayout());
163: jScrollPane1.setViewportView(fileTable);
164:
165: add(jScrollPane1, java.awt.BorderLayout.CENTER);
166:
167: jPanel1 = new javax.swing.JPanel();
168:
169: if (allowChange) {
170: addFileBtn = new javax.swing.JButton();
171: removeBtn = new javax.swing.JButton();
172:
173: addFileBtn.setText("Add file");
174: addFileBtn
175: .addActionListener(new java.awt.event.ActionListener() {
176: public void actionPerformed(
177: java.awt.event.ActionEvent evt) {
178: addFileBtnActionPerformed(evt);
179: }
180: });
181:
182: jPanel1.add(addFileBtn);
183:
184: removeBtn.setText("Remove selected");
185: removeBtn.setEnabled(false);
186: removeBtn
187: .addActionListener(new java.awt.event.ActionListener() {
188: public void actionPerformed(
189: java.awt.event.ActionEvent evt) {
190: removeBtnActionPerformed(evt);
191: }
192: });
193:
194: jPanel1.add(removeBtn);
195: }
196:
197: exportBtn = new javax.swing.JButton();
198: exportBtn.setText("Export selected");
199: exportBtn.setEnabled(false);
200: exportBtn
201: .addActionListener(new java.awt.event.ActionListener() {
202: public void actionPerformed(
203: java.awt.event.ActionEvent evt) {
204: exportBtnActionPerformed(evt);
205: }
206: });
207:
208: jPanel1.add(exportBtn);
209: jPanel1.add(new JButton(new ShowOnlineHelpAction(
210: HelpUrls.ATTACHMENTS_HELP_URL)));
211: add(jPanel1, java.awt.BorderLayout.SOUTH);
212:
213: fileTable.getSelectionModel().addListSelectionListener(
214: new ListSelectionListener() {
215: public void valueChanged(ListSelectionEvent e) {
216: if (removeBtn != null)
217: removeBtn.setEnabled(fileTable
218: .getSelectedRowCount() > 0);
219:
220: exportBtn.setEnabled(fileTable
221: .getSelectedRowCount() > 0);
222: }
223: });
224:
225: fileTable.addMouseListener(new MouseAdapter() {
226: public void mouseClicked(MouseEvent e) {
227: if (e.getClickCount() < 2)
228: return;
229:
230: int ix = fileTable.getSelectedRow();
231: if (ix == -1)
232: return;
233:
234: Attachment attachment = isResponse ? mockOperation
235: .getAttachments()[ix] : mockOperation
236: .getMockResult().getMockRequest()
237: .getRequestAttachments()[ix];
238: String url = attachment.getUrl();
239: if (url != null) {
240: Tools.openURL(url);
241: } else {
242: Toolkit.getDefaultToolkit().beep();
243: }
244: }
245: });
246: }
247:
248: protected void exportBtnActionPerformed(ActionEvent evt) {
249: File file = UISupport.getFileDialogs().saveAs(this ,
250: "Export Attachment..");
251: while (file != null
252: && file.exists()
253: && !UISupport.confirm("File " + file.getName()
254: + " exists, overwrite?", "Export Attachment")) {
255: file = UISupport.getFileDialogs().saveAs(this ,
256: "Export Attachment..");
257: }
258:
259: if (file != null) {
260: Attachment attachment = tableModel
261: .getAttachmentAt(fileTable.getSelectedRow());
262: try {
263: FileOutputStream out = new FileOutputStream(file);
264: long total = Tools.writeAll(out, attachment
265: .getInputStream());
266: UISupport.showInfoMessage("Written [" + total
267: + "] bytes to " + file.getName());
268: } catch (Exception e) {
269: UISupport.showErrorMessage(e);
270: }
271: }
272: }
273:
274: private void addFileBtnActionPerformed(
275: java.awt.event.ActionEvent evt) {// GEN-FIRST:event_addFileBtnActionPerformed
276: if (fc == null)
277: fc = new JFileChooser();
278:
279: int returnVal = fc.showOpenDialog(this );
280:
281: if (returnVal == JFileChooser.APPROVE_OPTION) {
282: File file = fc.getSelectedFile();
283: Boolean retval = UISupport.confirmOrCancel(
284: "Cache attachment in request?", "Att Attachment");
285: if (retval == null)
286: return;
287: try {
288: tableModel.addFile(file, retval);
289: } catch (IOException e) {
290: UISupport.showErrorMessage(e);
291: }
292: } else {
293: System.out.println("Open command cancelled by user.");
294: }
295: }// GEN-LAST:event_addFileBtnActionPerformed
296:
297: private void removeBtnActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_removeBtnActionPerformed
298: if (UISupport.confirm("Remove selected attachments?",
299: "Remove Attachments"))
300: tableModel.removeAttachment(fileTable.getSelectedRows());
301: }// GEN-LAST:event_removeBtnActionPerformed
302:
303: // Variables declaration - do not modify//GEN-BEGIN:variables
304: private javax.swing.JButton addFileBtn;
305: private JTable fileTable;
306: private javax.swing.JPanel jPanel1;
307: private javax.swing.JScrollPane jScrollPane1;
308: private javax.swing.JButton removeBtn;
309: private AttachmentPartCellEditor attachmentPartCellEditor;
310:
311: // End of variables declaration//GEN-END:variables
312:
313: private class AttachmentPartCellEditor extends DefaultCellEditor {
314: public AttachmentPartCellEditor() {
315: super (new JComboBox(new PartsComboBoxModel()));
316: }
317:
318: public void release() {
319: ((PartsComboBoxModel) ((JComboBox) editorComponent)
320: .getModel()).release();
321: }
322:
323: public Component getTableCellEditorComponent(JTable table,
324: Object value, boolean isSelected, int row, int column) {
325: ((PartsComboBoxModel) ((JComboBox) editorComponent)
326: .getModel()).init(tableModel.getAttachmentAt(row));
327: return super .getTableCellEditorComponent(table, value,
328: isSelected, row, column);
329: }
330: }
331:
332: private final class PartsComboBoxModel extends AbstractListModel
333: implements ComboBoxModel, PropertyChangeListener {
334: private Attachment attachment;
335: private WsdlAttachmentPart[] parts;
336:
337: public PartsComboBoxModel() {
338: mockOperation.addPropertyChangeListener(this );
339: }
340:
341: public void release() {
342: mockOperation.removePropertyChangeListener(this );
343: }
344:
345: public void init(Attachment attachment) {
346: System.out.println("Initializing parts..");
347: this .attachment = attachment;
348: parts = mockOperation.getDefinedAttachmentParts();
349: }
350:
351: public Object getElementAt(int index) {
352: return parts == null ? null : parts[index].getName();
353: }
354:
355: public int getSize() {
356: return parts == null ? 0 : parts.length;
357: }
358:
359: public Object getSelectedItem() {
360: return attachment == null ? null : attachment.getPart();
361: }
362:
363: public void setSelectedItem(Object anItem) {
364: if (attachment != null)
365: attachment.setPart((String) anItem);
366: }
367:
368: public void propertyChange(PropertyChangeEvent arg0) {
369: if (arg0.getPropertyName().equals(
370: WsdlRequest.ATTACHMENTS_PROPERTY)) {
371: // delete our current one?
372: if (arg0.getOldValue() == attachment
373: && arg0.getNewValue() == null) {
374: attachment = null;
375: parts = null;
376: }
377: }
378: }
379: }
380: }
|