1 | /*
|
---|
2 | * Copyright 2005 Paul Hinds
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 | package org.tp23.antinstaller.input;
|
---|
17 |
|
---|
18 |
|
---|
19 | import java.util.MissingResourceException;
|
---|
20 | import java.util.ResourceBundle;
|
---|
21 | import java.util.Locale;
|
---|
22 | import org.tp23.antinstaller.InstallerContext;
|
---|
23 | import org.tp23.antinstaller.ValidationException;
|
---|
24 | import org.tp23.antinstaller.Installer;
|
---|
25 |
|
---|
26 |
|
---|
27 | /**
|
---|
28 | *
|
---|
29 | * <p>Input type to choose a single value from a (numbered) list of options </p>
|
---|
30 | * <p>N.B. subclassed for TargetSelectInput </p>
|
---|
31 | * <p>Copyright: Copyright (c) 2004</p>
|
---|
32 | * <p>Company: tp23</p>
|
---|
33 | * @author Paul Hinds
|
---|
34 | * @version $Id: SelectInput.java,v 1.4 2006/12/07 02:50:27 teknopaul Exp $
|
---|
35 | */
|
---|
36 | public class SelectInput
|
---|
37 | extends InputField {
|
---|
38 |
|
---|
39 | private int optionIdx = 0;
|
---|
40 | private SelectInput.Option[] options;
|
---|
41 | private boolean useAsLocale = false;
|
---|
42 |
|
---|
43 | public SelectInput() {
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void setUseAsLocale(boolean ual) {
|
---|
47 | this.useAsLocale = ual;
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | public SelectInput.Option[] getOptions() {
|
---|
52 | return options;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public void setOptions(SelectInput.Option[] options) {
|
---|
56 | this.options = options;
|
---|
57 | }
|
---|
58 | public Option getNewOption(){
|
---|
59 | return new Option();
|
---|
60 | }
|
---|
61 |
|
---|
62 | public class Option {
|
---|
63 |
|
---|
64 | private int idx = ++optionIdx;
|
---|
65 | private String text;
|
---|
66 | public String value;
|
---|
67 |
|
---|
68 | public void setText(String text) {
|
---|
69 | this.text = text;
|
---|
70 | }
|
---|
71 | public String getText() {
|
---|
72 | if( org.tp23.antinstaller.Installer.langPack != null ){
|
---|
73 | return org.tp23.antinstaller.Installer.langPack.getString(getProperty() + "." + idx +".displayText");
|
---|
74 | }
|
---|
75 | return text;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void setValue(String value){
|
---|
80 | setInputResult(value);
|
---|
81 | if ( useAsLocale ) {
|
---|
82 | //expect something like 'en' or or something like 'en_US'
|
---|
83 | if ( value.length() == 2 ) {
|
---|
84 | Locale.setDefault( new Locale(value) );
|
---|
85 | } else if ( value.length() == 5 ) {
|
---|
86 | Locale.setDefault( new Locale(value.substring(0,2), value.substring(3,5)) );
|
---|
87 | } //else { you're out of luck }
|
---|
88 | org.tp23.antinstaller.Installer.langPack = ResourceBundle.getBundle("resources.LanguagePack");
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public boolean validate(InstallerContext cxt) throws ValidationException{
|
---|
93 | if(getInputResult() == null){
|
---|
94 | return false;
|
---|
95 | }
|
---|
96 | String value = getInputResult();
|
---|
97 | boolean ok = false;
|
---|
98 | for (int i = 0; i < options.length; i++) {
|
---|
99 | ok |= options[i].value.equals(value);
|
---|
100 | }
|
---|
101 | return ok;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Used by checkConfig to validate the configuration file.
|
---|
108 | * Not used at runtime.
|
---|
109 | * @return boolean
|
---|
110 | */
|
---|
111 | public boolean validateObject() {
|
---|
112 | if(getDisplayText()==null){
|
---|
113 | System.out.println("Select:displayText must be set");
|
---|
114 | return false;
|
---|
115 | }
|
---|
116 | if(getProperty()==null){
|
---|
117 | System.out.println("Select:property must be set");
|
---|
118 | return false;
|
---|
119 | }
|
---|
120 | if(getDefaultValue()==null){
|
---|
121 | System.out.println("Select:defaultValue must be set");
|
---|
122 | return false;
|
---|
123 | }
|
---|
124 | if(getOptions()==null){
|
---|
125 | System.out.println("Select:option must have at least two options");
|
---|
126 | return false;
|
---|
127 | }
|
---|
128 | if(getOptions().length<2){
|
---|
129 | System.out.println("Select:option must have at least two options");
|
---|
130 | return false;
|
---|
131 | }
|
---|
132 | for (int i = 0; i < getOptions().length; i++) {
|
---|
133 | Option o = getOptions()[i];
|
---|
134 | if(o.getText()==null){
|
---|
135 | System.out.println("Select:option:text must be set");
|
---|
136 | return false;
|
---|
137 | }
|
---|
138 | if(o.value==null){
|
---|
139 | System.out.println("Select:option:value must be set");
|
---|
140 | return false;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | boolean defaultExists = false;
|
---|
144 | for (int i = 0; i < getOptions().length; i++) {
|
---|
145 | Option o = getOptions()[i];
|
---|
146 | if(o.value.equals(getDefaultValue())){
|
---|
147 | defaultExists=true;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | if(!defaultExists){
|
---|
151 | System.out.println("Select:option:Default must be one of the options");
|
---|
152 | return false;
|
---|
153 | }
|
---|
154 | return true;
|
---|
155 | }
|
---|
156 | }
|
---|