PageRenderTime 204ms CodeModel.GetById 101ms app.highlight 15ms RepoModel.GetById 85ms app.codeStats 0ms

/src/mpv5/ui/panels/calendar/ScheduleCalendar.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 262 lines | 126 code | 30 blank | 106 comment | 19 complexity | 99164248bfb6bc43220e7b93e2ee518c MD5 | raw file
  1/*
  2 *  JCalendar.java  - A bean for choosing a date
  3 *  Copyright (C) 2004 Kai Toedter
  4 *  kai@toedter.com
  5 *  www.toedter.com
  6 *
  7 *  This program is free software; you can redistribute it and/or
  8 *  modify it under the terms of the GNU General Public License
  9 *  as published by the Free Software Foundation; either version 2
 10 *  of the License, or (at your option) any later version.
 11 *
 12 *  This program is distributed in the hope that it will be useful,
 13 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 *  GNU General Public License for more details.
 16 *
 17 *  You should have received a copy of the GNU General Public License
 18 *  along with this program; if not, write to the Free Software
 19 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 20 */
 21package mpv5.ui.panels.calendar;
 22
 23import java.awt.BorderLayout;
 24import java.awt.Font;
 25import java.beans.PropertyChangeEvent;
 26import java.beans.PropertyChangeListener;
 27import java.util.Calendar;
 28import java.util.Date;
 29import java.util.Locale;
 30
 31import javax.swing.BorderFactory;
 32import javax.swing.JPanel;
 33import mpv5.globals.LocalSettings;
 34
 35/**
 36 * JCalendar is a bean for entering a date by choosing the year, month and day.
 37 * 
 38 * @author Kai Toedter
 39 * @version $LastChangedRevision: 95 $
 40 * @version $LastChangedDate: 2006-05-05 18:43:15 +0200 (Fr, 05 Mai 2006) $
 41 */
 42public final class ScheduleCalendar extends JPanel implements PropertyChangeListener {
 43
 44    private static final long serialVersionUID = 8913369762644440133L;
 45
 46    private Calendar calendar;
 47    /** the day chooser */
 48    protected ScheduleCalendarDayChooser dayChooser;
 49    /** indicates if weeks of year shall be visible */
 50    protected boolean weekOfYearVisible = true;
 51    /** the locale */
 52    protected Locale locale;
 53    /** the month chooser */
 54    protected ScheduleMonthChooser monthChooser;
 55    private JPanel monthYearPanel;
 56    /** the year chhoser */
 57    protected ScheduleYearChooser yearChooser;
 58    protected Date minSelectableDate;
 59    protected Date maxSelectableDate;
 60    private static ScheduleCalendar icke;
 61
 62    /**
 63     * The jc instance
 64     * @return
 65     */
 66    public static ScheduleCalendar instanceOf() {
 67        if (icke == null) {
 68            icke = new ScheduleCalendar(null, null, true, true);
 69        }
 70        return icke;
 71    }
 72   
 73    
 74    /**
 75     * JCalendar constructor with month spinner parameter.
 76     *
 77     * @param date
 78     *            the date
 79     * @param locale
 80     *            the locale
 81     * @param monthSpinner
 82     *            false, if no month spinner should be used
 83     * @param weekOfYearVisible
 84     *            true, if weeks of year shall be visible
 85     */
 86    private ScheduleCalendar(Date date, Locale locale, boolean monthSpinner, boolean weekOfYearVisible) {
 87      
 88        // needed for setFont() etc.
 89        dayChooser = null;
 90        monthChooser = null;
 91        yearChooser = null;
 92        this.weekOfYearVisible = weekOfYearVisible;
 93
 94        if (locale == null) {
 95            this.locale = Locale.getDefault();
 96        } else {
 97            this.locale = locale;
 98        }
 99
100        calendar = Calendar.getInstance();
101
102        setLayout(new BorderLayout());
103
104        monthYearPanel = new JPanel();
105        monthYearPanel.setLayout(new BorderLayout());
106
107        monthChooser = new ScheduleMonthChooser(monthSpinner);
108        yearChooser = new ScheduleYearChooser();
109        yearChooser.setSize(yearChooser.getWidth(), yearChooser.getHeight() + 20);
110        monthChooser.setSize(monthChooser.getWidth(), monthChooser.getHeight() + 20);
111        monthChooser.setYearChooser(yearChooser);
112        monthYearPanel.add(monthChooser, BorderLayout.WEST);
113        monthYearPanel.add(yearChooser, BorderLayout.CENTER);
114        monthYearPanel.setBorder(BorderFactory.createEmptyBorder());
115
116        dayChooser = ScheduleCalendarDayChooser.instanceOf();
117        dayChooser.addPropertyChangeListener(this);
118        monthChooser.setDayChooser(dayChooser);
119        monthChooser.addPropertyChangeListener(this);
120        yearChooser.setDayChooser(dayChooser);
121        yearChooser.addPropertyChangeListener(this);
122        add(monthYearPanel, BorderLayout.NORTH);
123        add(dayChooser, BorderLayout.CENTER);
124        add(new ScheduleNavigator(monthChooser, yearChooser), BorderLayout.SOUTH);
125
126        // Set the initialized flag before setting the calendar. This will
127        // cause the other components to be updated properly.
128        if (date != null) {
129            calendar.setTime(date);
130        }
131
132        setCalendar(calendar);
133        yearChooser.setFont(Font.decode(LocalSettings.getProperty(LocalSettings.DEFAULT_FONT)).deriveFont(Font.BOLD, 14));
134        monthChooser.setFont(Font.decode(LocalSettings.getProperty(LocalSettings.DEFAULT_FONT)).deriveFont(Font.PLAIN, 14));
135    }
136
137    /**
138     * Gets the dayChooser attribute of the JCalendar object
139     *
140     * @return the dayChooser value
141     */
142    public ScheduleCalendarDayChooser getDayChooser() {
143        return dayChooser;
144    }
145
146    /**
147     * Gets the monthChooser attribute of the JCalendar object
148     *
149     * @return the monthChooser value
150     */
151    public ScheduleMonthChooser getMonthChooser() {
152        return monthChooser;
153    }
154
155    /**
156     * Gets the yearChooser attribute of the JCalendar object
157     *
158     * @return the yearChooser value
159     */
160    public ScheduleYearChooser getYearChooser() {
161        return yearChooser;
162    }
163
164    /**
165     * JCalendar is a PropertyChangeListener, for its day, month and year
166     * chooser.
167     *
168     * @param evt
169     *            the property change event
170     */
171    public void propertyChange(PropertyChangeEvent evt) {
172        if (calendar != null) {
173            Calendar c = (Calendar) calendar.clone();
174
175            if (evt.getPropertyName().equals("day")) {
176                c.set(Calendar.DAY_OF_MONTH, ((Integer) evt.getNewValue()).intValue());
177                setCalendar(c, false);
178            } else if (evt.getPropertyName().equals("month")) {
179                c.set(Calendar.MONTH, ((Integer) evt.getNewValue()).intValue());
180                setCalendar(c, false);
181            } else if (evt.getPropertyName().equals("year")) {
182                c.set(Calendar.YEAR, ((Integer) evt.getNewValue()).intValue());
183                setCalendar(c, false);
184            } else if (evt.getPropertyName().equals("date")) {
185                c.setTime((Date) evt.getNewValue());
186                setCalendar(c, true);
187            }
188        }
189    }
190
191    /**
192     * Sets the calendar property. This is a bound property.
193     *
194     * @param c
195     *            the new calendar
196     * @throws NullPointerException -
197     *             if c is null;
198     * @see #getCalendar
199     */
200    public void setCalendar(Calendar c) {
201        setCalendar(c, true);
202    }
203
204    /**
205     * Sets the calendar attribute of the JCalendar object
206     *
207     * @param c
208     *            the new calendar value
209     * @param update
210     *            the new calendar value
211     * @throws NullPointerException -
212     *             if c is null;
213     */
214    private void setCalendar(Calendar c, boolean update) {
215        if (c == null) {
216            setDate(null);
217        }
218        Calendar oldCalendar = calendar;
219        calendar = c;
220
221        if (update) {
222            // Thanks to Jeff Ulmer for correcting a bug in the sequence :)
223            yearChooser.setYear(c.get(Calendar.YEAR));
224            monthChooser.setMonth(c.get(Calendar.MONTH));
225            dayChooser.setDay(c.get(Calendar.DATE));
226        }
227
228        firePropertyChange("calendar", oldCalendar, calendar);
229    }
230
231    /**
232     * Returns a Date object.
233     *
234     * @return a date object constructed from the calendar property.
235     */
236    public Date getDate() {
237        return new Date(calendar.getTimeInMillis());
238    }
239
240    /**
241     * Sets the date. Fires the property change "date".
242     *
243     * @param date
244     *            the new date.
245     * @throws NullPointerException -
246     *             if tha date is null
247     */
248    public void setDate(Date date) {
249        Date oldDate = calendar.getTime();
250        calendar.setTime(date);
251        int year = calendar.get(Calendar.YEAR);
252        int month = calendar.get(Calendar.MONTH);
253        int day = calendar.get(Calendar.DAY_OF_MONTH);
254
255        yearChooser.setYear(year);
256        monthChooser.setMonth(month);
257        dayChooser.setCalendar(calendar);
258        dayChooser.setDay(day);
259        
260        firePropertyChange("date", oldDate, date);
261    }
262}