KickJava   Java API By Example, From Geeks To Geeks.

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


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 Folder.
15  */

16 public class FolderTransferHandler extends TransferHandler {
17
18   static DataFlavor[] acceptableFlavors = new DataFlavor[] {
19     MessageProxyTransferable.sMessageProxyDataFlavor
20   };
21
22   public boolean importData(JComponent c, Transferable t) {
23     if (!canImport(c, t.getTransferDataFlavors())) {
24       return false;
25     } else {
26       FolderInfo fi = DndUtils.getFolderInfo(c);
27       if (fi != null) {
28     MessageProxy mp = null;
29     try {
30       mp = (MessageProxy) t.getTransferData(MessageProxyTransferable.sMessageProxyDataFlavor);
31       if (mp != null) {
32         // check to see if we're moving to the same folder. if so,
33
// don't allow it.
34
if (mp.getFolderInfo() == fi) {
35           return false;
36         }
37         mp.getMessageInfo().copyMessage(fi);
38         mp.setImportDone(true);
39         if (mp.removeMessageOnCompletion()) {
40           DndUtils.clearClipboard(c);
41         }
42         return true;
43       }
44     } catch (Exception JavaDoc e) {
45       if (mp != null)
46         mp.showError( Pooka.getProperty("error.Message.CopyErrorMessage", "Error: could not copy messages to folder: ") + fi.toString() +"\n", e);
47       if (Pooka.isDebug())
48         e.printStackTrace();
49       return false;
50     }
51       } else {
52     return false;
53       }
54     }
55     
56     return false;
57   }
58   
59   protected Transferable createTransferable(JComponent c) {
60     if (c instanceof net.suberic.pooka.gui.FolderDisplayPanel) {
61       return new MessageProxyTransferable(((FolderDisplayPanel) c).getSelectedMessage());
62     } else if (c instanceof JTable) {
63       try {
64     Object JavaDoc o = SwingUtilities.getAncestorOfClass(Class.forName("net.suberic.pooka.gui.FolderDisplayPanel"), c);
65     if (o != null ) {
66       
67       Transferable returnValue = new MessageProxyTransferable(((FolderDisplayPanel) o).getSelectedMessage());
68       return returnValue;
69     } else {
70       return null;
71     }
72       } catch (Exception JavaDoc e) {
73     return null;
74       }
75     } else {
76       return null;
77     }
78   }
79
80   public int getSourceActions(JComponent c) {
81     return COPY_OR_MOVE;
82   }
83
84   protected void exportDone(JComponent c, Transferable data, int action) {
85     if (action == MOVE) {
86       try {
87     MessageProxy mp = (MessageProxy) data.getTransferData(MessageProxyTransferable.sMessageProxyDataFlavor);
88     if (mp != null) {
89       mp.setDeleteInProgress(true);
90       mp.setActionType(action);
91       if (mp.removeMessageOnCompletion()) {
92         DndUtils.clearClipboard(c);
93       }
94     }
95       } catch (Exception JavaDoc e) {
96     e.printStackTrace();
97       }
98     }
99   }
100
101   public boolean canImport(JComponent c, DataFlavor[] flavors) {
102     boolean returnValue = (DndUtils.matchDataFlavor(acceptableFlavors, flavors) != null);
103     return returnValue;
104   }
105
106   /**
107    * Causes a transfer from the given component to the
108    * given clipboard. This method is called by the default cut and
109    * copy actions registered in a component's action map.
110    * <p>
111    * The transfer <em>will</em> have been completed at the
112    * return of this call. The transfer will take place using the
113    * <code>java.awt.datatransfer</code> mechanism,
114    * requiring no further effort from the developer.
115    * The <code>exportDone</code> method will be called when the
116    * transfer has completed.
117    *
118    * @param comp the component holding the data to be transferred; this
119    * argument is provided to enable sharing of <code>TransferHandler</code>s by
120    * multiple components
121    * @param clip the clipboard to transfer the data into
122    * @param action the transfer action requested; this should
123    * be a value of either <code>COPY</code> or <code>MOVE</code>;
124    * the operation performed is the intersection of the transfer
125    * capabilities given by getSourceActions and the requested action;
126    * the intersection may result in an action of <code>NONE</code>
127    * if the requested action isn't supported
128    */

129   public void exportToClipboard(JComponent comp, Clipboard clip, int action) {
130     boolean exportSuccess = false;
131     Transferable t = null;
132     
133     int clipboardAction = getSourceActions(comp) & action;
134     if (clipboardAction != NONE) {
135       t = createTransferable(comp);
136       if (t != null) {
137     if (t instanceof MessageProxyTransferable) {
138       clip.setContents(t, ((MessageProxyTransferable) t).getMessageProxy());
139     } else {
140       clip.setContents(t, null);
141     }
142     exportSuccess = true;
143       }
144     }
145     
146     if (exportSuccess) {
147       exportDone(comp, t, clipboardAction);
148     } else {
149       exportDone(comp, null, NONE);
150     }
151   }
152   
153   
154   public static Action getCutAction(JComponent pSource) {
155     return new FolderTransferAction("cut-to-clipboard", pSource);
156   }
157   
158   public static Action getCopyAction(JComponent pSource) {
159     return new FolderTransferAction("copy-to-clipboard", pSource);
160   }
161   
162   public static Action getPasteAction(JComponent pSource) {
163     return new FolderTransferAction("paste-from-clipboard", pSource);
164   }
165   
166   static class FolderTransferAction extends AbstractAction implements javax.swing.plaf.UIResource JavaDoc {
167     
168     JComponent mSource = null;
169
170     FolderTransferAction(String JavaDoc name, JComponent pSource) {
171       super(name);
172       mSource = pSource;
173       // Will cause the system clipboard state to be updated.
174

175       DndUtils.canAccessSystemClipboard();
176     }
177     
178     public void actionPerformed(ActionEvent e) {
179       Object JavaDoc src = mSource;
180
181       if (src instanceof JComponent) {
182     JComponent c = (JComponent) src;
183     TransferHandler th = c.getTransferHandler();
184     Clipboard clipboard = DndUtils.getClipboard(c);
185     String JavaDoc name = (String JavaDoc) getValue(Action.NAME);
186     if ((clipboard != null) && (th != null) && (name != null)) {
187       if ("cut-to-clipboard".equals(name)) {
188         th.exportToClipboard(c, clipboard, MOVE);
189       } else if ("copy-to-clipboard".equals(name)) {
190         th.exportToClipboard(c, clipboard, COPY);
191       } else if ("paste-from-clipboard".equals(name)) {
192         Transferable trans = clipboard.getContents(null);
193         if (trans != null) {
194           th.importData(c, trans);
195         }
196       }
197     }
198       }
199     }
200   }
201 }
202
Popular Tags