KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.IWorkspaceRunnable;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.jface.dialogs.ErrorDialog;
23 import org.eclipse.jface.viewers.StructuredSelection;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.ide.undo.CreateMarkersOperation;
26 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
27 import org.eclipse.ui.internal.views.tasklist.TaskListMessages;
28 import org.eclipse.ui.part.MarkerTransfer;
29
30 /**
31  * Standard action for pasting tasks from the clipboard.
32  * <p>
33  * This class may be instantiated; it is not intended to be subclassed.
34  * </p>
35  *
36  * @since 2.0
37  */

38 class PasteTaskAction extends TaskAction {
39
40     /**
41      * Creates a new action.
42      *
43      * @param tasklist the task list
44      * @param id the id
45      */

46     public PasteTaskAction(TaskList tasklist, String JavaDoc id) {
47         super(tasklist, id);
48         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
49                 ITaskListHelpContextIds.PASTE_TASK_ACTION);
50     }
51
52     /**
53      * Implementation of method defined on <code>IAction</code>.
54      */

55     public void run() {
56         // Get the markers from the clipboard
57
MarkerTransfer transfer = MarkerTransfer.getInstance();
58         final IMarker[] markerData = (IMarker[]) getTaskList().getClipboard()
59                 .getContents(transfer);
60
61         if (markerData == null) {
62             return;
63         }
64
65         final ArrayList JavaDoc newMarkerAttributes = new ArrayList JavaDoc();
66         final ArrayList JavaDoc newMarkerResources = new ArrayList JavaDoc();
67
68         try {
69             getTaskList().getWorkspace().run(new IWorkspaceRunnable() {
70                 public void run(IProgressMonitor monitor) throws CoreException {
71                     for (int i = 0; i < markerData.length; i++) {
72                         // Only paste tasks
73
if (!markerData[i].getType().equals(IMarker.TASK)) {
74                             continue;
75                         }
76                         newMarkerResources.add(markerData[i].getResource());
77                         newMarkerAttributes.add(markerData[i].getAttributes());
78                     }
79                 }
80             }, null);
81         } catch (CoreException e) {
82             ErrorDialog.openError(getShell(), TaskListMessages.PasteTask_errorMessage,
83                     null, e.getStatus());
84             return;
85         }
86         
87         final Map JavaDoc [] attrs = (Map JavaDoc []) newMarkerAttributes.toArray(new Map JavaDoc [newMarkerAttributes.size()]);
88         final IResource [] resources = (IResource []) newMarkerResources.toArray(new IResource [newMarkerResources.size()]);
89         final CreateMarkersOperation op = new CreateMarkersOperation(IMarker.TASK, attrs,
90                 resources, getText());
91         execute(op, TaskListMessages.PasteTask_errorMessage, null,
92                 WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
93
94         // Need to do this in an asyncExec, even though we're in the UI thread here,
95
// since the task list updates itself with the addition in an asyncExec,
96
// which hasn't been processed yet.
97
// Must be done outside the create marker operation above since notification for add is
98
// sent after the operation is executed.
99
if (op.getMarkers() != null) {
100             getShell().getDisplay().asyncExec(new Runnable JavaDoc() {
101                 public void run() {
102                     TaskList taskList = getTaskList();
103                     taskList.setSelection(new StructuredSelection(op.getMarkers()),
104                             true);
105                 }
106             });
107         }
108     }
109
110 }
111
112
Popular Tags