01: /*
02: * soapUI, copyright (C) 2004-2007 eviware.com
03: *
04: * soapUI is free software; you can redistribute it and/or modify it under the
05: * terms of version 2.1 of the GNU Lesser General Public License as published by
06: * the Free Software Foundation.
07: *
08: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
09: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: * See the GNU Lesser General Public License for more details at gnu.org.
11: */
12:
13: package com.eviware.soapui.impl.wsdl.panels.attachments;
14:
15: import java.awt.datatransfer.DataFlavor;
16: import java.awt.datatransfer.Transferable;
17: import java.awt.datatransfer.UnsupportedFlavorException;
18: import java.io.File;
19: import java.io.IOException;
20: import java.util.List;
21:
22: import javax.swing.JComponent;
23: import javax.swing.TransferHandler;
24:
25: import com.eviware.soapui.SoapUI;
26: import com.eviware.soapui.support.UISupport;
27:
28: /**
29: * Handles drop of files on the AttachmentPanel
30: *
31: * @author emibre
32: */
33:
34: public class FileTransferHandler extends TransferHandler {
35: private DataFlavor fileFlavor;
36: private AttachmentTableModel attachmentModel;
37:
38: /** Creates a new instance of FileTransferHandler */
39: public FileTransferHandler(AttachmentTableModel attachmentModel) {
40: fileFlavor = DataFlavor.javaFileListFlavor;
41: this .attachmentModel = attachmentModel;
42: }
43:
44: public boolean canImport(JComponent c, DataFlavor[] flavors) {
45: return hasFileFlavor(flavors);
46: }
47:
48: private boolean hasFileFlavor(DataFlavor[] flavors) {
49: for (int i = 0; i < flavors.length; i++) {
50: if (fileFlavor.equals(flavors[i])) {
51: return true;
52: }
53: }
54: return false;
55: }
56:
57: @SuppressWarnings("unchecked")
58: public boolean importData(JComponent c, Transferable t) {
59: try {
60: List<File> files = (List<File>) t
61: .getTransferData(fileFlavor);
62: for (File f : files) {
63: System.out.println("Got a file: " + f.getName());
64: Boolean retval = UISupport.confirmOrCancel(
65: "Cache attachment in request?",
66: "Att Attachment");
67: if (retval == null)
68: return false;
69:
70: attachmentModel.addFile(f, retval);
71: }
72:
73: } catch (IOException ex) {
74: SoapUI.logError(ex);
75: } catch (UnsupportedFlavorException ex) {
76: SoapUI.logError(ex);
77: }
78: return false;
79: }
80:
81: public int getSourceActions(JComponent c) {
82: return COPY_OR_MOVE;
83: }
84: }
|