1:
43:
44: package ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
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:
64: import ;
65:
66:
73: public class SerialDateChooserPanel extends JPanel implements ActionListener {
74:
75:
76: public static final Color DEFAULT_DATE_BUTTON_COLOR = Color.red;
77:
78:
79: public static final Color DEFAULT_MONTH_BUTTON_COLOR = Color.lightGray;
80:
81:
82: private SerialDate date;
83:
84:
85: private Color dateButtonColor;
86:
87:
88: private Color monthButtonColor;
89:
90:
91: private Color chosenOtherButtonColor = Color.darkGray;
92:
93:
94: private int firstDayOfWeek = Calendar.SUNDAY;
95:
96:
97: private int yearSelectionRange = 20;
98:
99:
100: private Font dateFont = new Font("SansSerif", Font.PLAIN, 10);
101:
102:
103: private JComboBox monthSelector = null;
104:
105:
106: private JComboBox yearSelector = null;
107:
108:
109: private JButton todayButton = null;
110:
111:
112: private JButton[] buttons = null;
113:
114:
115: private boolean refreshing = false;
116:
117:
120: public SerialDateChooserPanel() {
121:
122: this(SerialDate.createInstance(new Date()), false,
123: DEFAULT_DATE_BUTTON_COLOR,
124: DEFAULT_MONTH_BUTTON_COLOR);
125:
126: }
127:
128:
135: public SerialDateChooserPanel(final SerialDate date, final boolean controlPanel) {
136:
137: this(date, controlPanel,
138: DEFAULT_DATE_BUTTON_COLOR,
139: DEFAULT_MONTH_BUTTON_COLOR);
140:
141: }
142:
143:
151: public SerialDateChooserPanel(final SerialDate date, final boolean controlPanel,
152: final Color dateButtonColor, final Color monthButtonColor) {
153:
154: super(new BorderLayout());
155:
156: this.date = date;
157: this.dateButtonColor = dateButtonColor;
158: this.monthButtonColor = monthButtonColor;
159:
160: add(constructSelectionPanel(), BorderLayout.NORTH);
161: add(getCalendarPanel(), BorderLayout.CENTER);
162: if (controlPanel) {
163: add(constructControlPanel(), BorderLayout.SOUTH);
164: }
165:
166: }
167:
168:
173: public void setDate(final SerialDate date) {
174:
175: this.date = date;
176: this.monthSelector.setSelectedIndex(date.getMonth() - 1);
177: refreshYearSelector();
178: refreshButtons();
179:
180: }
181:
182:
187: public SerialDate getDate() {
188: return this.date;
189: }
190:
191:
196: public void actionPerformed(final ActionEvent e) {
197:
198: if (e.getActionCommand().equals("monthSelectionChanged")) {
199: final JComboBox c = (JComboBox) e.getSource();
200: this.date = SerialDate.createInstance(
201: this.date.getDayOfMonth(), c.getSelectedIndex() + 1, this.date.getYYYY()
202: );
203: refreshButtons();
204: }
205: else if (e.getActionCommand().equals("yearSelectionChanged")) {
206: if (!this.refreshing) {
207: final JComboBox c = (JComboBox) e.getSource();
208: final Integer y = (Integer) c.getSelectedItem();
209: this.date = SerialDate.createInstance(
210: this.date.getDayOfMonth(), this.date.getMonth(), y.intValue()
211: );
212: refreshYearSelector();
213: refreshButtons();
214: }
215: }
216: else if (e.getActionCommand().equals("todayButtonClicked")) {
217: setDate(SerialDate.createInstance(new Date()));
218: }
219: else if (e.getActionCommand().equals("dateButtonClicked")) {
220: final JButton b = (JButton) e.getSource();
221: final int i = Integer.parseInt(b.getName());
222: final SerialDate first = getFirstVisibleDate();
223: final SerialDate selected = SerialDate.addDays(i, first);
224: setDate(selected);
225: }
226:
227: }
228:
229:
235: private JPanel getCalendarPanel() {
236:
237: final JPanel panel = new JPanel(new GridLayout(7, 7));
238: panel.add(new JLabel("Sun", SwingConstants.CENTER));
239: panel.add(new JLabel("Mon", SwingConstants.CENTER));
240: panel.add(new JLabel("Tue", SwingConstants.CENTER));
241: panel.add(new JLabel("Wed", SwingConstants.CENTER));
242: panel.add(new JLabel("Thu", SwingConstants.CENTER));
243: panel.add(new JLabel("Fri", SwingConstants.CENTER));
244: panel.add(new JLabel("Sat", SwingConstants.CENTER));
245:
246: this.buttons = new JButton[42];
247: for (int i = 0; i < 42; i++) {
248: final JButton button = new JButton("");
249: button.setMargin(new Insets(1, 1, 1, 1));
250: button.setName(Integer.toString(i));
251: button.setFont(this.dateFont);
252: button.setFocusPainted(false);
253: button.setActionCommand("dateButtonClicked");
254: button.addActionListener(this);
255: this.buttons[i] = button;
256: panel.add(button);
257: }
258: return panel;
259:
260: }
261:
262:
269: protected Color getButtonColor(final SerialDate targetDate) {
270:
271: if (this.date.equals(this.date)) {
272: return this.dateButtonColor;
273: }
274: else if (targetDate.getMonth() == this.date.getMonth()) {
275: return this.monthButtonColor;
276: }
277: else {
278: return this.chosenOtherButtonColor;
279: }
280:
281: }
282:
283:
289: protected SerialDate getFirstVisibleDate() {
290:
291: SerialDate result = SerialDate.createInstance(1, this.date.getMonth(), this.date.getYYYY());
292: result = SerialDate.addDays(-1, result);
293: while (result.getDayOfWeek() != getFirstDayOfWeek()) {
294: result = SerialDate.addDays(-1, result);
295: }
296: return result;
297:
298: }
299:
300:
305: private int getFirstDayOfWeek() {
306: return this.firstDayOfWeek;
307: }
308:
309:
312: protected void refreshButtons() {
313:
314: SerialDate current = getFirstVisibleDate();
315: for (int i = 0; i < 42; i++) {
316: final JButton button = this.buttons[i];
317: button.setText(String.valueOf(current.getDayOfWeek()));
318: button.setBackground(getButtonColor(current));
319: current = SerialDate.addDays(1, current);
320: }
321:
322: }
323:
324:
328: private void refreshYearSelector() {
329: if (!this.refreshing) {
330: this.refreshing = true;
331: this.yearSelector.removeAllItems();
332: final Vector v = getYears(this.date.getYYYY());
333: for (Enumeration e = v.elements(); e.hasMoreElements();) {
334: this.yearSelector.addItem(e.nextElement());
335: }
336: this.yearSelector.setSelectedItem(new Integer(this.date.getYYYY()));
337: this.refreshing = false;
338: }
339: }
340:
341:
349: private Vector getYears(final int chosenYear) {
350: final Vector v = new Vector();
351: for (int i = chosenYear - this.yearSelectionRange;
352: i <= chosenYear + this.yearSelectionRange; i++) {
353: v.addElement(new Integer(i));
354: }
355: return v;
356: }
357:
358:
364: private JPanel constructSelectionPanel() {
365: final JPanel p = new JPanel();
366: this.monthSelector = new JComboBox(SerialDate.getMonths());
367: this.monthSelector.addActionListener(this);
368: this.monthSelector.setActionCommand("monthSelectionChanged");
369: p.add(this.monthSelector);
370:
371: this.yearSelector = new JComboBox(getYears(0));
372: this.yearSelector.addActionListener(this);
373: this.yearSelector.setActionCommand("yearSelectionChanged");
374: p.add(this.yearSelector);
375:
376: return p;
377: }
378:
379:
385: private JPanel constructControlPanel() {
386:
387: final JPanel p = new JPanel();
388: p.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
389: this.todayButton = new JButton("Today");
390: this.todayButton.addActionListener(this);
391: this.todayButton.setActionCommand("todayButtonClicked");
392: p.add(this.todayButton);
393: return p;
394:
395: }
396:
397: }