01: /*
02: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: */
16: package de.odysseus.calyxo.forms.convert;
17:
18: import java.util.Date;
19: import java.util.Calendar;
20:
21: import java.text.ParseException;
22:
23: import java.util.Locale;
24:
25: /**
26: * Calendar converter.
27: *
28: * @author Christoph Beck
29: */
30: public class CalendarConverter extends SimpleConverter implements
31: Cloneable {
32: private DateConverter dateConverter = null;
33:
34: /**
35: * Default constructor.
36: */
37: public CalendarConverter() {
38: this (new DateConverter());
39: }
40:
41: /**
42: * Constructor.
43: * @param dateConverter delegate
44: */
45: protected CalendarConverter(DateConverter dateConverter) {
46: this .dateConverter = dateConverter;
47: }
48:
49: /**
50: * Format method.
51: */
52: public String format(Object value) {
53: if (value == null)
54: return null;
55: return dateConverter.format(((Calendar) value).getTime());
56: }
57:
58: protected CalendarConverter copy() {
59: try {
60: return (CalendarConverter) clone();
61: } catch (CloneNotSupportedException e) {
62: // should not happen
63: e.printStackTrace();
64: return null;
65: }
66: }
67:
68: public void localize(Locale locale) {
69: dateConverter.localize(locale);
70: }
71:
72: /**
73: * Parse method.
74: */
75: public Object parse(String value) throws ParseException {
76: Date d = (Date) dateConverter.parse(value);
77: if (d == null)
78: return null;
79: Calendar c = Calendar.getInstance();
80: c.setTime(d);
81: return c;
82: }
83: }
|