| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2000, 2008 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 | * Chris Gross (schtoo@schtoo.com) - support for ISafeRunnableRunner added |
| 11 | * (bug 49497 [RCP] JFace dependency on org.eclipse.core.runtime enlarges standalone JFace applications) |
| 12 | *******************************************************************************/ |
| 13 | package org.eclipse.jface.util; |
| 14 | |
| 15 | import org.eclipse.core.runtime.ISafeRunnable; |
| 16 | import org.eclipse.core.runtime.IStatus; |
| 17 | import org.eclipse.core.runtime.OperationCanceledException; |
| 18 | import org.eclipse.core.runtime.Status; |
| 19 | import org.eclipse.jface.resource.JFaceResources; |
| 20 | |
| 21 | /** |
| 22 | * Implements a default implementation of ISafeRunnable. The default |
| 23 | * implementation of <code>handleException</code> opens a dialog to show any |
| 24 | * errors as they accumulate. |
| 25 | * <p> |
| 26 | * This may be executed on any thread. |
| 27 | */ |
| 28 | public abstract class SafeRunnable implements ISafeRunnable { |
| 29 | |
| 30 | private static boolean ignoreErrors = false; |
| 31 | |
| 32 | private static ISafeRunnableRunner runner; |
| 33 | |
| 34 | private String message; |
| 35 | |
| 36 | /** |
| 37 | * Creates a new instance of SafeRunnable with a default error message. |
| 38 | */ |
| 39 | public SafeRunnable() { |
| 40 | // do nothing |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Creates a new instance of SafeRunnable with the given error message. |
| 45 | * |
| 46 | * @param message |
| 47 | * the error message to use |
| 48 | */ |
| 49 | public SafeRunnable(String message) { |
| 50 | this.message = message; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * (non-Javadoc) |
| 55 | * |
| 56 | * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable) |
| 57 | */ |
| 58 | public void handleException(Throwable e) { |
| 59 | // Workaround to avoid interactive error dialogs during |
| 60 | // automated testing |
| 61 | if (ignoreErrors) |
| 62 | return; |
| 63 | |
| 64 | if (message == null) |
| 65 | message = JFaceResources.getString("SafeRunnable.errorMessage"); //$NON-NLS-1$ |
| 66 | |
| 67 | Policy.getStatusHandler().show( |
| 68 | new Status(IStatus.ERROR, Policy.JFACE, message, e), |
| 69 | JFaceResources.getString("SafeRunnable.errorMessage")); //$NON-NLS-1$ |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Flag to avoid interactive error dialogs during automated testing. |
| 74 | * |
| 75 | * @param flag |
| 76 | * @return true if errors should be ignored |
| 77 | * @deprecated use getIgnoreErrors() |
| 78 | */ |
| 79 | public static boolean getIgnoreErrors(boolean flag) { |
| 80 | return ignoreErrors; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Flag to avoid interactive error dialogs during automated testing. |
| 85 | * |
| 86 | * @return true if errors should be ignored |
| 87 | * |
| 88 | * @since 3.0 |
| 89 | */ |
| 90 | public static boolean getIgnoreErrors() { |
| 91 | return ignoreErrors; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Flag to avoid interactive error dialogs during automated testing. |
| 96 | * |
| 97 | * @param flag |
| 98 | * set to true if errors should be ignored |
| 99 | */ |
| 100 | public static void setIgnoreErrors(boolean flag) { |
| 101 | ignoreErrors = flag; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns the safe runnable runner. |
| 106 | * |
| 107 | * @return the safe runnable runner |
| 108 | * |
| 109 | * @since 3.1 |
| 110 | */ |
| 111 | public static ISafeRunnableRunner getRunner() { |
| 112 | if (runner == null) { |
| 113 | runner = createDefaultRunner(); |
| 114 | } |
| 115 | return runner; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Creates the default safe runnable runner. |
| 120 | * |
| 121 | * @return the default safe runnable runner |
| 122 | * @since 3.1 |
| 123 | */ |
| 124 | private static ISafeRunnableRunner createDefaultRunner() { |
| 125 | return new ISafeRunnableRunner() { |
| 126 | public void run(ISafeRunnable code) { |
| 127 | try { |
| 128 | code.run(); |
| 129 | } catch (Exception e) { |
| 130 | handleException(code, e); |
| 131 | } catch (LinkageError e) { |
| 132 | handleException(code, e); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private void handleException(ISafeRunnable code, Throwable e) { |
| 137 | if (!(e instanceof OperationCanceledException)) { |
| 138 | try { |
| 139 | Policy.getLog() |
| 140 | .log( |
| 141 | new Status(IStatus.ERROR, Policy.JFACE, |
| 142 | IStatus.ERROR, |
| 143 | "Exception occurred", e)); //$NON-NLS-1$ |
| 144 | } catch (Exception ex) { |
| 145 | e.printStackTrace(); |
| 146 | } |
| 147 | } |
| 148 | code.handleException(e); |
| 149 | } |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Sets the safe runnable runner. |
| 155 | * |
| 156 | * @param runner |
| 157 | * the runner to set, or <code>null</code> to reset to the |
| 158 | * default runner |
| 159 | * @since 3.1 |
| 160 | */ |
| 161 | public static void setRunner(ISafeRunnableRunner runner) { |
| 162 | SafeRunnable.runner = runner; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Runs the given safe runnable using the safe runnable runner. This is a |
| 167 | * convenience method, equivalent to: |
| 168 | * <code>SafeRunnable.getRunner().run(runnable)</code>. |
| 169 | * |
| 170 | * @param runnable |
| 171 | * the runnable to run |
| 172 | * @since 3.1 |
| 173 | */ |
| 174 | public static void run(ISafeRunnable runnable) { |
| 175 | getRunner().run(runnable); |
| 176 | } |
| 177 | |
| 178 | } |