2
* Copyright (C) 2004 Kai Toedter
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
package com.toedter.calendar;
22
import java.awt.BorderLayout;
23
import java.awt.Color;
25
import java.beans.PropertyChangeEvent;
26
import java.beans.PropertyChangeListener;
27
import java.util.Calendar;
28
import java.util.Date;
29
import java.util.Locale;
31
import javax.swing.BorderFactory;
32
import javax.swing.JFrame;
33
import javax.swing.JPanel;
37
* JCalendar is a bean for entering a date by choosing the year, month and day.
42
public class JCalendar extends JPanel implements PropertyChangeListener {
43
private Calendar calendar;
45
/** the day chooser */
46
protected JDayChooser dayChooser;
47
private boolean initialized = false;
49
/** indicates if weeks of year shall be visible */
50
protected boolean weekOfYearVisible = true;
53
protected Locale locale;
55
/** the month chooser */
56
protected JMonthChooser monthChooser;
57
private JPanel monthYearPanel;
59
/** the year chhoser */
60
protected JYearChooser yearChooser;
63
* Default JCalendar constructor.
66
this(null, null, true, true);
70
* JCalendar constructor specifying the month spinner type.
72
* @param monthSpinner false, if no month spinner should be used
74
public JCalendar(boolean monthSpinner) {
75
this(null, null, monthSpinner, true);
79
* JCalendar constructor which allows the initial date to be set.
81
* @param date the date
83
public JCalendar(Date date) {
84
this(date, null, true, true);
88
* JCalendar constructor allowing the initial locale to be set.
90
* @param locale the new locale
92
public JCalendar(Locale locale) {
93
this(null, locale, true, true);
97
* JCalendar constructor specifying both the initial date and locale.
99
* @param date the date
100
* @param locale the new locale
102
public JCalendar(Date date, Locale locale) {
103
this(date, locale, true, true);
107
* JCalendar constructor specifying both the initial date and the month
110
* @param date the date
111
* @param monthSpinner false, if no month spinner should be used
113
public JCalendar(Date date, boolean monthSpinner) {
114
this(date, null, monthSpinner, true);
118
* JCalendar constructor specifying both the locale and the month spinner.
120
* @param locale the locale
121
* @param monthSpinner false, if no month spinner should be used
123
public JCalendar(Locale locale, boolean monthSpinner) {
124
this(null, locale, monthSpinner, true);
128
* JCalendar constructor with month spinner parameter.
130
* @param date the date
131
* @param locale the locale
132
* @param monthSpinner false, if no month spinner should be used
133
* @param weekOfYearVisible true, if weeks of year shall be visible
135
public JCalendar(Date date, Locale locale, boolean monthSpinner,
136
boolean weekOfYearVisible) {
137
// needed for setFont() etc.
141
this.weekOfYearVisible = weekOfYearVisible;
143
this.locale = locale;
145
if (locale == null) {
146
this.locale = Locale.getDefault();
149
calendar = Calendar.getInstance();
151
setLayout(new BorderLayout());
153
monthYearPanel = new JPanel();
154
monthYearPanel.setLayout(new BorderLayout());
156
monthChooser = new JMonthChooser(monthSpinner);
157
yearChooser = new JYearChooser();
158
monthChooser.setYearChooser(yearChooser);
159
monthYearPanel.add(monthChooser, BorderLayout.WEST);
160
monthYearPanel.add(yearChooser, BorderLayout.CENTER);
161
monthYearPanel.setBorder(BorderFactory.createEmptyBorder());
163
dayChooser = new JDayChooser(weekOfYearVisible);
164
dayChooser.addPropertyChangeListener(this);
165
monthChooser.setDayChooser(dayChooser);
166
monthChooser.addPropertyChangeListener(this);
167
yearChooser.setDayChooser(dayChooser);
168
yearChooser.addPropertyChangeListener(this);
169
add(monthYearPanel, BorderLayout.NORTH);
170
add(dayChooser, BorderLayout.CENTER);
172
// Set the initialized flag before setting the calendar. This will
173
// cause the other components to be updated properly.
175
calendar.setTime(date);
179
setCalendar(calendar);
183
* Creates a JFrame with a JCalendar inside and can be used for testing.
185
* @param s The command line arguments
187
public static void main(String[] s) {
188
JFrame frame = new JFrame("JCalendar");
189
frame.getContentPane().add(new JCalendar());
191
frame.setVisible(true);
195
* Returns the calendar property.
197
* @return the value of the calendar property.
199
public Calendar getCalendar() {
204
* Gets the dayChooser attribute of the JCalendar object
206
* @return the dayChooser value
208
public JDayChooser getDayChooser() {
213
* Returns the locale.
215
* @return the value of the locale property.
219
public Locale getLocale() {
224
* Gets the monthChooser attribute of the JCalendar object
226
* @return the monthChooser value
228
public JMonthChooser getMonthChooser() {
233
* Returns "JCalendar".
235
* @return "JCalendar"
237
public String getName() {
242
* Gets the yearChooser attribute of the JCalendar object
244
* @return the yearChooser value
246
public JYearChooser getYearChooser() {
251
* Indicates if the weeks of year are visible..
253
* @return boolean true, if weeks of year are visible
255
public boolean isWeekOfYearVisible() {
256
return dayChooser.isWeekOfYearVisible();
260
* JCalendar is a PropertyChangeListener, for its day, month and year
263
* @param evt the property change event
265
public void propertyChange(PropertyChangeEvent evt) {
266
if (calendar != null) {
267
Calendar c = (Calendar) calendar.clone();
269
if (evt.getPropertyName().equals("day")) {
270
c.set(Calendar.DAY_OF_MONTH,
271
((Integer) evt.getNewValue()).intValue());
272
setCalendar(c, false);
273
} else if (evt.getPropertyName().equals("month")) {
274
c.set(Calendar.MONTH, ((Integer) evt.getNewValue()).intValue());
275
setCalendar(c, false);
276
} else if (evt.getPropertyName().equals("year")) {
277
c.set(Calendar.YEAR, ((Integer) evt.getNewValue()).intValue());
278
setCalendar(c, false);
279
} else if (evt.getPropertyName().equals("date")) {
280
c.setTime((Date) evt.getNewValue());
281
setCalendar(c, true);
287
* Sets the background color.
289
* @param bg the new background
291
public void setBackground(Color bg) {
292
super.setBackground(bg);
294
if (dayChooser != null) {
295
dayChooser.setBackground(bg);
300
* Sets the calendar property. This is a bound property.
302
* @param c the new calendar
306
public void setCalendar(Calendar c) {
307
setCalendar(c, true);
311
* Sets the calendar attribute of the JCalendar object
313
* @param c the new calendar value
314
* @param update the new calendar value
316
private void setCalendar(Calendar c, boolean update) {
317
Calendar oldCalendar = calendar;
321
// Thanks to Jeff Ulmer for correcting a bug in the sequence :)
322
yearChooser.setYear(c.get(Calendar.YEAR));
323
monthChooser.setMonth(c.get(Calendar.MONTH));
324
dayChooser.setDay(c.get(Calendar.DATE));
327
firePropertyChange("calendar", oldCalendar, calendar);
331
* Enable or disable the JCalendar.
333
* @param enabled the new enabled value
335
public void setEnabled(boolean enabled) {
336
super.setEnabled(enabled);
338
if (dayChooser != null) {
339
dayChooser.setEnabled(enabled);
340
monthChooser.setEnabled(enabled);
341
yearChooser.setEnabled(enabled);
346
* Returns true, if enabled.
348
* @return true, if enabled.
350
public boolean isEnabled() {
351
return super.isEnabled();
355
* Sets the font property.
357
* @param font the new font
359
public void setFont(Font font) {
362
if (dayChooser != null) {
363
dayChooser.setFont(font);
364
monthChooser.setFont(font);
365
yearChooser.setFont(font);
370
* Sets the foreground color.
372
* @param fg the new foreground
374
public void setForeground(Color fg) {
375
super.setForeground(fg);
377
if (dayChooser != null) {
378
dayChooser.setForeground(fg);
379
monthChooser.setForeground(fg);
380
yearChooser.setForeground(fg);
385
* Sets the locale property. This is a bound property.
387
* @param l the new locale value
391
public void setLocale(Locale l) {
395
Locale oldLocale = locale;
397
dayChooser.setLocale(locale);
398
monthChooser.setLocale(locale);
399
firePropertyChange("locale", oldLocale, locale);
404
* Sets the week of year visible.
406
* @param weekOfYearVisible true, if weeks of year shall be visible
408
public void setWeekOfYearVisible(boolean weekOfYearVisible) {
409
dayChooser.setWeekOfYearVisible(weekOfYearVisible);
410
setLocale(locale); // hack for doing complete new layout :)
414
* Gets the visibility of the decoration background.
416
* @return true, if the decoration background is visible.
418
public boolean isDecorationBackgroundVisible() {
419
return dayChooser.isDecorationBackgroundVisible();
423
* Sets the decoration background visible.
425
* @param decorationBackgroundVisible true, if the decoration background
428
public void setDecorationBackgroundVisible(
429
boolean decorationBackgroundVisible) {
430
dayChooser.setDecorationBackgroundVisible(decorationBackgroundVisible);
431
setLocale(locale); // hack for doing complete new layout :)
435
* Gets the visibility of the decoration border.
437
* @return true, if the decoration border is visible.
439
public boolean isDecorationBordersVisible() {
440
return dayChooser.isDecorationBordersVisible();
444
* Sets the decoration borders visible.
446
* @param decorationBordersVisible true, if the decoration borders should
449
public void setDecorationBordersVisible(boolean decorationBordersVisible) {
450
dayChooser.setDecorationBordersVisible(decorationBordersVisible);
451
setLocale(locale); // hack for doing complete new layout :)
455
* Returns the color of the decoration (day names and weeks).
457
* @return the color of the decoration (day names and weeks).
459
public Color getDecorationBackgroundColor() {
460
return dayChooser.getDecorationBackgroundColor();
464
* Sets the background of days and weeks of year buttons.
466
* @param decorationBackgroundColor the background color
468
public void setDecorationBackgroundColor(Color decorationBackgroundColor) {
469
dayChooser.setDecorationBackgroundColor(decorationBackgroundColor);
473
* Returns the Sunday foreground.
475
* @return Color the Sunday foreground.
477
public Color getSundayForeground() {
478
return dayChooser.getSundayForeground();
482
* Returns the weekday foreground.
484
* @return Color the weekday foreground.
486
public Color getWeekdayForeground() {
487
return dayChooser.getWeekdayForeground();
491
* Sets the Sunday foreground.
493
* @param sundayForeground the sundayForeground to set
495
public void setSundayForeground(Color sundayForeground) {
496
dayChooser.setSundayForeground(sundayForeground);
500
* Sets the weekday foreground.
502
* @param weekdayForeground the weekdayForeground to set
504
public void setWeekdayForeground(Color weekdayForeground) {
505
dayChooser.setWeekdayForeground(weekdayForeground);
509
* Returns a Date object.
511
* @return a date object constructed from the calendar property.
513
public Date getDate() {
514
return new Date(calendar.getTimeInMillis());
518
* Sets the date. Fires the property change "date".
520
* @param date the new date.
522
public void setDate(Date date) {
523
Date oldDate = calendar.getTime();
524
calendar.setTime(date);
526
yearChooser.setYear(calendar.get(Calendar.YEAR));
527
monthChooser.setMonth(calendar.get(Calendar.MONTH));
528
dayChooser.setDay(calendar.get(Calendar.DATE));
530
firePropertyChange("date", oldDate, date);