|
1 |
| package photospace.beans; |
|
2 |
| |
|
3 |
| import java.beans.*; |
|
4 |
| import java.text.*; |
|
5 |
| import java.util.*; |
|
6 |
| |
|
7 |
| public class DateEditor extends PropertyEditorSupport |
|
8 |
| { |
|
9 |
| |
|
10 |
| private final DateFormat dateFormat; |
|
11 |
| private List formats = new ArrayList(); |
|
12 |
| |
|
13 |
| private final boolean allowEmpty; |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
1
| public DateEditor(DateFormat dateFormat, boolean allowEmpty)
|
|
26 |
| { |
|
27 |
1
| this.dateFormat = dateFormat;
|
|
28 |
1
| this.allowEmpty = allowEmpty;
|
|
29 |
1
| addDateFormat(dateFormat);
|
|
30 |
1
| addDateFormat(new SimpleDateFormat ("MM/dd/yyyy HH:mmaa"));
|
|
31 |
1
| addDateFormat(DateFormat.getDateInstance(DateFormat.SHORT));
|
|
32 |
1
| addDateFormat(DateFormat.getDateInstance(DateFormat.MEDIUM));
|
|
33 |
1
| addDateFormat(DateFormat.getDateInstance(DateFormat.LONG));
|
|
34 |
1
| addDateFormat(DateFormat.getDateInstance(DateFormat.FULL));
|
|
35 |
1
| addDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
|
|
36 |
1
| addDateFormat(new SimpleDateFormat("yyyy/MM/dd"));
|
|
37 |
1
| addDateFormat(new SimpleDateFormat("yyyy.MM.dd"));
|
|
38 |
1
| addDateFormat(new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss aa"));
|
|
39 |
1
| addDateFormat(new SimpleDateFormat("dd-MMM-yyyy"));
|
|
40 |
1
| addDateFormat(new SimpleDateFormat("dd MMM yyyy"));
|
|
41 |
1
| addDateFormat(new SimpleDateFormat("EEE MMM dd kk:mm:ss zz yyyy"));
|
|
42 |
1
| addDateFormat(new SimpleDateFormat("yyyy"));
|
|
43 |
| } |
|
44 |
| |
|
45 |
14
| public void addDateFormat(DateFormat format)
|
|
46 |
| { |
|
47 |
14
| formats.add(format);
|
|
48 |
| } |
|
49 |
| |
|
50 |
0
| public void setAsText(String text) throws IllegalArgumentException
|
|
51 |
| { |
|
52 |
0
| if (this.allowEmpty && (text == null || "".equals(text.trim())))
|
|
53 |
| { |
|
54 |
| |
|
55 |
0
| setValue(null);
|
|
56 |
| } |
|
57 |
| else |
|
58 |
| { |
|
59 |
0
| Object value = null;
|
|
60 |
0
| for (Iterator i = formats.iterator(); i.hasNext();)
|
|
61 |
| { |
|
62 |
0
| DateFormat format = (DateFormat) i.next();
|
|
63 |
0
| try
|
|
64 |
| { |
|
65 |
0
| value = format.parse(text);
|
|
66 |
0
| break;
|
|
67 |
| } |
|
68 |
| catch (ParseException ex) {} |
|
69 |
| } |
|
70 |
0
| if (value == null) throw new IllegalArgumentException("Could not parse date: " + text);
|
|
71 |
0
| setValue(value);
|
|
72 |
| } |
|
73 |
| } |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
0
| public String getAsText()
|
|
79 |
| { |
|
80 |
0
| return this.dateFormat.format((Date) getValue());
|
|
81 |
| } |
|
82 |
| |
|
83 |
| } |