KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > dnd > AttachmentTransferable


1 package net.suberic.pooka.gui.dnd;
2
3 import net.suberic.pooka.*;
4 import net.suberic.pooka.gui.*;
5
6 import java.awt.datatransfer.*;
7 import java.io.IOException JavaDoc;
8 import java.io.File JavaDoc;
9
10 public class AttachmentTransferable implements Transferable {
11   
12   Attachment mAttachment = null;
13   MessageProxy mMessageProxy = null;
14   File JavaDoc mTmpFile = null;
15   boolean mFileWritten = false;
16
17   public AttachmentTransferable(Attachment pAttachment, MessageProxy pMessageProxy) throws java.io.IOException JavaDoc {
18     setAttachment(pAttachment);
19     setMessageProxy(pMessageProxy);
20     // create a temp file in the tmp directory.
21

22     String JavaDoc filename = pAttachment.getName();
23     if (filename == null || filename.length() == 0) {
24       filename = "Attachment";
25     }
26     mTmpFile = DndUtils.createTemporaryFile(filename);
27   }
28
29   public DataFlavor[] getTransferDataFlavors() {
30     return new DataFlavor[] {
31       DataFlavor.javaFileListFlavor
32     };
33   }
34
35   public boolean isDataFlavorSupported(DataFlavor flavor) {
36     if (flavor == DataFlavor.javaFileListFlavor)
37       return true;
38     else
39       return false;
40   }
41
42   public Object JavaDoc getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException JavaDoc {
43     if (isDataFlavorSupported(flavor)) {
44       
45       java.util.LinkedList JavaDoc list = new java.util.LinkedList JavaDoc();
46
47       list.add(mTmpFile);
48
49       if (! mFileWritten) {
50     writeFile();
51     mFileWritten = true;
52       }
53       return list;
54     } else {
55       throw new UnsupportedFlavorException(flavor);
56     }
57   }
58
59   /**
60    * Writes the File object for this attachment.
61    */

62   public void writeFile() {
63     AttachmentHandler handler = new AttachmentHandler(mMessageProxy);
64     try {
65       handler.saveFileAs(mAttachment, mTmpFile);
66     } catch (IOException JavaDoc exc) {
67       handler.showError(Pooka.getProperty("error.SaveFile", "Error saving file") + ":\n", Pooka.getProperty("error.SaveFile", "Error saving file"), exc);
68     }
69   }
70
71   /**
72    * Returns the Attachment.
73    */

74   public Attachment getAttachment() {
75     return mAttachment;
76   }
77  
78   /**
79    * Sets the Attachment.
80    */

81   public void setAttachment(Attachment pAttachment) {
82     mAttachment = pAttachment;
83   }
84
85   /**
86    * Returns the MessageProxy.
87    */

88   public MessageProxy getMessageProxy() {
89     return mMessageProxy;
90   }
91   
92   /**
93    * Sets the MessageProxy.
94    */

95   public void setMessageProxy(MessageProxy pMessageProxy) {
96     mMessageProxy = pMessageProxy;
97   }
98   
99   
100 }
101
Popular Tags