1 11 package org.eclipse.jdt.internal.junit.ui; 12 13 import java.io.BufferedReader ; 14 import java.io.IOException ; 15 import java.io.PrintWriter ; 16 import java.io.StringReader ; 17 import java.io.StringWriter ; 18 19 import org.eclipse.swt.SWTError; 20 import org.eclipse.swt.dnd.Clipboard; 21 import org.eclipse.swt.dnd.DND; 22 import org.eclipse.swt.dnd.TextTransfer; 23 import org.eclipse.swt.dnd.Transfer; 24 import org.eclipse.ui.help.WorkbenchHelp; 25 26 import org.eclipse.jface.action.Action; 27 import org.eclipse.jface.dialogs.MessageDialog; 28 import org.eclipse.jface.util.Assert; 29 30 33 public class CopyTraceAction extends Action { 34 private FailureTrace fView; 35 36 private final Clipboard fClipboard; 37 38 41 public CopyTraceAction(FailureTrace view, Clipboard clipboard) { 42 super(JUnitMessages.getString("CopyTrace.action.label")); Assert.isNotNull(clipboard); 44 WorkbenchHelp.setHelp(this, IJUnitHelpContextIds.COPYTRACE_ACTION); 45 fView= view; 46 fClipboard= clipboard; 47 } 48 49 52 public void run() { 53 String trace= fView.getTrace(); 54 if (trace == null) 55 trace= ""; 57 TextTransfer plainTextTransfer = TextTransfer.getInstance(); 58 try{ 59 fClipboard.setContents( 60 new String []{ convertLineTerminators(trace) }, 61 new Transfer[]{ plainTextTransfer }); 62 } catch (SWTError e){ 63 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) 64 throw e; 65 if (MessageDialog.openQuestion(fView.getComposite().getShell(), JUnitMessages.getString("CopyTraceAction.problem"), JUnitMessages.getString("CopyTraceAction.clipboard_busy"))) run(); 67 } 68 } 69 70 private String convertLineTerminators(String in) { 71 StringWriter stringWriter= new StringWriter (); 72 PrintWriter printWriter= new PrintWriter (stringWriter); 73 StringReader stringReader= new StringReader (in); 74 BufferedReader bufferedReader= new BufferedReader (stringReader); 75 String line; 76 try { 77 while ((line= bufferedReader.readLine()) != null) { 78 printWriter.println(line); 79 } 80 } catch (IOException e) { 81 return in; } 83 return stringWriter.toString(); 84 } 85 } 86 | Popular Tags |