1 11 12 package org.eclipse.ui.views.markers.internal; 13 14 import java.util.ArrayList ; 15 import java.util.Map ; 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 39 public class ActionPasteMarker extends MarkerSelectionProviderAction { 40 41 private IWorkbenchPart part; 42 43 private Clipboard clipboard; 44 45 private String [] pastableTypes; 46 47 private String markerName; 48 49 58 public ActionPasteMarker(IWorkbenchPart part, ISelectionProvider provider, 59 String markerName) { 60 super(provider, MarkerMessages.pasteAction_title); 61 this.part = part; 62 this.pastableTypes = new String [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 76 public void run() { 77 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 newMarkerTypes = new ArrayList (); 89 final ArrayList newMarkerAttributes = new ArrayList (); 90 final ArrayList newMarkerResources = new ArrayList (); 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 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 [] types = (String []) newMarkerTypes 111 .toArray(new String [newMarkerTypes.size()]); 112 final Map [] attrs = (Map []) newMarkerAttributes 113 .toArray(new Map [newMarkerAttributes.size()]); 114 final IResource[] resources = (IResource[]) newMarkerResources 115 .toArray(new IResource[newMarkerResources.size()]); 116 String 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 if (getSelectionProvider() != null && op.getMarkers() != null) { 132 part.getSite().getShell().getDisplay().asyncExec(new Runnable () { 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 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 183 void setPastableTypes(String [] strings) { 184 pastableTypes = strings; 185 } 186 } 187 | Popular Tags |