Author: tchemit Date: 2012-07-05 15:51:54 +0200 (Thu, 05 Jul 2012) New Revision: 2381 Url: http://nuiton.org/repositories/revision/jaxx/2381 Log: #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-04 16:12:58 UTC (rev 2380) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/application/ActionWorker.java 2012-07-05 13:51:54 UTC (rev 2381) @@ -70,6 +70,16 @@ this.target = target; } + /** + * Set the target to execute. + * + * @param targetWithException the target to execute + * @since 2.5.1 + */ + public void setTarget(RunnableWithException targetWithException) { + setTarget(new RunnableBridge(targetWithException)); + } + @Override protected R doInBackground() throws Exception { startTime = System.nanoTime(); @@ -78,6 +88,8 @@ } try { getTarget().run(); + } catch (RunnableBridgeException e) { + error = (Exception) e.getCause(); } catch (Exception e) { error = e; } finally { @@ -139,4 +151,49 @@ FAIL } + /** + * Contract like {@link Runnable} but with exception autorized. + * + * @since 2.5.1 + */ + public static interface RunnableWithException { + + void run() throws Exception; + } + + /** + * 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) { + this.target = target; + } + + @Override + public void run() { + try { + target.run(); + } catch (Exception e) { + throw new RunnableBridgeException(e); + } + } + } + }