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.validator;
042:
043: import java.text.MessageFormat;
044: import java.util.Date;
045: import javax.faces.application.FacesMessage;
046: import javax.faces.component.UIComponent;
047: import javax.faces.context.FacesContext;
048: import javax.faces.convert.Converter;
049: import javax.faces.validator.Validator;
050: import javax.faces.validator.ValidatorException;
051: import com.sun.rave.web.ui.theme.Theme;
052: import com.sun.rave.web.ui.util.ConversionUtilities;
053: import com.sun.rave.web.ui.util.ThemeUtilities;
054: import com.sun.rave.web.ui.component.DateManager;
055:
056: /**
057: * <p> Use this validator to check the number of characters in a string when
058: * you need to set the validation messages.</p>
059: *
060: * @author avk
061: */
062: public class DateInRangeValidator implements Validator {
063:
064: /**
065: * <p>The converter id for this converter.</p>
066: */
067: public static final String VALIDATOR_ID = "com.sun.rave.web.ui.DateInRange";
068:
069: private static final boolean DEBUG = false;
070:
071: /** Creates a new instance of StringLengthValidator */
072: public DateInRangeValidator() {
073: }
074:
075: /**
076: * <p> Validate the value with regard to a <code>UIComponent</code> and a
077: * <code>FacesContext</code>.</p>
078: *
079: * @param context The FacesContext
080: * @param component The component to be validated
081: * @param value The submitted value of the component
082: *
083: * @exception ValidatorException if the value is not valid
084: */
085: public void validate(FacesContext context, UIComponent component,
086: Object value) throws ValidatorException {
087:
088: //log("validate()" + String.valueOf(value));
089: if ((context == null) || (component == null)) {
090: //if(DEBUG) log("\tContext or component is null");
091: throw new NullPointerException();
092: }
093:
094: if (!(value instanceof Date)) {
095: return;
096: }
097:
098: DateManager dateManager = null;
099: if (component instanceof DateManager) {
100: dateManager = (DateManager) component;
101: } else if (component.getParent() instanceof DateManager) {
102: dateManager = (DateManager) (component.getParent());
103: }
104: if (dateManager == null) {
105: //log("Didn't find a DateManager " + component.getClass().toString());
106: return;
107: }
108:
109: Date date = (Date) value;
110: Date minDate = dateManager.getFirstAvailableDate();
111: if (minDate != null && date.before(minDate)) {
112: //log("Date is before minDAte!");
113: FacesMessage msg = getFacesMessage(component, context,
114: minDate, "DateInRangeValidator.after");
115: throw new ValidatorException(msg);
116: }
117: Date maxDate = dateManager.getLastAvailableDate();
118: if (maxDate != null && maxDate.before(date)) {
119: //log("Date is after maxDAte!");
120: FacesMessage msg = getFacesMessage(component, context,
121: maxDate, "DateInRangeValidator.before");
122: throw new ValidatorException(msg);
123:
124: }
125: }
126:
127: private FacesMessage getFacesMessage(UIComponent component,
128: FacesContext context, Date date, String key) {
129:
130: String message = ThemeUtilities.getTheme(context).getMessage(
131: key);
132: String arg = ConversionUtilities.convertValueToString(
133: component, date);
134: MessageFormat mf = new MessageFormat(message, context
135: .getViewRoot().getLocale());
136: Object[] params = { arg };
137: return new FacesMessage(mf.format(params));
138: }
139:
140: private void log(String s) {
141: System.out.println(this .getClass().getName() + "::" + s); //NOI18N
142: }
143: }
|