File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\ChoiceParameterDefinition.java.html

1 package hudson.model;
2
3 import org.kohsuke.stapler.StaplerRequest;
4 import org.kohsuke.stapler.DataBoundConstructor;
5 import org.kohsuke.stapler.export.Exported;
6 import org.apache.commons.lang.StringUtils;
7 import net.sf.json.JSONObject;
8 import hudson.Extension;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Arrays;
13
14 /**
15 * @author huybrechts
16 */
17 public class ChoiceParameterDefinition extends SimpleParameterDefinition {
18 private final List<String> choices;
19 private final String defaultValue;
20
21 @DataBoundConstructor
22 public ChoiceParameterDefinition(String name, String choices, String description) {
23 super(name, description);
24 this.choices = Arrays.asList(choices.split("\\r?\\n"));
25 if (choices.length()==0) {
26 throw new IllegalArgumentException("No choices found");
27 }
28 defaultValue = null;
29 }
30
31 public ChoiceParameterDefinition(String name, String[] choices, String description) {
32 super(name, description);
33 this.choices = new ArrayList<String>(Arrays.asList(choices));
34 if (this.choices.isEmpty()) {
35 throw new IllegalArgumentException("No choices found");
36 }
37 defaultValue = null;
38 }
39
40 private ChoiceParameterDefinition(String name, List<String> choices, String defaultValue, String description) {
41 super(name, description);
42 this.choices = choices;
43 this.defaultValue = defaultValue;
44 }
45
46 @Override
47 public ParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) {
48 if (defaultValue instanceof StringParameterValue) {
49 StringParameterValue value = (StringParameterValue) defaultValue;
50 return new ChoiceParameterDefinition(getName(), choices, value.value, getDescription());
51 } else {
52 return this;
53 }
54 }
55
56 @Exported
57 public List<String> getChoices() {
58 return choices;
59 }
60
61 public String getChoicesText() {
62 return StringUtils.join(choices, "\n");
63 }
64
65 @Override
66 public StringParameterValue getDefaultParameterValue() {
67 return new StringParameterValue(getName(), defaultValue == null ? choices.get(0) : defaultValue, getDescription());
68 }
69
70 private StringParameterValue checkValue(StringParameterValue value) {
71 if (!choices.contains(value.value))
72 throw new IllegalArgumentException("Illegal choice: " + value.value);
73 return value;
74 }
75
76 @Override
77 public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
78 StringParameterValue value = req.bindJSON(StringParameterValue.class, jo);
79 value.setDescription(getDescription());
80 return checkValue(value);
81 }
82
83 public StringParameterValue createValue(String value) {
84 return checkValue(new StringParameterValue(getName(), value, getDescription()));
85 }
86
87 @Extension
88 public static class DescriptorImpl extends ParameterDescriptor {
89 @Override
90 public String getDisplayName() {
91 return Messages.ChoiceParameterDefinition_DisplayName();
92 }
93
94 @Override
95 public String getHelpFile() {
96 return "/help/parameter/choice.html";
97 }
98 }
99
100 }