1 11 12 package org.eclipse.ui.views.markers.internal; 13 14 import org.eclipse.core.resources.IMarker; 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.jface.dialogs.ErrorDialog; 17 import org.eclipse.jface.dialogs.MessageDialog; 18 import org.eclipse.jface.viewers.ISelectionProvider; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 import org.eclipse.swt.SWTError; 21 import org.eclipse.swt.dnd.Clipboard; 22 import org.eclipse.swt.dnd.DND; 23 import org.eclipse.swt.dnd.TextTransfer; 24 import org.eclipse.swt.dnd.Transfer; 25 import org.eclipse.ui.ISharedImages; 26 import org.eclipse.ui.IWorkbenchPart; 27 import org.eclipse.ui.PlatformUI; 28 import org.eclipse.ui.part.MarkerTransfer; 29 30 33 public class ActionCopyMarker extends MarkerSelectionProviderAction { 34 35 private IWorkbenchPart part; 36 37 private Clipboard clipboard; 38 39 private IField[] properties; 40 41 47 public ActionCopyMarker(IWorkbenchPart part, ISelectionProvider provider) { 48 super(provider, MarkerMessages.copyAction_title); 49 this.part = part; 50 setImageDescriptor(PlatformUI.getWorkbench().getSharedImages() 51 .getImageDescriptor(ISharedImages.IMG_TOOL_COPY)); 52 setEnabled(false); 53 } 54 55 61 void setClipboard(Clipboard clipboard) { 62 this.clipboard = clipboard; 63 } 64 65 71 void setProperties(IField[] properties) { 72 this.properties = properties; 73 } 74 75 80 public void run() { 81 IMarker[] markers = getSelectedMarkers(); 82 setClipboard(markers, createMarkerReport(markers)); 83 } 84 85 90 public void selectionChanged(IStructuredSelection selection) { 91 setEnabled(Util.allConcreteSelection(selection)); 92 } 93 94 private void setClipboard(IMarker[] markers, String markerReport) { 95 try { 96 Object [] data; 98 Transfer[] transferTypes; 99 if (markerReport == null) { 100 data = new Object [] { markers }; 101 transferTypes = new Transfer[] { MarkerTransfer.getInstance() }; 102 } else { 103 data = new Object [] { markers, markerReport }; 104 transferTypes = new Transfer[] { MarkerTransfer.getInstance(), 105 TextTransfer.getInstance() }; 106 } 107 108 clipboard.setContents(data, transferTypes); 109 } catch (SWTError e) { 110 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) { 111 throw e; 112 } 113 if (MessageDialog.openQuestion(part.getSite().getShell(), 114 MarkerMessages.CopyToClipboardProblemDialog_title, 115 MarkerMessages.CopyToClipboardProblemDialog_message)) { 116 setClipboard(markers, markerReport); 117 } 118 } 119 } 120 121 128 String createMarkerReport(IMarker[] rawMarkers) { 129 ConcreteMarker[] markers; 130 try { 131 markers = MarkerList.createMarkers(rawMarkers); 132 } catch (CoreException e) { 133 ErrorDialog.openError(part.getSite().getShell(), 134 MarkerMessages.Error, null, e.getStatus()); 135 return ""; } 137 138 StringBuffer report = new StringBuffer (); 139 140 final String NEWLINE = System.getProperty("line.separator"); final char DELIMITER = '\t'; 142 143 if (properties == null) { 144 return null; 145 } 146 147 for (int i = 0; i < properties.length; i++) { 149 report.append(properties[i].getDescription()); 150 if (i == properties.length - 1) { 151 report.append(NEWLINE); 152 } else { 153 report.append(DELIMITER); 154 } 155 } 156 157 for (int i = 0; i < markers.length; i++) { 158 ConcreteMarker marker = markers[i]; 159 for (int j = 0; j < properties.length; j++) { 160 report.append(properties[j].getValue(marker)); 161 if (j == properties.length - 1) { 162 report.append(NEWLINE); 163 } else { 164 report.append(DELIMITER); 165 } 166 } 167 } 168 169 return report.toString(); 170 } 171 } 172 | Popular Tags |