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 ; 8 import java.io.File ; 9 10 public class AttachmentTransferable implements Transferable { 11 12 Attachment mAttachment = null; 13 MessageProxy mMessageProxy = null; 14 File mTmpFile = null; 15 boolean mFileWritten = false; 16 17 public AttachmentTransferable(Attachment pAttachment, MessageProxy pMessageProxy) throws java.io.IOException { 18 setAttachment(pAttachment); 19 setMessageProxy(pMessageProxy); 20 22 String 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 getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { 43 if (isDataFlavorSupported(flavor)) { 44 45 java.util.LinkedList list = new java.util.LinkedList (); 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 62 public void writeFile() { 63 AttachmentHandler handler = new AttachmentHandler(mMessageProxy); 64 try { 65 handler.saveFileAs(mAttachment, mTmpFile); 66 } catch (IOException exc) { 67 handler.showError(Pooka.getProperty("error.SaveFile", "Error saving file") + ":\n", Pooka.getProperty("error.SaveFile", "Error saving file"), exc); 68 } 69 } 70 71 74 public Attachment getAttachment() { 75 return mAttachment; 76 } 77 78 81 public void setAttachment(Attachment pAttachment) { 82 mAttachment = pAttachment; 83 } 84 85 88 public MessageProxy getMessageProxy() { 89 return mMessageProxy; 90 } 91 92 95 public void setMessageProxy(MessageProxy pMessageProxy) { 96 mMessageProxy = pMessageProxy; 97 } 98 99 100 } 101 | Popular Tags |