01: //The contents of this file are subject to the Mozilla Public License Version 1.1
02: //(the "License"); you may not use this file except in compliance with the
03: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
04: //
05: //Software distributed under the License is distributed on an "AS IS" basis,
06: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
07: //for the specific language governing rights and
08: //limitations under the License.
09: //
10: //The Original Code is "The Columba Project"
11: //
12: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14: //
15: //All Rights Reserved.
16: package org.columba.mail.gui.composer;
17:
18: import java.awt.datatransfer.DataFlavor;
19: import java.awt.datatransfer.Transferable;
20: import java.awt.datatransfer.UnsupportedFlavorException;
21: import java.io.File;
22: import java.io.IOException;
23: import java.util.Iterator;
24: import java.util.List;
25: import java.util.logging.Logger;
26:
27: import javax.swing.JComponent;
28: import javax.swing.TransferHandler;
29:
30: /**
31: * TransferHandler to import attachments to an email.
32: * This transfer handler can only import file lists from the file system.
33: * This transfer handler is a not 100% standard way of implementing a TransferHandle,
34: * and should only be used by components in the composer frame.
35: * @author redsolo
36: */
37: class ComposerAttachmentTransferHandler extends TransferHandler {
38:
39: private AttachmentController attachmentController;
40:
41: private static final Logger LOG = Logger
42: .getLogger("org.columba.mail.gui.composer");
43:
44: /**
45: * @param controller the attachment controller to add stuff into.
46: */
47: public ComposerAttachmentTransferHandler(
48: AttachmentController controller) {
49: attachmentController = controller;
50: }
51:
52: /**
53: * Returns true if the one of the flavors is the @link DataFlavor#javaFileListFlavor.
54: * @param comp component that is queried if it can import one of the flavors.
55: * @param transferFlavors data flavors that the DnD action supports.
56: * @return true if the one of the flavors is the javaFileListFlavor data flavor.
57: */
58: public boolean canImport(JComponent comp,
59: DataFlavor[] transferFlavors) {
60: boolean canImport = false;
61: for (int i = 0; (i < transferFlavors.length) && (!canImport); i++) {
62: if (transferFlavors[i]
63: .equals(DataFlavor.javaFileListFlavor)) {
64: canImport = true;
65: }
66: }
67: return canImport;
68: }
69:
70: /**
71: * Imports files into the Attachment controller.
72: * @param comp the component that is importing the data.
73: * @param data the data.
74: * @return if the files were added to the attachment controller; false otherwise.
75: */
76: public boolean importData(JComponent comp, Transferable data) {
77: boolean dataWasImported = false;
78:
79: try {
80: List files = (List) data
81: .getTransferData(DataFlavor.javaFileListFlavor);
82: for (Iterator iterator = files.iterator(); iterator
83: .hasNext();) {
84:
85: attachmentController.addFileAttachment((File) iterator
86: .next());
87: }
88: dataWasImported = true;
89: } catch (UnsupportedFlavorException e) {
90: LOG
91: .warning("A transferable with unsupported flavors tried to import data into the attachment gui.");
92: } catch (IOException e) {
93: LOG
94: .warning("The data that was DnD into the attachment was no longer available.");
95: }
96: return dataWasImported;
97: }
98: }
|