From 4ab54fe231569ee20c82845bfaab9baf1f6bceab Mon Sep 17 00:00:00 2001 From: Ted Williams Date: Tue, 23 Jan 2007 17:11:32 +0000 Subject: [PATCH] this executor utilizes the SWT UI dispatch thread. --- .../ui/concurrent/SWTDispatchDsfExecutor.java | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 plugins/org.eclipse.dd.dsf.ui/src/org/eclipse/dd/dsf/ui/concurrent/SWTDispatchDsfExecutor.java diff --git a/plugins/org.eclipse.dd.dsf.ui/src/org/eclipse/dd/dsf/ui/concurrent/SWTDispatchDsfExecutor.java b/plugins/org.eclipse.dd.dsf.ui/src/org/eclipse/dd/dsf/ui/concurrent/SWTDispatchDsfExecutor.java new file mode 100644 index 00000000000..71e22929638 --- /dev/null +++ b/plugins/org.eclipse.dd.dsf.ui/src/org/eclipse/dd/dsf/ui/concurrent/SWTDispatchDsfExecutor.java @@ -0,0 +1,137 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems 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: + * Wind River Systems - initial API and implementation + *******************************************************************************/ + +package org.eclipse.dd.dsf.ui.concurrent; + +import java.util.concurrent.Callable; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +import org.eclipse.dd.dsf.concurrent.DefaultDsfExecutor; +import org.eclipse.swt.widgets.Display; + +public class SWTDispatchDsfExecutor extends DefaultDsfExecutor +{ + + public SWTDispatchDsfExecutor() + { + super(); + } + + private Callable createSWTDispatchCallable(final Callable callable) + { + return new Callable() + { + public V call() throws Exception + { + final Object[] v = new Object[1]; + final Throwable[] e = new Throwable[1]; + + Display.getDefault().syncExec(new Runnable() + { + public void run() + { + try + { + v[0] = callable.call(); + } + catch(Throwable exception) + { + e[0] = exception; + } + } + }); + + if(e[0] instanceof RuntimeException) + throw (RuntimeException) e[0]; + else if(e[0] instanceof Exception) + throw (Exception) e[0]; + + return (V) v[0]; + } + }; + } + + private Runnable createSWTDispatchRunnable(final Runnable runnable) + { + return new Runnable() + { + public void run() + { + final Throwable[] e = new Throwable[1]; + + Display.getDefault().syncExec(new Runnable() + { + public void run() + { + try + { + runnable.run(); + } + catch(Throwable exception) + { + e[0] = exception; + } + } + }); + + if(e[0] instanceof RuntimeException) + throw (RuntimeException) e[0]; + } + }; + } + + @Override + public ScheduledFuture schedule(final Callable callable, long delay, + TimeUnit unit) { + return super.schedule(createSWTDispatchCallable(callable), delay, unit); + } + + @Override + public ScheduledFuture schedule(Runnable command, long delay, + TimeUnit unit) { + return super.schedule(createSWTDispatchRunnable(command), delay, unit); + } + + @Override + public ScheduledFuture scheduleAtFixedRate(Runnable command, + long initialDelay, long period, TimeUnit unit) { + return super.scheduleAtFixedRate(createSWTDispatchRunnable(command), initialDelay, period, unit); + } + + @Override + public ScheduledFuture scheduleWithFixedDelay(Runnable command, + long initialDelay, long delay, TimeUnit unit) { + return super.scheduleWithFixedDelay(createSWTDispatchRunnable(command), initialDelay, delay, unit); + } + + @Override + public void execute(Runnable command) { + super.execute(createSWTDispatchRunnable(command)); + } + + @Override + public Future submit(Callable callable) { + return super.submit(createSWTDispatchCallable(callable)); + } + + @Override + public Future submit(Runnable command, T result) { + return super.submit(createSWTDispatchRunnable(command), result); + } + + @Override + public Future submit(Runnable command) { + return super.submit(createSWTDispatchRunnable(command)); + } + +}