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.bookmarkexplorer;
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.resources.ResourcesPlugin;
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.IProgressMonitor;
021: import org.eclipse.jface.dialogs.ErrorDialog;
022: import org.eclipse.jface.viewers.StructuredSelection;
023: import org.eclipse.ui.PlatformUI;
024: import org.eclipse.ui.ide.undo.CreateMarkersOperation;
025: import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
026: import org.eclipse.ui.internal.views.bookmarkexplorer.BookmarkMessages;
027: import org.eclipse.ui.part.MarkerTransfer;
028:
029: /**
030: * Pastes one or more bookmark(s) from the clipboard into the bookmark navigator.
031: */
032: class PasteBookmarkAction extends BookmarkAction {
033:
034: private BookmarkNavigator view;
035:
036: /**
037: * The constructor.
038: *
039: * @param view the view
040: */
041: public PasteBookmarkAction(BookmarkNavigator view) {
042: super (view, BookmarkMessages.PasteBookmark_text);
043: this .view = view;
044: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
045: IBookmarkHelpContextIds.PASTE_BOOKMARK_ACTION);
046: setEnabled(false);
047: }
048:
049: /**
050: * Copies the marker(s) from the clipboard to the bookmark navigator view.
051: */
052: public void run() {
053: // Get the markers from the clipboard
054: MarkerTransfer transfer = MarkerTransfer.getInstance();
055: final IMarker[] markerData = (IMarker[]) view.getClipboard()
056: .getContents(transfer);
057:
058: if (markerData == null) {
059: return;
060: }
061: final ArrayList newMarkerAttributes = new ArrayList();
062: final ArrayList newMarkerResources = new ArrayList();
063: try {
064: ResourcesPlugin.getWorkspace().run(
065: new IWorkspaceRunnable() {
066: public void run(IProgressMonitor monitor)
067: throws CoreException {
068: for (int i = 0; i < markerData.length; i++) {
069: // Collect the info about the markers to be pasted.
070: // Ignore any markers that aren't bookmarks.
071: if (!markerData[i].getType().equals(
072: IMarker.BOOKMARK)) {
073: continue;
074: }
075: newMarkerResources.add(markerData[i]
076: .getResource());
077: newMarkerAttributes.add(markerData[i]
078: .getAttributes());
079: }
080: }
081: }, null);
082: } catch (CoreException e) {
083: ErrorDialog.openError(view.getShell(),
084: BookmarkMessages.PasteBookmark_errorTitle, null, e
085: .getStatus());
086: return;
087: }
088: final Map[] attrs = (Map[]) newMarkerAttributes
089: .toArray(new Map[newMarkerAttributes.size()]);
090: final IResource[] resources = (IResource[]) newMarkerResources
091: .toArray(new IResource[newMarkerResources.size()]);
092: final CreateMarkersOperation op = new CreateMarkersOperation(
093: IMarker.BOOKMARK, attrs, resources,
094: BookmarkMessages.PasteBookmark_undoText);
095: execute(op, BookmarkMessages.PasteBookmark_errorTitle, null,
096: WorkspaceUndoUtil.getUIInfoAdapter(view.getShell()));
097:
098: // Need to do this in an asyncExec, even though we're in the UI thread here,
099: // since the bookmark navigator updates itself with the addition in an asyncExec,
100: // which hasn't been processed yet.
101: // Must be done outside the create marker operation above since notification for add is
102: // sent after the operation is executed.
103: if (op.getMarkers() != null) {
104: view.getShell().getDisplay().asyncExec(new Runnable() {
105: public void run() {
106: view.getViewer().setSelection(
107: new StructuredSelection(op.getMarkers()));
108: view.updatePasteEnablement();
109: }
110: });
111: }
112: }
113:
114: }
|