~ubuntu-branches/ubuntu/hardy/libjcalendar-java/hardy

« back to all changes in this revision

Viewing changes to src/com/toedter/calendar/JCalendar.java

  • Committer: Bazaar Package Importer
  • Author(s): Hilko Bengen
  • Date: 2005-08-24 12:23:48 UTC
  • Revision ID: james.westby@ubuntu.com-20050824122348-sb0i5x1k5rt91njk
Tags: upstream-1.2.2
ImportĀ upstreamĀ versionĀ 1.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2004 Kai Toedter
 
3
 *  kai@toedter.com
 
4
 *  www.toedter.com
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or
 
7
 *  modify it under the terms of the GNU Lesser General Public License
 
8
 *  as published by the Free Software Foundation; either version 2
 
9
 *  of the License, or (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU Lesser General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU Lesser General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
package com.toedter.calendar;
 
21
 
 
22
import java.awt.BorderLayout;
 
23
import java.awt.Color;
 
24
import java.awt.Font;
 
25
import java.beans.PropertyChangeEvent;
 
26
import java.beans.PropertyChangeListener;
 
27
import java.util.Calendar;
 
28
import java.util.Date;
 
29
import java.util.Locale;
 
30
 
 
31
import javax.swing.BorderFactory;
 
32
import javax.swing.JFrame;
 
33
import javax.swing.JPanel;
 
34
 
 
35
 
 
36
/**
 
37
 * JCalendar is a bean for entering a date by choosing the year, month and day.
 
38
 *
 
39
 * @author Kai Toedter
 
40
 * @version 1.2
 
41
 */
 
42
public class JCalendar extends JPanel implements PropertyChangeListener {
 
43
    private Calendar calendar;
 
44
 
 
45
    /** the day chooser */
 
46
    protected JDayChooser dayChooser;
 
47
    private boolean initialized = false;
 
48
 
 
49
    /** indicates if weeks of year shall be visible */
 
50
    protected boolean weekOfYearVisible = true;
 
51
 
 
52
    /** the locale */
 
53
    protected Locale locale;
 
54
 
 
55
    /** the month chooser */
 
56
    protected JMonthChooser monthChooser;
 
57
    private JPanel monthYearPanel;
 
58
 
 
59
    /** the year chhoser */
 
60
    protected JYearChooser yearChooser;
 
61
 
 
62
    /**
 
63
     * Default JCalendar constructor.
 
64
     */
 
65
    public JCalendar() {
 
66
        this(null, null, true, true);
 
67
    }
 
68
 
 
69
    /**
 
70
     * JCalendar constructor specifying the month spinner type.
 
71
     *
 
72
     * @param monthSpinner false, if no month spinner should be used
 
73
     */
 
74
    public JCalendar(boolean monthSpinner) {
 
75
        this(null, null, monthSpinner, true);
 
76
    }
 
77
 
 
78
    /**
 
79
     * JCalendar constructor which allows the initial date to be set.
 
80
     *
 
81
     * @param date the date
 
82
     */
 
83
    public JCalendar(Date date) {
 
84
        this(date, null, true, true);
 
85
    }
 
86
 
 
87
    /**
 
88
     * JCalendar constructor allowing the initial locale to be set.
 
89
     *
 
90
     * @param locale the new locale
 
91
     */
 
92
    public JCalendar(Locale locale) {
 
93
        this(null, locale, true, true);
 
94
    }
 
95
 
 
96
    /**
 
97
     * JCalendar constructor specifying both the initial date and locale.
 
98
     *
 
99
     * @param date the date
 
100
     * @param locale the new locale
 
101
     */
 
102
    public JCalendar(Date date, Locale locale) {
 
103
        this(date, locale, true, true);
 
104
    }
 
105
 
 
106
    /**
 
107
     * JCalendar constructor specifying both the initial date and the month
 
108
     * spinner type.
 
109
     *
 
110
     * @param date the date
 
111
     * @param monthSpinner false, if no month spinner should be used
 
112
     */
 
113
    public JCalendar(Date date, boolean monthSpinner) {
 
114
        this(date, null, monthSpinner, true);
 
115
    }
 
116
 
 
117
    /**
 
118
     * JCalendar constructor specifying both the locale and the month spinner.
 
119
     *
 
120
     * @param locale the locale
 
121
     * @param monthSpinner false, if no month spinner should be used
 
122
     */
 
123
    public JCalendar(Locale locale, boolean monthSpinner) {
 
124
        this(null, locale, monthSpinner, true);
 
125
    }
 
126
 
 
127
    /**
 
128
     * JCalendar constructor with month spinner parameter.
 
129
     *
 
130
     * @param date the date
 
131
     * @param locale the locale
 
132
     * @param monthSpinner false, if no month spinner should be used
 
133
     * @param weekOfYearVisible true, if weeks of year shall be visible
 
134
     */
 
135
    public JCalendar(Date date, Locale locale, boolean monthSpinner,
 
136
        boolean weekOfYearVisible) {
 
137
        // needed for setFont() etc.
 
138
        dayChooser = null;
 
139
        monthChooser = null;
 
140
        yearChooser = null;
 
141
        this.weekOfYearVisible = weekOfYearVisible;
 
142
 
 
143
        this.locale = locale;
 
144
 
 
145
        if (locale == null) {
 
146
            this.locale = Locale.getDefault();
 
147
        }
 
148
 
 
149
        calendar = Calendar.getInstance();
 
150
 
 
151
        setLayout(new BorderLayout());
 
152
        
 
153
        monthYearPanel = new JPanel();
 
154
        monthYearPanel.setLayout(new BorderLayout());
 
155
 
 
156
        monthChooser = new JMonthChooser(monthSpinner);
 
157
        yearChooser = new JYearChooser();
 
158
        monthChooser.setYearChooser(yearChooser);
 
159
        monthYearPanel.add(monthChooser, BorderLayout.WEST);
 
160
        monthYearPanel.add(yearChooser, BorderLayout.CENTER);
 
161
        monthYearPanel.setBorder(BorderFactory.createEmptyBorder());
 
162
        
 
163
        dayChooser = new JDayChooser(weekOfYearVisible);
 
164
        dayChooser.addPropertyChangeListener(this);
 
165
        monthChooser.setDayChooser(dayChooser);
 
166
        monthChooser.addPropertyChangeListener(this);
 
167
        yearChooser.setDayChooser(dayChooser);
 
168
        yearChooser.addPropertyChangeListener(this);
 
169
        add(monthYearPanel, BorderLayout.NORTH);
 
170
        add(dayChooser, BorderLayout.CENTER);
 
171
 
 
172
        // Set the initialized flag before setting the calendar. This will
 
173
        // cause the other components to be updated properly.
 
174
        if (date != null) {
 
175
            calendar.setTime(date);
 
176
        }
 
177
 
 
178
        initialized = true;
 
179
        setCalendar(calendar);
 
180
    }
 
181
 
 
182
    /**
 
183
     * Creates a JFrame with a JCalendar inside and can be used for testing.
 
184
     *
 
185
     * @param s The command line arguments
 
186
     */
 
187
    public static void main(String[] s) {
 
188
        JFrame frame = new JFrame("JCalendar");
 
189
        frame.getContentPane().add(new JCalendar());
 
190
        frame.pack();
 
191
        frame.setVisible(true);
 
192
    }
 
193
 
 
194
    /**
 
195
     * Returns the calendar property.
 
196
     *
 
197
     * @return the value of the calendar property.
 
198
     */
 
199
    public Calendar getCalendar() {
 
200
        return calendar;
 
201
    }
 
202
 
 
203
    /**
 
204
     * Gets the dayChooser attribute of the JCalendar object
 
205
     *
 
206
     * @return the dayChooser value
 
207
     */
 
208
    public JDayChooser getDayChooser() {
 
209
        return dayChooser;
 
210
    }
 
211
 
 
212
    /**
 
213
     * Returns the locale.
 
214
     *
 
215
     * @return the value of the locale property.
 
216
     *
 
217
     * @see #setLocale
 
218
     */
 
219
    public Locale getLocale() {
 
220
        return locale;
 
221
    }
 
222
 
 
223
    /**
 
224
     * Gets the monthChooser attribute of the JCalendar object
 
225
     *
 
226
     * @return the monthChooser value
 
227
     */
 
228
    public JMonthChooser getMonthChooser() {
 
229
        return monthChooser;
 
230
    }
 
231
 
 
232
    /**
 
233
     * Returns "JCalendar".
 
234
     *
 
235
     * @return "JCalendar"
 
236
     */
 
237
    public String getName() {
 
238
        return "JCalendar";
 
239
    }
 
240
 
 
241
    /**
 
242
     * Gets the yearChooser attribute of the JCalendar object
 
243
     *
 
244
     * @return the yearChooser value
 
245
     */
 
246
    public JYearChooser getYearChooser() {
 
247
        return yearChooser;
 
248
    }
 
249
 
 
250
    /**
 
251
     * Indicates if the weeks of year are visible..
 
252
     *
 
253
     * @return boolean true, if weeks of year are visible
 
254
     */
 
255
    public boolean isWeekOfYearVisible() {
 
256
        return dayChooser.isWeekOfYearVisible();
 
257
    }
 
258
 
 
259
    /**
 
260
     * JCalendar is a PropertyChangeListener, for its day, month and year
 
261
     * chooser.
 
262
     *
 
263
     * @param evt the property change event
 
264
     */
 
265
    public void propertyChange(PropertyChangeEvent evt) {
 
266
        if (calendar != null) {
 
267
            Calendar c = (Calendar) calendar.clone();
 
268
 
 
269
            if (evt.getPropertyName().equals("day")) {
 
270
                c.set(Calendar.DAY_OF_MONTH,
 
271
                    ((Integer) evt.getNewValue()).intValue());
 
272
                setCalendar(c, false);
 
273
            } else if (evt.getPropertyName().equals("month")) {
 
274
                c.set(Calendar.MONTH, ((Integer) evt.getNewValue()).intValue());
 
275
                setCalendar(c, false);
 
276
            } else if (evt.getPropertyName().equals("year")) {
 
277
                c.set(Calendar.YEAR, ((Integer) evt.getNewValue()).intValue());
 
278
                setCalendar(c, false);
 
279
            } else if (evt.getPropertyName().equals("date")) {
 
280
                c.setTime((Date) evt.getNewValue());
 
281
                setCalendar(c, true);
 
282
            }
 
283
        }
 
284
    }
 
285
 
 
286
    /**
 
287
     * Sets the background color.
 
288
     *
 
289
     * @param bg the new background
 
290
     */
 
291
    public void setBackground(Color bg) {
 
292
        super.setBackground(bg);
 
293
 
 
294
        if (dayChooser != null) {
 
295
            dayChooser.setBackground(bg);
 
296
        }
 
297
    }
 
298
 
 
299
    /**
 
300
     * Sets the calendar property. This is a bound property.
 
301
     *
 
302
     * @param c the new calendar
 
303
     *
 
304
     * @see #getCalendar
 
305
     */
 
306
    public void setCalendar(Calendar c) {
 
307
        setCalendar(c, true);
 
308
    }
 
309
 
 
310
    /**
 
311
     * Sets the calendar attribute of the JCalendar object
 
312
     *
 
313
     * @param c the new calendar value
 
314
     * @param update the new calendar value
 
315
     */
 
316
    private void setCalendar(Calendar c, boolean update) {
 
317
        Calendar oldCalendar = calendar;
 
318
        calendar = c;
 
319
 
 
320
        if (update) {
 
321
            // Thanks to Jeff Ulmer for correcting a bug in the sequence :)
 
322
            yearChooser.setYear(c.get(Calendar.YEAR));
 
323
            monthChooser.setMonth(c.get(Calendar.MONTH));
 
324
            dayChooser.setDay(c.get(Calendar.DATE));
 
325
        }
 
326
 
 
327
        firePropertyChange("calendar", oldCalendar, calendar);
 
328
    }
 
329
 
 
330
    /**
 
331
     * Enable or disable the JCalendar.
 
332
     *
 
333
     * @param enabled the new enabled value
 
334
     */
 
335
    public void setEnabled(boolean enabled) {
 
336
        super.setEnabled(enabled);
 
337
 
 
338
        if (dayChooser != null) {
 
339
            dayChooser.setEnabled(enabled);
 
340
            monthChooser.setEnabled(enabled);
 
341
            yearChooser.setEnabled(enabled);
 
342
        }
 
343
    }
 
344
 
 
345
    /**
 
346
     * Returns true, if enabled.
 
347
     *
 
348
     * @return true, if enabled.
 
349
     */
 
350
    public boolean isEnabled() {
 
351
        return super.isEnabled();
 
352
    }
 
353
 
 
354
    /**
 
355
     * Sets the font property.
 
356
     *
 
357
     * @param font the new font
 
358
     */
 
359
    public void setFont(Font font) {
 
360
        super.setFont(font);
 
361
 
 
362
        if (dayChooser != null) {
 
363
            dayChooser.setFont(font);
 
364
            monthChooser.setFont(font);
 
365
            yearChooser.setFont(font);
 
366
        }
 
367
    }
 
368
 
 
369
    /**
 
370
     * Sets the foreground color.
 
371
     *
 
372
     * @param fg the new foreground
 
373
     */
 
374
    public void setForeground(Color fg) {
 
375
        super.setForeground(fg);
 
376
 
 
377
        if (dayChooser != null) {
 
378
            dayChooser.setForeground(fg);
 
379
            monthChooser.setForeground(fg);
 
380
            yearChooser.setForeground(fg);
 
381
        }
 
382
    }
 
383
 
 
384
    /**
 
385
     * Sets the locale property. This is a bound property.
 
386
     *
 
387
     * @param l the new locale value
 
388
     *
 
389
     * @see #getLocale
 
390
     */
 
391
    public void setLocale(Locale l) {
 
392
        if (!initialized) {
 
393
            super.setLocale(l);
 
394
        } else {
 
395
            Locale oldLocale = locale;
 
396
            locale = l;
 
397
            dayChooser.setLocale(locale);
 
398
            monthChooser.setLocale(locale);
 
399
            firePropertyChange("locale", oldLocale, locale);
 
400
        }
 
401
    }
 
402
 
 
403
    /**
 
404
     * Sets the week of year visible.
 
405
     *
 
406
     * @param weekOfYearVisible true, if weeks of year shall be visible
 
407
     */
 
408
    public void setWeekOfYearVisible(boolean weekOfYearVisible) {
 
409
        dayChooser.setWeekOfYearVisible(weekOfYearVisible);
 
410
        setLocale(locale); // hack for doing complete new layout :)
 
411
    }
 
412
 
 
413
    /**
 
414
     * Gets the visibility of the decoration background.
 
415
     *
 
416
     * @return true, if the decoration background is visible.
 
417
     */
 
418
    public boolean isDecorationBackgroundVisible() {
 
419
        return dayChooser.isDecorationBackgroundVisible();
 
420
    }
 
421
 
 
422
    /**
 
423
     * Sets the decoration background visible.
 
424
     *
 
425
     * @param decorationBackgroundVisible true, if the decoration background
 
426
     *        should be visible.
 
427
     */
 
428
    public void setDecorationBackgroundVisible(
 
429
        boolean decorationBackgroundVisible) {
 
430
        dayChooser.setDecorationBackgroundVisible(decorationBackgroundVisible);
 
431
        setLocale(locale); // hack for doing complete new layout :)
 
432
    }
 
433
 
 
434
    /**
 
435
     * Gets the visibility of the decoration border.
 
436
     *
 
437
     * @return true, if the decoration border is visible.
 
438
     */
 
439
    public boolean isDecorationBordersVisible() {
 
440
        return dayChooser.isDecorationBordersVisible();
 
441
    }
 
442
 
 
443
    /**
 
444
     * Sets the decoration borders visible.
 
445
     *
 
446
     * @param decorationBordersVisible true, if the decoration borders should
 
447
     *        be visible.
 
448
     */
 
449
    public void setDecorationBordersVisible(boolean decorationBordersVisible) {
 
450
        dayChooser.setDecorationBordersVisible(decorationBordersVisible);
 
451
        setLocale(locale); // hack for doing complete new layout :)
 
452
    }
 
453
 
 
454
    /**
 
455
     * Returns the color of the decoration (day names and weeks).
 
456
     *
 
457
     * @return the color of the decoration (day names and weeks).
 
458
     */
 
459
    public Color getDecorationBackgroundColor() {
 
460
        return dayChooser.getDecorationBackgroundColor();
 
461
    }
 
462
 
 
463
    /**
 
464
     * Sets the background of days and weeks of year buttons.
 
465
     *
 
466
     * @param decorationBackgroundColor the background color
 
467
     */
 
468
    public void setDecorationBackgroundColor(Color decorationBackgroundColor) {
 
469
        dayChooser.setDecorationBackgroundColor(decorationBackgroundColor);
 
470
    }
 
471
 
 
472
    /**
 
473
     * Returns the Sunday foreground.
 
474
     *
 
475
     * @return Color the Sunday foreground.
 
476
     */
 
477
    public Color getSundayForeground() {
 
478
        return dayChooser.getSundayForeground();
 
479
    }
 
480
 
 
481
    /**
 
482
     * Returns the weekday foreground.
 
483
     *
 
484
     * @return Color the weekday foreground.
 
485
     */
 
486
    public Color getWeekdayForeground() {
 
487
        return dayChooser.getWeekdayForeground();
 
488
    }
 
489
 
 
490
    /**
 
491
     * Sets the Sunday foreground.
 
492
     *
 
493
     * @param sundayForeground the sundayForeground to set
 
494
     */
 
495
    public void setSundayForeground(Color sundayForeground) {
 
496
        dayChooser.setSundayForeground(sundayForeground);
 
497
    }
 
498
 
 
499
    /**
 
500
     * Sets the weekday foreground.
 
501
     *
 
502
     * @param weekdayForeground the weekdayForeground to set
 
503
     */
 
504
    public void setWeekdayForeground(Color weekdayForeground) {
 
505
        dayChooser.setWeekdayForeground(weekdayForeground);
 
506
    }
 
507
 
 
508
    /**
 
509
     * Returns a Date object.
 
510
     *
 
511
     * @return a date object constructed from the calendar property.
 
512
     */
 
513
    public Date getDate() {
 
514
        return new Date(calendar.getTimeInMillis());
 
515
    }
 
516
 
 
517
    /**
 
518
     * Sets the date. Fires the property change "date".
 
519
     *
 
520
     * @param date the new date.
 
521
     */
 
522
    public void setDate(Date date) {
 
523
        Date oldDate = calendar.getTime();
 
524
        calendar.setTime(date);
 
525
 
 
526
        yearChooser.setYear(calendar.get(Calendar.YEAR));
 
527
        monthChooser.setMonth(calendar.get(Calendar.MONTH));
 
528
        dayChooser.setDay(calendar.get(Calendar.DATE));
 
529
 
 
530
        firePropertyChange("date", oldDate, date);
 
531
    }
 
532
}