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.text.DateFormat;
019: import java.text.ParseException;
020: import java.text.SimpleDateFormat;
021: import java.util.Date;
022:
023: import org.tp23.antinstaller.InstallerContext;
024: import org.tp23.antinstaller.ValidationException;
025:
026: /**
027: *
028: * <p>Free text input type with validation a SimpleDataFormat instance. </p>
029: * <p>By default the date format is <code>dd/MM/yyyy</code> unless it
030: * is overriden in teh antinstall-config.xml file. </p>
031: * @author Paul Hinds
032: * @version $Id: DateInput.java,v 1.1.1.1 2005/10/18 18:20:54 teknopaul Exp $
033: */
034: public class DateInput extends InputField {
035:
036: private String dateFormat = "dd/MM/yyyy";
037: private DateFormat formatter = new SimpleDateFormat(dateFormat);
038:
039: public DateInput() {
040: formatter.setLenient(false);
041: }
042:
043: public String getDateFormat() {
044: return dateFormat;
045: }
046:
047: public void setDateFormat(String dateFormat) {
048: try {
049: formatter = new SimpleDateFormat(dateFormat);
050: formatter.setLenient(false);
051: this .dateFormat = dateFormat;
052: } catch (RuntimeException e) {
053: throw new InputException("Invalid date format in DateInput");
054: }
055: }
056:
057: public void setValue(String dir) {
058: setInputResult(dir);
059: }
060:
061: /**
062: * Called to validate the user input
063: */
064: public boolean validate(InstallerContext cxt)
065: throws ValidationException {
066: if (getInputResult() == null)
067: return false;
068: String toTest = getInputResult();
069: try {
070: formatter.parse(toTest);
071: } catch (ParseException ex) {
072: return false;
073: }
074: return true;
075: }
076:
077: public void setDefaultValue(String defaultValue) {
078: if (defaultValue.equals("TODAY")) {
079: this .defaultValue = formatter.format(new Date());
080: } else {
081: this .defaultValue = defaultValue;
082: }
083: }
084:
085: /**
086: * Used by checkConfig to validate the configuration file.
087: * Not used at runtime.
088: * @return boolean
089: */
090: public boolean validateObject() {
091: if (getDisplayText() == null) {
092: System.out.println("Date:displayText must be set");
093: return false;
094: }
095: if (getProperty() == null) {
096: System.out.println("Date:property must be set");
097: return false;
098: }
099: if (getDefaultValue() == null) {
100: System.out.println("Date:defaultValue must be set");
101: return false;
102: }
103: return true;
104: }
105: }
|