/* * Copyright 2005 Paul Hinds * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.tp23.antinstaller.input; import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.Locale; import org.tp23.antinstaller.InstallerContext; import org.tp23.antinstaller.ValidationException; /** * *

Input type to choose a single value from a (numbered) list of options

*

This input is designed to handle larger lists of options

*

Copyright: Copyright (c) 2004

*

Company: tp23

* @author Paul Hinds * @version $Id: LargeSelectInput.java,v 1.3 2006/12/07 02:50:27 teknopaul Exp $ */ public class LargeSelectInput extends InputField{ private int optionIdx = 0; private LargeSelectInput.Option[] options; private boolean useAsLocale = false; public LargeSelectInput() { } public void setUseAsLocale(boolean ual) { this.useAsLocale = ual; } public LargeSelectInput.Option[] getOptions() { return options; } public void setOptions(LargeSelectInput.Option[] options) { this.options = options; } public Option getNewOption(){ return new Option(); } public class Option { private int idx = ++optionIdx; private String text; public String value; public void setText(String text) { this.text = text; } public String getText() { if( org.tp23.antinstaller.Installer.langPack != null ) { //return org.tp23.antinstaller.Installer.langPack.getString(getProperty() + "." + idx +".displayText"); try { String r = org.tp23.antinstaller.Installer.langPack.getString( getProperty() + "." + idx + ".displayText" ); return r; } catch ( java.util.MissingResourceException mre ) {} // ignore, signifies no language packs installed } return text; } } public void setValue(String value){ //param name used to be "dir" not "value", but bringing in line with SelectInput.java setInputResult(value); if ( useAsLocale ) { //expect something like 'en' or or something like 'en_US' Locale newLocale = null; if ( value.length() == 2 ) { newLocale = new Locale(value); Locale.setDefault( newLocale ); } else if ( value.length() == 5 ) { newLocale = new Locale(value.substring(0,2), value.substring(3,5)); Locale.setDefault( newLocale ); } //else { you're out of luck } org.tp23.antinstaller.Installer.langPack = ResourceBundle.getBundle("resources.LanguagePack", newLocale ); } } /** * Called to validate the user input */ public boolean validate(InstallerContext cxt) throws ValidationException{ if(getInputResult()==null)return false; String value = getInputResult(); boolean ok = false; for (int i = 0; i < options.length; i++) { ok |= options[i].value.equals(value); } return ok; } /** * Used by checkConfig to validate the configuration file. * Not used at runtime. * @return boolean */ public boolean validateObject() { if(getDisplayText()==null){ System.out.println("LargeSelect:displayText must be set"); return false; } if(getProperty()==null){ System.out.println("LargeSelect:property must be set"); return false; } if(getDefaultValue()==null){ System.out.println("LargeSelect:defaultValue must be set"); return false; } if(getOptions()==null){ System.out.println("LargeSelect:option must have at least two options"); return false; } if(getOptions().length<2){ System.out.println("LargeSelect:option must have at least two options"); return false; } for (int i = 0; i < getOptions().length; i++) { Option o = getOptions()[i]; if(o.getText()==null){ System.out.println("LargeSelect:option:text must be set"); return false; } if(o.value==null){ System.out.println("LargeSelect:option:value must be set"); return false; } } boolean defaultExists = false; for (int i = 0; i < getOptions().length; i++) { Option o = getOptions()[i]; if(o.value.equals(getDefaultValue())){ defaultExists=true; } } if(!defaultExists){ System.out.println("LargeSelect:option:Default must be one of the options"); return false; } return true; } }