KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > tasklist > CopyTaskAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.views.tasklist;
13
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.swt.SWTError;
21 import org.eclipse.swt.dnd.DND;
22 import org.eclipse.swt.dnd.TextTransfer;
23 import org.eclipse.swt.dnd.Transfer;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.internal.views.tasklist.TaskListMessages;
26 import org.eclipse.ui.part.MarkerTransfer;
27
28 /**
29  * Copies a task to the clipboard.
30  */

31 class CopyTaskAction extends TaskAction {
32  
33     /**
34      * Creates the action.
35      * @param tasklist the task list
36      * @param id the id
37      */

38     public CopyTaskAction(TaskList tasklist, String JavaDoc id) {
39         super(tasklist, id);
40         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
41                 ITaskListHelpContextIds.COPY_TASK_ACTION);
42     }
43
44     /**
45      * Performs this action.
46      */

47     public void run() {
48         // Get the selected markers
49
TaskList taskList = getTaskList();
50         TableViewer viewer = taskList.getTableViewer();
51         IStructuredSelection selection = (IStructuredSelection) viewer
52                 .getSelection();
53         if (selection.isEmpty()) {
54             return;
55         }
56         taskList.cancelEditing();
57         List JavaDoc list = selection.toList();
58         IMarker[] markers = new IMarker[list.size()];
59         list.toArray(markers);
60
61         setClipboard(markers, TaskList.createMarkerReport(markers));
62
63         //Update paste enablement
64
taskList.updatePasteEnablement();
65     }
66
67     private void setClipboard(IMarker[] markers, String JavaDoc markerReport) {
68         try {
69             // Place the markers on the clipboard
70
Object JavaDoc[] data = new Object JavaDoc[] { markers, markerReport };
71             Transfer[] transferTypes = new Transfer[] {
72                     MarkerTransfer.getInstance(), TextTransfer.getInstance() };
73
74             // set the clipboard contents
75
getTaskList().getClipboard().setContents(data, 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, TaskListMessages.CopyToClipboardProblemDialog_message)) {
84                 setClipboard(markers, markerReport);
85             }
86         }
87     }
88 }
89
90
Popular Tags