KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.swt.SWTError;
14 import org.eclipse.swt.dnd.Clipboard;
15 import org.eclipse.swt.dnd.DND;
16 import org.eclipse.swt.dnd.TextTransfer;
17 import org.eclipse.swt.dnd.Transfer;
18
19 import org.eclipse.jface.action.Action;
20 import org.eclipse.jface.dialogs.MessageDialog;
21
22 import org.eclipse.ui.PlatformUI;
23
24 import org.eclipse.jdt.internal.ui.JavaPlugin;
25
26 import org.eclipse.jdt.internal.junit.model.TestElement;
27
28 /**
29  * Copies the names of the methods that failed and their traces to the clipboard.
30  */

31 public class CopyFailureListAction extends Action {
32     
33     private final Clipboard fClipboard;
34     private final TestRunnerViewPart fRunner;
35         
36     public CopyFailureListAction(TestRunnerViewPart runner, Clipboard clipboard) {
37         super(JUnitMessages.CopyFailureList_action_label);
38         fRunner= runner;
39         fClipboard= clipboard;
40         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJUnitHelpContextIds.COPYFAILURELIST_ACTION);
41     }
42
43     /*
44      * @see IAction#run()
45      */

46     public void run() {
47         TextTransfer plainTextTransfer = TextTransfer.getInstance();
48                     
49         try {
50             fClipboard.setContents(
51                     new String JavaDoc[] { getAllFailureTraces() },
52                     new Transfer[] { plainTextTransfer });
53         } catch (SWTError e){
54             if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD)
55                 throw e;
56             if (MessageDialog.openQuestion(JavaPlugin.getActiveWorkbenchShell(), JUnitMessages.CopyFailureList_problem, JUnitMessages.CopyFailureList_clipboard_busy))
57                 run();
58         }
59     }
60     
61     public String JavaDoc getAllFailureTraces() {
62         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
63         TestElement[] failures= fRunner.getAllFailures();
64         
65         String JavaDoc lineDelim= System.getProperty("line.separator", "\n"); //$NON-NLS-1$//$NON-NLS-2$
66
for (int i= 0; i < failures.length; i++) {
67             TestElement failure= failures[i];
68             buf.append(failure.getTestName()).append(lineDelim);
69             String JavaDoc failureTrace= failure.getTrace();
70             if (failureTrace != null) {
71                 int start= 0;
72                 while (start < failureTrace.length()) {
73                     int idx= failureTrace.indexOf('\n', start);
74                     if (idx != -1) {
75                         String JavaDoc line= failureTrace.substring(start, idx);
76                         buf.append(line).append(lineDelim);
77                         start= idx + 1;
78                     } else {
79                         start= Integer.MAX_VALUE;
80                     }
81                 }
82             }
83         }
84         return buf.toString();
85     }
86
87
88 }
89
Popular Tags