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