JAL-4199 Change actions to be task factories
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Tue, 4 Jul 2023 08:13:53 +0000 (10:13 +0200)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Wed, 12 Jul 2023 14:25:05 +0000 (16:25 +0200)
src/jalview/ws2/actions/alignment/AlignmentAction.java
src/jalview/ws2/actions/annotation/AnnotationAction.java
src/jalview/ws2/actions/api/ActionI.java

index 7f935bf..986029c 100644 (file)
@@ -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<AlignmentResult>
     client = builder.client;
   }
 
-  @Override
+  @Deprecated
   public TaskI<AlignmentResult> perform(AlignmentViewport viewport,
       List<ArgumentI> args, Credentials credentials,
       TaskEventListener<AlignmentResult> 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<ArgumentI> 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.
index 02829fd..b8450a9 100644 (file)
@@ -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<AnnotationResult>
     requireAlignedSequences = builder.requireAlignedSequences;
     filterSymbols = builder.filterSymbols;
   }
-
+  
   @Override
-  public TaskI<AnnotationResult> perform(AlignmentViewport viewport,
-      List<ArgumentI> args, Credentials credentials,
-      TaskEventListener<AnnotationResult> handler)
+  public AnnotationTask createTask(AlignViewportI viewport,
+      List<ArgumentI> 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.
    */
index 52d70df..e84fd58 100644 (file)
@@ -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<R>
   EnumSet<CredentialType> 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<R>
    *          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<R> perform(AlignmentViewport viewport, List<ArgumentI> args,
-      Credentials credentials, TaskEventListener<R> handler);
+  TaskI<R> createTask(AlignViewportI viewport, List<ArgumentI> args,
+      Credentials credentials);
 
   /**
    * Return if the action is currently active for the given viewport. Active