1
16package org.tp23.antinstaller.input;
17
18
19import java.util.MissingResourceException;
20import java.util.ResourceBundle;
21
22import org.tp23.antinstaller.InstallerContext;
23import org.tp23.antinstaller.ValidationException;
24
25
26
27
36public class SelectInput
37 extends InputField{
38
39 private static ResourceBundle langPack = null;
41 private int optionIdx = 0;
42 static{
43 try {
44 langPack = ResourceBundle.getBundle("resources.LanguagePack");
45 } catch (MissingResourceException e) {
46 }
48 }
49
50 private SelectInput.Option[] options;
51
52 public SelectInput() {
53 }
54
55
56 public SelectInput.Option[] getOptions() {
57 return options;
58 }
59
60 public void setOptions(SelectInput.Option[] options) {
61 this.options = options;
62 }
63 public Option getNewOption(){
64 return new Option();
65 }
66
67 public class Option {
68
69 private int idx = ++optionIdx;
70 private String text;
71 public String value;
72
73 public void setText(String text) {
74 this.text = text;
75 }
76 public String getText() {
77 if(langPack != null){
78 return langPack.getString(getProperty() + "." + idx +".displayText");
79 }
80 return text;
81 }
82 }
83
84 public void setValue(String value){
85 setInputResult(value);
86 }
87
88 public boolean validate(InstallerContext cxt) throws ValidationException{
89 if(getInputResult() == null){
90 return false;
91 }
92 String value = getInputResult();
93 boolean ok = false;
94 for (int i = 0; i < options.length; i++) {
95 ok |= options[i].value.equals(value);
96 }
97 return ok;
98 }
99
00
01
02
07 public boolean validateObject() {
08 if(getDisplayText()==null){
09 System.out.println("Select:displayText must be set");
10 return false;
11 }
12 if(getProperty()==null){
13 System.out.println("Select:property must be set");
14 return false;
15 }
16 if(getDefaultValue()==null){
17 System.out.println("Select:defaultValue must be set");
18 return false;
19 }
20 if(getOptions()==null){
21 System.out.println("Select:option must have at least two options");
22 return false;
23 }
24 if(getOptions().length<2){
25 System.out.println("Select:option must have at least two options");
26 return false;
27 }
28 for (int i = 0; i < getOptions().length; i++) {
29 Option o = getOptions()[i];
30 if(o.getText()==null){
31 System.out.println("Select:option:text must be set");
32 return false;
33 }
34 if(o.value==null){
35 System.out.println("Select:option:value must be set");
36 return false;
37 }
38 }
39 boolean defaultExists = false;
40 for (int i = 0; i < getOptions().length; i++) {
41 Option o = getOptions()[i];
42 if(o.value.equals(getDefaultValue())){
43 defaultExists=true;
44 }
45 }
46 if(!defaultExists){
47 System.out.println("Select:option:Default must be one of the options");
48 return false;
49 }
50 return true;
51 }
52}
53