KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > markers > internal > ActionPasteMarker


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.markers.internal;
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.ISelectionProvider;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.osgi.util.NLS;
27 import org.eclipse.swt.dnd.Clipboard;
28 import org.eclipse.ui.ISharedImages;
29 import org.eclipse.ui.IWorkbenchPart;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.ide.undo.CreateMarkersOperation;
32 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
33 import org.eclipse.ui.part.MarkerTransfer;
34
35 /**
36  * Pastes one or more bookmark(s) from the clipboard into the bookmark
37  * navigator.
38  */

39 public class ActionPasteMarker extends MarkerSelectionProviderAction {
40
41     private IWorkbenchPart part;
42
43     private Clipboard clipboard;
44
45     private String JavaDoc[] pastableTypes;
46
47     private String JavaDoc markerName;
48
49     /**
50      * Creates the action.
51      *
52      * @param part
53      * @param provider
54      * @param markerName
55      * the name used to describe the specific kind of marker being
56      * pasted.
57      */

58     public ActionPasteMarker(IWorkbenchPart part, ISelectionProvider provider,
59             String JavaDoc markerName) {
60         super(provider, MarkerMessages.pasteAction_title);
61         this.part = part;
62         this.pastableTypes = new String JavaDoc[0];
63         this.markerName = markerName;
64         setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
65                 .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
66         setEnabled(false);
67     }
68
69     void setClipboard(Clipboard clipboard) {
70         this.clipboard = clipboard;
71     }
72
73     /**
74      * Copies the marker(s) from the clipboard to the bookmark navigator view.
75      */

76     public void run() {
77         // Get the markers from the clipboard
78
MarkerTransfer transfer = MarkerTransfer.getInstance();
79         IMarker[] markerData = (IMarker[]) clipboard.getContents(transfer);
80         paste(markerData);
81     }
82
83     void paste(final IMarker[] markers) {
84         if (markers == null) {
85             return;
86         }
87
88         final ArrayList JavaDoc newMarkerTypes = new ArrayList JavaDoc();
89         final ArrayList JavaDoc newMarkerAttributes = new ArrayList JavaDoc();
90         final ArrayList JavaDoc newMarkerResources = new ArrayList JavaDoc();
91
92         try {
93             ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
94                 public void run(IProgressMonitor monitor) throws CoreException {
95                     for (int i = 0; i < markers.length; i++) {
96                         // Collect info about the markers to be pasted.
97
newMarkerTypes.add(markers[i].getType());
98                         newMarkerResources.add(markers[i].getResource());
99                         newMarkerAttributes.add(markers[i].getAttributes());
100
101                     }
102                 }
103             }, null);
104         } catch (CoreException e) {
105             ErrorDialog.openError(part.getSite().getShell(),
106                     MarkerMessages.PasteMarker_errorTitle, null, e.getStatus());
107             return;
108         }
109
110         final String JavaDoc[] types = (String JavaDoc[]) newMarkerTypes
111                 .toArray(new String JavaDoc[newMarkerTypes.size()]);
112         final Map JavaDoc[] attrs = (Map JavaDoc[]) newMarkerAttributes
113                 .toArray(new Map JavaDoc[newMarkerAttributes.size()]);
114         final IResource[] resources = (IResource[]) newMarkerResources
115                 .toArray(new IResource[newMarkerResources.size()]);
116         String JavaDoc operationTitle = NLS.bind(MarkerMessages.qualifiedMarkerCommand_title,
117                 MarkerMessages.pasteAction_title, markerName);
118         final CreateMarkersOperation op = new CreateMarkersOperation(types,
119                 attrs, resources, operationTitle);
120         execute(op, MarkerMessages.PasteMarker_errorTitle, null,
121                 WorkspaceUndoUtil.getUIInfoAdapter(part.getSite().getShell()));
122
123         // Need to do this in an asyncExec, even though we're in the UI thread
124
// here,
125
// since the marker view updates itself with the addition in an
126
// asyncExec,
127
// which hasn't been processed yet.
128
// Must be done outside the create marker operation above since
129
// notification for add is
130
// sent after the operation is executed.
131
if (getSelectionProvider() != null && op.getMarkers() != null) {
132             part.getSite().getShell().getDisplay().asyncExec(new Runnable JavaDoc() {
133                 public void run() {
134                     getSelectionProvider().setSelection(
135                             new StructuredSelection(op.getMarkers()));
136                 }
137             });
138         }
139     }
140
141     void updateEnablement() {
142         setEnabled(false);
143         if (clipboard == null) {
144             return;
145         }
146
147         // Paste if clipboard contains pastable markers
148
MarkerTransfer transfer = MarkerTransfer.getInstance();
149         IMarker[] markerData = (IMarker[]) clipboard.getContents(transfer);
150         if (markerData == null || markerData.length < 1
151                 || pastableTypes == null) {
152             return;
153         }
154         for (int i = 0; i < markerData.length; i++) {
155             try {
156                 IMarker marker = markerData[i];
157                 if (!marker.exists()) {
158                     break;
159                 }
160                 boolean pastable = false;
161                 for (int j = 0; j < pastableTypes.length; j++) {
162                     if (marker.isSubtypeOf(pastableTypes[j])) {
163                         pastable = true;
164                         break;
165                     }
166                 }
167                 if (!pastable) {
168                     return;
169                 }
170                 if (!Util.isEditable(marker)) {
171                     return;
172                 }
173             } catch (CoreException e) {
174                 return;
175             }
176         }
177         setEnabled(true);
178     }
179
180     /**
181      * @param strings
182      */

183     void setPastableTypes(String JavaDoc[] strings) {
184         pastableTypes = strings;
185     }
186 }
187
Popular Tags