1 12 package org.eclipse.jdt.internal.ui.callhierarchy; 13 14 import java.io.BufferedReader ; 15 import java.io.IOException ; 16 import java.io.PrintWriter ; 17 import java.io.StringReader ; 18 import java.io.StringWriter ; 19 20 import org.eclipse.core.runtime.Assert; 21 22 import org.eclipse.swt.SWTError; 23 import org.eclipse.swt.dnd.Clipboard; 24 import org.eclipse.swt.dnd.DND; 25 import org.eclipse.swt.dnd.TextTransfer; 26 import org.eclipse.swt.dnd.Transfer; 27 import org.eclipse.swt.widgets.TreeItem; 28 29 import org.eclipse.jface.action.Action; 30 import org.eclipse.jface.dialogs.MessageDialog; 31 import org.eclipse.jface.viewers.ISelection; 32 import org.eclipse.jface.viewers.ISelectionProvider; 33 34 import org.eclipse.ui.PlatformUI; 35 36 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 37 import org.eclipse.jdt.internal.ui.util.SelectionUtil; 38 39 class CopyCallHierarchyAction extends Action { 40 private static final char INDENTATION= '\t'; 41 42 private CallHierarchyViewPart fView; 43 private CallHierarchyViewer fViewer; 44 45 private final Clipboard fClipboard; 46 47 public CopyCallHierarchyAction(CallHierarchyViewPart view, Clipboard clipboard, CallHierarchyViewer viewer) { 48 super(CallHierarchyMessages.CopyCallHierarchyAction_label); 49 Assert.isNotNull(clipboard); 50 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CALL_HIERARCHY_COPY_ACTION); 51 fView= view; 52 fClipboard= clipboard; 53 fViewer= viewer; 54 } 55 56 public boolean canActionBeAdded() { 57 Object element = SelectionUtil.getSingleElement(getSelection()); 58 return element != null; 59 } 60 61 private ISelection getSelection() { 62 ISelectionProvider provider = fView.getSite().getSelectionProvider(); 63 64 if (provider != null) { 65 return provider.getSelection(); 66 } 67 68 return null; 69 } 70 71 74 public void run() { 75 StringBuffer buf= new StringBuffer (); 76 addCalls(fViewer.getTree().getSelection()[0], 0, buf); 77 78 TextTransfer plainTextTransfer = TextTransfer.getInstance(); 79 try{ 80 fClipboard.setContents( 81 new String []{ convertLineTerminators(buf.toString()) }, 82 new Transfer[]{ plainTextTransfer }); 83 } catch (SWTError e){ 84 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) 85 throw e; 86 if (MessageDialog.openQuestion(fView.getViewSite().getShell(), CallHierarchyMessages.CopyCallHierarchyAction_problem, CallHierarchyMessages.CopyCallHierarchyAction_clipboard_busy)) 87 run(); 88 } 89 } 90 91 97 private void addCalls(TreeItem item, int indent, StringBuffer buf) { 98 for (int i= 0; i < indent; i++) { 99 buf.append(INDENTATION); 100 } 101 102 buf.append(item.getText()); 103 buf.append('\n'); 104 105 if (item.getExpanded()) { 106 TreeItem[] items= item.getItems(); 107 for (int i= 0; i < items.length; i++) { 108 addCalls(items[i], indent + 1, buf); 109 } 110 } 111 } 112 113 static String convertLineTerminators(String in) { 114 StringWriter stringWriter= new StringWriter (); 115 PrintWriter printWriter= new PrintWriter (stringWriter); 116 StringReader stringReader= new StringReader (in); 117 BufferedReader bufferedReader= new BufferedReader (stringReader); 118 try { 119 String line= bufferedReader.readLine(); 120 while (line != null) { 121 printWriter.print(line); 122 line= bufferedReader.readLine(); 123 if (line != null && line.length() != 0) 124 printWriter.println(); 125 126 } 127 } catch (IOException e) { 128 return in; } 130 return stringWriter.toString(); 131 } 132 } 133 | Popular Tags |