Mercurial > projects > dwt-addons
diff dwtx/jface/util/SafeRunnable.d @ 10:b6c35faf97c8
Viewers
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 31 Mar 2008 00:47:19 +0200 |
parents | |
children | 644f1334b451 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtx/jface/util/SafeRunnable.d Mon Mar 31 00:47:19 2008 +0200 @@ -0,0 +1,213 @@ +/******************************************************************************* + * Copyright (c) 2000, 2007 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * Chris Gross (schtoo@schtoo.com) - support for ISafeRunnableRunner added + * (bug 49497 [RCP] JFace dependency on dwtx.core.runtime enlarges standalone JFace applications) + * Port to the D programming language: + * Frank Benoit <benoit@tionex.de> + *******************************************************************************/ +module dwtx.jface.util.SafeRunnable; + +import dwtx.jface.util.ISafeRunnableRunner; +import dwtx.jface.util.SafeRunnableDialog; +import dwtx.jface.util.Policy; + +import dwt.events.DisposeEvent; +import dwt.events.DisposeListener; +import dwt.widgets.Display; +import dwtx.core.runtime.ISafeRunnable; +import dwtx.core.runtime.IStatus; +import dwtx.core.runtime.OperationCanceledException; +import dwtx.core.runtime.Status; +import dwtx.jface.resource.JFaceResources; + +import dwt.dwthelper.utils; +import dwt.dwthelper.Runnable; + +/** + * Implements a default implementation of ISafeRunnable. The default + * implementation of <code>handleException</code> opens a message dialog. + * <p> + * <b>Note:<b> This class can open an error dialog and should not be used + * outside of the UI Thread. + * </p> + */ +public abstract class SafeRunnable : ISafeRunnable { + + private static bool ignoreErrors = false; + + private static ISafeRunnableRunner runner; + + private String message; + + private static SafeRunnableDialog dialog; + + /** + * Creates a new instance of SafeRunnable with a default error message. + */ + public this() { + // do nothing + } + + /** + * Creates a new instance of SafeRunnable with the given error message. + * + * @param message + * the error message to use + */ + public this(String message) { + this.message = message; + } + + + /* (non-Javadoc) + * @see dwtx.core.runtime.ISafeRunnable#handleException(java.lang.Throwable) + */ + public void handleException(Exception e) { + // Workaround to avoid interactive error dialogs during automated + // testing + if (!ignoreErrors) { + if (message is null) + message = JFaceResources.getString("SafeRunnable.errorMessage"); //$NON-NLS-1$ + + Runnable runnable = new class Runnable { + IStatus status; + this(){ + status = new Status(IStatus.ERROR, Policy.JFACE, message,e); + } + public void run() { + if (dialog is null || dialog.getShell().isDisposed()) { + dialog = new SafeRunnableDialog(status); + dialog.create(); + dialog.getShell().addDisposeListener( + new class DisposeListener { + public void widgetDisposed(DisposeEvent e) { + dialog = null; + } + }); + dialog.open(); + } else { + dialog.addStatus(status); + dialog.refresh(); + } + } + }; + if (Display.getCurrent() !is null) { + runnable.run(); + } else { + Display.getDefault().asyncExec(runnable); + } + } + } + + /** + * Flag to avoid interactive error dialogs during automated testing. + * + * @param flag + * @return true if errors should be ignored + * @deprecated use getIgnoreErrors() + */ + public static bool getIgnoreErrors(bool flag) { + return ignoreErrors; + } + + /** + * Flag to avoid interactive error dialogs during automated testing. + * + * @return true if errors should be ignored + * + * @since 3.0 + */ + public static bool getIgnoreErrors() { + return ignoreErrors; + } + + /** + * Flag to avoid interactive error dialogs during automated testing. + * + * @param flag + * set to true if errors should be ignored + */ + public static void setIgnoreErrors(bool flag) { + ignoreErrors = flag; + } + + /** + * Returns the safe runnable runner. + * + * @return the safe runnable runner + * + * @since 3.1 + */ + public static ISafeRunnableRunner getRunner() { + if (runner is null) { + runner = createDefaultRunner(); + } + return runner; + } + + /** + * Creates the default safe runnable runner. + * + * @return the default safe runnable runner + * @since 3.1 + */ + private static ISafeRunnableRunner createDefaultRunner() { + return new class ISafeRunnableRunner { + public void run(ISafeRunnable code) { + try { + code.run(); + } catch (Exception e) { + handleException(code, e); +// } catch (LinkageError e) { +// handleException(code, e); + } + } + + private void handleException(ISafeRunnable code, Exception e) { + if (!(cast(OperationCanceledException)e )) { + try { + Policy.getLog().log( + new Status(IStatus.ERROR, Policy.JFACE, + IStatus.ERROR, "Exception occurred", e)); //$NON-NLS-1$ + } catch (Exception ex) { + ExceptionPrintStackTrace(e); + } + } + code.handleException(e); + } + }; + } + + /** + * Sets the safe runnable runner. + * + * @param runner + * the runner to set, or <code>null</code> to reset to the + * default runner + * @since 3.1 + */ + public static void setRunner(ISafeRunnableRunner runner) { + SafeRunnable.runner = runner; + } + + /** + * Runs the given safe runnable using the safe runnable runner. This is a + * convenience method, equivalent to: + * <code>SafeRunnable.getRunner().run(runnable)</code>. + * + * @param runnable + * the runnable to run + * @since 3.1 + */ + public static void run(ISafeRunnable runnable) { + getRunner().run(runnable); + } + +}