001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: DateField.java,v $
023   Revision 1.4  2004/05/12 19:15:02  markl
024   comment block updates
025
026   Revision 1.3  2004/03/16 06:43:39  markl
027   LocaleManager method change
028
029   Revision 1.2  2004/01/23 00:03:58  markl
030   javadoc corrections
031
032   Revision 1.1  2003/01/19 09:37:59  markl
033   New class.
034   ----------------------------------------------------------------------------
035*/
036
037package kiwi.ui;
038
039import java.text.*;
040import java.util.*;
041
042import kiwi.util.*;
043
044/** A subclass of <code>DataField</code> for the input and display of
045 * dates.
046 *
047 * @author Mark Lindner
048 *
049 * @since Kiwi 1.4
050 */
051
052public class DateField extends DataField
053  {
054  private LocaleManager locmgr = LocaleManager.getDefault();
055  private ParsePosition pos = new ParsePosition(0);
056  /** The formatter used to parse and format dates in this field. */
057  protected DateFormat dateFormat;
058  /** The current date entered in this field. */
059  protected Date date = null;
060
061  /** Construct a new <code>DateField</code> with the specified width and a
062   * default, locale-specific date format.
063   *
064   * @param width The width of the field.
065   */
066  
067  public DateField(int width)
068    {
069    this(width, null);
070    }
071
072  /** Construct a new <code>DateField</code> with the specified width and
073   * date format.
074   *
075   * @param width The width of the field.
076   * @param format The date format.
077   */
078  
079  public DateField(int width, String format) throws IllegalArgumentException
080    {
081    super(width);
082
083    LocaleManager locmgr = LocaleManager.getDefault();
084    
085    if(format == null)
086      dateFormat = locmgr.getShortDateFormat();
087    else
088      dateFormat = new SimpleDateFormat(format);
089
090    setFont(KiwiUtils.boldFont);
091    }
092
093  /** Parse the contents of the field as a date, and return the date.
094   *
095   * @return The parsed date, or <code>null</code> if parsing failed.
096   */
097
098  public Date getDate()
099    {
100    checkInput();
101    
102    return(date);
103    }
104
105  /** Set the data to be displayed by this field. The date is formatted as a
106   * string, according to the rules of the current locale, and displayed in the
107   * field. Invalid input flagging is automatically turned off.
108   */
109
110  public void setDate(Date date)
111    {
112    this.date = date;
113
114    setText((date == null) ? "" : dateFormat.format(date));
115    
116    invalid = false;
117    paintInvalid(invalid);
118    }
119
120  /** Determine if the given input is valid for this field.
121   *
122   * @return <code>true</code> if the input is valid, and <code>false</code>
123   * otherwise.
124   */
125  
126  protected boolean checkInput()
127    {
128    invalid = false;
129
130    try
131      {
132      pos.setIndex(0);
133      String s = getText();
134      Date d = dateFormat.parse(s, pos);
135      trapGarbage(s);
136      setDate(d);
137      }
138    catch(ParseException ex)
139      {
140      invalid = true;
141      date = null;
142      }
143
144    paintInvalid(invalid);
145
146    return(!invalid);
147    }  
148
149  /*
150   */
151  
152  private void trapGarbage(String s) throws ParseException
153    {
154    if(pos.getIndex() != s.length())
155      throw(new ParseException("Garbage in string " + s, pos.getIndex()));
156    }
157    
158  }
159
160/* end of source file */