001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.input;
017:
018: import org.tp23.antinstaller.InstallerContext;
019: import org.tp23.antinstaller.ValidationException;
020:
021: /**
022: *
023: * <p>Input type to choose a single value from a (numbered) list of options </p>
024: * <p>N.B. subclassed for TargetSelectInput </p>
025: * <p>Copyright: Copyright (c) 2004</p>
026: * <p>Company: tp23</p>
027: * @author Paul Hinds
028: * @version $Id: SelectInput.java,v 1.4 2006/12/07 02:50:27 teknopaul Exp $
029: */
030: public class SelectInput extends InputField {
031:
032: private int optionIdx = 0;
033:
034: private SelectInput.Option[] options;
035:
036: public SelectInput() {
037: }
038:
039: public SelectInput.Option[] getOptions() {
040: return options;
041: }
042:
043: public void setOptions(SelectInput.Option[] options) {
044: this .options = options;
045: }
046:
047: public Option getNewOption() {
048: return new Option();
049: }
050:
051: public class Option {
052:
053: private int idx = ++optionIdx;
054: private String text;
055: public String value;
056:
057: public void setText(String text) {
058: this .text = text;
059: }
060:
061: public String getText() {
062: if (langPack.isI18n()) {
063: return langPack.getString(getProperty() + "." + idx
064: + ".displayText");
065: }
066: return text;
067: }
068: }
069:
070: public void setValue(String value) {
071: setInputResult(value);
072: }
073:
074: public boolean validate(InstallerContext cxt)
075: throws ValidationException {
076: if (getInputResult() == null) {
077: return false;
078: }
079: String value = getInputResult();
080: boolean ok = false;
081: for (int i = 0; i < options.length; i++) {
082: ok |= options[i].value.equals(value);
083: }
084: return ok;
085: }
086:
087: /**
088: * Used by checkConfig to validate the configuration file.
089: * Not used at runtime.
090: * @return boolean
091: */
092: public boolean validateObject() {
093: if (getDisplayText() == null) {
094: System.out.println("Select:displayText must be set");
095: return false;
096: }
097: if (getProperty() == null) {
098: System.out.println("Select:property must be set");
099: return false;
100: }
101: if (getDefaultValue() == null) {
102: System.out.println("Select:defaultValue must be set");
103: return false;
104: }
105: if (getOptions() == null) {
106: System.out
107: .println("Select:option must have at least two options");
108: return false;
109: }
110: if (getOptions().length < 2) {
111: System.out
112: .println("Select:option must have at least two options");
113: return false;
114: }
115: for (int i = 0; i < getOptions().length; i++) {
116: Option o = getOptions()[i];
117: if (o.getText() == null) {
118: System.out.println("Select:option:text must be set");
119: return false;
120: }
121: if (o.value == null) {
122: System.out.println("Select:option:value must be set");
123: return false;
124: }
125: }
126: boolean defaultExists = false;
127: for (int i = 0; i < getOptions().length; i++) {
128: Option o = getOptions()[i];
129: if (o.value.equals(getDefaultValue())) {
130: defaultExists = true;
131: }
132: }
133: if (!defaultExists) {
134: System.out
135: .println("Select:option:Default must be one of the options");
136: return false;
137: }
138: return true;
139: }
140: }
|