Source code: org/eclipse/jface/util/SafeRunnable.java
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.jface.util;
12
13 import org.eclipse.core.runtime.*;
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.jface.resource.JFaceResources;
16
17 /**
18 * Implements a default implementation of ISafeRunnable.
19 * The default implementation of <code>handleException</code>
20 * opens a message dialog.
21 * <p><b>Note:<b> This class can open an error dialog and should not
22 * be used outside of the UI Thread.</p>
23 */
24 public abstract class SafeRunnable implements ISafeRunnable {
25 private String message;
26 private static boolean ignoreErrors = false;
27
28 /**
29 * Creates a new instance of SafeRunnable with a default error message.
30 */
31 public SafeRunnable() {
32 // do nothing
33 }
34
35 /**
36 * Creates a new instance of SafeRunnable with the given error message.
37 *
38 * @param message the error message to use
39 */
40 public SafeRunnable(String message) {
41 this.message = message;
42 }
43
44 /* (non-Javadoc)
45 * Method declared on ISafeRunnable.
46 */
47 public void handleException(Throwable e) {
48 // Workaround to avoid interactive error dialogs during automated testing
49 if (!ignoreErrors) {
50 if(message == null)
51 message = JFaceResources.getString("SafeRunnable.errorMessage"); //$NON-NLS-1$
52 MessageDialog.openError(null, JFaceResources.getString("Error"), message); //$NON-NLS-1$
53 }
54 }
55
56 /**
57 * Flag to avoid interactive error dialogs during automated testing.
58 * @deprecated use getIgnoreErrors()
59 */
60 public static boolean getIgnoreErrors(boolean flag) {
61 return ignoreErrors;
62 }
63
64 /**
65 * Flag to avoid interactive error dialogs during automated testing.
66 *
67 * @since 3.0
68 */
69 public static boolean getIgnoreErrors() {
70 return ignoreErrors;
71 }
72
73 /**
74 * Flag to avoid interactive error dialogs during automated testing.
75 */
76 public static void setIgnoreErrors(boolean flag) {
77 ignoreErrors = flag;
78 }
79 }