KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.pooka.gui.dnd;
2
3 import javax.swing.*;
4 import java.io.*;
5 import java.util.*;
6 import java.awt.*;
7 import java.awt.datatransfer.*;
8 import java.awt.event.*;
9
10 import net.suberic.pooka.*;
11 import net.suberic.pooka.gui.*;
12
13 /**
14  * A TransferHandler for a MessageProxy object.
15  */

16 public class NewMessageTransferHandler extends TransferHandler {
17   private DataFlavor[] usableDataFlavors = new DataFlavor[] {
18     MessageProxyTransferable.sMessageProxyDataFlavor,
19     DataFlavor.javaFileListFlavor,
20     DataFlavor.stringFlavor
21   };
22
23   public boolean importData(JComponent c, Transferable t) {
24     DataFlavor matchedFlavor = DndUtils.matchDataFlavor(usableDataFlavors, t.getTransferDataFlavors());
25     if (matchedFlavor != null) {
26       if (matchedFlavor == MessageProxyTransferable.sMessageProxyDataFlavor) {
27     return importMessageProxy(c, t);
28       } else if (matchedFlavor == DataFlavor.javaFileListFlavor) {
29     return importFileList(c, t);
30       } else if (matchedFlavor == DataFlavor.stringFlavor) {
31     try {
32       String JavaDoc value = (String JavaDoc) t.getTransferData(DataFlavor.stringFlavor);
33       return importFileList(c, t);
34       
35     } catch (Exception JavaDoc e) {
36       e.printStackTrace();
37     }
38     
39     return false;
40       } else {
41     // weird
42
return false;
43       }
44     } else {
45       return false;
46     }
47   }
48
49   /**
50    * Imports a MessageProxy.
51    */

52   public boolean importMessageProxy(JComponent c, Transferable t) {
53     // this is exactly the same as importing a File, but with some added
54
// checks and such.
55
try {
56       NewMessageDisplayPanel nmdp = (NewMessageDisplayPanel) SwingUtilities.getAncestorOfClass(Class.forName("net.suberic.pooka.gui.NewMessageDisplayPanel"), c);
57       if (nmdp != null) {
58     java.util.List JavaDoc fileList = DndUtils.extractFileList(t);
59     
60     if (fileList != null) {
61       Iterator it = fileList.iterator();
62       while (it.hasNext()) {
63         File f = (File) it.next();
64         if (f != null) {
65           // attach each file.
66
nmdp.getNewMessageProxy().getNewMessageInfo().attachFile(f, "message/rfc822");
67         
68           nmdp.attachmentAdded(nmdp.getNewMessageProxy().getNewMessageInfo().getAttachments().size() -1);
69         }
70       }
71       
72       // we don't want an attachment to delete the message, so set
73
// cutDisallowed on the Proxy.
74
try {
75         MessageProxy mp = (MessageProxy) t.getTransferData(MessageProxyTransferable.sMessageProxyDataFlavor);
76         mp.setCutDisallowed(true);
77         mp.setImportDone(true);
78         if (mp.removeMessageOnCompletion() || mp.getActionType() == MOVE) {
79           DndUtils.clearClipboard(c);
80         }
81         //DndUtils.getClipboard(c).setContents(null, null);
82
} catch (Exception JavaDoc otherExc) {
83         otherExc.printStackTrace();
84         // ignore here.
85
}
86       return true;
87     }
88       }
89     } catch (Exception JavaDoc e) {
90       e.printStackTrace();
91     }
92
93     return false;
94   }
95   
96   /**
97    * Imports a File or list of Files.
98    */

99   public boolean importFileList(JComponent c, Transferable t) {
100     try {
101       NewMessageDisplayPanel nmdp = (NewMessageDisplayPanel) SwingUtilities.getAncestorOfClass(Class.forName("net.suberic.pooka.gui.NewMessageDisplayPanel"), c);
102       if (nmdp != null) {
103     java.util.List JavaDoc fileList = DndUtils.extractFileList(t);
104     
105     if (fileList != null) {
106       Iterator it = fileList.iterator();
107       while (it.hasNext()) {
108         File f = (File) it.next();
109         if (f != null) {
110           nmdp.getNewMessageProxy().getNewMessageInfo().attachFile(f);
111         
112           nmdp.attachmentAdded(nmdp.getNewMessageProxy().getNewMessageInfo().getAttachments().size() -1);
113         }
114       }
115     
116       return true;
117     }
118       }
119     } catch (Exception JavaDoc e) {
120       e.printStackTrace();
121     }
122
123     return false;
124   }
125
126   public boolean canImport(JComponent c, DataFlavor[] flavors) {
127     if (DndUtils.matchDataFlavor(usableDataFlavors, flavors) != null) {
128       return true;
129     } else {
130       return false;
131     }
132   }
133
134   /**
135    * Returns if this is a MessageProxy.
136    */

137   public boolean isMessageProxy(Transferable t) {
138     DataFlavor[] flavors = t.getTransferDataFlavors();
139     if (flavors == null)
140       return false;
141
142     for (int i = 0; i < flavors.length; i++) {
143       if (flavors[i] == MessageProxyTransferable.sMessageProxyDataFlavor)
144     return true;
145     }
146
147     return false;
148   }
149 }
150
Popular Tags