986029c557573539d82f787d0d9efe6f29010115
[jalview.git] / src / jalview / ws2 / actions / alignment / AlignmentAction.java
1 package jalview.ws2.actions.alignment;
2
3 import java.util.List;
4 import java.util.Objects;
5
6 import jalview.api.AlignViewportI;
7 import jalview.viewmodel.AlignmentViewport;
8 import jalview.ws.params.ArgumentI;
9 import jalview.ws2.actions.BaseAction;
10 import jalview.ws2.actions.BaseTask;
11 import jalview.ws2.actions.PollingTaskExecutor;
12 import jalview.ws2.actions.api.TaskEventListener;
13 import jalview.ws2.actions.api.TaskI;
14 import jalview.ws2.api.Credentials;
15 import jalview.ws2.client.api.AlignmentWebServiceClientI;
16
17 /**
18  * Implementation of the {@link BaseAction} that runs alignment services. This
19  * type of action requires {@link AlignmentWebServiceClientI} to retrieve
20  * alignment result from the server.
21  * 
22  * @author mmwarowny
23  *
24  */
25 public class AlignmentAction extends BaseAction<AlignmentResult>
26 {
27   /**
28    * A builder for AlignemntActions. Adds {@code client} and {@code submitGaps}
29    * parameters to the base builder.
30    * 
31    * @author mmwarowny
32    */
33   public static class Builder extends BaseAction.Builder<AlignmentAction>
34   {
35     protected AlignmentWebServiceClientI client;
36
37     protected boolean submitGaps = false;
38
39     public Builder(AlignmentWebServiceClientI client)
40     {
41       super();
42       Objects.requireNonNull(client);
43       this.client = client;
44     }
45
46     public void submitGaps(boolean val)
47     {
48       submitGaps = val;
49     }
50
51     public AlignmentAction build()
52     {
53       return new AlignmentAction(this);
54     }
55   }
56
57   public static Builder newBuilder(AlignmentWebServiceClientI client)
58   {
59     return new Builder(client);
60   }
61
62   protected final boolean submitGaps;
63
64   protected final AlignmentWebServiceClientI client;
65
66   public AlignmentAction(Builder builder)
67   {
68     super(builder);
69     submitGaps = builder.submitGaps;
70     client = builder.client;
71   }
72
73   @Deprecated
74   public TaskI<AlignmentResult> perform(AlignmentViewport viewport,
75       List<ArgumentI> args, Credentials credentials,
76       TaskEventListener<AlignmentResult> handler)
77   {
78     var task = createTask(viewport, args, credentials);
79     var executor = PollingTaskExecutor.fromPool(viewport.getServiceExecutor());
80     task.addTaskEventListener(handler);
81     var future = executor.submit(task);
82     task.setCancelAction(() -> {
83       future.cancel(true);
84     });
85     return task;
86   }
87
88   @Override
89   public AlignmentTask createTask(AlignViewportI viewport,
90       List<ArgumentI> args, Credentials credentials)
91   {
92     return new AlignmentTask(
93         client, this, args, credentials, viewport, submitGaps);
94   }
95
96   /**
97    * Returns if the action is active for the given viewport. Alignment services
98    * are non-interactive, so the action is never active.
99    */
100   @Override
101   public boolean isActive(AlignmentViewport viewport)
102   {
103     return false;
104   }
105
106 }