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 | try {
|
---|
74 | String r = org.tp23.antinstaller.Installer.langPack.getString( getProperty() + "." + idx + ".displayText" );
|
---|
75 | return r;
|
---|
76 | } catch ( java.util.MissingResourceException mre ) {} // restoring original comment: ignore, signifies no language packs installed
|
---|
77 | }
|
---|
78 | return text;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void setValue(String value){
|
---|
83 | setInputResult(value);
|
---|
84 | if ( useAsLocale ) {
|
---|
85 | //expect something like 'en' or or something like 'en_US'
|
---|
86 | Locale newLocale = null;
|
---|
87 | if ( value.length() == 2 ) {
|
---|
88 | newLocale = new Locale(value);
|
---|
89 | Locale.setDefault( newLocale );
|
---|
90 | } else if ( value.length() == 5 ) {
|
---|
91 | newLocale = new Locale(value.substring(0,2), value.substring(3,5));
|
---|
92 | Locale.setDefault( newLocale );
|
---|
93 | } //else { you're out of luck }
|
---|
94 | org.tp23.antinstaller.Installer.langPack = ResourceBundle.getBundle("resources.LanguagePack", newLocale );
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | public boolean validate(InstallerContext cxt) throws ValidationException{
|
---|
99 | if(getInputResult() == null){
|
---|
100 | return false;
|
---|
101 | }
|
---|
102 | String value = getInputResult();
|
---|
103 | boolean ok = false;
|
---|
104 | for (int i = 0; i < options.length; i++) {
|
---|
105 | ok |= options[i].value.equals(value);
|
---|
106 | }
|
---|
107 | return ok;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Used by checkConfig to validate the configuration file.
|
---|
114 | * Not used at runtime.
|
---|
115 | * @return boolean
|
---|
116 | */
|
---|
117 | public boolean validateObject() {
|
---|
118 | if(getDisplayText()==null){
|
---|
119 | System.out.println("Select:displayText must be set");
|
---|
120 | return false;
|
---|
121 | }
|
---|
122 | if(getProperty()==null){
|
---|
123 | System.out.println("Select:property must be set");
|
---|
124 | return false;
|
---|
125 | }
|
---|
126 | if(getDefaultValue()==null){
|
---|
127 | System.out.println("Select:defaultValue must be set");
|
---|
128 | return false;
|
---|
129 | }
|
---|
130 | if(getOptions()==null){
|
---|
131 | System.out.println("Select:option must have at least two options");
|
---|
132 | return false;
|
---|
133 | }
|
---|
134 | if(getOptions().length<2){
|
---|
135 | System.out.println("Select:option must have at least two options");
|
---|
136 | return false;
|
---|
137 | }
|
---|
138 | for (int i = 0; i < getOptions().length; i++) {
|
---|
139 | Option o = getOptions()[i];
|
---|
140 | if(o.getText()==null){
|
---|
141 | System.out.println("Select:option:text must be set");
|
---|
142 | return false;
|
---|
143 | }
|
---|
144 | if(o.value==null){
|
---|
145 | System.out.println("Select:option:value must be set");
|
---|
146 | return false;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | boolean defaultExists = false;
|
---|
150 | for (int i = 0; i < getOptions().length; i++) {
|
---|
151 | Option o = getOptions()[i];
|
---|
152 | if(o.value.equals(getDefaultValue())){
|
---|
153 | defaultExists=true;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | if(!defaultExists){
|
---|
157 | System.out.println("Select:option:Default must be one of the options");
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 | return true;
|
---|
161 | }
|
---|
162 | }
|
---|