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.text.DateFormat;
20import java.text.ParseException;
21import java.text.SimpleDateFormat;
22import java.util.Date;
23
24import org.tp23.antinstaller.InstallerContext;
25import org.tp23.antinstaller.ValidationException;
26
27
28/**
29 *
30 * <p>Free text input type with validation a SimpleDataFormat instance. </p>
31 * <p>By default the date format is <code>dd/MM/yyyy</code> unless it
32 * is overriden in teh antinstall-config.xml file. </p>
33 * @author Paul Hinds
34 * @version $Id: DateInput.java,v 1.1.1.1 2005/10/18 18:20:54 teknopaul Exp $
35 */
36public class DateInput
37    extends InputField{
38
39    private String dateFormat = "dd/MM/yyyy";
40    private DateFormat formatter = new SimpleDateFormat(dateFormat);
41
42    public DateInput() {
43        formatter.setLenient(false);
44    }
45
46    public String getDateFormat() {
47        return dateFormat;
48    }
49    public void setDateFormat(String dateFormat) {
50        try {
51            formatter = new SimpleDateFormat(dateFormat);
52            formatter.setLenient(false);
53            this.dateFormat = dateFormat;
54        }
55        catch (RuntimeException e) {
56            throw new InputException("Invalid date format in DateInput");
57        }
58    }
59
60    public void setValue(String dir){
61        setInputResult(dir);
62    }
63
64    /**
65     * Called to validate the user input
66     */
67    public boolean validate(InstallerContext cxt) throws ValidationException{
68        if (getInputResult() == null)return false;
69        String toTest = getInputResult();
70        try {
71            formatter.parse(toTest);
72        }
73        catch (ParseException ex) {
74            return false;
75        }
76        return true;
77    }
78
79    public void setDefaultValue(String defaultValue) {
80        if(defaultValue.equals("TODAY")){
81            this.defaultValue = formatter.format(new Date());
82        } else {
83            this.defaultValue = defaultValue;
84        }
85    }
86    
87    /**
88     * Used by checkConfig to validate the configuration file.
89     * Not used at runtime.
90     * @return boolean
91     */
92    public boolean validateObject() {
93        if(getDisplayText()==null){
94            System.out.println("Date:displayText must be set");
95            return false;
96        }
97        if(getProperty()==null){
98            System.out.println("Date:property must be set");
99            return false;
00        }
01        if(getDefaultValue()==null){
02            System.out.println("Date:defaultValue must be set");
03            return false;
04        }
05        return true;
06    }
07}
08