KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-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.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 /**
34  * Copies a test failure stack trace to the clipboard.
35  */

36 public class JUnitCopyAction extends SelectionListenerAction {
37     private FailureTrace fView;
38     
39     private final Clipboard fClipboard;
40
41     private TestElement fTestElement;
42
43     /**
44      * Constructor for CopyTraceAction.
45      * @param view
46      * @param clipboard
47      */

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     /*
57      * @see IAction#run()
58      */

59     public void run() {
60         String JavaDoc trace= fView.getTrace();
61         String JavaDoc 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 JavaDoc[]{ 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 JavaDoc convertLineTerminators(String JavaDoc in) {
89         StringWriter JavaDoc stringWriter= new StringWriter JavaDoc();
90         PrintWriter JavaDoc printWriter= new PrintWriter JavaDoc(stringWriter);
91         StringReader JavaDoc stringReader= new StringReader JavaDoc(in);
92         BufferedReader JavaDoc bufferedReader= new BufferedReader JavaDoc(stringReader);
93         String JavaDoc line;
94         try {
95             while ((line= bufferedReader.readLine()) != null) {
96                 printWriter.println(line);
97             }
98         } catch (IOException JavaDoc e) {
99             return in; // return the trace unfiltered
100
}
101         return stringWriter.toString();
102     }
103 }
104
Popular Tags