DateInputBoxControl.java :  » Web-Framework » jWic » de » jwic » controls » Java Open Source

Java Open Source » Web Framework » jWic 
jWic » de » jwic » controls » DateInputBoxControl.java
/*
 * Copyright 2005 jWic group (http://www.jwic.de)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * de.jwic.controls.DateInputBoxControl
 * $Id: DateInputBoxControl.java,v 1.3 2006/03/23 11:24:19 lordsam Exp $
 */
package de.jwic.controls;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import de.jwic.base.IControlContainer;

/**
 * DateInputBoxControl displays a InputBoxContol with an date picker link.
 * 
 * $Id: DateInputBoxControl.java,v 1.3 2006/03/23 11:24:19 lordsam Exp $
 * @version $Revision: 1.3 $
 * @author JBornemann
 */
public class DateInputBoxControl extends InputBoxControl {

  private static final long serialVersionUID = 1L;

  protected String cssClassBorder = null;
  
  
  
  /**
   * @param container
   */
  public DateInputBoxControl(IControlContainer container) {
    super(container);
    initialize();
  }
  /**
   * @param container
   * @param name
   */
  public DateInputBoxControl(IControlContainer container, String name) {
    super(container, name);
    initialize();
  }

  /**
   * DateInputBoxControl constructor.
   */
  private void initialize() {
    // new default width
    width = 80;
    // new default maxLength
    maxLength = 10;
  }

  /**
   * @return Returns the cssClassBorder.
   */
  public String getCssClassBorder() {
    return cssClassBorder;
  }
  /**
   * @param cssClassBorder The cssClassBorder to set.
   */
  public void setCssClassBorder(String cssClassBorder) {
    this.cssClassBorder = cssClassBorder;
  }
  /**
   * @return Returns the DateFormat used by this control for formating and parsing.
   */
  protected DateFormat getDateFormat() {
    String pattern = getDatePattern();
    SimpleDateFormat format = pattern != null ? 
          new SimpleDateFormat(pattern) : 
          new SimpleDateFormat();
    return format;
  }
  /**
   * Returns the pattern used for the DateFormat.
   * "dd.MM.yyyy" for DE or "MM/dd/yyyy" for EN.
   * @return
   */
  public String getDatePattern() {
    String lang_id = getSessionContext().getLocale().getLanguage().toUpperCase();
    if (lang_id.equals("DE")) {
      return "dd.MM.yyyy";
    } else if (lang_id.equals("EN")) {
      return "MM/dd/yyyy";
    }
    return null; 
  }
  /**
   * @return Returns the date.
   */
  public Date getDate() {
    DateFormat format = getDateFormat();
    try {
      return format.parse(getText());
    } catch (ParseException pe) {
      return null;
    }
  }
  
  /**
   * Returns true if the value entered by the user is a  
   * valid date or if the field is empty.
   * @return
   */
  public boolean isValid() {
    
    // an empty field is valid! 
    if (getText().length() == 0) {
      return true;
    }
    
    DateFormat format = getDateFormat();
    try {
      format.parse(getText());
    } catch (ParseException pe) {
      return false;
    }
    return true;
  }
  
  /**
   * @param date The date to set.
   */
  public void setDate(Date date) {
    DateFormat format = getDateFormat();
    setText(date == null ? "" : format.format(date));
  }
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.