KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > ui > CopyTraceAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.junit.ui;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.io.StringReader JavaDoc;
17 import java.io.StringWriter JavaDoc;
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 /**
31  * Copies a test failure stack trace to the clipboard.
32  */

33 public class CopyTraceAction extends Action {
34     private FailureTrace fView;
35     
36     private final Clipboard fClipboard;
37
38     /**
39      * Constructor for CopyTraceAction.
40      */

41     public CopyTraceAction(FailureTrace view, Clipboard clipboard) {
42         super(JUnitMessages.getString("CopyTrace.action.label")); //$NON-NLS-1$
43
Assert.isNotNull(clipboard);
44         WorkbenchHelp.setHelp(this, IJUnitHelpContextIds.COPYTRACE_ACTION);
45         fView= view;
46         fClipboard= clipboard;
47     }
48
49     /*
50      * @see IAction#run()
51      */

52     public void run() {
53         String JavaDoc trace= fView.getTrace();
54         if (trace == null)
55             trace= ""; //$NON-NLS-1$
56

57         TextTransfer plainTextTransfer = TextTransfer.getInstance();
58         try{
59             fClipboard.setContents(
60                 new String JavaDoc[]{ 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"))) //$NON-NLS-1$ //$NON-NLS-2$
66
run();
67         }
68     }
69     
70     private String JavaDoc convertLineTerminators(String JavaDoc in) {
71         StringWriter JavaDoc stringWriter= new StringWriter JavaDoc();
72         PrintWriter JavaDoc printWriter= new PrintWriter JavaDoc(stringWriter);
73         StringReader JavaDoc stringReader= new StringReader JavaDoc(in);
74         BufferedReader JavaDoc bufferedReader= new BufferedReader JavaDoc(stringReader);
75         String JavaDoc line;
76         try {
77             while ((line= bufferedReader.readLine()) != null) {
78                 printWriter.println(line);
79             }
80         } catch (IOException JavaDoc e) {
81             return in; // return the trace unfiltered
82
}
83         return stringWriter.toString();
84     }
85 }
86
Popular Tags