Author: tchemit Date: 2010-06-05 09:50:59 +0200 (Sat, 05 Jun 2010) New Revision: 1949 Url: http://nuiton.org/repositories/revision/jaxx/1949 Log: ActionWorker is generic Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/application/ActionExecutor.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/application/ActionWorker.java Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/application/ActionExecutor.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/application/ActionExecutor.java 2010-06-05 07:50:34 UTC (rev 1948) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/application/ActionExecutor.java 2010-06-05 07:50:59 UTC (rev 1949) @@ -51,7 +51,7 @@ LogFactory.getLog(ActionExecutor.class); /** current tasks */ - protected final Set<ActionWorker> tasks = new HashSet<ActionWorker>(); + protected final Set<ActionWorker<?, ?>> tasks = new HashSet<ActionWorker<?, ?>>(); /** the listener of running action */ protected final PropertyChangeListener workerListener; @@ -61,36 +61,35 @@ * * @param source the action worker containing the action to perform */ - public abstract void onActionStart(ActionWorker source); + public abstract void onActionStart(ActionWorker<?, ?> source); /** * Hook when a action has failed. * * @param source the action worker containing the action to perform */ - public abstract void onActionFail(ActionWorker source); + public abstract void onActionFail(ActionWorker<?, ?> source); - /** * Hook when a action has been canceled. * * @param source the action worker containing the action to perform */ - public abstract void onActionCancel(ActionWorker source); + public abstract void onActionCancel(ActionWorker<?, ?> source); /** * Hook when a action has end with no failure or cancel. * * @param source the action worker containing the action to perform */ - public abstract void onActionEnd(ActionWorker source); + public abstract void onActionEnd(ActionWorker<?, ?> source); /** * Hook atfer action is consumed. * * @param source the action worker containing the action to perform */ - public abstract void onAfterAction(ActionWorker source); + public abstract void onAfterAction(ActionWorker<?, ?> source); public ActionExecutor() { workerListener = new PropertyChangeListener() { @@ -105,11 +104,10 @@ } if ("state".equals(evt.getPropertyName())) { - ActionWorker source = (ActionWorker) evt.getSource(); + ActionWorker<?, ?> source = (ActionWorker<?, ?>) evt.getSource(); SwingWorker.StateValue state = (SwingWorker.StateValue) evt.getNewValue(); - if (state == SwingWorker.StateValue.STARTED) { // starting new action @@ -156,13 +154,14 @@ * @param action the action to perform * @return the worker that will launch the action */ - public ActionWorker addAction(String actionLabel, Runnable action) { + public ActionWorker<?, ?> addAction(String actionLabel, Runnable action) { - ActionWorker worker; + ActionWorker<?, ?> worker; if (action instanceof ActionWorker) { - worker = (ActionWorker) action; + worker = (ActionWorker<?, ?>) action; } else { + worker = new ActionWorker(actionLabel, action); } worker.addPropertyChangeListener(workerListener); @@ -189,7 +188,7 @@ } // ask executor to terminate - for (ActionWorker task : tasks) { + for (ActionWorker<?, ?> task : tasks) { task.cancel(true); } @@ -210,7 +209,7 @@ return getTasks().size(); } - public Set<ActionWorker> getTasks() { + public Set<ActionWorker<?, ?>> getTasks() { return tasks; } 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 2010-06-05 07:50:34 UTC (rev 1948) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/application/ActionWorker.java 2010-06-05 07:50:59 UTC (rev 1949) @@ -36,16 +36,14 @@ * @author tchemit <chemit@codelutin.com> * @since 2.1 */ -public class ActionWorker extends SwingWorker<Void, Object> { +public class ActionWorker<R,P> extends SwingWorker<R, P> { - /** Logger */ - private static final Log log = - LogFactory.getLog(ActionWorker.class); + private static final Log log = LogFactory.getLog(ActionWorker.class); protected final String actionLabel; - protected final Runnable target; + protected Runnable target; protected ActionStatus status; @@ -55,19 +53,31 @@ protected long endTime; + public ActionWorker(String actionLabel) { + this.actionLabel = actionLabel; + } + public ActionWorker(String actionLabel, Runnable target) { this.target = target; this.actionLabel = actionLabel; } + public Runnable getTarget() { + return target; + } + + public void setTarget(Runnable target) { + this.target = target; + } + @Override - protected Void doInBackground() throws Exception { + protected R doInBackground() throws Exception { startTime = System.nanoTime(); if (log.isDebugEnabled()) { log.debug("Action [" + getActionLabel() + "] is starting..."); } try { - target.run(); + getTarget().run(); } catch (Exception e) { error = e; } finally {
participants (1)
-
tchemit@users.nuiton.org