From e5b04620dec84525a8e425503599f6338e31191e Mon Sep 17 00:00:00 2001 From: Mateusz Warowny Date: Tue, 4 Jul 2023 10:13:53 +0200 Subject: [PATCH] JAL-4199 Change actions to be task factories --- .../ws2/actions/alignment/AlignmentAction.java | 24 ++++++++++++++++---- .../ws2/actions/annotation/AnnotationAction.java | 15 +++++------- src/jalview/ws2/actions/api/ActionI.java | 15 +++++------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/jalview/ws2/actions/alignment/AlignmentAction.java b/src/jalview/ws2/actions/alignment/AlignmentAction.java index 7f935bf..986029c 100644 --- a/src/jalview/ws2/actions/alignment/AlignmentAction.java +++ b/src/jalview/ws2/actions/alignment/AlignmentAction.java @@ -3,9 +3,12 @@ package jalview.ws2.actions.alignment; import java.util.List; import java.util.Objects; +import jalview.api.AlignViewportI; import jalview.viewmodel.AlignmentViewport; import jalview.ws.params.ArgumentI; import jalview.ws2.actions.BaseAction; +import jalview.ws2.actions.BaseTask; +import jalview.ws2.actions.PollingTaskExecutor; import jalview.ws2.actions.api.TaskEventListener; import jalview.ws2.actions.api.TaskI; import jalview.ws2.api.Credentials; @@ -67,18 +70,29 @@ public class AlignmentAction extends BaseAction client = builder.client; } - @Override + @Deprecated public TaskI perform(AlignmentViewport viewport, List args, Credentials credentials, TaskEventListener handler) { - var msa = viewport.getAlignmentView(true); - var task = new AlignmentTask( - client, this, args, credentials, msa, viewport, submitGaps, handler); - task.start(viewport.getServiceExecutor()); + var task = createTask(viewport, args, credentials); + var executor = PollingTaskExecutor.fromPool(viewport.getServiceExecutor()); + task.addTaskEventListener(handler); + var future = executor.submit(task); + task.setCancelAction(() -> { + future.cancel(true); + }); return task; } + @Override + public AlignmentTask createTask(AlignViewportI viewport, + List args, Credentials credentials) + { + return new AlignmentTask( + client, this, args, credentials, viewport, submitGaps); + } + /** * Returns if the action is active for the given viewport. Alignment services * are non-interactive, so the action is never active. diff --git a/src/jalview/ws2/actions/annotation/AnnotationAction.java b/src/jalview/ws2/actions/annotation/AnnotationAction.java index 02829fd..b8450a9 100644 --- a/src/jalview/ws2/actions/annotation/AnnotationAction.java +++ b/src/jalview/ws2/actions/annotation/AnnotationAction.java @@ -3,6 +3,7 @@ package jalview.ws2.actions.annotation; import java.util.List; import java.util.Objects; +import jalview.api.AlignViewportI; import jalview.viewmodel.AlignmentViewport; import jalview.ws.params.ArgumentI; import jalview.ws2.actions.BaseAction; @@ -84,18 +85,14 @@ public class AnnotationAction extends BaseAction requireAlignedSequences = builder.requireAlignedSequences; filterSymbols = builder.filterSymbols; } - + @Override - public TaskI perform(AlignmentViewport viewport, - List args, Credentials credentials, - TaskEventListener handler) + public AnnotationTask createTask(AlignViewportI viewport, + List args, Credentials credentials) { - var task = new AnnotationTask(client, this, args, credentials, viewport, - handler); - task.start(viewport.getCalcManager()); - return task; + return new AnnotationTask(client, this, args, credentials, viewport); } - + /** * Return if this action is an alignment analysis service. */ diff --git a/src/jalview/ws2/actions/api/ActionI.java b/src/jalview/ws2/actions/api/ActionI.java index 52d70df..e84fd58 100644 --- a/src/jalview/ws2/actions/api/ActionI.java +++ b/src/jalview/ws2/actions/api/ActionI.java @@ -5,6 +5,7 @@ import java.util.List; import javax.swing.Icon; +import jalview.api.AlignViewportI; import jalview.viewmodel.AlignmentViewport; import jalview.ws.params.ArgumentI; import jalview.ws2.api.CredentialType; @@ -115,10 +116,8 @@ public interface ActionI EnumSet getRequiredCredentials(); /** - * Run the action, create and start a new task with provided viewport, - * arguments and credentials and attach the handler to the task. The - * implementations of this method are responsible for starting the task using - * execution method appropriate for the action class. + * Create a new task with provided viewport, arguments and credentials ready + * to be started. * * @param viewport * current alignment viewport @@ -126,12 +125,10 @@ public interface ActionI * job parameters appropriate for the service * @param credentials * optional user credentials - * @param handler - * event handler attached to the new task - * @return new running task + * @return new task */ - TaskI perform(AlignmentViewport viewport, List args, - Credentials credentials, TaskEventListener handler); + TaskI createTask(AlignViewportI viewport, List args, + Credentials credentials); /** * Return if the action is currently active for the given viewport. Active -- 1.7.10.2