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.markers.internal;
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.ISelectionProvider;
023: import org.eclipse.jface.viewers.StructuredSelection;
024: import org.eclipse.osgi.util.NLS;
025: import org.eclipse.swt.dnd.Clipboard;
026: import org.eclipse.ui.ISharedImages;
027: import org.eclipse.ui.IWorkbenchPart;
028: import org.eclipse.ui.PlatformUI;
029: import org.eclipse.ui.ide.undo.CreateMarkersOperation;
030: import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
031: import org.eclipse.ui.part.MarkerTransfer;
032:
033: /**
034: * Pastes one or more bookmark(s) from the clipboard into the bookmark
035: * navigator.
036: */
037: public class ActionPasteMarker extends MarkerSelectionProviderAction {
038:
039: private IWorkbenchPart part;
040:
041: private Clipboard clipboard;
042:
043: private String[] pastableTypes;
044:
045: private String markerName;
046:
047: /**
048: * Creates the action.
049: *
050: * @param part
051: * @param provider
052: * @param markerName
053: * the name used to describe the specific kind of marker being
054: * pasted.
055: */
056: public ActionPasteMarker(IWorkbenchPart part,
057: ISelectionProvider provider, String markerName) {
058: super (provider, MarkerMessages.pasteAction_title);
059: this .part = part;
060: this .pastableTypes = new String[0];
061: this .markerName = markerName;
062: setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
063: .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
064: setEnabled(false);
065: }
066:
067: void setClipboard(Clipboard clipboard) {
068: this .clipboard = clipboard;
069: }
070:
071: /**
072: * Copies the marker(s) from the clipboard to the bookmark navigator view.
073: */
074: public void run() {
075: // Get the markers from the clipboard
076: MarkerTransfer transfer = MarkerTransfer.getInstance();
077: IMarker[] markerData = (IMarker[]) clipboard
078: .getContents(transfer);
079: paste(markerData);
080: }
081:
082: void paste(final IMarker[] markers) {
083: if (markers == null) {
084: return;
085: }
086:
087: final ArrayList newMarkerTypes = new ArrayList();
088: final ArrayList newMarkerAttributes = new ArrayList();
089: final ArrayList newMarkerResources = new ArrayList();
090:
091: try {
092: ResourcesPlugin.getWorkspace().run(
093: new IWorkspaceRunnable() {
094: public void run(IProgressMonitor monitor)
095: throws CoreException {
096: for (int i = 0; i < markers.length; i++) {
097: // Collect info about the markers to be pasted.
098: newMarkerTypes
099: .add(markers[i].getType());
100: newMarkerResources.add(markers[i]
101: .getResource());
102: newMarkerAttributes.add(markers[i]
103: .getAttributes());
104:
105: }
106: }
107: }, null);
108: } catch (CoreException e) {
109: ErrorDialog.openError(part.getSite().getShell(),
110: MarkerMessages.PasteMarker_errorTitle, null, e
111: .getStatus());
112: return;
113: }
114:
115: final String[] types = (String[]) newMarkerTypes
116: .toArray(new String[newMarkerTypes.size()]);
117: final Map[] attrs = (Map[]) newMarkerAttributes
118: .toArray(new Map[newMarkerAttributes.size()]);
119: final IResource[] resources = (IResource[]) newMarkerResources
120: .toArray(new IResource[newMarkerResources.size()]);
121: String operationTitle = NLS.bind(
122: MarkerMessages.qualifiedMarkerCommand_title,
123: MarkerMessages.pasteAction_title, markerName);
124: final CreateMarkersOperation op = new CreateMarkersOperation(
125: types, attrs, resources, operationTitle);
126: execute(op, MarkerMessages.PasteMarker_errorTitle, null,
127: WorkspaceUndoUtil.getUIInfoAdapter(part.getSite()
128: .getShell()));
129:
130: // Need to do this in an asyncExec, even though we're in the UI thread
131: // here,
132: // since the marker view updates itself with the addition in an
133: // asyncExec,
134: // which hasn't been processed yet.
135: // Must be done outside the create marker operation above since
136: // notification for add is
137: // sent after the operation is executed.
138: if (getSelectionProvider() != null && op.getMarkers() != null) {
139: part.getSite().getShell().getDisplay().asyncExec(
140: new Runnable() {
141: public void run() {
142: getSelectionProvider().setSelection(
143: new StructuredSelection(op
144: .getMarkers()));
145: }
146: });
147: }
148: }
149:
150: void updateEnablement() {
151: setEnabled(false);
152: if (clipboard == null) {
153: return;
154: }
155:
156: // Paste if clipboard contains pastable markers
157: MarkerTransfer transfer = MarkerTransfer.getInstance();
158: IMarker[] markerData = (IMarker[]) clipboard
159: .getContents(transfer);
160: if (markerData == null || markerData.length < 1
161: || pastableTypes == null) {
162: return;
163: }
164: for (int i = 0; i < markerData.length; i++) {
165: try {
166: IMarker marker = markerData[i];
167: if (!marker.exists()) {
168: break;
169: }
170: boolean pastable = false;
171: for (int j = 0; j < pastableTypes.length; j++) {
172: if (marker.isSubtypeOf(pastableTypes[j])) {
173: pastable = true;
174: break;
175: }
176: }
177: if (!pastable) {
178: return;
179: }
180: if (!Util.isEditable(marker)) {
181: return;
182: }
183: } catch (CoreException e) {
184: return;
185: }
186: }
187: setEnabled(true);
188: }
189:
190: /**
191: * @param strings
192: */
193: void setPastableTypes(String[] strings) {
194: pastableTypes = strings;
195: }
196: }
|