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.core.runtime.Assert; 20 21 import org.eclipse.swt.SWTError; 22 import org.eclipse.swt.dnd.Clipboard; 23 import org.eclipse.swt.dnd.DND; 24 import org.eclipse.swt.dnd.TextTransfer; 25 import org.eclipse.swt.dnd.Transfer; 26 import org.eclipse.ui.PlatformUI; 27 import org.eclipse.ui.actions.SelectionListenerAction; 28 29 import org.eclipse.jdt.internal.junit.model.TestElement; 30 31 import org.eclipse.jface.dialogs.MessageDialog; 32 33 36 public class JUnitCopyAction extends SelectionListenerAction { 37 private FailureTrace fView; 38 39 private final Clipboard fClipboard; 40 41 private TestElement fTestElement; 42 43 48 public JUnitCopyAction(FailureTrace view, Clipboard clipboard) { 49 super(JUnitMessages.CopyTrace_action_label); 50 Assert.isNotNull(clipboard); 51 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJUnitHelpContextIds.COPYTRACE_ACTION); 52 fView= view; 53 fClipboard= clipboard; 54 } 55 56 59 public void run() { 60 String trace= fView.getTrace(); 61 String source= null; 62 if (trace != null) { 63 source= convertLineTerminators(trace); 64 } else if (fTestElement != null) { 65 source= fTestElement.getTestName(); 66 } 67 if (source == null || source.length() == 0) 68 return; 69 70 TextTransfer plainTextTransfer = TextTransfer.getInstance(); 71 try{ 72 fClipboard.setContents( 73 new String []{ convertLineTerminators(source) }, 74 new Transfer[]{ plainTextTransfer }); 75 } catch (SWTError e){ 76 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) 77 throw e; 78 if (MessageDialog.openQuestion(fView.getComposite().getShell(), JUnitMessages.CopyTraceAction_problem, JUnitMessages.CopyTraceAction_clipboard_busy)) 79 run(); 80 } 81 } 82 83 84 public void handleTestSelected(TestElement test) { 85 fTestElement= test; 86 } 87 88 private String convertLineTerminators(String in) { 89 StringWriter stringWriter= new StringWriter (); 90 PrintWriter printWriter= new PrintWriter (stringWriter); 91 StringReader stringReader= new StringReader (in); 92 BufferedReader bufferedReader= new BufferedReader (stringReader); 93 String line; 94 try { 95 while ((line= bufferedReader.readLine()) != null) { 96 printWriter.println(line); 97 } 98 } catch (IOException e) { 99 return in; } 101 return stringWriter.toString(); 102 } 103 } 104 | Popular Tags |