PageRenderTime 174ms CodeModel.GetById 90ms app.highlight 19ms RepoModel.GetById 62ms app.codeStats 0ms

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

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 396 lines | 200 code | 49 blank | 147 comment | 41 complexity | b71148285fef28eae6f3917ad80b1872 MD5 | raw file
  1/*
  2 *  JMonthChooser.java  - A bean for choosing a month
  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.Component;
 25import java.awt.Dimension;
 26import java.awt.Font;
 27import java.awt.event.ItemEvent;
 28import java.awt.event.ItemListener;
 29import java.text.DateFormatSymbols;
 30import java.util.Calendar;
 31import java.util.Locale;
 32
 33import javax.swing.JComboBox;
 34import javax.swing.JFrame;
 35import javax.swing.JPanel;
 36import javax.swing.JSpinner;
 37import javax.swing.JTextField;
 38import javax.swing.SpinnerNumberModel;
 39import javax.swing.UIManager;
 40import javax.swing.border.EmptyBorder;
 41import javax.swing.event.ChangeEvent;
 42import javax.swing.event.ChangeListener;
 43
 44/**
 45 * JMonthChooser is a bean for choosing a month.
 46 * 
 47 * @author Kai Toedter
 48 * @version $LastChangedRevision: 100 $
 49 * @version $LastChangedDate: 2006-06-04 14:36:06 +0200 (So, 04 Jun 2006) $
 50 */
 51public class ScheduleMonthChooser extends JPanel implements ItemListener,
 52        ChangeListener {
 53
 54    private static final long serialVersionUID = -2028361332231218527L;
 55    /** true, if the month chooser has a spinner component */
 56    protected boolean hasSpinner;
 57    private Locale locale;
 58    private int month;
 59    private int oldSpinnerValue = 0;
 60
 61    // needed for comparison
 62    private ScheduleCalendarDayChooser dayChooser;
 63    private ScheduleYearChooser yearChooser;
 64    private JComboBox comboBox;
 65    private JSpinner spinner;
 66    private boolean initialized;
 67    private boolean localInitialize;
 68
 69    /**
 70     * Default JMonthChooser constructor.
 71     */
 72    public ScheduleMonthChooser() {
 73        this(true);
 74    }
 75
 76    /**
 77     * JMonthChooser constructor with month spinner parameter.
 78     *
 79     * @param hasSpinner
 80     *            true, if the month chooser should have a spinner component
 81     */
 82    public ScheduleMonthChooser(boolean hasSpinner) {
 83        super();
 84        setName("JMonthChooser");
 85        this.hasSpinner = hasSpinner;
 86
 87        setLayout(new BorderLayout());
 88
 89        comboBox = new JComboBox();
 90        comboBox.addItemListener(this);
 91
 92        // comboBox.addPopupMenuListener(this);
 93        locale = Locale.getDefault();
 94        initNames();
 95
 96        if (hasSpinner) {
 97            spinner = new JSpinner() {
 98
 99                private static final long serialVersionUID = 1L;
100                private JTextField textField = new JTextField();
101
102                @Override
103                public Dimension getPreferredSize() {
104                    textField.setPreferredSize(new Dimension(textField.getPreferredSize().width, 33));
105                    textField.setFont(new Font("Dialog", Font.BOLD, 16));
106                    Dimension size = super.getPreferredSize();
107                    return new Dimension(size.width + 15, textField.getPreferredSize().height);
108                }
109            };
110            spinner.addChangeListener(this);
111            spinner.setEditor(comboBox);
112            comboBox.setBorder(new EmptyBorder(0, 0, 0, 0));
113            updateUI();
114
115            add(spinner, BorderLayout.WEST);
116        } else {
117            add(comboBox, BorderLayout.WEST);
118        }
119
120        initialized = true;
121        setMonth(Calendar.getInstance().get(Calendar.MONTH));
122    }
123
124    /**
125     * Initializes the locale specific month names.
126     */
127    public void initNames() {
128        localInitialize = true;
129
130        DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(locale);
131        String[] monthNames = dateFormatSymbols.getMonths();
132
133        if (comboBox.getItemCount() == 12) {
134            comboBox.removeAllItems();
135        }
136
137        for (int i = 0; i < 12; i++) {
138            comboBox.addItem(monthNames[i]);
139        }
140
141        localInitialize = false;
142        comboBox.setSelectedIndex(month);
143    }
144
145    /**
146     * Is invoked if the state of the spnner changes.
147     *
148     * @param e
149     *            the change event.
150     */
151    public void stateChanged(ChangeEvent e) {
152        SpinnerNumberModel model = (SpinnerNumberModel) ((JSpinner) e.getSource()).getModel();
153        int value = model.getNumber().intValue();
154        boolean increase = (value > oldSpinnerValue) ? true : false;
155        oldSpinnerValue = value;
156
157        int month = getMonth();
158
159        if (increase) {
160            month += 1;
161
162            if (month == 12) {
163                month = 0;
164
165                if (yearChooser != null) {
166                    int year = yearChooser.getYear();
167                    year += 1;
168                    yearChooser.setYear(year);
169                }
170            }
171        } else {
172            month -= 1;
173
174            if (month == -1) {
175                month = 11;
176
177                if (yearChooser != null) {
178                    int year = yearChooser.getYear();
179                    year -= 1;
180                    yearChooser.setYear(year);
181                }
182            }
183        }
184
185        setMonth(month);
186    }
187
188    /**
189     * The ItemListener for the months.
190     *
191     * @param e
192     *            the item event
193     */
194    public void itemStateChanged(ItemEvent e) {
195        if (e.getStateChange() == ItemEvent.SELECTED) {
196            int index = comboBox.getSelectedIndex();
197
198            if ((index >= 0) && (index != month)) {
199                setMonth(index, false);
200            }
201        }
202    }
203
204    /**
205     * Sets the month attribute of the JMonthChooser object. Fires a property
206     * change "month".
207     *
208     * @param newMonth
209     *            the new month value
210     * @param select
211     *            true, if the month should be selcted in the combo box.
212     */
213    private void setMonth(int newMonth, boolean select) {
214        if (!initialized || localInitialize) {
215            return;
216        }
217
218        int oldMonth = month;
219        month = newMonth;
220
221        if (select) {
222            comboBox.setSelectedIndex(month);
223        }
224
225        if (dayChooser != null) {
226            dayChooser.setMonth(month);
227        }
228
229        firePropertyChange("month", oldMonth, month);
230    }
231
232    /**
233     * Sets the month. This is a bound property. Valuse are valid between 0
234     * (January) and 11 (December). A value < 0 will be treated as 0, a value >
235     * 11 will be treated as 11.
236     *
237     * @param newMonth
238     *            the new month value
239     *
240     * @see #getMonth
241     */
242    public void setMonth(int newMonth) {
243        if (newMonth < 0 || newMonth == Integer.MIN_VALUE) {
244            setMonth(0, true);
245        } else if (newMonth > 11) {
246            setMonth(11, true);
247        } else {
248            setMonth(newMonth, true);
249        }
250    }
251
252    /**
253     * Returns the month.
254     *
255     * @return the month value
256     */
257    public int getMonth() {
258        return month;
259    }
260
261    /**
262     * Convenience method set a day chooser.
263     *
264     * @param dayChooser
265     *            the day chooser
266     */
267    public void setDayChooser(ScheduleCalendarDayChooser dayChooser) {
268        this.dayChooser = dayChooser;
269    }
270
271    /**
272     * Convenience method set a year chooser. If set, the spin for the month
273     * buttons will spin the year as well
274     *
275     * @param yearChooser
276     *            the new yearChooser value
277     */
278    public void setYearChooser(ScheduleYearChooser yearChooser) {
279        this.yearChooser = yearChooser;
280    }
281
282    /**
283     * Returns the locale.
284     *
285     * @return the locale value
286     *
287     * @see #setLocale
288     */
289    public Locale getLocale() {
290        return locale;
291    }
292
293    /**
294     * Set the locale and initializes the new month names.
295     *
296     * @param l
297     *            the new locale value
298     *
299     * @see #getLocale
300     */
301    public void setLocale(Locale l) {
302        if (!initialized) {
303            super.setLocale(l);
304        } else {
305            locale = l;
306            initNames();
307        }
308    }
309
310    /**
311     * Enable or disable the JMonthChooser.
312     *
313     * @param enabled
314     *            the new enabled value
315     */
316    public void setEnabled(boolean enabled) {
317        super.setEnabled(enabled);
318        comboBox.setEnabled(enabled);
319
320        if (spinner != null) {
321            spinner.setEnabled(enabled);
322        }
323    }
324
325    /**
326     * Returns the month chooser's comboBox text area (which allow the focus to
327     * be set to it).
328     *
329     * @return the combo box
330     */
331    public Component getComboBox() {
332        return this.comboBox;
333    }
334
335    /**
336     * Returns the month chooser's comboBox bar (which allow the focus to be set
337     * to it).
338     *
339     * @return Component the spinner or null, if the month chooser has no
340     *         spinner
341     */
342    public Component getSpinner() {
343        // Returns <null> if there is no spinner.
344        return spinner;
345    }
346
347    /**
348     * Returns the type of spinner the month chooser is using.
349     *
350     * @return true, if the month chooser has a spinner
351     */
352    public boolean hasSpinner() {
353        return hasSpinner;
354    }
355
356    /**
357     * Sets the font for this component.
358     *
359     * @param font the desired <code>Font</code> for this component
360     */
361    public void setFont(Font font) {
362        if (comboBox != null) {
363            comboBox.setFont(font);
364        }
365        super.setFont(font);
366    }
367
368    /**
369     * Updates the UI.
370     *
371     * @see javax.swing.JPanel#updateUI()
372     */
373    public void updateUI() {
374        final JSpinner testSpinner = new JSpinner();
375        if (spinner != null) {
376            if ("Windows".equals(UIManager.getLookAndFeel().getID())) {
377                spinner.setBorder(testSpinner.getBorder());
378            } else {
379                spinner.setBorder(new EmptyBorder(0, 0, 0, 0));
380            }
381        }
382    }
383
384    /**
385     * Creates a JFrame with a JMonthChooser inside and can be used for testing.
386     *
387     * @param s
388     *            The command line arguments
389     */
390    public static void main(String[] s) {
391        JFrame frame = new JFrame("MonthChooser");
392        frame.getContentPane().add(new ScheduleMonthChooser());
393        frame.pack();
394        frame.setVisible(true);
395    }
396}