package com.toedter.calendar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.toedter.components.UTF8ResourceBundle;
public class JCalendar extends JPanel
implements PropertyChangeListener {
private static final long serialVersionUID = 8913369762644440133L;
private Calendar calendar;
private boolean initialized = false;
private final JPanel monthYearPanel;
private final JPanel specialButtonPanel;
private boolean isTodayButtonVisible;
private boolean isNullDateButtonVisible;
private final String defaultTodayButtonText = "Today";
private final String defaultNullDateButtonText = "No Date";
private String todayButtonText;
private String nullDateButtonText;
protected JDayChooser dayChooser;
protected boolean weekOfYearVisible = true;
protected Locale locale;
protected JMonthChooser monthChooser;
protected JYearChooser yearChooser;
private final JButton todayButton;
private final JButton nullDateButton;
this(null, null, true, true);
}
this(date, null, true, true);
}
this(null, null, true, true);
setCalendar(calendar);
}
this(null, locale, true, true);
}
this(date, locale, true, true);
}
public JCalendar(Date date,
boolean monthSpinner) {
this(date, null, monthSpinner, true);
}
public JCalendar(Locale locale,
boolean monthSpinner) {
this(null, locale, monthSpinner, true);
}
this(null, null, monthSpinner, true);
}
public JCalendar(Date date, Locale locale,
boolean monthSpinner,
boolean weekOfYearVisible) {
setName("JCalendar");
dayChooser = null;
monthChooser = null;
yearChooser = null;
this.weekOfYearVisible = weekOfYearVisible;
if (locale == null) {
this.locale = Locale.getDefault();
} else {
this.locale = locale;
}
calendar = Calendar.getInstance(this.locale);
setLayout(new BorderLayout());
monthYearPanel = new JPanel();
monthYearPanel.setLayout(new BorderLayout());
monthChooser = new JMonthChooser(monthSpinner);
yearChooser = new JYearChooser();
monthChooser.setYearChooser(yearChooser);
monthChooser.setLocale(this.locale);
monthYearPanel.add(monthChooser, BorderLayout.WEST);
monthYearPanel.add(yearChooser, BorderLayout.CENTER);
monthYearPanel.setBorder(BorderFactory.createEmptyBorder());
dayChooser = new JDayChooser(weekOfYearVisible);
dayChooser.addPropertyChangeListener(this);
dayChooser.setLocale(this.locale);
monthChooser.setDayChooser(dayChooser);
monthChooser.addPropertyChangeListener(this);
yearChooser.setDayChooser(dayChooser);
yearChooser.addPropertyChangeListener(this);
add(monthYearPanel, BorderLayout.NORTH);
add(dayChooser, BorderLayout.CENTER);
specialButtonPanel = new JPanel();
todayButton = new JButton();
todayButton.addActionListener(new ActionListener() {
setDate(new Date());
}
});
nullDateButton = new JButton();
nullDateButton.addActionListener(new ActionListener() {
dayChooser.firePropertyChange("day", 0, -1);
}
});
specialButtonPanel.setVisible(false);
add(specialButtonPanel, BorderLayout.SOUTH);
if (date != null) {
calendar.setTime(date);
}
initialized = true;
setCalendar(calendar);
}
public static void main(String[] s) {
JFrame frame = new JFrame("JCalendar");
JCalendar jcalendar = new JCalendar();
frame.getContentPane().add(jcalendar);
frame.pack();
frame.setVisible(true);
}
return calendar;
}
return dayChooser;
}
return locale;
}
return monthChooser;
}
return yearChooser;
}
return dayChooser.isWeekOfYearVisible();
}
if (calendar != null) {
Calendar c = (Calendar) calendar.clone();
if (evt.getPropertyName().equals("day")) {
c.set(Calendar.DAY_OF_MONTH,
((Integer) evt.getNewValue()).intValue());
setCalendar(c, false);
} else if (evt.getPropertyName().equals("month")) {
c.set(Calendar.MONTH, ((Integer) evt.getNewValue()).intValue());
setCalendar(c, false);
} else if (evt.getPropertyName().equals("year")) {
c.set(Calendar.YEAR, ((Integer) evt.getNewValue()).intValue());
setCalendar(c, false);
} else if (evt.getPropertyName().equals("date")) {
c.setTime((Date) evt.getNewValue());
setCalendar(c, true);
}
}
}
super.setBackground(bg);
if (dayChooser != null) {
dayChooser.setBackground(bg);
}
}
setCalendar(c, true);
}
if (c == null) {
setDate(null);
}
Calendar oldCalendar = calendar;
calendar = c;
if (update) {
yearChooser.setYear(c.get(Calendar.YEAR));
monthChooser.setMonth(c.get(Calendar.MONTH));
dayChooser.setDay(c.get(Calendar.DATE));
}
firePropertyChange("calendar", oldCalendar, calendar);
}
super.setEnabled(enabled);
if (dayChooser != null) {
dayChooser.setEnabled(enabled);
monthChooser.setEnabled(enabled);
yearChooser.setEnabled(enabled);
}
}
return super.isEnabled();
}
super.setFont(font);
if (dayChooser != null) {
dayChooser.setFont(font);
monthChooser.setFont(font);
yearChooser.setFont(font);
}
}
super.setForeground(fg);
if (dayChooser != null) {
dayChooser.setForeground(fg);
monthChooser.setForeground(fg);
yearChooser.setForeground(fg);
}
}
if (!initialized) {
super.setLocale(l);
} else {
Locale oldLocale = locale;
locale = l;
dayChooser.setLocale(locale);
monthChooser.setLocale(locale);
relayoutSpecialButtonPanel();
firePropertyChange("locale", oldLocale, locale);
}
}
dayChooser.setWeekOfYearVisible(weekOfYearVisible);
setLocale(locale);
}
return dayChooser.isDecorationBackgroundVisible();
}
boolean decorationBackgroundVisible) {
dayChooser.setDecorationBackgroundVisible(decorationBackgroundVisible);
setLocale(locale);
}
return dayChooser.isDecorationBordersVisible();
}
dayChooser.setDecorationBordersVisible(decorationBordersVisible);
setLocale(locale);
}
return dayChooser.getDecorationBackgroundColor();
}
dayChooser.setDecorationBackgroundColor(decorationBackgroundColor);
}
return dayChooser.getSundayForeground();
}
return dayChooser.getWeekdayForeground();
}
dayChooser.setSundayForeground(sundayForeground);
}
dayChooser.setWeekdayForeground(weekdayForeground);
}
return new Date(calendar.getTimeInMillis());
}
Date oldDate = calendar.getTime();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
yearChooser.setYear(year);
monthChooser.setMonth(month);
dayChooser.setCalendar(calendar);
dayChooser.setDay(day);
firePropertyChange("date", oldDate, date);
}
dayChooser.setSelectableDateRange(min, max);
};
return dayChooser.getMaxSelectableDate();
}
return dayChooser.getMinSelectableDate();
}
dayChooser.setMaxSelectableDate(max);
}
dayChooser.setMinSelectableDate(min);
}
return dayChooser.getMaxDayCharacters();
}
dayChooser.setMaxDayCharacters(maxDayCharacters);
}
this.isTodayButtonVisible = isTodayButtonVisible;
relayoutSpecialButtonPanel();
}
return isTodayButtonVisible;
}
this.isNullDateButtonVisible = isNullDateButtonVisible;
relayoutSpecialButtonPanel();
}
return isNullDateButtonVisible;
}
ResourceBundle resourceBundle = null;
try {
resourceBundle = UTF8ResourceBundle.getBundle(
"com.toedter.calendar.jcalendar", locale);
} catch (Exception e) {
System.out.println(e.getMessage());
}
specialButtonPanel.removeAll();
int buttonCount = 0;
if (isTodayButtonVisible) {
String text = todayButtonText;
if (text == null && resourceBundle != null) {
try {
text = resourceBundle.getString("todayButton.text");
} catch (Exception e) {
}
}
if (text == null) {
text = defaultTodayButtonText;
}
todayButton.setText(text);
specialButtonPanel.add(todayButton);
buttonCount++;
}
if (isNullDateButtonVisible) {
String text = nullDateButtonText;
if (text == null && resourceBundle != null) {
try {
text = resourceBundle.getString("nullDateButton.text");
} catch (Exception e) {
}
}
if (text == null) {
text = defaultNullDateButtonText;
}
nullDateButton.setText(text);
specialButtonPanel.add(nullDateButton);
buttonCount++;
}
specialButtonPanel.setLayout(new GridLayout(1, buttonCount));
if (isTodayButtonVisible) {
specialButtonPanel.add(todayButton);
}
if (isNullDateButtonVisible) {
specialButtonPanel.add(nullDateButton);
}
specialButtonPanel.setVisible(isNullDateButtonVisible
|| isTodayButtonVisible);
todayButton.invalidate();
todayButton.repaint();
nullDateButton.invalidate();
nullDateButton.repaint();
specialButtonPanel.invalidate();
specialButtonPanel.doLayout();
specialButtonPanel.repaint();
invalidate();
repaint();
}
return todayButtonText;
}
if (todayButtonText != null & todayButtonText.trim().length() == 0) {
this.todayButtonText = null;
} else {
this.todayButtonText = todayButtonText;
}
relayoutSpecialButtonPanel();
}
return nullDateButtonText;
}
if (nullDateButtonText != null
& nullDateButtonText.trim().length() == 0) {
this.nullDateButtonText = null;
} else {
this.nullDateButtonText = nullDateButtonText;
}
relayoutSpecialButtonPanel();
}
}