001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.views.tasklist;
011:
012: import java.util.ArrayList;
013: import java.util.Map;
014:
015: import org.eclipse.core.resources.IMarker;
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.resources.IWorkspaceRunnable;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.jface.dialogs.ErrorDialog;
021: import org.eclipse.jface.viewers.StructuredSelection;
022: import org.eclipse.ui.PlatformUI;
023: import org.eclipse.ui.ide.undo.CreateMarkersOperation;
024: import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
025: import org.eclipse.ui.internal.views.tasklist.TaskListMessages;
026: import org.eclipse.ui.part.MarkerTransfer;
027:
028: /**
029: * Standard action for pasting tasks from the clipboard.
030: * <p>
031: * This class may be instantiated; it is not intended to be subclassed.
032: * </p>
033: *
034: * @since 2.0
035: */
036: class PasteTaskAction extends TaskAction {
037:
038: /**
039: * Creates a new action.
040: *
041: * @param tasklist the task list
042: * @param id the id
043: */
044: public PasteTaskAction(TaskList tasklist, String id) {
045: super (tasklist, id);
046: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
047: ITaskListHelpContextIds.PASTE_TASK_ACTION);
048: }
049:
050: /**
051: * Implementation of method defined on <code>IAction</code>.
052: */
053: public void run() {
054: // Get the markers from the clipboard
055: MarkerTransfer transfer = MarkerTransfer.getInstance();
056: final IMarker[] markerData = (IMarker[]) getTaskList()
057: .getClipboard().getContents(transfer);
058:
059: if (markerData == null) {
060: return;
061: }
062:
063: final ArrayList newMarkerAttributes = new ArrayList();
064: final ArrayList newMarkerResources = new ArrayList();
065:
066: try {
067: getTaskList().getWorkspace().run(new IWorkspaceRunnable() {
068: public void run(IProgressMonitor monitor)
069: throws CoreException {
070: for (int i = 0; i < markerData.length; i++) {
071: // Only paste tasks
072: if (!markerData[i].getType().equals(
073: IMarker.TASK)) {
074: continue;
075: }
076: newMarkerResources.add(markerData[i]
077: .getResource());
078: newMarkerAttributes.add(markerData[i]
079: .getAttributes());
080: }
081: }
082: }, null);
083: } catch (CoreException e) {
084: ErrorDialog.openError(getShell(),
085: TaskListMessages.PasteTask_errorMessage, null, e
086: .getStatus());
087: return;
088: }
089:
090: final Map[] attrs = (Map[]) newMarkerAttributes
091: .toArray(new Map[newMarkerAttributes.size()]);
092: final IResource[] resources = (IResource[]) newMarkerResources
093: .toArray(new IResource[newMarkerResources.size()]);
094: final CreateMarkersOperation op = new CreateMarkersOperation(
095: IMarker.TASK, attrs, resources, getText());
096: execute(op, TaskListMessages.PasteTask_errorMessage, null,
097: WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
098:
099: // Need to do this in an asyncExec, even though we're in the UI thread here,
100: // since the task list updates itself with the addition in an asyncExec,
101: // which hasn't been processed yet.
102: // Must be done outside the create marker operation above since notification for add is
103: // sent after the operation is executed.
104: if (op.getMarkers() != null) {
105: getShell().getDisplay().asyncExec(new Runnable() {
106: public void run() {
107: TaskList taskList = getTaskList();
108: taskList.setSelection(new StructuredSelection(op
109: .getMarkers()), true);
110: }
111: });
112: }
113: }
114:
115: }
|