37fbe3928189b309891f741f45b218073955eabf
[jalview.git] / src / jalview / ws2 / actions / BaseAction.java
1 package jalview.ws2.actions;
2
3 import java.util.EnumSet;
4
5 import jalview.ws2.actions.api.ActionI;
6 import jalview.ws2.api.CredentialType;
7
8 /**
9  * An abstract base class storing common data and implementing their getters
10  * defined in {@link ActionI} interface. The concrete action implementations are
11  * encouraged to extend this class and provide their own {@code perform} and
12  * {@code isActive} implementations.
13  * 
14  * @author mmwarowny
15  *
16  * @param <T>
17  *          task result type
18  */
19 public abstract class BaseAction<T> implements ActionI<T>
20 {
21   public static class Builder
22   {
23     protected String name;
24
25     protected String tooltip = "";
26
27     protected String subcategory = null;
28
29     protected int minSequences = -1;
30
31     protected int maxSequences = -1;
32
33     protected boolean allowProtein = true;
34
35     protected boolean allowNucleotide = true;
36
37     protected EnumSet<CredentialType> requiredCredentials = EnumSet.noneOf(CredentialType.class);
38
39     public Builder(String name)
40     {
41       this.name = name;
42     }
43
44     public void tooltip(String val)
45     {
46       tooltip = val;
47     }
48
49     public void subcategory(String val)
50     {
51       subcategory = val;
52     }
53
54     public void minSequences(int val)
55     {
56       minSequences = val;
57     }
58
59     public void maxSequecnes(int val)
60     {
61       maxSequences = val;
62     }
63
64     public void allowProtein(boolean val)
65     {
66       allowProtein = val;
67     }
68
69     public void allowNucleotide(boolean val)
70     {
71       allowNucleotide = val;
72     }
73
74     public void addRequiredCredential(CredentialType val)
75     {
76       requiredCredentials.add(val);
77     }
78
79     public void requiredCredentials(EnumSet<CredentialType> val)
80     {
81       requiredCredentials = val;
82     }
83   }
84
85   protected final String name;
86
87   protected final String tooltip;
88
89   protected final String subcategory;
90
91   protected final int minSequences;
92
93   protected final int maxSequences;
94
95   protected final boolean allowProtein;
96
97   protected final boolean allowNucleotide;
98
99   protected final EnumSet<CredentialType> requiredCredentials;
100
101   protected BaseAction(Builder builder)
102   {
103     this.name = builder.name;
104     this.tooltip = builder.tooltip;
105     this.subcategory = builder.subcategory;
106     this.minSequences = builder.minSequences;
107     this.maxSequences = builder.maxSequences;
108     this.allowProtein = builder.allowProtein;
109     this.allowNucleotide = builder.allowNucleotide;
110     this.requiredCredentials = builder.requiredCredentials;
111   }
112
113   public static Builder newBuilder(String name)
114   {
115     return new Builder(name);
116   }
117
118   @Override
119   public String getName()
120   {
121     return name;
122   }
123
124   @Override
125   public String getTooltip()
126   {
127     return tooltip;
128   }
129
130   @Override
131   public String getSubcategory()
132   {
133     return subcategory;
134   }
135
136   @Override
137   public int getMinSequences()
138   {
139     return minSequences;
140   }
141
142   @Override
143   public int getMaxSequences()
144   {
145     return maxSequences;
146   }
147
148   @Override
149   public boolean doAllowProtein()
150   {
151     return allowProtein;
152   }
153
154   @Override
155   public boolean doAllowNucleotide()
156   {
157     return allowNucleotide;
158   }
159
160   @Override
161   public EnumSet<CredentialType> getRequiredCredentials()
162   {
163     return requiredCredentials;
164   }
165 }