KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > composer > ComposerAttachmentTransferHandler


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
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 JavaDoc;
19 import java.awt.datatransfer.Transferable JavaDoc;
20 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.TransferHandler JavaDoc;
29
30
31 /**
32  * TransferHandler to import attachments to an email.
33  * This transfer handler can only import file lists from the file system.
34  * This transfer handler is a not 100% standard way of implementing a TransferHandle,
35  * and should only be used by components in the composer frame.
36  * @author redsolo
37  */

38  class ComposerAttachmentTransferHandler extends TransferHandler JavaDoc {
39
40     private AttachmentController attachmentController;
41
42     private static final Logger JavaDoc LOG = Logger.getLogger("org.columba.mail.gui.composer");
43
44     /**
45      * @param controller the attachment controller to add stuff into.
46      */

47     public ComposerAttachmentTransferHandler(AttachmentController controller) {
48         attachmentController = controller;
49     }
50
51     /**
52      * Returns true if the one of the flavors is the @link DataFlavor#javaFileListFlavor.
53      * @param comp component that is queried if it can import one of the flavors.
54      * @param transferFlavors data flavors that the DnD action supports.
55      * @return true if the one of the flavors is the javaFileListFlavor data flavor.
56      */

57     public boolean canImport(JComponent JavaDoc comp, DataFlavor JavaDoc[] transferFlavors) {
58         boolean canImport = false;
59         for (int i = 0; (i < transferFlavors.length) && (!canImport); i++) {
60             if (transferFlavors[i].equals(DataFlavor.javaFileListFlavor)) {
61                 canImport = true;
62             }
63         }
64         return canImport;
65     }
66
67     /**
68      * Imports files into the Attachment controller.
69      * @param comp the component that is importing the data.
70      * @param data the data.
71      * @return if the files were added to the attachment controller; false otherwise.
72      */

73     public boolean importData(JComponent JavaDoc comp, Transferable JavaDoc data) {
74         boolean dataWasImported = false;
75
76         try {
77             List JavaDoc files = (List JavaDoc) data.getTransferData(DataFlavor.javaFileListFlavor);
78             for (Iterator JavaDoc iterator = files.iterator(); iterator.hasNext();) {
79
80                 attachmentController.addFileAttachment((File JavaDoc) iterator.next());
81             }
82             dataWasImported = true;
83         } catch (UnsupportedFlavorException JavaDoc e) {
84             LOG.warning("A transferable with unsupported flavors tried to import data into the attachment gui.");
85         } catch (IOException JavaDoc e) {
86             LOG.warning("The data that was DnD into the attachment was no longer available.");
87         }
88         return dataWasImported;
89     }
90 }
91
Popular Tags