01: /*
02: * Beryl - A web platform based on XML, XSLT and Java
03: * This file is part of the Beryl XML GUI
04: *
05: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11:
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
20: */
21:
22: package org.beryl.gui.validators;
23:
24: import java.text.ParseException;
25: import java.text.SimpleDateFormat;
26:
27: import org.beryl.gui.InternationalizationManager;
28: import org.beryl.gui.View;
29: import org.beryl.gui.widgets.TextField;
30:
31: ;
32:
33: /**
34: * A validator for dates in different formats
35: */
36:
37: public class DateFieldValidator implements Validator {
38: public static final SimpleDateFormat EUROPEAN_DATETIME = new SimpleDateFormat(
39: "dd.MM.yyyy HH:mm");
40: public static final SimpleDateFormat EUROPEAN_DATE = new SimpleDateFormat(
41: "dd.MM.yyyy");
42: public static final SimpleDateFormat US_DATETIME = new SimpleDateFormat(
43: "MM/dd/yyyy hh:mm a");
44: public static final SimpleDateFormat US_DATE = new SimpleDateFormat(
45: "MM/dd/yyyy");
46: private SimpleDateFormat format = null;
47: private int length = -1;
48:
49: public DateFieldValidator(SimpleDateFormat format) {
50: this .format = format;
51: length = format.toPattern().length();
52: }
53:
54: public void validate(View view) throws ValidationException {
55: TextField field = (TextField) view;
56: String text = field.getText();
57:
58: try {
59: format.parse(field.getText());
60: if (text.length() != length)
61: throw new ValidationException(
62: view,
63: InternationalizationManager
64: .getString("xmlgui.validator.date.invalid"));
65: } catch (ParseException e) {
66: throw new ValidationException(view,
67: InternationalizationManager
68: .getString("xmlgui.validator.date.invalid"));
69: }
70: }
71: }
|