1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
public class JCalendar
extends JPanel {
private Locale _locale = getDefaultLocale();
private GregorianCalendar _calendar = new GregorianCalendar();
private DateFormatSymbols _dateSymbols = new DateFormatSymbols();
private SimpleDateFormat _formatMY = new SimpleDateFormat("MMMM yyyy");
private int _firstDayOfWeek = _calendar.getFirstDayOfWeek();
private final JLabel _monthYear = new JLabel("", SwingUtilities.CENTER);
private final JLabel[] _daysOfWeek = new JLabel[7];
private final JButton[] _daysNumber = new JButton[42];
private final ActionListener _getDate = new ActionListener() {
public void actionPerformed(final ActionEvent dt) {
System.out.println(dt.getActionCommand());
}
};
private final ActionListener _changeMonth = new ActionListener() {
public void actionPerformed(final ActionEvent ae) {
final int nb = "next".equals(ae.getActionCommand()) ? 1 : -1;
_calendar.add(Calendar.MONTH, nb);
updateMonthYear();
updateDaysNumber();
}
};
public JCalendar() {
setLayout(new BorderLayout());
// Month Panel
final JPanel monthPanel = new JPanel();
final JButton previous = new JButton("<<");
previous.addActionListener(_changeMonth);
final JButton next = new JButton(">>");
next.addActionListener(_changeMonth);
next.setActionCommand("next");
_monthYear.setPreferredSize(new Dimension(120, 20));
monthPanel.add(previous);
monthPanel.add(_monthYear);
monthPanel.add(next);
add(monthPanel, BorderLayout.NORTH);
// Day Panel
final JPanel dayPanel = new JPanel();
dayPanel.setLayout(new GridLayout(7, 7));
for (int i = 0; i < 7; ++i) {
dayPanel.add(_daysOfWeek[i] = new JLabel("", SwingUtilities.CENTER));
}
for (int i = 0; i < _daysNumber.length; ++i) {
dayPanel.add(_daysNumber[i] = new JButton());
}
add(dayPanel, BorderLayout.CENTER);
// Remplissage des composants
updateMonthYear();
updateDaysOfWeek();
updateDaysNumber();
}
// Réactualise la locale et réaffiche le contenu des composants
private void updateLocale(final Locale locale) {
_locale = locale;
final Date tmp = _calendar.getTime();
_calendar = new GregorianCalendar(_locale);
_calendar.setTime(tmp);
_firstDayOfWeek = _calendar.getFirstDayOfWeek();
_dateSymbols = new DateFormatSymbols(_locale);
_formatMY = new SimpleDateFormat("MMMM yyyy", _locale);
updateMonthYear();
updateDaysOfWeek();
updateDaysNumber();
}
// Affiche le mois et l'année en cours
private void updateMonthYear() {
_monthYear.setText(_formatMY.format(_calendar.getTime()));
}
// Affiche les jours de la semaine
private void updateDaysOfWeek() {
final String[] weekDays = _dateSymbols.getShortWeekdays();
for (int i = 1; i < weekDays.length; ++i) {
final int index = (i - 2 + _firstDayOfWeek) % 7 + 1;
_daysOfWeek[i - 1].setText(weekDays[index]);
}
}
// Affiche le numéro des jours
private void updateDaysNumber() {
final Date tmp = _calendar.getTime();
_calendar.set(Calendar.DAY_OF_MONTH, 1);
final int firstDay = _calendar.get(Calendar.DAY_OF_WEEK);
final int LocalFirstDay = (firstDay - _firstDayOfWeek + 7) % 7 + 1;
boolean full = false;
for (int i = 0; i < _daysNumber.length; ++i) {
//Détermine si le composant est affiché ou non
final boolean isNotEmpty = i < LocalFirstDay - 1 || full;
_daysNumber[i].setVisible(!isNotEmpty);
_daysNumber[i].addActionListener(_getDate);
// Affichage du jour
if (!isNotEmpty) {
final int dayOfMonth = _calendar.get(Calendar.DAY_OF_MONTH);
_daysNumber[i].setText(String.valueOf(dayOfMonth));
_calendar.add(Calendar.DAY_OF_MONTH, 1);
full = 1 == _calendar.get(Calendar.DAY_OF_MONTH);
}
_daysNumber[i].setActionCommand(_daysNumber[i].getText());
}
_calendar.setTime(tmp);
}
public static void main(String[] args) {
final JDialog frame = new JDialog();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(new JCalendar());
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
} |