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 java.util.MissingResourceException;
019:
020: import org.tp23.antinstaller.InstallerContext;
021: import org.tp23.antinstaller.ValidationException;
022:
023: /**
024: *
025: * <p>Input type to choose a single value from a (numbered) list of options </p>
026: * <p>This input is designed to handle larger lists of options </p>
027: * <p>Copyright: Copyright (c) 2004</p>
028: * <p>Company: tp23</p>
029: * @author Paul Hinds
030: * @version $Id: LargeSelectInput.java,v 1.3 2006/12/07 02:50:27 teknopaul Exp $
031: */
032: public class LargeSelectInput extends InputField {
033:
034: private int optionIdx = 0;
035:
036: private LargeSelectInput.Option[] options;
037:
038: public LargeSelectInput() {
039: }
040:
041: public LargeSelectInput.Option[] getOptions() {
042: return options;
043: }
044:
045: public void setOptions(LargeSelectInput.Option[] options) {
046: this .options = options;
047: }
048:
049: public Option getNewOption() {
050: return new Option();
051: }
052:
053: public class Option {
054:
055: private int idx = ++optionIdx;
056: private String text;
057: public String value;
058:
059: public void setText(String text) {
060: this .text = text;
061: }
062:
063: public String getText() {
064: if (langPack.isI18n()) {
065: try {
066: return langPack.getString(getProperty() + "." + idx
067: + ".displayText");
068: } catch (MissingResourceException e) {
069:
070: // use default if missing
071: }
072: }
073: return text;
074: }
075: }
076:
077: public void setValue(String dir) {
078: setInputResult(dir);
079: }
080:
081: /**
082: * Called to validate the user input
083: */
084: public boolean validate(InstallerContext cxt)
085: throws ValidationException {
086: if (getInputResult() == null) {
087: return false;
088: }
089: String value = getInputResult();
090: boolean ok = false;
091: for (int i = 0; i < options.length; i++) {
092: ok |= options[i].value.equals(value);
093: }
094: return ok;
095: }
096:
097: /**
098: * Used by checkConfig to validate the configuration file.
099: * Not used at runtime.
100: * @return boolean
101: */
102: public boolean validateObject() {
103: if (getDisplayText() == null) {
104: System.out.println("LargeSelect:displayText must be set");
105: return false;
106: }
107: if (getProperty() == null) {
108: System.out.println("LargeSelect:property must be set");
109: return false;
110: }
111: if (getDefaultValue() == null) {
112: System.out.println("LargeSelect:defaultValue must be set");
113: return false;
114: }
115: if (getOptions() == null) {
116: System.out
117: .println("LargeSelect:option must have at least two options");
118: return false;
119: }
120: if (getOptions().length < 2) {
121: System.out
122: .println("LargeSelect:option must have at least two options");
123: return false;
124: }
125: for (int i = 0; i < getOptions().length; i++) {
126: Option o = getOptions()[i];
127: if (o.getText() == null) {
128: System.out
129: .println("LargeSelect:option:text must be set");
130: return false;
131: }
132: if (o.value == null) {
133: System.out
134: .println("LargeSelect:option:value must be set");
135: return false;
136: }
137: }
138: boolean defaultExists = false;
139: for (int i = 0; i < getOptions().length; i++) {
140: Option o = getOptions()[i];
141: if (o.value.equals(getDefaultValue())) {
142: defaultExists = true;
143: }
144: }
145: if (!defaultExists) {
146: System.out
147: .println("LargeSelect:option:Default must be one of the options");
148: return false;
149: }
150: return true;
151: }
152: }
|