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 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 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 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 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 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 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 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 { 167 168 JComponent mSource = null; 169 170 FolderTransferAction(String name, JComponent pSource) { 171 super(name); 172 mSource = pSource; 173 175 DndUtils.canAccessSystemClipboard(); 176 } 177 178 public void actionPerformed(ActionEvent e) { 179 Object 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 name = (String ) 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 |