KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
8
9 import java.io.*;
10
11 /**
12  * A Transferable version of a MessageProxy.
13  *
14  * Note that, since this class requires that the object not get deleted until
15  * a paste is done, we implement a system which delays the removal until
16  * after the full transfer is completed. See <code>importDone</code> and
17  * <code>actionType</code>.
18  */

19 public class MessageProxyTransferable implements Transferable {
20
21   // DataFlavor information
22
public static DataFlavor sMessageProxyDataFlavor = null;
23   static {
24     try {
25       sMessageProxyDataFlavor = new DataFlavor(Class.forName("net.suberic.pooka.gui.MessageProxy"), "MessageProxy");
26     } catch (Exception JavaDoc e) {
27       e.printStackTrace();
28     }
29   }
30
31   MessageProxy mMessageProxy = null;
32   int mActionType = javax.swing.TransferHandler.COPY;
33   boolean mImportDone = false;
34   
35   public MessageProxyTransferable(MessageProxy pMessageProxy) {
36     setMessageProxy(pMessageProxy);
37   }
38   
39   public DataFlavor[] getTransferDataFlavors() {
40     return new DataFlavor[] {
41       sMessageProxyDataFlavor
42     };
43   }
44
45   public boolean isDataFlavorSupported(DataFlavor flavor) {
46     if (flavor == sMessageProxyDataFlavor)
47       return true;
48     else
49       return false;
50   }
51
52   public Object JavaDoc getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
53     if (flavor == sMessageProxyDataFlavor) {
54       return mMessageProxy;
55       /*
56     } else if (flavor != null && flavor.isFlavorJavaFileListType()) {
57       ArrayList returnValue = new ArrayList();
58       MessageInfo info = mMessageProxy.getMessageInfo();
59       if (info instanceof MultiMessageInfo) {
60     MultiMessageInfo multi = (MultiMessageInfo) info;
61     for (int i = 0; i < multi.getMessageCount(); i++) {
62       returnValue.add(extractMessageInfo(multi.getMessageInfo(i)));
63     }
64       } else {
65     returnValue.add(extractMessageInfo(info));
66       }
67
68       return returnValue;
69       */

70     } else {
71       throw new UnsupportedFlavorException(flavor);
72     }
73   }
74
75   /**
76    * Extracts the given MessageInfo into a file.
77    */

78   private File extractMessageInfo(MessageInfo info) throws java.io.IOException JavaDoc {
79     try {
80       String JavaDoc name = "pooka_message";
81       try {
82     javax.mail.Message JavaDoc m = info.getMessage();
83     if (m instanceof net.suberic.pooka.UIDMimeMessage) {
84       name = "message_" + ((net.suberic.pooka.UIDMimeMessage) m).getUID() ;
85     } else {
86       name = "message_" + m.getMessageNumber();
87     }
88       } catch (Exception JavaDoc me) {
89     // ignore.
90
}
91       File f = DndUtils.createTemporaryFile(name);
92       info.saveMessageAs(f);
93       return f;
94     } catch (javax.mail.MessagingException JavaDoc me) {
95       IOException ioe = new IOException("Error saving file");
96       ioe.initCause(me);
97       throw ioe;
98     }
99   }
100
101   /**
102    * Returns the MessageProxy.
103    */

104   public MessageProxy getMessageProxy() {
105     return mMessageProxy;
106   }
107  
108   /**
109    * Sets the MessageProxy.
110    */

111   public void setMessageProxy(MessageProxy pMessageProxy) {
112     mMessageProxy = pMessageProxy;
113   }
114
115 }
116
Popular Tags