8a1d382006bce4c9348877981b7812359702531b
[jalview.git] / src / jalview / ws / slivkaws / SlivkaParamSet.java
1 package jalview.ws.slivkaws;
2
3 import jalview.ws.params.ArgumentI;
4 import jalview.ws.params.WsParamSetI;
5 import jalview.ws.params.simple.BooleanOption;
6 import jalview.ws.params.simple.DoubleParameter;
7 import jalview.ws.params.simple.IntegerParameter;
8 import jalview.ws.params.simple.StringParameter;
9
10 import java.io.IOException;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14
15 import uk.ac.dundee.compbio.slivkaclient.Parameter;
16 import uk.ac.dundee.compbio.slivkaclient.SlivkaService;
17
18 public class SlivkaParamSet implements WsParamSetI
19 {
20   private SlivkaService service;
21
22   private List<ArgumentI> args = new ArrayList<>();
23
24   SlivkaParamSet(SlivkaService service) throws IOException
25   {
26     this.service = service;
27     for (Parameter param : service.getParameters())
28     {
29       Object defaultValue = param.getDefault() instanceof List ? ((List<?>) param.getDefault()).get(0)
30           : param.getDefault();
31       if (param instanceof Parameter.FlagParameter)
32       {
33         args.add(new BooleanOption(param.getId(), param.getDescription(),
34             param.getName(), param.isRequired(), (Boolean) defaultValue, null));
35       }
36       else if (param instanceof Parameter.TextParameter)
37       {
38         args.add(new StringParameter(param.getId(), param.getDescription(),
39             param.isRequired(), (String) defaultValue, (String) defaultValue));
40       }
41       else if (param instanceof Parameter.IntegerParameter)
42       {
43         Integer min = ((Parameter.IntegerParameter) param).getMin();
44         Integer max = ((Parameter.IntegerParameter) param).getMax();
45         Integer defVal = defaultValue != null ? ((Number) defaultValue).intValue() : null;
46         args.add(new IntegerParameter(param.getId(), param.getDescription(),
47             param.isRequired(), defVal, (min == null) ? Integer.MIN_VALUE : min,
48             (max == null) ? Integer.MAX_VALUE : max));
49       }
50       else if (param instanceof Parameter.DecimalParameter)
51       {
52         Double min = ((Parameter.DecimalParameter) param).getMin();
53         Double max = ((Parameter.DecimalParameter) param).getMax();
54         Double defVal = defaultValue != null ? ((Number) defaultValue).doubleValue() : null;
55         args.add(new DoubleParameter(param.getId(), param.getDescription(),
56             param.isRequired(), defVal, (min == null) ? -Double.MAX_VALUE : min,
57             (max == null) ? Double.MAX_VALUE : max));
58       }
59       else if (param instanceof Parameter.ChoiceParameter)
60       {
61         List<String> choices = ((Parameter.ChoiceParameter) param)
62             .getChoices();
63         if (param.isArray())
64         {
65           int i = 0;
66           List<?> selected = param.getDefault() != null
67               ? (List<?>) param.getDefault()
68               : Collections.EMPTY_LIST;
69           for (String choice : choices)
70           {
71             args.add(new BooleanOption(
72                 String.format("%s$%d", param.getId(), i++),
73                 param.getDescription(), choice, param.isRequired(),
74                 selected.contains(choice), choice, null));
75           }
76         }
77         else
78         {
79           args.add(new StringParameter(
80               param.getId(), param.getDescription(),
81               param.isRequired(), (String) param.getDefault(),
82               (String) defaultValue, choices, choices));
83         }
84       }
85       else if (param instanceof Parameter.FileParameter)
86       {
87         // skip: files are provided from sequences
88       }
89       else
90       {
91         String defaultVal = param.getDefault() != null
92             ? param.getDefault().toString()
93             : null;
94         args.add(new StringParameter(param.getId(), param.getDescription(),
95             param.isRequired(), defaultVal, defaultVal));
96       }
97     }
98   }
99
100   @Override
101   public String getName()
102   {
103     return "Default";
104   }
105
106   @Override
107   public String getDescription()
108   {
109     return "";
110   }
111
112   @Override
113   public String[] getApplicableUrls()
114   {
115     return new String[] { service.getUrl().toString() };
116   }
117
118   @Override
119   public String getSourceFile()
120   {
121     return null;
122   }
123
124   @Override
125   public void setSourceFile(String newfile)
126   {
127   }
128
129   @Override
130   public boolean isModifiable()
131   {
132     return true;
133   }
134
135   @Override
136   public List<ArgumentI> getArguments()
137   {
138     return args;
139   }
140
141   @Override
142   public void setArguments(List<ArgumentI> args)
143   {
144     throw new RuntimeException();
145   }
146
147 }