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 |
|
---|
23 | import org.tp23.antinstaller.InstallerContext;
|
---|
24 | import org.tp23.antinstaller.ValidationException;
|
---|
25 |
|
---|
26 |
|
---|
27 |
|
---|
28 | /**
|
---|
29 | *
|
---|
30 | * <p>Input type to choose a single value from a (numbered) list of options </p>
|
---|
31 | * <p>This input is designed to handle larger lists of options </p>
|
---|
32 | * <p>Copyright: Copyright (c) 2004</p>
|
---|
33 | * <p>Company: tp23</p>
|
---|
34 | * @author Paul Hinds
|
---|
35 | * @version $Id: LargeSelectInput.java,v 1.3 2006/12/07 02:50:27 teknopaul Exp $
|
---|
36 | */
|
---|
37 | public class LargeSelectInput
|
---|
38 | extends InputField{
|
---|
39 |
|
---|
40 | private int optionIdx = 0;
|
---|
41 |
|
---|
42 | private LargeSelectInput.Option[] options;
|
---|
43 |
|
---|
44 | private boolean useAsLocale = false;
|
---|
45 |
|
---|
46 | public LargeSelectInput() {
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void setUseAsLocale(boolean ual) {
|
---|
50 | this.useAsLocale = ual;
|
---|
51 | }
|
---|
52 |
|
---|
53 | public LargeSelectInput.Option[] getOptions() {
|
---|
54 | return options;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public void setOptions(LargeSelectInput.Option[] options) {
|
---|
58 | this.options = options;
|
---|
59 | }
|
---|
60 | public Option getNewOption(){
|
---|
61 | return new Option();
|
---|
62 | }
|
---|
63 |
|
---|
64 | public class Option {
|
---|
65 |
|
---|
66 | private int idx = ++optionIdx;
|
---|
67 | private String text;
|
---|
68 | public String value;
|
---|
69 | public void setText(String text) {
|
---|
70 | this.text = text;
|
---|
71 | }
|
---|
72 | public String getText() {
|
---|
73 | if( org.tp23.antinstaller.Installer.langPack != null ) {
|
---|
74 | //return org.tp23.antinstaller.Installer.langPack.getString(getProperty() + "." + idx +".displayText");
|
---|
75 | try {
|
---|
76 | String r = org.tp23.antinstaller.Installer.langPack.getString( getProperty() + "." + idx + ".displayText" );
|
---|
77 | return r;
|
---|
78 | } catch ( java.util.MissingResourceException mre ) {} // ignore, signifies no language packs installed
|
---|
79 | }
|
---|
80 | return text;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | public void setValue(String value){ //param name used to be "dir" not "value", but bringing in line with SelectInput.java
|
---|
85 | setInputResult(value);
|
---|
86 | if ( useAsLocale ) {
|
---|
87 | //expect something like 'en' or or something like 'en_US'
|
---|
88 | Locale newLocale = null;
|
---|
89 | if ( value.length() == 2 ) {
|
---|
90 | newLocale = new Locale(value);
|
---|
91 | Locale.setDefault( newLocale );
|
---|
92 | } else if ( value.length() == 5 ) {
|
---|
93 | newLocale = new Locale(value.substring(0,2), value.substring(3,5));
|
---|
94 | Locale.setDefault( newLocale );
|
---|
95 | } //else { you're out of luck }
|
---|
96 | org.tp23.antinstaller.Installer.langPack = ResourceBundle.getBundle("resources.LanguagePack", newLocale );
|
---|
97 | }
|
---|
98 | }
|
---|
99 | /**
|
---|
100 | * Called to validate the user input
|
---|
101 | */
|
---|
102 | public boolean validate(InstallerContext cxt) throws ValidationException{
|
---|
103 | if(getInputResult()==null)return false;
|
---|
104 | String value = getInputResult();
|
---|
105 | boolean ok = false;
|
---|
106 | for (int i = 0; i < options.length; i++) {
|
---|
107 | ok |= options[i].value.equals(value);
|
---|
108 | }
|
---|
109 | return ok;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Used by checkConfig to validate the configuration file.
|
---|
116 | * Not used at runtime.
|
---|
117 | * @return boolean
|
---|
118 | */
|
---|
119 | public boolean validateObject() {
|
---|
120 | if(getDisplayText()==null){
|
---|
121 | System.out.println("LargeSelect:displayText must be set");
|
---|
122 | return false;
|
---|
123 | }
|
---|
124 | if(getProperty()==null){
|
---|
125 | System.out.println("LargeSelect:property must be set");
|
---|
126 | return false;
|
---|
127 | }
|
---|
128 | if(getDefaultValue()==null){
|
---|
129 | System.out.println("LargeSelect:defaultValue must be set");
|
---|
130 | return false;
|
---|
131 | }
|
---|
132 | if(getOptions()==null){
|
---|
133 | System.out.println("LargeSelect:option must have at least two options");
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 | if(getOptions().length<2){
|
---|
137 | System.out.println("LargeSelect:option must have at least two options");
|
---|
138 | return false;
|
---|
139 | }
|
---|
140 | for (int i = 0; i < getOptions().length; i++) {
|
---|
141 | Option o = getOptions()[i];
|
---|
142 | if(o.getText()==null){
|
---|
143 | System.out.println("LargeSelect:option:text must be set");
|
---|
144 | return false;
|
---|
145 | }
|
---|
146 | if(o.value==null){
|
---|
147 | System.out.println("LargeSelect:option:value must be set");
|
---|
148 | return false;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | boolean defaultExists = false;
|
---|
152 | for (int i = 0; i < getOptions().length; i++) {
|
---|
153 | Option o = getOptions()[i];
|
---|
154 | if(o.value.equals(getDefaultValue())){
|
---|
155 | defaultExists=true;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | if(!defaultExists){
|
---|
159 | System.out.println("LargeSelect:option:Default must be one of the options");
|
---|
160 | return false;
|
---|
161 | }
|
---|
162 | return true;
|
---|
163 | }
|
---|
164 | }
|
---|