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