JAL-1601 Create alignment copying method in AlignmentUtils
[jalview.git] / src / jalview / ws2 / actions / BaseAction.java
1 package jalview.ws2.actions;
2
3 import java.util.EnumSet;
4 import java.util.List;
5 import java.util.Objects;
6
7 import jalview.gui.AlignViewport;
8 import jalview.viewmodel.AlignmentViewport;
9 import jalview.ws.params.ArgumentI;
10 import jalview.ws2.actions.api.ActionI;
11 import jalview.ws2.actions.api.TaskEventListener;
12 import jalview.ws2.actions.api.TaskI;
13 import jalview.ws2.api.CredentialType;
14 import jalview.ws2.api.Credentials;
15 import jalview.ws2.api.WebService;
16
17 /**
18  * An abstract base class storing common data and implementing their getters
19  * defined in {@link ActionI} interface. The concrete action implementations are
20  * encouraged to extend this class and provide their own {@code perform} and
21  * {@code isActive} implementations.
22  * 
23  * @author mmwarowny
24  * @param <R>
25  *          task result type
26  */
27 public abstract class BaseAction<R> implements ActionI<R>
28 {
29   public static abstract class Builder<A extends BaseAction<?>>
30   {
31     protected WebService<A> webService;
32
33     protected String name = null;
34
35     protected String tooltip = "";
36
37     protected String subcategory = null;
38
39     protected int minSequences = -1;
40
41     protected int maxSequences = -1;
42
43     protected boolean allowProtein = true;
44
45     protected boolean allowNucleotide = true;
46
47     protected EnumSet<CredentialType> requiredCredentials = EnumSet.noneOf(CredentialType.class);
48
49     public Builder()
50     {
51     }
52
53     public void name(String val)
54     {
55       this.name = val;
56     }
57
58     public void webService(WebService<A> val)
59     {
60       this.webService = val;
61     }
62
63     public void tooltip(String val)
64     {
65       tooltip = val;
66     }
67
68     public void subcategory(String val)
69     {
70       subcategory = val;
71     }
72
73     public void minSequences(int val)
74     {
75       minSequences = val;
76     }
77
78     public void maxSequecnes(int val)
79     {
80       maxSequences = val;
81     }
82
83     public void allowProtein(boolean val)
84     {
85       allowProtein = val;
86     }
87
88     public void allowNucleotide(boolean val)
89     {
90       allowNucleotide = val;
91     }
92
93     public void addRequiredCredential(CredentialType val)
94     {
95       requiredCredentials.add(val);
96     }
97
98     public void requiredCredentials(EnumSet<CredentialType> val)
99     {
100       requiredCredentials = val;
101     }
102   }
103
104   protected final WebService<? extends ActionI<R>> webService;
105
106   protected final String name;
107
108   protected final String tooltip;
109
110   protected final String subcategory;
111
112   protected final int minSequences;
113
114   protected final int maxSequences;
115
116   protected final boolean allowProtein;
117
118   protected final boolean allowNucleotide;
119
120   protected final EnumSet<CredentialType> requiredCredentials;
121
122   protected BaseAction(Builder<? extends BaseAction<R>> builder)
123   {
124     Objects.requireNonNull(builder.webService);
125     this.webService = builder.webService;
126     this.name = builder.name;
127     this.tooltip = builder.tooltip;
128     this.subcategory = builder.subcategory;
129     this.minSequences = builder.minSequences;
130     this.maxSequences = builder.maxSequences;
131     this.allowProtein = builder.allowProtein;
132     this.allowNucleotide = builder.allowNucleotide;
133     this.requiredCredentials = builder.requiredCredentials;
134   }
135
136   @Override
137   public WebService<? extends ActionI<R>> getWebService()
138   {
139     return webService;
140   }
141
142   @Override
143   public String getName()
144   {
145     return name;
146   }
147
148   /**
149    * Returns a full name of the action which comprises of the service name and
150    * the action name if present.
151    * 
152    * @return full name of this action
153    */
154   public String getFullName()
155   {
156     if (name == null || name.isEmpty())
157       return webService.getName();
158     else
159       return webService.getName() + " " + name;
160   }
161
162   @Override
163   public String getTooltip()
164   {
165     return tooltip;
166   }
167
168   @Override
169   public String getSubcategory()
170   {
171     return subcategory;
172   }
173
174   @Override
175   public int getMinSequences()
176   {
177     return minSequences;
178   }
179
180   @Override
181   public int getMaxSequences()
182   {
183     return maxSequences;
184   }
185
186   @Override
187   public boolean doAllowProtein()
188   {
189     return allowProtein;
190   }
191
192   @Override
193   public boolean doAllowNucleotide()
194   {
195     return allowNucleotide;
196   }
197
198   @Override
199   public EnumSet<CredentialType> getRequiredCredentials()
200   {
201     return requiredCredentials;
202   }
203 }