Author: tchemit Date: 2012-07-05 16:56:58 +0200 (Thu, 05 Jul 2012) New Revision: 2382 Url: http://nuiton.org/repositories/revision/jaxx/2382 Log: fixes #2163 Improve ActionWorker api Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/application/ActionWorker.java Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/application/ActionWorker.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/application/ActionWorker.java 2012-07-05 13:51:54 UTC (rev 2381) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/application/ActionWorker.java 2012-07-05 14:56:58 UTC (rev 2382) @@ -24,11 +24,13 @@ */ package jaxx.runtime.swing.application; +import com.google.common.base.Preconditions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.util.StringUtil; import javax.swing.SwingWorker; +import java.util.concurrent.Callable; /** * Action worker to execute a incoming action. @@ -43,7 +45,7 @@ protected final String actionLabel; - protected Runnable target; + protected Callable<R> target; protected ActionStatus status; @@ -58,38 +60,39 @@ } public ActionWorker(String actionLabel, Runnable target) { - this.target = target; + this.target = new RunnableBridge<R>(target); this.actionLabel = actionLabel; } - public Runnable getTarget() { + public Callable<R> getTarget() { return target; } public void setTarget(Runnable target) { - this.target = target; + setTarget(new RunnableBridge<R>(target)); } /** * Set the target to execute. * - * @param targetWithException the target to execute + * @param target the target to execute * @since 2.5.1 */ - public void setTarget(RunnableWithException targetWithException) { - setTarget(new RunnableBridge(targetWithException)); + public void setTarget(Callable<R> target) { + this.target = target; } @Override protected R doInBackground() throws Exception { + Preconditions.checkNotNull(target != null, "Targe field can not be null here."); startTime = System.nanoTime(); if (log.isDebugEnabled()) { log.debug("Action [" + getActionLabel() + "] is starting..."); } + R result = null; try { - getTarget().run(); - } catch (RunnableBridgeException e) { - error = (Exception) e.getCause(); + + result = getTarget().call(); } catch (Exception e) { error = e; } finally { @@ -97,7 +100,7 @@ log.debug("Action [" + getActionLabel() + "] is ending..."); } } - return null; + return result; } public boolean isFailed() { @@ -152,47 +155,23 @@ } /** - * Contract like {@link Runnable} but with exception autorized. + * Transform a {@link Runnable} into a {@link Callable}. * + * @param <R> type of return (used to maintain generic checks in this class). * @since 2.5.1 */ - public static interface RunnableWithException { + private static class RunnableBridge<R> implements Callable<R> { - void run() throws Exception; - } + private final Runnable target; - /** - * This exception is used by a {@link Runnable} when executing a - * {@link RunnableWithException} inside the - * {@link ActionWorker#doInBackground()} - * method. - * - * @since 2.5.1 - */ - private static class RunnableBridgeException extends RuntimeException { - - private static final long serialVersionUID = 1L; - - private RunnableBridgeException(Throwable cause) { - super(cause); - } - } - - public static class RunnableBridge implements Runnable { - - private final RunnableWithException target; - - public RunnableBridge(RunnableWithException target) { + public RunnableBridge(Runnable target) { this.target = target; } @Override - public void run() { - try { - target.run(); - } catch (Exception e) { - throw new RunnableBridgeException(e); - } + public R call() throws Exception { + target.run(); + return null; } }