001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package com.sun.rave.faces.converter;
043:
044: import java.util.Calendar;
045: import java.util.Date;
046: import java.util.Locale;
047: import java.util.TimeZone;
048:
049: import javax.faces.component.StateHolder;
050: import javax.faces.component.UIComponent;
051: import javax.faces.context.FacesContext;
052: import javax.faces.convert.Converter;
053: import javax.faces.convert.ConverterException;
054: import javax.faces.convert.DateTimeConverter;
055:
056: /**
057: * <p><code>Converter<code> implementation for <code>java.util.Calendar</code>
058: * values. Delegates detailed processing, and all property settings, to an
059: * internal instance of <code>dateTimeConverter</code> that is used to perform
060: * the actual conversion processing.</p>
061: */
062:
063: public class CalendarConverter implements Converter, StateHolder {
064:
065: // ------------------------------------------------------ Manifest Constants
066:
067: /**
068: * <p>The standard converter id for this converter.</p>
069: */
070: public static final String CONVERTER_ID = "com.sun.jsfcl.convert.Calendar";
071:
072: private static final TimeZone DEFAULT_TIME_ZONE = TimeZone
073: .getTimeZone("GMT");
074:
075: // ------------------------------------------------------ Instance Variables
076:
077: /**
078: * <p>Instance of <code>DateTimeConverter that is delegated to for
079: * all actual processing.</p>
080: */
081: private DateTimeConverter converter = new DateTimeConverter();
082:
083: // -------------------------------------------------------------- Properties
084:
085: /**
086: * <p>Return the style to be used to format or parse dates. If not set,
087: * the default value, <code>default<code>, is returned.</p>
088: */
089: public String getDateStyle() {
090:
091: return converter.getDateStyle();
092:
093: }
094:
095: /**
096: * <p>Set the style to be used to format or parse dates. Valid values
097: * are <code>default</code>, <code>short</code>, <code>medium</code>,
098: * <code>long</code>, and <code>full</code>.
099: * An invalid value will cause a {@link ConverterException} when
100: * <code>getAsObject()</code> or <code>getAsString()</code> is called.</p>
101: *
102: * @param dateStyle The new style code
103: */
104: public void setDateStyle(String dateStyle) {
105:
106: converter.setDateStyle(dateStyle);
107:
108: }
109:
110: /**
111: * <p>Return the <code>Locale</code> to be used when parsing or formatting
112: * dates and times. If not explicitly set, the <code>Locale</code> stored
113: * in the {@link javax.faces.component.UIViewRoot} for the current
114: * request is returned.</p>
115: */
116: public Locale getLocale() {
117:
118: return converter.getLocale();
119:
120: }
121:
122: /**
123: * <p>Set the <code>Locale</code> to be used when parsing or formatting
124: * dates and times. If set to <code>null</code>, the <code>Locale</code>
125: * stored in the {@link javax.faces.component.UIViewRoot} for the current
126: * request will be utilized.</p>
127: *
128: * @param locale The new <code>Locale</code> (or <code>null</code>)
129: */
130: public void setLocale(Locale locale) {
131:
132: converter.setLocale(locale);
133:
134: }
135:
136: /**
137: * <p>Return the format pattern to be used when formatting and
138: * parsing dates and times.</p>
139: */
140: public String getPattern() {
141:
142: return converter.getPattern();
143:
144: }
145:
146: /**
147: * <p>Set the format pattern to be used when formatting and parsing
148: * dates and times. Valid values are those supported by
149: * <code>java.text.SimpleDateFormat</code>.
150: * An invalid value will cause a {@link ConverterException} when
151: * <code>getAsObject()</code> or <code>getAsString()</code> is called.</p>
152: *
153: * @param pattern The new format pattern
154: */
155: public void setPattern(String pattern) {
156:
157: converter.setPattern(pattern);
158:
159: }
160:
161: /**
162: * <p>Return the style to be used to format or parse times. If not set,
163: * the default value, <code>default</code>, is returned.</p>
164: */
165: public String getTimeStyle() {
166:
167: return converter.getTimeStyle();
168:
169: }
170:
171: /**
172: * <p>Set the style to be used to format or parse times. Valid values
173: * are <code>default</code>, <code>short</code>, <code>medium</code>,
174: * <code>long</code>, and <code>full</code>.
175: * An invalid value will cause a {@link ConverterException} when
176: * <code>getAsObject()</code> or <code>getAsString()</code> is called.</p>
177: *
178: * @param timeStyle The new style code
179: */
180: public void setTimeStyle(String timeStyle) {
181:
182: converter.setTimeStyle(timeStyle);
183:
184: }
185:
186: /**
187: * <p>Return the <code>TimeZone</code> used to interpret a time value.
188: * If not explicitly set, the default time zone of <code>GMT</code>
189: * returned.</p>
190: */
191: public TimeZone getTimeZone() {
192:
193: return converter.getTimeZone();
194:
195: }
196:
197: /**
198: * <p>Set the <code>TimeZone</code> used to interpret a time value.</p>
199: *
200: * @param timeZone The new time zone
201: */
202: public void setTimeZone(TimeZone timeZone) {
203:
204: converter.setTimeZone(timeZone);
205:
206: }
207:
208: /**
209: * <p>Return the type of value to be formatted or parsed.
210: * If not explicitly set, the default type, <code>date</code>
211: * is returned.</p>
212: */
213: public String getType() {
214:
215: return converter.getType();
216:
217: }
218:
219: /**
220: * <p>Set the type of value to be formatted or parsed.
221: * Valid values are <code>both</code>, <code>date</code>, or
222: * <code>time</code>.
223: * An invalid value will cause a {@link ConverterException} when
224: * <code>getAsObject()</code> or <code>getAsString()</code> is called.</p>
225: *
226: * @param type The new date style
227: */
228: public void setType(String type) {
229:
230: converter.setType(type);
231:
232: }
233:
234: // ------------------------------------------------------- Converter Methods
235:
236: /**
237: * @exception ConverterException
238: * @exception NullPointerException
239: */
240: public Object getAsObject(FacesContext context,
241: UIComponent component, String value) {
242:
243: Date date = (Date) converter.getAsObject(context, component,
244: value);
245: if (date == null) {
246: return null;
247: }
248:
249: Locale locale = getLocale();
250: TimeZone timeZone = getTimeZone();
251: Calendar instance = null;
252: if (timeZone != null) {
253: instance = Calendar.getInstance(timeZone, locale);
254: } else {
255: instance = Calendar.getInstance(locale);
256: }
257: instance.setTime(date);
258: return instance;
259:
260: }
261:
262: /**
263: * @exception ConverterException
264: * @exception NullPointerException
265: */
266: public String getAsString(FacesContext context,
267: UIComponent component, Object value) {
268:
269: if ((value != null) && (value instanceof Calendar)) {
270: return converter.getAsString(context, component,
271: ((Calendar) value).getTime());
272: } else {
273: return converter.getAsString(context, component, value);
274: }
275:
276: }
277:
278: // ----------------------------------------------------- StateHolder Methods
279:
280: public Object saveState(FacesContext context) {
281:
282: return ((StateHolder) converter).saveState(context);
283:
284: }
285:
286: public void restoreState(FacesContext context, Object state) {
287:
288: DateTimeConverter converter = new DateTimeConverter();
289: converter.restoreState(context, state);
290:
291: }
292:
293: private boolean transientFlag = false;
294:
295: public boolean isTransient() {
296: return (transientFlag);
297: }
298:
299: public void setTransient(boolean transientFlag) {
300: this.transientFlag = transientFlag;
301: }
302:
303: }
|