source: release-kits/lirk3/bin/ant-installer/src/org/tp23/antinstaller/input/SelectInput.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 3.6 KB
Line 
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 */
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/**
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 */
36public class SelectInput
37 extends InputField{
38
39 // i18n support
40 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 // ignore, signifies no lang packs installed
47 }
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
100
101
102 /**
103 * Used by checkConfig to validate the configuration file.
104 * Not used at runtime.
105 * @return boolean
106 */
107 public boolean validateObject() {
108 if(getDisplayText()==null){
109 System.out.println("Select:displayText must be set");
110 return false;
111 }
112 if(getProperty()==null){
113 System.out.println("Select:property must be set");
114 return false;
115 }
116 if(getDefaultValue()==null){
117 System.out.println("Select:defaultValue must be set");
118 return false;
119 }
120 if(getOptions()==null){
121 System.out.println("Select:option must have at least two options");
122 return false;
123 }
124 if(getOptions().length<2){
125 System.out.println("Select:option must have at least two options");
126 return false;
127 }
128 for (int i = 0; i < getOptions().length; i++) {
129 Option o = getOptions()[i];
130 if(o.getText()==null){
131 System.out.println("Select:option:text must be set");
132 return false;
133 }
134 if(o.value==null){
135 System.out.println("Select:option:value must be set");
136 return false;
137 }
138 }
139 boolean defaultExists = false;
140 for (int i = 0; i < getOptions().length; i++) {
141 Option o = getOptions()[i];
142 if(o.value.equals(getDefaultValue())){
143 defaultExists=true;
144 }
145 }
146 if(!defaultExists){
147 System.out.println("Select:option:Default must be one of the options");
148 return false;
149 }
150 return true;
151 }
152}
Note: See TracBrowser for help on using the repository browser.