KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > bookmarkexplorer > PasteBookmarkAction


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.bookmarkexplorer;
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.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.jface.dialogs.ErrorDialog;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.ide.undo.CreateMarkersOperation;
27 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
28 import org.eclipse.ui.internal.views.bookmarkexplorer.BookmarkMessages;
29 import org.eclipse.ui.part.MarkerTransfer;
30
31 /**
32  * Pastes one or more bookmark(s) from the clipboard into the bookmark navigator.
33  */

34 class PasteBookmarkAction extends BookmarkAction {
35
36     private BookmarkNavigator view;
37
38     /**
39      * The constructor.
40      *
41      * @param view the view
42      */

43     public PasteBookmarkAction(BookmarkNavigator view) {
44         super(view, BookmarkMessages.PasteBookmark_text);
45         this.view = view;
46         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
47                 IBookmarkHelpContextIds.PASTE_BOOKMARK_ACTION);
48         setEnabled(false);
49     }
50
51     /**
52      * Copies the marker(s) from the clipboard to the bookmark navigator view.
53      */

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