DateButton.java :  » Game » galatichero » net » narusas » cafelibrary » ui » Java Open Source

Java Open Source » Game » galatichero 
galatichero » net » narusas » cafelibrary » ui » DateButton.java
package net.narusas.cafelibrary.ui;

import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;

import net.narusas.ui.component.GridentButton;

/**
 * * Custom button for entering dates. The <code>DateButton</code> class * is
 * just a standard button that defines an additional bound * property: "date".
 * The button displays the date property as its * label. When clicked, it does
 * not generate an * <code>ActionEvent</code>, but displays a
 * {@link DateChooser} dialog * instead, that allows you to change the date.
 * When the date is * changed, a <code>PropertyChangeEvent</code> is
 * generated, according * the contract for bound properties. *
 */
public class DateButton extends GridentButton {
  /** Format to use to display the date property. */
  private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy.MM.dd");
  /** DateChooser instance to use to change the date. */
  private static final DateChooser DATE_CHOOSER = new DateChooser((JFrame) null, " ");
  /** Date property. */
  private Date date;

  /**
   * * Called when the button is clicked, in order to fire an *
   * <code>ActionEvent</code>. Displays the dialog to change the * date
   * instead of generating the event and updates the date * property. * *
   * 
   * @param e
   *            <code>ActionEvent</code> to fire *
   */
  protected void fireActionPerformed(ActionEvent e) {
    Date newDate = DATE_CHOOSER.select(date);
    if (newDate == null)
      return;
    setDate(newDate);
  }

  /**
   * * Constructs a new <code>DateButton</code> object with a given * date. * *
   * 
   * @param date
   *            initial date *
   */
  public DateButton(Date date) {
    super(DATE_FORMAT.format(date));
    this.date = date;
  }

  /**
   * * Constructs a new <code>DateButton</code> object with the system *
   * date as the initial date. *
   */
  public DateButton() {
    this(new Date());
  }

  /**
   * * Gets the value of the date property. * *
   * 
   * @return the current value of the date property *
   */
  public Date getDate() {
    return date;
  }

  /**
   * * Sets the valus of the date property. * *
   * 
   * @param date
   *            new value of the date property * *
   * @return the old value of the date property *
   */
  public void setDate(Date date) {
    Date old = this.date;
    this.date = date;
    setText(DATE_FORMAT.format(date));
    firePropertyChange("date", old, date);
  }

  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.getContentPane().add(new DateButton());
    f.setSize(200, 200);
    f.setVisible(true);
  }
}
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.