1:
47:
48: package ;
49:
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60:
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68:
69: import ;
70:
71:
76: public class DateChooserPanel extends JPanel implements ActionListener {
77:
78:
81: private Calendar chosenDate;
82:
83:
86: private Color chosenDateButtonColor;
87:
88:
91: private Color chosenMonthButtonColor;
92:
93:
96: private Color chosenOtherButtonColor;
97:
98:
101: private int firstDayOfWeek;
102:
103:
106: private int yearSelectionRange = 20;
107:
108:
111: private Font dateFont = new Font("SansSerif", Font.PLAIN, 10);
112:
113:
116: private JComboBox monthSelector;
117:
118:
121: private JComboBox yearSelector;
122:
123:
126: private JButton todayButton;
127:
128:
131: private JButton[] buttons;
132:
133:
137: private boolean refreshing = false;
138:
139:
143: private int[] WEEK_DAYS;
144:
145:
149: public DateChooserPanel() {
150: this(Calendar.getInstance(), false);
151: }
152:
153:
160: public DateChooserPanel(final Calendar calendar,
161: final boolean controlPanel) {
162:
163: super(new BorderLayout());
164:
165: this.chosenDateButtonColor = UIManager.getColor("textHighlight");
166: this.chosenMonthButtonColor = UIManager.getColor("control");
167: this.chosenOtherButtonColor = UIManager.getColor("controlShadow");
168:
169:
170: this.chosenDate = calendar;
171: this.firstDayOfWeek = calendar.getFirstDayOfWeek();
172: this.WEEK_DAYS = new int[7];
173: for (int i = 0; i < 7; i++) {
174: this.WEEK_DAYS[i] = ((this.firstDayOfWeek + i - 1) % 7) + 1;
175: }
176:
177: add(constructSelectionPanel(), BorderLayout.NORTH);
178: add(getCalendarPanel(), BorderLayout.CENTER);
179: if (controlPanel) {
180: add(constructControlPanel(), BorderLayout.SOUTH);
181: }
182: setDate(calendar.getTime());
183: }
184:
185:
190: public void setDate(final Date theDate) {
191:
192: this.chosenDate.setTime(theDate);
193: this.monthSelector.setSelectedIndex(this.chosenDate.get(
194: Calendar.MONTH));
195: refreshYearSelector();
196: refreshButtons();
197:
198: }
199:
200:
205: public Date getDate() {
206: return this.chosenDate.getTime();
207: }
208:
209:
214: public void actionPerformed(final ActionEvent e) {
215:
216: if (e.getActionCommand().equals("monthSelectionChanged")) {
217: final JComboBox c = (JComboBox) e.getSource();
218:
219:
220:
221:
222:
223: int dayOfMonth = this.chosenDate.get(Calendar.DAY_OF_MONTH);
224: this.chosenDate.set(Calendar.DAY_OF_MONTH, 1);
225: this.chosenDate.set(Calendar.MONTH, c.getSelectedIndex());
226: int maxDayOfMonth = this.chosenDate.getActualMaximum(
227: Calendar.DAY_OF_MONTH);
228: this.chosenDate.set(Calendar.DAY_OF_MONTH, Math.min(dayOfMonth,
229: maxDayOfMonth));
230: refreshButtons();
231: }
232: else if (e.getActionCommand().equals("yearSelectionChanged")) {
233: if (!this.refreshing) {
234: final JComboBox c = (JComboBox) e.getSource();
235: final Integer y = (Integer) c.getSelectedItem();
236:
237:
238:
239:
240:
241: int dayOfMonth = this.chosenDate.get(Calendar.DAY_OF_MONTH);
242: this.chosenDate.set(Calendar.DAY_OF_MONTH, 1);
243: this.chosenDate.set(Calendar.YEAR, y.intValue());
244: int maxDayOfMonth = this.chosenDate.getActualMaximum(
245: Calendar.DAY_OF_MONTH);
246: this.chosenDate.set(Calendar.DAY_OF_MONTH, Math.min(dayOfMonth,
247: maxDayOfMonth));
248: refreshYearSelector();
249: refreshButtons();
250: }
251: }
252: else if (e.getActionCommand().equals("todayButtonClicked")) {
253: setDate(new Date());
254: }
255: else if (e.getActionCommand().equals("dateButtonClicked")) {
256: final JButton b = (JButton) e.getSource();
257: final int i = Integer.parseInt(b.getName());
258: final Calendar cal = getFirstVisibleDate();
259: cal.add(Calendar.DATE, i);
260: setDate(cal.getTime());
261: }
262: }
263:
264:
270: private JPanel getCalendarPanel() {
271:
272: final JPanel p = new JPanel(new GridLayout(7, 7));
273: final DateFormatSymbols dateFormatSymbols = new DateFormatSymbols();
274: final String[] weekDays = dateFormatSymbols.getShortWeekdays();
275:
276: for (int i = 0; i < this.WEEK_DAYS.length; i++) {
277: p.add(new JLabel(weekDays[this.WEEK_DAYS[i]],
278: SwingConstants.CENTER));
279: }
280:
281: this.buttons = new JButton[42];
282: for (int i = 0; i < 42; i++) {
283: final JButton b = new JButton("");
284: b.setMargin(new Insets(1, 1, 1, 1));
285: b.setName(Integer.toString(i));
286: b.setFont(this.dateFont);
287: b.setFocusPainted(false);
288: b.setActionCommand("dateButtonClicked");
289: b.addActionListener(this);
290: this.buttons[i] = b;
291: p.add(b);
292: }
293: return p;
294:
295: }
296:
297:
303: private Color getButtonColor(final Calendar theDate) {
304: if (equalDates(theDate, this.chosenDate)) {
305: return this.chosenDateButtonColor;
306: }
307: else if (theDate.get(Calendar.MONTH) == this.chosenDate.get(
308: Calendar.MONTH)) {
309: return this.chosenMonthButtonColor;
310: }
311: else {
312: return this.chosenOtherButtonColor;
313: }
314: }
315:
316:
323: private boolean equalDates(final Calendar c1, final Calendar c2) {
324: if ((c1.get(Calendar.DATE) == c2.get(Calendar.DATE))
325: && (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
326: && (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))) {
327: return true;
328: }
329: else {
330: return false;
331: }
332: }
333:
334:
340: private Calendar getFirstVisibleDate() {
341: final Calendar c = Calendar.getInstance();
342: c.set(this.chosenDate.get(Calendar.YEAR), this.chosenDate.get(
343: Calendar.MONTH), 1);
344: c.add(Calendar.DATE, -1);
345: while (c.get(Calendar.DAY_OF_WEEK) != getFirstDayOfWeek()) {
346: c.add(Calendar.DATE, -1);
347: }
348: return c;
349: }
350:
351:
357: private int getFirstDayOfWeek() {
358: return this.firstDayOfWeek;
359: }
360:
361:
364: private void refreshButtons() {
365: final Calendar c = getFirstVisibleDate();
366: for (int i = 0; i < 42; i++) {
367: final JButton b = this.buttons[i];
368: b.setText(Integer.toString(c.get(Calendar.DATE)));
369: b.setBackground(getButtonColor(c));
370: c.add(Calendar.DATE, 1);
371: }
372: }
373:
374:
378: private void refreshYearSelector() {
379: if (!this.refreshing) {
380: this.refreshing = true;
381: this.yearSelector.removeAllItems();
382: final Integer[] years = getYears(this.chosenDate.get(
383: Calendar.YEAR));
384: for (int i = 0; i < years.length; i++) {
385: this.yearSelector.addItem(years[i]);
386: }
387: this.yearSelector.setSelectedItem(new Integer(this.chosenDate.get(
388: Calendar.YEAR)));
389: this.refreshing = false;
390: }
391: }
392:
393:
401: private Integer[] getYears(final int chosenYear) {
402: final int size = this.yearSelectionRange * 2 + 1;
403: final int start = chosenYear - this.yearSelectionRange;
404:
405: final Integer[] years = new Integer[size];
406: for (int i = 0; i < size; i++) {
407: years[i] = new Integer(i + start);
408: }
409: return years;
410: }
411:
412:
418: private JPanel constructSelectionPanel() {
419: final JPanel p = new JPanel();
420:
421: final int minMonth = this.chosenDate.getMinimum(Calendar.MONTH);
422: final int maxMonth = this.chosenDate.getMaximum(Calendar.MONTH);
423: final String[] months = new String[maxMonth - minMonth + 1];
424: System.arraycopy(SerialDate.getMonths(), minMonth, months, 0,
425: months.length);
426:
427: this.monthSelector = new JComboBox(months);
428: this.monthSelector.addActionListener(this);
429: this.monthSelector.setActionCommand("monthSelectionChanged");
430: p.add(this.monthSelector);
431:
432: this.yearSelector = new JComboBox(getYears(0));
433: this.yearSelector.addActionListener(this);
434: this.yearSelector.setActionCommand("yearSelectionChanged");
435: p.add(this.yearSelector);
436:
437: return p;
438: }
439:
440:
446: private JPanel constructControlPanel() {
447:
448: final JPanel p = new JPanel();
449: p.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
450: this.todayButton = new JButton("Today");
451: this.todayButton.addActionListener(this);
452: this.todayButton.setActionCommand("todayButtonClicked");
453: p.add(this.todayButton);
454: return p;
455:
456: }
457:
458:
463: public Color getChosenDateButtonColor() {
464: return this.chosenDateButtonColor;
465: }
466:
467:
472: public void setChosenDateButtonColor(final Color chosenDateButtonColor) {
473: if (chosenDateButtonColor == null) {
474: throw new NullPointerException("UIColor must not be null.");
475: }
476: final Color oldValue = this.chosenDateButtonColor;
477: this.chosenDateButtonColor = chosenDateButtonColor;
478: refreshButtons();
479: firePropertyChange("chosenDateButtonColor", oldValue,
480: chosenDateButtonColor);
481: }
482:
483:
488: public Color getChosenMonthButtonColor() {
489: return this.chosenMonthButtonColor;
490: }
491:
492:
497: public void setChosenMonthButtonColor(final Color chosenMonthButtonColor) {
498: if (chosenMonthButtonColor == null) {
499: throw new NullPointerException("UIColor must not be null.");
500: }
501: final Color oldValue = this.chosenMonthButtonColor;
502: this.chosenMonthButtonColor = chosenMonthButtonColor;
503: refreshButtons();
504: firePropertyChange("chosenMonthButtonColor", oldValue,
505: chosenMonthButtonColor);
506: }
507:
508:
513: public Color getChosenOtherButtonColor() {
514: return this.chosenOtherButtonColor;
515: }
516:
517:
522: public void setChosenOtherButtonColor(final Color chosenOtherButtonColor) {
523: if (chosenOtherButtonColor == null) {
524: throw new NullPointerException("UIColor must not be null.");
525: }
526: final Color oldValue = this.chosenOtherButtonColor;
527: this.chosenOtherButtonColor = chosenOtherButtonColor;
528: refreshButtons();
529: firePropertyChange("chosenOtherButtonColor", oldValue,
530: chosenOtherButtonColor);
531: }
532:
533:
538: public int getYearSelectionRange() {
539: return this.yearSelectionRange;
540: }
541:
542:
547: public void setYearSelectionRange(final int yearSelectionRange) {
548: final int oldYearSelectionRange = this.yearSelectionRange;
549: this.yearSelectionRange = yearSelectionRange;
550: refreshYearSelector();
551: firePropertyChange("yearSelectionRange", oldYearSelectionRange,
552: yearSelectionRange);
553: }
554: }