KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > table > TableViewTransferHandler


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.gui.table;
17
18 import java.awt.datatransfer.Clipboard JavaDoc;
19 import java.awt.datatransfer.DataFlavor JavaDoc;
20 import java.awt.datatransfer.Transferable JavaDoc;
21 import java.awt.event.InputEvent JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23
24 import javax.swing.JComponent JavaDoc;
25 import javax.swing.TransferHandler JavaDoc;
26
27 import org.columba.api.gui.frame.IFrameMediator;
28 import org.columba.core.command.CommandProcessor;
29 import org.columba.core.facade.DialogFacade;
30 import org.columba.mail.command.IMailFolderCommandReference;
31 import org.columba.mail.folder.IMailbox;
32 import org.columba.mail.folder.command.CopyMessageCommand;
33 import org.columba.mail.folder.command.MoveMessageCommand;
34 import org.columba.mail.gui.frame.MailFrameMediator;
35
36 /**
37  * A transfer handler for the TableView control.
38  * <p>
39  * For now the transfer handler supports only moving or copying messages from
40  * this control. ie it can only export messages.
41  *
42  * @author redsolo
43  */

44 public class TableViewTransferHandler extends TransferHandler JavaDoc {
45     private IFrameMediator frameController;
46
47     /** JDK 1.4+ logging framework logger, used for logging. */
48     private static final Logger JavaDoc LOG = Logger
49             .getLogger("org.columba.mail.gui.table");
50
51     /**
52      * true, if operation is a drag'n'drop operation
53      */

54     private boolean isDragOperation;
55
56     /**
57      * true, if operation is a cut/copy/paste accelerator key operation using
58      * the clipboard
59      */

60     private boolean isClipboardOperation;
61
62     /**
63      * TransferHandler action
64      */

65     private int action;
66
67     /**
68      * Creates a TransferHandle for a table view.
69      *
70      * @param cont
71      * the fram controller, its used to get the selected messages.
72      */

73     public TableViewTransferHandler(IFrameMediator cont) {
74         frameController = cont;
75
76         isDragOperation = false;
77         isClipboardOperation = false;
78
79     }
80
81     /** {@inheritDoc} */
82     public boolean canImport(JComponent JavaDoc comp, DataFlavor JavaDoc[] transferFlavors) {
83         return false;
84     }
85
86     /** {@inheritDoc} */
87     protected Transferable JavaDoc createTransferable(JComponent JavaDoc c) {
88         MessageReferencesTransfer transferable = null;
89
90         if (c instanceof TableView) {
91             transferable = new MessageReferencesTransfer(
92                     ((MailFrameMediator) frameController).getTableSelection());
93
94             transferable.setClipboardOperation(isClipboardOperation);
95             transferable.setDragOperation(isDragOperation);
96             transferable.setAction(action);
97         }
98
99         return transferable;
100     }
101
102     /** {@inheritDoc} */
103     protected void exportDone(JComponent JavaDoc source, Transferable JavaDoc data, int action) {
104
105         /*
106          * if (data instanceof MessageReferencesTransfer) {
107          * MessageReferencesTransfer messageTransfer =
108          * (MessageReferencesTransfer) data; messageTransfer.setAction(action); }
109          */

110
111 // if (data instanceof MessageReferencesTransfer) {
112
//
113
// MessageReferencesTransfer messageTransfer = (MessageReferencesTransfer) data;
114
//
115
// }
116

117         /*
118          * if ((action == TransferHandler.MOVE) && (data instanceof
119          * MessageReferencesTransfer) && (source instanceof TableView)) { //
120          * Remove the moved messages. MessageReferencesTransfer messageTransfer =
121          * (MessageReferencesTransfer) data; IMailFolderCommandReference
122          * messageRefs = messageTransfer .getFolderReferences();
123          *
124          * messageRefs.setMarkVariant(MarkMessageCommand.MARK_AS_EXPUNGED);
125          *
126          * MarkMessageCommand markCommand = new MarkMessageCommand(messageRefs);
127          * ExpungeFolderCommand expungeCommand = new ExpungeFolderCommand(
128          * messageRefs);
129          *
130          * CompoundCommand command = new CompoundCommand();
131          * command.add(markCommand); command.add(expungeCommand);
132          * CommandProcessor.getInstance().addOp(command); }
133          */

134
135     }
136
137     /** {@inheritDoc} */
138     public int getSourceActions(JComponent JavaDoc c) {
139         int action = TransferHandler.NONE;
140
141         if (c instanceof TableView) {
142             action = TransferHandler.COPY_OR_MOVE;
143         }
144
145         return action;
146     }
147
148     /** {@inheritDoc} */
149     public boolean importData(JComponent JavaDoc source, Transferable JavaDoc transferProxy) {
150         boolean dataWasImported = false;
151
152         if (source instanceof TableView) {
153             TableView tableView = (TableView) source;
154
155             try {
156                 DataFlavor JavaDoc[] dataFlavors = transferProxy
157                         .getTransferDataFlavors();
158
159                 for (int i = 0; (i < dataFlavors.length) && (!dataWasImported); i++) {
160                     if (dataFlavors[i].equals(MessageReferencesTransfer.FLAVOR)) {
161                         MessageReferencesTransfer messageTransferable = (MessageReferencesTransfer) transferProxy
162                                 .getTransferData(MessageReferencesTransfer.FLAVOR);
163                         dataWasImported = importMessageReferences(tableView,
164                                 messageTransferable);
165                     }
166                 }
167             } catch (Exception JavaDoc e) { // UnsupportedFlavorException, IOException
168
DialogFacade.showExceptionDialog(e);
169             }
170         }
171
172         return dataWasImported;
173     }
174
175     /**
176      * Try to import the message references. This method copies the messages to
177      * the new folder. Note that it will not delete them, since this is done by
178      * the transferhandler that initiated the drag.
179      *
180      * @param treeView
181      * the tree view to import data into.
182      * @param transferable
183      * the message references.
184      * @return true if the messages could be imported; false otherwise.
185      */

186     private boolean importMessageReferences(TableView tableView,
187             MessageReferencesTransfer transferable) {
188         boolean dataWasImported = false;
189
190         /*
191          * TreeController treeController = (TreeController) ((TreeViewOwner)
192          * frameController) .getTreeController();
193          *
194          * TreeView treeView = treeController.getView();
195          */

196         IMailbox destFolder = (IMailbox) ((MailFrameMediator) frameController)
197                 .getTableSelection().getSourceFolder();
198         /*
199          * AbstractMessageFolder destFolder = (AbstractMessageFolder) treeView
200          * .getDropTargetFolder();
201          */

202
203         IMailFolderCommandReference result = transferable.getFolderReferences();
204         result.setDestinationFolder(destFolder);
205
206         if (transferable.getAction() == TransferHandler.MOVE) {
207             // move
208
MoveMessageCommand command = new MoveMessageCommand(result);
209             CommandProcessor.getInstance().addOp(command);
210         } else {
211             // copy
212
CopyMessageCommand command = new CopyMessageCommand(result);
213             CommandProcessor.getInstance().addOp(command);
214         }
215         dataWasImported = true;
216
217         return dataWasImported;
218     }
219
220     /**
221      * Called when user starts a drag'n'drop operation using the mouse only.
222      *
223      * @see javax.swing.TransferHandler#exportAsDrag(javax.swing.JComponent,
224      * java.awt.event.InputEvent, int)
225      */

226     public void exportAsDrag(JComponent JavaDoc comp, InputEvent JavaDoc e, int action) {
227         this.isDragOperation = true;
228         this.action = action;
229
230         super.exportAsDrag(comp, e, action);
231     }
232
233     /**
234      * Called when the user calls cut/copy shortcuts to export the selected data
235      * into the clipboard.
236      *
237      * @see javax.swing.TransferHandler#exportToClipboard(javax.swing.JComponent,
238      * java.awt.datatransfer.Clipboard, int)
239      */

240     public void exportToClipboard(JComponent JavaDoc comp, Clipboard JavaDoc clip, int action)
241             throws IllegalStateException JavaDoc {
242
243         this.isClipboardOperation = true;
244         this.action = action;
245
246         if (action == TransferHandler.MOVE) {
247             LOG
248                     .info("Selected messages will be moved, when selecting \"Paste\"");
249             frameController
250                     .fireStatusMessageChanged("Selected messages will be moved, when selecting \"Paste\"");
251         } else if (action == TransferHandler.COPY) {
252             LOG
253                     .info("Selected messages will be copied, when selecting \"Paste\"");
254             frameController
255                     .fireStatusMessageChanged("Selected messages will be copied, when selecting \"Paste\"");
256         }
257
258         super.exportToClipboard(comp, clip, action);
259     }
260 }
Popular Tags