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: DateChooserField.java,v $
023   Revision 1.4  2004/05/12 19:15:46  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/03/15 05:43:36  markl
030   added missing javadoc
031
032   Revision 1.1  2003/01/19 09:37:59  markl
033   New class.
034   ----------------------------------------------------------------------------
035*/
036
037package kiwi.ui;
038
039import java.awt.*;
040import java.awt.event.*;
041import javax.swing.*;
042import javax.swing.event.*;
043import java.util.*;
044import java.text.*;
045
046import kiwi.event.*;
047import kiwi.util.*;
048
049/** A date entry component which consists of a combination of a
050 * <code>DateField</code> and a <code>DateChooser</code> in a popup.
051 * The popup is activated by clicking on the button to the right of the input
052 * field.
053 *
054 * <p><center>
055 * <img src="snapshot/DateChooserField.gif"><br>
056 * <i>An example DateChooserField.</i>
057 * <p>
058 * <img src="snapshot/DateChooserField_popup.gif"><br>
059 * <i>An example DateChooserField with the popup activated.</i>
060 * </center>
061 *
062 * @author Mark Lindner
063 *
064 * @since Kiwi 1.4
065 *
066 * @see kiwi.ui.DateField
067 * @see kiwi.ui.DateChooser
068 * @see kiwi.ui.dialog.DateChooserDialog
069 */
070
071public class DateChooserField extends KPanel
072  {
073  private SimpleDateFormat dateFormat;
074  /** */
075  protected DateField t_date;
076  /** */
077  protected JButton b_chooser;
078  private JPopupMenu menu;
079  /** */
080  protected DateChooser chooser;
081
082  /** Constructs a <code>DateChooserField</code> object using the
083   * default date format.
084   */
085
086  public DateChooserField()
087    {
088    this(null);
089    }
090
091  /** Construct a <code>DateChooserField</code> with the specified width,
092   * using the default date format.
093   *
094   * @param width The width for the field.
095   */
096
097  public DateChooserField(int width)
098    {
099    this(width, null);
100    }
101
102  /** Construct a <code>DateChooserField</code> with the default width,
103   * using the specified date format.
104   *
105   * @param format The format to display the date in.
106   */
107  
108  public DateChooserField(String format)
109    {
110    this(10, format);
111    }
112  
113  /** Construct a <code>DateChooserField</code> with the specified width and
114   * date format.
115   *
116   * @param width The width for the field.
117   * @param format The format to display the date in.
118   */
119
120  public DateChooserField(int width, String format)
121    {  
122    setLayout(new BorderLayout(2, 2));
123
124    LocaleData loc = LocaleManager.getDefault().getLocaleData("KiwiDialogs");
125    
126    t_date = new DateField(width, format);
127    t_date.setOpaque(false);
128    add("Center", t_date);
129    
130    b_chooser = new KButton(KiwiUtils.getResourceManager()
131                            .getIcon("date.gif"));
132    b_chooser.setToolTipText(loc.getMessage("kiwi.tooltip.select_date"));
133    add("East", b_chooser);
134    
135    menu = new JPopupMenu();
136    menu.setOpaque(false);
137    chooser = new DateChooser();
138    chooser.setOpaque(false);
139
140    KPanel p_popup = new KPanel(UIChangeManager.getInstance()
141                                .getDefaultTexture());
142    p_popup.setLayout(new GridLayout(1, 0));
143    p_popup.setBorder(KiwiUtils.defaultBorder);
144    p_popup.add(chooser);
145    menu.add(p_popup);
146
147    b_chooser.addActionListener(new ActionListener()
148        {
149        public void actionPerformed(ActionEvent evt)
150          {
151          Date d = t_date.getDate();
152          if(d != null)
153            {
154            Calendar c = Calendar.getInstance();
155            c.setTime(d);
156            chooser.setSelectedDate(c);
157            }
158          
159          menu.show(b_chooser, 0, 0);
160          }
161        });
162    
163    chooser.addActionListener(new ActionListener()
164        {
165        public void actionPerformed(ActionEvent evt)
166          {
167          if(evt.getActionCommand().equals(DateChooser.DATE_CHANGE_CMD))
168            {
169            Date d = chooser.getSelectedDate().getTime();
170            t_date.setDate(d);
171            menu.setVisible(false);
172            }
173          }
174        });
175    }
176  
177  /**
178   * Get the date that is currently displayed in the field.
179   *
180   * @return The date.
181   */
182  
183  public Date getDate()
184    {
185    return(t_date.getDate());
186    }
187
188  /**
189   * Set the date to be displayed in the field.
190   *
191   * @param date The new date.
192   */
193
194  public void setDate(Date date)
195    {
196    t_date.setDate(date);
197    }
198  
199  }
200
201/* end of source file */