01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.views.tasklist;
11:
12: import java.util.List;
13:
14: import org.eclipse.core.resources.IMarker;
15: import org.eclipse.jface.dialogs.MessageDialog;
16: import org.eclipse.jface.viewers.IStructuredSelection;
17: import org.eclipse.jface.viewers.TableViewer;
18: import org.eclipse.swt.SWTError;
19: import org.eclipse.swt.dnd.DND;
20: import org.eclipse.swt.dnd.TextTransfer;
21: import org.eclipse.swt.dnd.Transfer;
22: import org.eclipse.ui.PlatformUI;
23: import org.eclipse.ui.internal.views.tasklist.TaskListMessages;
24: import org.eclipse.ui.part.MarkerTransfer;
25:
26: /**
27: * Copies a task to the clipboard.
28: */
29: class CopyTaskAction extends TaskAction {
30:
31: /**
32: * Creates the action.
33: * @param tasklist the task list
34: * @param id the id
35: */
36: public CopyTaskAction(TaskList tasklist, String id) {
37: super (tasklist, id);
38: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
39: ITaskListHelpContextIds.COPY_TASK_ACTION);
40: }
41:
42: /**
43: * Performs this action.
44: */
45: public void run() {
46: // Get the selected markers
47: TaskList taskList = getTaskList();
48: TableViewer viewer = taskList.getTableViewer();
49: IStructuredSelection selection = (IStructuredSelection) viewer
50: .getSelection();
51: if (selection.isEmpty()) {
52: return;
53: }
54: taskList.cancelEditing();
55: List list = selection.toList();
56: IMarker[] markers = new IMarker[list.size()];
57: list.toArray(markers);
58:
59: setClipboard(markers, TaskList.createMarkerReport(markers));
60:
61: //Update paste enablement
62: taskList.updatePasteEnablement();
63: }
64:
65: private void setClipboard(IMarker[] markers, String markerReport) {
66: try {
67: // Place the markers on the clipboard
68: Object[] data = new Object[] { markers, markerReport };
69: Transfer[] transferTypes = new Transfer[] {
70: MarkerTransfer.getInstance(),
71: TextTransfer.getInstance() };
72:
73: // set the clipboard contents
74: getTaskList().getClipboard().setContents(data,
75: transferTypes);
76: } catch (SWTError e) {
77: if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
78: throw e;
79: }
80: if (MessageDialog
81: .openQuestion(
82: getShell(),
83: TaskListMessages.CopyToClipboardProblemDialog_title,
84: TaskListMessages.CopyToClipboardProblemDialog_message)) {
85: setClipboard(markers, markerReport);
86: }
87: }
88: }
89: }
|