001: /*
002: * 02/02/2002 - 20:54:54
003: *
004: * JCalendar.java - JCalendar Java Bean
005: * Copyright (C) 2002 Kai Toedter
006: * kai@toedter.com
007: * www.toedter.com
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
022: */
023: package com.toedter.calendar;
024:
025: import java.awt.BorderLayout;
026: import java.awt.Color;
027: import java.awt.Font;
028: import java.awt.GridLayout;
029: import java.beans.PropertyChangeEvent;
030: import java.beans.PropertyChangeListener;
031: import java.util.Calendar;
032: import java.util.Locale;
033:
034: import javax.swing.JFrame;
035: import javax.swing.JPanel;
036:
037: /**
038: * JCalendar is a bean for entering a date by choosing the year, month and day.
039: *
040: *@author Kai Toedter
041: *@version 1.1.4 07/16/02
042: */
043: public class JCalendar extends JPanel implements PropertyChangeListener {
044: private static final long serialVersionUID = 3258410646839308599L;
045:
046: /**
047: * Default JCalendar constructor.
048: */
049: public JCalendar() {
050: this (JMonthChooser.RIGHT_SPINNER);
051: }
052:
053: /**
054: * JCalendar constructor with month spinner parameter.
055: *
056: *@param monthSpinner Possible values are JMonthChooser.RIGHT_SPINNER,
057: * JMonthChooser.LEFT_SPINNER, JMonthChooser.NO_SPINNER
058: */
059: public JCalendar(int monthSpinner) {
060: // needed for setFont() etc.
061: dayChooser = null;
062: monthChooser = null;
063: yearChooser = null;
064:
065: locale = Locale.getDefault();
066: calendar = Calendar.getInstance();
067:
068: setLayout(new BorderLayout());
069: JPanel myPanel = new JPanel();
070: myPanel.setLayout(new GridLayout(1, 3));
071: monthChooser = new JMonthChooser(monthSpinner);
072: yearChooser = new JYearChooser();
073: monthChooser.setYearChooser(yearChooser);
074: myPanel.add(monthChooser);
075: myPanel.add(yearChooser);
076: dayChooser = new JDayChooser();
077: dayChooser.addPropertyChangeListener(this );
078: monthChooser.setDayChooser(dayChooser);
079: monthChooser.addPropertyChangeListener(this );
080: yearChooser.setDayChooser(dayChooser);
081: yearChooser.addPropertyChangeListener(this );
082: add(myPanel, BorderLayout.NORTH);
083: add(dayChooser, BorderLayout.CENTER);
084: initialized = true;
085: }
086:
087: /**
088: * Sets the calendar attribute of the JCalendar object
089: *
090: *@param c The new calendar value
091: *@param update The new calendar value
092: */
093: private void setCalendar(Calendar c, boolean update) {
094: Calendar oldCalendar = calendar;
095: calendar = c;
096: if (update) {
097: // Thanks to Jeff Ulmer for correcting a bug in the sequence :)
098: yearChooser.setYear(c.get(Calendar.YEAR));
099: monthChooser.setMonth(c.get(Calendar.MONTH));
100: dayChooser.setDay(c.get(Calendar.DATE));
101: }
102: firePropertyChange("calendar", oldCalendar, calendar);
103: }
104:
105: /**
106: * Sets the calendar property. This is a bound property.
107: *
108: *@param c the new calendar
109: *@see #getCalendar
110: */
111: public void setCalendar(Calendar c) {
112: setCalendar(c, true);
113: }
114:
115: /**
116: * Returns the calendar property.
117: *
118: *@return the value of the calendar property.
119: *@see #setCalendar
120: */
121: public Calendar getCalendar() {
122: return calendar;
123: }
124:
125: /**
126: * Sets the locale property. This is a bound property.
127: *
128: *@param l The new locale value
129: *@see #getLocale
130: */
131: public void setLocale(Locale l) {
132: if (!initialized) {
133: super .setLocale(l);
134: } else {
135: Locale oldLocale = locale;
136: locale = l;
137: dayChooser.setLocale(locale);
138: monthChooser.setLocale(locale);
139: firePropertyChange("locale", oldLocale, locale);
140: }
141: }
142:
143: /**
144: * Returns the locale.
145: *
146: *@return the value of the locale property.
147: *@see #setLocale
148: */
149: public Locale getLocale() {
150: return locale;
151: }
152:
153: /**
154: * Sets the font property.
155: *
156: *@param font the new font
157: */
158: public void setFont(Font font) {
159: super .setFont(font);
160: if (dayChooser != null) {
161: dayChooser.setFont(font);
162: monthChooser.setFont(font);
163: yearChooser.setFont(font);
164: }
165: }
166:
167: /**
168: * Sets the foreground color.
169: *
170: *@param fg the new foreground
171: */
172: public void setForeground(Color fg) {
173: super .setForeground(fg);
174: if (dayChooser != null) {
175: dayChooser.setForeground(fg);
176: monthChooser.setForeground(fg);
177: yearChooser.setForeground(fg);
178: }
179: }
180:
181: /**
182: * Sets the background color.
183: *
184: *@param bg the new background
185: */
186: public void setBackground(Color bg) {
187: super .setBackground(bg);
188: if (dayChooser != null) {
189: dayChooser.setBackground(bg);
190: }
191: }
192:
193: /**
194: * JCalendar is a PropertyChangeListener, for its day, month and year chooser.
195: *
196: *@param evt Description of the Parameter
197: */
198: public void propertyChange(PropertyChangeEvent evt) {
199: if (calendar != null) {
200: Calendar c = (Calendar) calendar.clone();
201: if (evt.getPropertyName().equals("day")) {
202: c.set(Calendar.DAY_OF_MONTH, ((Integer) evt
203: .getNewValue()).intValue());
204: setCalendar(c, false);
205: } else if (evt.getPropertyName().equals("month")) {
206: c.set(Calendar.MONTH, ((Integer) evt.getNewValue())
207: .intValue());
208: setCalendar(c, false);
209: } else if (evt.getPropertyName().equals("year")) {
210: c.set(Calendar.YEAR, ((Integer) evt.getNewValue())
211: .intValue());
212: setCalendar(c, false);
213: }
214: }
215: }
216:
217: /**
218: * Returns "JCalendar".
219: *
220: *@return The name value
221: */
222: public String getName() {
223: return "JCalendar";
224: }
225:
226: /**
227: * Enable or disable the JCalendar.
228: *
229: *@param enabled The new enabled value
230: */
231: public void setEnabled(boolean enabled) {
232: super .setEnabled(enabled);
233: if (dayChooser != null) {
234: dayChooser.setEnabled(enabled);
235: monthChooser.setEnabled(enabled);
236: yearChooser.setEnabled(enabled);
237: }
238: }
239:
240: /**
241: * Gets the dayChooser attribute of the JCalendar object
242: *
243: *@return The dayChooser value
244: */
245: public JDayChooser getDayChooser() {
246: return dayChooser;
247: }
248:
249: /**
250: * Gets the monthChooser attribute of the JCalendar object
251: *
252: *@return The monthChooser value
253: */
254: public JMonthChooser getMonthChooser() {
255: return monthChooser;
256: }
257:
258: /**
259: * Gets the yearChooser attribute of the JCalendar object
260: *
261: *@return The yearChooser value
262: */
263: public JYearChooser getYearChooser() {
264: return yearChooser;
265: }
266:
267: /**
268: * Creates a JFrame with a JCalendar inside and can be used for testing.
269: *
270: *@param s The command line arguments
271: */
272: public static void main(String[] s) {
273: JFrame frame = new JFrame("JCalendar");
274: frame.getContentPane().add(new JCalendar());
275: frame.pack();
276: frame.setVisible(true);
277: }
278:
279: /**
280: * the year chhoser
281: */
282: protected JYearChooser yearChooser;
283:
284: /**
285: * the month chooser
286: */
287: protected JMonthChooser monthChooser;
288:
289: /**
290: * the day chooser
291: */
292: protected JDayChooser dayChooser;
293:
294: private Calendar calendar;
295: private Locale locale;
296: private boolean initialized = false;
297: }
|