1
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
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
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
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