Merge branch 'JAL-3878_ws-overhaul-3' into mmw/Release_2_12_ws_merge
[jalview.git] / src / jalview / ws2 / actions / alignment / AlignmentAction.java
diff --git a/src/jalview/ws2/actions/alignment/AlignmentAction.java b/src/jalview/ws2/actions/alignment/AlignmentAction.java
new file mode 100644 (file)
index 0000000..7f935bf
--- /dev/null
@@ -0,0 +1,92 @@
+package jalview.ws2.actions.alignment;
+
+import java.util.List;
+import java.util.Objects;
+
+import jalview.viewmodel.AlignmentViewport;
+import jalview.ws.params.ArgumentI;
+import jalview.ws2.actions.BaseAction;
+import jalview.ws2.actions.api.TaskEventListener;
+import jalview.ws2.actions.api.TaskI;
+import jalview.ws2.api.Credentials;
+import jalview.ws2.client.api.AlignmentWebServiceClientI;
+
+/**
+ * Implementation of the {@link BaseAction} that runs alignment services. This
+ * type of action requires {@link AlignmentWebServiceClientI} to retrieve
+ * alignment result from the server.
+ * 
+ * @author mmwarowny
+ *
+ */
+public class AlignmentAction extends BaseAction<AlignmentResult>
+{
+  /**
+   * A builder for AlignemntActions. Adds {@code client} and {@code submitGaps}
+   * parameters to the base builder.
+   * 
+   * @author mmwarowny
+   */
+  public static class Builder extends BaseAction.Builder<AlignmentAction>
+  {
+    protected AlignmentWebServiceClientI client;
+
+    protected boolean submitGaps = false;
+
+    public Builder(AlignmentWebServiceClientI client)
+    {
+      super();
+      Objects.requireNonNull(client);
+      this.client = client;
+    }
+
+    public void submitGaps(boolean val)
+    {
+      submitGaps = val;
+    }
+
+    public AlignmentAction build()
+    {
+      return new AlignmentAction(this);
+    }
+  }
+
+  public static Builder newBuilder(AlignmentWebServiceClientI client)
+  {
+    return new Builder(client);
+  }
+
+  protected final boolean submitGaps;
+
+  protected final AlignmentWebServiceClientI client;
+
+  public AlignmentAction(Builder builder)
+  {
+    super(builder);
+    submitGaps = builder.submitGaps;
+    client = builder.client;
+  }
+
+  @Override
+  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());
+    return task;
+  }
+
+  /**
+   * Returns if the action is active for the given viewport. Alignment services
+   * are non-interactive, so the action is never active.
+   */
+  @Override
+  public boolean isActive(AlignmentViewport viewport)
+  {
+    return false;
+  }
+
+}