001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.uilib.form.control;
016:
017: import java.sql.Timestamp;
018: import java.util.Calendar;
019: import org.araneaframework.uilib.ConfigurationContext;
020:
021: /**
022: * This class represents a {@link org.araneaframework.uilib.form.control.TimestampControl},
023: * that holds only date - that is it's default pattern is "dd.MM.yyyy".
024: *
025: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
026: */
027: public class DateControl extends TimestampControl {
028: protected int defaultHour;
029: protected int defaultMinute;
030: protected int defaultSecond;
031:
032: /**
033: * This is the default date format for this control.
034: */
035: public final static String DEFAULT_FORMAT = "dd.MM.yyyy";
036:
037: /**
038: * Creates the control initializing the pattern to {@link #DEFAULT_FORMAT}.
039: */
040: public DateControl() {
041: super (DEFAULT_FORMAT, DEFAULT_FORMAT);
042: }
043:
044: /**
045: * Creates the control initializing the pattern to <code>dateTimeFormat</code>.
046: * @param dateTimeFormat the custom pattern.
047: */
048: public DateControl(String dateTimeFormat, String defaultOutputFormat) {
049: super (dateTimeFormat, defaultOutputFormat);
050:
051: confOverridden = true;
052: }
053:
054: /**
055: * Overrides the default time that is set on all dates read from request.
056: */
057: public void setDefaultTime(int hour, int minute, int second) {
058: this .defaultHour = hour;
059: this .defaultMinute = minute;
060: this .defaultSecond = second;
061: }
062:
063: /**
064: * Returns "Timestamp".
065: * @return "Timestamp".
066: */
067: public String getRawValueType() {
068: return "Timestamp";
069: }
070:
071: //*********************************************************************
072: //* INTERNAL METHODS
073: //*********************************************************************
074:
075: protected void init() throws Exception {
076: super .init();
077:
078: if (!confOverridden) {
079: ConfigurationContext confCtx = (ConfigurationContext) getEnvironment()
080: .getEntry(ConfigurationContext.class);
081:
082: if (confCtx != null) {
083: String confFormat = (String) confCtx
084: .getEntry(ConfigurationContext.CUSTOM_DATE_FORMAT);
085: if (confFormat != null)
086: dateTimeInputPattern = confFormat;
087:
088: String confOutputFormat = (String) confCtx
089: .getEntry(ConfigurationContext.DEFAULT_DATE_OUTPUT_FORMAT);
090: if (confOutputFormat != null)
091: dateTimeOutputPattern = confOutputFormat;
092: }
093: }
094: }
095:
096: protected Object fromRequest(String parameterValue) {
097: Timestamp result = (Timestamp) super
098: .fromRequest(parameterValue);
099:
100: if (result != null) {
101: Calendar cal = getCalendarInstance();
102: cal.setTime(result);
103:
104: cal.set(Calendar.HOUR_OF_DAY, defaultHour);
105: cal.set(Calendar.MINUTE, defaultMinute);
106: cal.set(Calendar.SECOND, defaultSecond);
107: result = new Timestamp(cal.getTime().getTime());
108: }
109:
110: return result;
111: }
112:
113: /**
114: * Used by {@link DateControl#fromRequest(String)} to acquire <code>Calendar</code>
115: * instance for converting value read from request to <code>TimeStamp</code>
116: *
117: * @return <code>Calendar</code> using the default time zone and default locale.
118: * @since 1.0.3
119: */
120: protected Calendar getCalendarInstance() {
121: return Calendar.getInstance();
122: }
123: }
|