001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.gui.table;
017:
018: import java.awt.datatransfer.Clipboard;
019: import java.awt.datatransfer.DataFlavor;
020: import java.awt.datatransfer.Transferable;
021: import java.awt.event.InputEvent;
022: import java.util.logging.Logger;
023:
024: import javax.swing.JComponent;
025: import javax.swing.TransferHandler;
026:
027: import org.columba.api.gui.frame.IFrameMediator;
028: import org.columba.core.command.CommandProcessor;
029: import org.columba.core.facade.DialogFacade;
030: import org.columba.mail.command.IMailFolderCommandReference;
031: import org.columba.mail.folder.IMailbox;
032: import org.columba.mail.folder.command.CopyMessageCommand;
033: import org.columba.mail.folder.command.MoveMessageCommand;
034: import org.columba.mail.gui.frame.MailFrameMediator;
035:
036: /**
037: * A transfer handler for the TableView control.
038: * <p>
039: * For now the transfer handler supports only moving or copying messages from
040: * this control. ie it can only export messages.
041: *
042: * @author redsolo
043: */
044: public class TableViewTransferHandler extends TransferHandler {
045: private IFrameMediator frameController;
046:
047: /** JDK 1.4+ logging framework logger, used for logging. */
048: private static final Logger LOG = Logger
049: .getLogger("org.columba.mail.gui.table");
050:
051: /**
052: * true, if operation is a drag'n'drop operation
053: */
054: private boolean isDragOperation;
055:
056: /**
057: * true, if operation is a cut/copy/paste accelerator key operation using
058: * the clipboard
059: */
060: private boolean isClipboardOperation;
061:
062: /**
063: * TransferHandler action
064: */
065: private int action;
066:
067: /**
068: * Creates a TransferHandle for a table view.
069: *
070: * @param cont
071: * the fram controller, its used to get the selected messages.
072: */
073: public TableViewTransferHandler(IFrameMediator cont) {
074: frameController = cont;
075:
076: isDragOperation = false;
077: isClipboardOperation = false;
078:
079: }
080:
081: /** {@inheritDoc} */
082: public boolean canImport(JComponent comp,
083: DataFlavor[] transferFlavors) {
084: return false;
085: }
086:
087: /** {@inheritDoc} */
088: protected Transferable createTransferable(JComponent c) {
089: MessageReferencesTransfer transferable = null;
090:
091: if (c instanceof TableView) {
092: transferable = new MessageReferencesTransfer(
093: ((MailFrameMediator) frameController)
094: .getTableSelection());
095:
096: transferable.setClipboardOperation(isClipboardOperation);
097: transferable.setDragOperation(isDragOperation);
098: transferable.setAction(action);
099: }
100:
101: return transferable;
102: }
103:
104: /** {@inheritDoc} */
105: protected void exportDone(JComponent source, Transferable data,
106: int action) {
107:
108: /*
109: * if (data instanceof MessageReferencesTransfer) {
110: * MessageReferencesTransfer messageTransfer =
111: * (MessageReferencesTransfer) data; messageTransfer.setAction(action); }
112: */
113:
114: // if (data instanceof MessageReferencesTransfer) {
115: //
116: // MessageReferencesTransfer messageTransfer = (MessageReferencesTransfer) data;
117: //
118: // }
119: /*
120: * if ((action == TransferHandler.MOVE) && (data instanceof
121: * MessageReferencesTransfer) && (source instanceof TableView)) { //
122: * Remove the moved messages. MessageReferencesTransfer messageTransfer =
123: * (MessageReferencesTransfer) data; IMailFolderCommandReference
124: * messageRefs = messageTransfer .getFolderReferences();
125: *
126: * messageRefs.setMarkVariant(MarkMessageCommand.MARK_AS_EXPUNGED);
127: *
128: * MarkMessageCommand markCommand = new MarkMessageCommand(messageRefs);
129: * ExpungeFolderCommand expungeCommand = new ExpungeFolderCommand(
130: * messageRefs);
131: *
132: * CompoundCommand command = new CompoundCommand();
133: * command.add(markCommand); command.add(expungeCommand);
134: * CommandProcessor.getInstance().addOp(command); }
135: */
136:
137: }
138:
139: /** {@inheritDoc} */
140: public int getSourceActions(JComponent c) {
141: int action = TransferHandler.NONE;
142:
143: if (c instanceof TableView) {
144: action = TransferHandler.COPY_OR_MOVE;
145: }
146:
147: return action;
148: }
149:
150: /** {@inheritDoc} */
151: public boolean importData(JComponent source,
152: Transferable transferProxy) {
153: boolean dataWasImported = false;
154:
155: if (source instanceof TableView) {
156: TableView tableView = (TableView) source;
157:
158: try {
159: DataFlavor[] dataFlavors = transferProxy
160: .getTransferDataFlavors();
161:
162: for (int i = 0; (i < dataFlavors.length)
163: && (!dataWasImported); i++) {
164: if (dataFlavors[i]
165: .equals(MessageReferencesTransfer.FLAVOR)) {
166: MessageReferencesTransfer messageTransferable = (MessageReferencesTransfer) transferProxy
167: .getTransferData(MessageReferencesTransfer.FLAVOR);
168: dataWasImported = importMessageReferences(
169: tableView, messageTransferable);
170: }
171: }
172: } catch (Exception e) { // UnsupportedFlavorException, IOException
173: DialogFacade.showExceptionDialog(e);
174: }
175: }
176:
177: return dataWasImported;
178: }
179:
180: /**
181: * Try to import the message references. This method copies the messages to
182: * the new folder. Note that it will not delete them, since this is done by
183: * the transferhandler that initiated the drag.
184: *
185: * @param treeView
186: * the tree view to import data into.
187: * @param transferable
188: * the message references.
189: * @return true if the messages could be imported; false otherwise.
190: */
191: private boolean importMessageReferences(TableView tableView,
192: MessageReferencesTransfer transferable) {
193: boolean dataWasImported = false;
194:
195: /*
196: * TreeController treeController = (TreeController) ((TreeViewOwner)
197: * frameController) .getTreeController();
198: *
199: * TreeView treeView = treeController.getView();
200: */
201: IMailbox destFolder = (IMailbox) ((MailFrameMediator) frameController)
202: .getTableSelection().getSourceFolder();
203: /*
204: * AbstractMessageFolder destFolder = (AbstractMessageFolder) treeView
205: * .getDropTargetFolder();
206: */
207:
208: IMailFolderCommandReference result = transferable
209: .getFolderReferences();
210: result.setDestinationFolder(destFolder);
211:
212: if (transferable.getAction() == TransferHandler.MOVE) {
213: // move
214: MoveMessageCommand command = new MoveMessageCommand(result);
215: CommandProcessor.getInstance().addOp(command);
216: } else {
217: // copy
218: CopyMessageCommand command = new CopyMessageCommand(result);
219: CommandProcessor.getInstance().addOp(command);
220: }
221: dataWasImported = true;
222:
223: return dataWasImported;
224: }
225:
226: /**
227: * Called when user starts a drag'n'drop operation using the mouse only.
228: *
229: * @see javax.swing.TransferHandler#exportAsDrag(javax.swing.JComponent,
230: * java.awt.event.InputEvent, int)
231: */
232: public void exportAsDrag(JComponent comp, InputEvent e, int action) {
233: this .isDragOperation = true;
234: this .action = action;
235:
236: super .exportAsDrag(comp, e, action);
237: }
238:
239: /**
240: * Called when the user calls cut/copy shortcuts to export the selected data
241: * into the clipboard.
242: *
243: * @see javax.swing.TransferHandler#exportToClipboard(javax.swing.JComponent,
244: * java.awt.datatransfer.Clipboard, int)
245: */
246: public void exportToClipboard(JComponent comp, Clipboard clip,
247: int action) throws IllegalStateException {
248:
249: this .isClipboardOperation = true;
250: this .action = action;
251:
252: if (action == TransferHandler.MOVE) {
253: LOG
254: .info("Selected messages will be moved, when selecting \"Paste\"");
255: frameController
256: .fireStatusMessageChanged("Selected messages will be moved, when selecting \"Paste\"");
257: } else if (action == TransferHandler.COPY) {
258: LOG
259: .info("Selected messages will be copied, when selecting \"Paste\"");
260: frameController
261: .fireStatusMessageChanged("Selected messages will be copied, when selecting \"Paste\"");
262: }
263:
264: super.exportToClipboard(comp, clip, action);
265: }
266: }
|