JAL-3066 Create labels for fields and hack it to work with multiple valued parameters
[jalview.git] / src / jalview / ws / params / simple / Option.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ws.params.simple;
22
23 import jalview.ws.params.OptionI;
24
25 import java.net.URL;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 public class Option implements OptionI
30 {
31   String name;
32
33   String label;
34
35   /*
36    * current value in string format, or "null" if undefined
37    */
38   String value;
39
40   /*
41    * default value in string format, or "null" if undefined
42    */
43   String defvalue;
44
45   String description;
46
47   List<String> possibleVals;
48
49   /*
50    * optional display names corresponding to possibleVals
51    */
52   List<String> displayVals;
53
54   boolean required;
55
56   URL fdetails;
57
58   /**
59    * Copy constructor
60    * 
61    * @param opt
62    */
63   public Option(Option opt)
64   {
65     name = opt.name;
66     label = opt.label;
67     value = opt.value;
68     defvalue = opt.defvalue;
69     description = opt.description;
70     if (opt.possibleVals != null)
71     {
72       possibleVals = new ArrayList<>(opt.possibleVals);
73     }
74     required = opt.required;
75     // URLs are singletons - so we copy by reference. nasty but true.
76     fdetails = opt.fdetails;
77   }
78
79   public Option()
80   {
81   }
82
83   public Option(String name, String description, String label, boolean isrequired,
84       String defValue, String val, List<String> possibleVals, URL fdetails)
85   {
86     this(name, description, isrequired, defValue, val, possibleVals, fdetails);
87     this.label = label;
88   }
89
90   /**
91    * Constructor including display names for possible values
92    * 
93    * @param name2
94    * @param description2
95    * @param isrequired
96    * @param defValue
97    * @param val
98    * @param possibleVals
99    * @param fdetails
100    */
101   public Option(String name2, String description2, boolean isrequired,
102           String defValue, String val, List<String> possibleVals,
103           List<String> displayNames, URL fdetails)
104   {
105     name = name2;
106     description = description2;
107     this.value = val;
108     this.required = isrequired;
109     this.defvalue = defValue;
110     if (possibleVals != null)
111     {
112       this.possibleVals = new ArrayList<>(possibleVals);
113     }
114     if (displayNames != null)
115     {
116       this.displayVals = new ArrayList<>(displayNames);
117     }
118     this.fdetails = fdetails;
119   }
120
121   /**
122    * Constructor
123    * 
124    * @param name2
125    * @param description2
126    * @param isrequired
127    * @param defValue
128    * @param val
129    * @param possibleVals
130    * @param fdetails
131    */
132   public Option(String name2, String description2, boolean isrequired,
133           String defValue, String val, List<String> possibleVals,
134           URL fdetails)
135   {
136     this(name2, description2, isrequired, defValue, val, possibleVals, null,
137             fdetails);
138   }
139
140   @Override
141   public OptionI copy()
142   {
143     Option opt = new Option(this);
144     return opt;
145   }
146
147   /**
148    * toString method to help identify options in the debugger only
149    */
150   @Override
151   public String toString()
152   {
153     return this.getClass().getName() + ":" + name;
154   }
155
156   @Override
157   public String getName()
158   {
159     return name;
160   }
161
162   @Override
163   public String getLabel()
164   {
165     return label != null ? label : name;
166   }
167
168   @Override
169   public String getValue()
170   {
171     return value == null ? defvalue : value;
172   }
173
174   @Override
175   public void setValue(String selectedItem)
176   {
177     value = selectedItem;
178   }
179
180   @Override
181   public URL getFurtherDetails()
182   {
183     return fdetails;
184   }
185
186   @Override
187   public boolean isRequired()
188   {
189     return required;
190   }
191
192   @Override
193   public String getDescription()
194   {
195     return description;
196   }
197
198   @Override
199   public List<String> getPossibleValues()
200   {
201     return possibleVals;
202   }
203
204   @Override
205   public List<String> getDisplayNames()
206   {
207     return displayVals;
208   }
209 }