1 11 package org.eclipse.jdt.internal.ui.callhierarchy; 12 13 import java.util.Iterator ; 14 15 import org.eclipse.swt.SWTError; 16 import org.eclipse.swt.dnd.Clipboard; 17 import org.eclipse.swt.dnd.DND; 18 import org.eclipse.swt.dnd.TextTransfer; 19 import org.eclipse.swt.dnd.Transfer; 20 21 import org.eclipse.jface.action.Action; 22 import org.eclipse.jface.dialogs.MessageDialog; 23 import org.eclipse.jface.viewers.ISelectionChangedListener; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 import org.eclipse.jface.viewers.SelectionChangedEvent; 26 27 import org.eclipse.ui.IViewSite; 28 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds; 29 30 import org.eclipse.jdt.internal.corext.callhierarchy.CallLocation; 31 32 35 class LocationCopyAction extends Action { 36 private final Clipboard fClipboard; 37 private final IViewSite fViewSite; 38 private final LocationViewer fLocationViewer; 39 40 LocationCopyAction(IViewSite viewSite, Clipboard clipboard, LocationViewer locationViewer) { 41 fClipboard= clipboard; 42 fViewSite= viewSite; 43 fLocationViewer= locationViewer; 44 45 setText(CallHierarchyMessages.LocationCopyAction_copy); 46 setActionDefinitionId(IWorkbenchActionDefinitionIds.COPY); 47 48 locationViewer.addSelectionChangedListener(new ISelectionChangedListener() { 49 public void selectionChanged(SelectionChangedEvent event) { 50 setEnabled(! event.getSelection().isEmpty()); 51 } 52 }); 53 } 54 55 public void run() { 56 IStructuredSelection selection= (IStructuredSelection) fLocationViewer.getSelection(); 57 StringBuffer buf= new StringBuffer (); 58 for (Iterator iterator= selection.iterator(); iterator.hasNext();) { 59 CallLocation location= (CallLocation) iterator.next(); 60 buf.append(location.getLineNumber()).append('\t').append(location.getCallText()); 61 buf.append('\n'); 62 } 63 TextTransfer plainTextTransfer = TextTransfer.getInstance(); 64 try { 65 fClipboard.setContents( 66 new String []{ CopyCallHierarchyAction.convertLineTerminators(buf.toString()) }, 67 new Transfer[]{ plainTextTransfer }); 68 } catch (SWTError e){ 69 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) 70 throw e; 71 if (MessageDialog.openQuestion(fViewSite.getShell(), CallHierarchyMessages.CopyCallHierarchyAction_problem, CallHierarchyMessages.CopyCallHierarchyAction_clipboard_busy)) 72 run(); 73 } 74 } 75 } | Popular Tags |