01: /**
02: * Copyright 2006 Webmedia Group Ltd.
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: **/package org.araneaframework.uilib.form.constraint;
16:
17: import java.util.Calendar;
18: import java.util.Date;
19: import org.araneaframework.uilib.support.UiLibMessages;
20: import org.araneaframework.uilib.util.MessageUtil;
21:
22: /**
23: * Constraint that fails when checked java.util.Date is not after current date.
24: *
25: * @author <a href="mailto:ekabanov@webmedia.ee">Jevgeni Kabanov </a>
26: */
27: public class AfterTodayConstraint extends BaseFieldConstraint {
28: protected boolean allowToday;
29:
30: /**
31: * Whether today should be allowed as valid.
32: * @param allowToday when set, all dates from today are considered valid.
33: */
34: public AfterTodayConstraint(boolean allowToday) {
35: this .allowToday = allowToday;
36: }
37:
38: /**
39: */
40: protected void validateConstraint() {
41: Calendar today = Calendar.getInstance();
42:
43: if (allowToday) {
44: today.set(Calendar.HOUR_OF_DAY, 0);
45: today.set(Calendar.MINUTE, 0);
46: today.set(Calendar.SECOND, 0);
47: today.set(Calendar.MILLISECOND, 0);
48: } else {
49: today.set(Calendar.HOUR_OF_DAY, 23);
50: today.set(Calendar.MINUTE, 59);
51: today.set(Calendar.SECOND, 59);
52: today.set(Calendar.MILLISECOND, 999);
53: }
54:
55: if ((getValue() == null)
56: || (today.getTime().compareTo((Date) getValue()) == 1)) {
57: if (!allowToday) {
58: addError(MessageUtil.localizeAndFormat(
59: UiLibMessages.DATE_BEFORE_TODAY, t(getLabel()),
60: getEnvironment()));
61: } else {
62: addError(MessageUtil.localizeAndFormat(
63: UiLibMessages.DATE_BEFORE_TODAY_TODAY_ALLOWED,
64: t(getLabel()), getEnvironment()));
65: }
66: }
67: }
68:
69: }
|