001 package net.provision.soap; 002 003 import java.awt.event.*; 004 005 import java.text.*; 006 007 import java.util.*; 008 009 import javax.swing.*; 010 011 012 /** 013 * Custom button for entering dates. The <code>DateButton</code> class is just a 014 * standard button that defines an additional bound property: "date". The button 015 * displays the date property as its label. When clicked, it does not generate an 016 * <code>ActionEvent</code>, but displays a {@link DateChooser} dialog instead, that 017 * allows you to change the date. When the date is changed, a 018 * <code>PropertyChangeEvent</code> is generated, according the contract for bound 019 * properties. 020 */ 021 public class DateButton extends JButton { 022 /** 023 * Format to use to display the date property. 024 */ 025 public static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM-dd-yyyy"); 026 027 /** 028 * DateChooser instance to use to change the date. 029 */ 030 private static DateChooser DATE_CHOOSER = new DateChooser((JFrame)null, "Select Date"); 031 032 /** 033 * Date property. 034 */ 035 private Date date; 036 private boolean buttonEnabled = false; 037 038 public DateButton(Date date, JFrame owner) { 039 DATE_FORMAT.format(date); 040 this.date = date; 041 DATE_CHOOSER = new DateChooser(owner, "Select Date"); 042 } 043 044 /** 045 * Constructs a new <code>DateButton</code> object with a given date. 046 * 047 * @param date initial date 048 */ 049 public DateButton(Date date) { 050 DATE_FORMAT.format(date); 051 this.date = date; 052 } 053 054 /** 055 * Constructs a new <code>DateButton</code> object with the system date as the 056 * initial date. 057 */ 058 public DateButton() { 059 this(new Date()); 060 } 061 062 /** 063 * DOCUMENT ME! 064 * 065 * @param buttonEnabled DOCUMENT ME! 066 */ 067 public void setButtonState(boolean buttonEnabled) { 068 this.buttonEnabled = buttonEnabled; 069 } 070 071 // added 7/8/2004 so button could be disabled 072 // setEnabled() did not work because it made the font turn gray 073 public boolean getButtonState() { 074 return buttonEnabled; 075 } 076 077 /** 078 * Sets the valus of the date property. 079 * 080 * @param date new value of the date property 081 */ 082 public void setDate(Date date) { 083 Date old = this.date; 084 this.date = date; 085 setText(DATE_FORMAT.format(date)); 086 firePropertyChange("date", old, date); 087 } 088 089 /** 090 * Gets the value of the date property. 091 * 092 * @return the current value of the date property 093 */ 094 public Date getDate() { 095 return date; 096 } 097 098 /** 099 * Called when the button is clicked, in order to fire an <code>ActionEvent</code>. 100 * Displays the dialog to change the date instead of generating the event and 101 * updates the date property. 102 * 103 * @param e <code>ActionEvent</code> to fire 104 */ 105 protected void fireActionPerformed(ActionEvent e) { 106 if(getButtonState()) { 107 Date newDate = DATE_CHOOSER.select(date); 108 109 if(newDate == null) 110 return; 111 112 setDate(newDate); 113 } 114 } 115 }