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: package com.sun.rave.web.ui.converter;
042:
043: import java.io.Serializable;
044: import java.text.DateFormat;
045: import java.util.Date;
046: import javax.faces.component.UIComponent;
047: import javax.faces.context.FacesContext;
048: import javax.faces.convert.Converter;
049: import javax.faces.convert.ConverterException;
050: import com.sun.rave.web.ui.component.DateManager;
051: import com.sun.rave.web.ui.util.ThemeUtilities;
052: import java.text.MessageFormat;
053: import javax.faces.application.FacesMessage;
054:
055: /**
056: *
057: * @author avk
058: */
059: public class DateConverter implements Converter, Serializable {
060:
061: private static final String INVALID_DATE_ID = "DateConverter.invalidDate"; //NOI18N
062:
063: public DateConverter() {
064: }
065:
066: public String getAsString(FacesContext context,
067: UIComponent component, Object o) throws ConverterException {
068: try {
069: return getDateManager(component).getDateFormat().format(
070: (Date) o);
071: } catch (Exception ex) {
072: throw new ConverterException(ex);
073: }
074: }
075:
076: public Object getAsObject(FacesContext context,
077: UIComponent component, String s) throws ConverterException {
078: if (s.length() == 0) {
079: return null;
080: }
081: // <RAVE>
082: // Generate errors for dates that don't strictly follow format 6347646
083: DateFormat df = getDateManager(component).getDateFormat();
084: // Save old state in case there is other code that relies on it
085: boolean saveLenient = df.isLenient();
086: df.setLenient(false);
087: try {
088: Date date = df.parse(s);
089: return date;
090: } catch (Exception ex) {
091: FacesMessage facesMessage = null;
092: try {
093: String message = ThemeUtilities.getTheme(context)
094: .getMessage(INVALID_DATE_ID);
095: MessageFormat mf = new MessageFormat(message, context
096: .getViewRoot().getLocale());
097: String example = getDateManager(component)
098: .getDateFormat().format(new Date());
099: Object[] params = { s, example };
100: facesMessage = new FacesMessage(mf.format(params));
101: } catch (Exception e) {
102: throw new ConverterException(ex);
103: }
104: throw new ConverterException(facesMessage);
105: } finally {
106: // Restore original state
107: df.setLenient(saveLenient);
108: }
109: // </RAVE>
110: }
111:
112: private DateManager getDateManager(UIComponent component) {
113: DateManager dateManager = null;
114: if (component instanceof DateManager) {
115: dateManager = (DateManager) component;
116: } else if (component.getParent() instanceof DateManager) {
117: dateManager = (DateManager) (component.getParent());
118: }
119: if (dateManager == null) {
120: throw new RuntimeException(
121: "The DateConverter can only be used with components which implement DateManager"); //NOI18N
122: }
123: return dateManager;
124: }
125: }
|