1 11 12 package org.eclipse.ui.views.bookmarkexplorer; 13 14 import java.util.List ; 15 16 import org.eclipse.core.resources.IMarker; 17 import org.eclipse.jface.dialogs.MessageDialog; 18 import org.eclipse.jface.viewers.IStructuredSelection; 19 import org.eclipse.jface.viewers.StructuredViewer; 20 import org.eclipse.osgi.util.NLS; 21 import org.eclipse.swt.SWTError; 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.PlatformUI; 26 import org.eclipse.ui.internal.views.bookmarkexplorer.BookmarkMessages; 27 import org.eclipse.ui.part.MarkerTransfer; 28 29 32 class CopyBookmarkAction extends BookmarkAction { 33 34 39 public CopyBookmarkAction(BookmarkNavigator bookmarkNavigator) { 40 super(bookmarkNavigator, BookmarkMessages.CopyBookmark_text); 41 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 42 IBookmarkHelpContextIds.COPY_BOOKMARK_ACTION); 43 setEnabled(false); 44 } 45 46 49 public void run() { 50 BookmarkNavigator bookmarkNavigator = getView(); 52 StructuredViewer viewer = bookmarkNavigator.getViewer(); 53 IStructuredSelection selection = (IStructuredSelection) viewer 54 .getSelection(); 55 if (selection.isEmpty()) { 56 return; 57 } 58 List list = selection.toList(); 59 IMarker[] markers = new IMarker[list.size()]; 60 list.toArray(markers); 61 62 setClipboard(markers, createBookmarkReport(markers)); 63 } 64 65 68 public void selectionChanged(IStructuredSelection sel) { 69 setEnabled(!sel.isEmpty()); 70 } 71 72 private void setClipboard(IMarker[] markers, String markerReport) { 73 try { 74 Object [] data = new Object [] { markers, markerReport }; 76 Transfer[] transferTypes = new Transfer[] { 77 MarkerTransfer.getInstance(), TextTransfer.getInstance() }; 78 79 getView().getClipboard().setContents(data, transferTypes); 81 } catch (SWTError e) { 82 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) { 83 throw e; 84 } 85 if (MessageDialog 86 .openQuestion( 87 getView().getShell(), 88 BookmarkMessages.CopyToClipboardProblemDialog_title, BookmarkMessages.CopyToClipboardProblemDialog_message)) { 89 setClipboard(markers, markerReport); 90 } 91 } 92 } 93 94 private String createBookmarkReport(IMarker[] markers) { 95 String report = ""; 97 report += BookmarkMessages.ColumnDescription_header + '\t'; 99 report += BookmarkMessages.ColumnResource_header + '\t'; 100 report += BookmarkMessages.ColumnFolder_header + '\t'; 101 report += BookmarkMessages.ColumnLocation_header; 102 report += System.getProperty("line.separator"); 104 for (int i = 0; i < markers.length; i++) { 106 report += MarkerUtil.getMessage(markers[i]) + '\t'; 107 report += MarkerUtil.getResourceName(markers[i]) + '\t'; 108 report += MarkerUtil.getContainerName(markers[i]) + '\t'; 109 int line = MarkerUtil.getLineNumber(markers[i]); 110 report += NLS.bind(BookmarkMessages.LineIndicator_text, String.valueOf(line)); 111 report += System.getProperty("line.separator"); } 113 114 return report; 115 } 116 } 117 118 | Popular Tags |