001: /*
002: * 06/27/2002 - 20:54:54
003: *
004: * JMonthChooser.java - A bean for choosing a month
005: * Copyright (C) 2002 Kai Toedter
006: * kai@toedter.com
007: * www.toedter.com
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
022: */
023: package com.toedter.calendar;
024:
025: import java.awt.Adjustable;
026: import java.awt.BorderLayout;
027: import java.awt.Dimension;
028: import java.awt.event.AdjustmentEvent;
029: import java.awt.event.AdjustmentListener;
030: import java.awt.event.ItemEvent;
031: import java.awt.event.ItemListener;
032: import java.text.DateFormatSymbols;
033: import java.util.Calendar;
034: import java.util.Locale;
035:
036: import javax.swing.JComboBox;
037: import javax.swing.JFrame;
038: import javax.swing.JPanel;
039: import javax.swing.JScrollBar;
040:
041: /**
042: * JMonthChooser is a bean for choosing a month.
043: *
044: *@author Kai Toedter
045: *@version 1.1.3 07/16/02
046: */
047: public class JMonthChooser extends JPanel implements ItemListener,
048: AdjustmentListener {
049:
050: /**
051: *
052: */
053: private static final long serialVersionUID = 4121138021874874419L;
054: /**
055: * Displays a JSpinField on the right
056: */
057: public final static int RIGHT_SPINNER = 0;
058: /**
059: * Displays a JSpinField on the left
060: */
061: public final static int LEFT_SPINNER = 1;
062: /**
063: * Displays no JSpinField
064: */
065: public final static int NO_SPINNER = 2;
066:
067: /**
068: * Default JMonthChooser constructor.
069: */
070: public JMonthChooser() {
071: this (RIGHT_SPINNER);
072: }
073:
074: /**
075: * JMonthChooser constructor with month spinner parameter.
076: *
077: *@param spinner Possible values are RIGHT_SPINNER, LEFT_SPINNER, NO_SPINNER
078: */
079: public JMonthChooser(int spinner) {
080: super ();
081:
082: setLayout(new BorderLayout());
083:
084: comboBox = new JComboBox();
085: comboBox.addItemListener(this );
086: dayChooser = null;
087: locale = Locale.getDefault();
088: initNames();
089: add(comboBox, BorderLayout.CENTER);
090:
091: if (spinner != NO_SPINNER) {
092: // 10000 possible clicks in both directions should be enough :)
093: scrollBar = new JScrollBar(Adjustable.VERTICAL, 0, 0,
094: -10000, 10000);
095: scrollBar.setPreferredSize(new Dimension(scrollBar
096: .getPreferredSize().width,
097: this .getPreferredSize().height));
098: scrollBar.setVisibleAmount(0);
099: scrollBar.addAdjustmentListener(this );
100:
101: if (spinner == RIGHT_SPINNER) {
102: add(scrollBar, BorderLayout.EAST);
103: } else {
104: add(scrollBar, BorderLayout.WEST);
105: }
106: }
107:
108: initialized = true;
109: setMonth(Calendar.getInstance().get(Calendar.MONTH));
110: }
111:
112: /**
113: * Initializes the locale specific month names.
114: */
115: public void initNames() {
116: localInitialize = true;
117:
118: DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(
119: locale);
120: String[] monthNames = dateFormatSymbols.getMonths();
121:
122: if (comboBox.getItemCount() == 12) {
123: comboBox.removeAllItems();
124: }
125: for (int i = 0; i < 12; i++) {
126: comboBox.addItem(monthNames[i]);
127: }
128:
129: localInitialize = false;
130: comboBox.setSelectedIndex(month);
131: }
132:
133: /**
134: * The ItemListener for the months.
135: *
136: *@param iEvt Description of the Parameter
137: */
138: public void itemStateChanged(ItemEvent iEvt) {
139: int index = comboBox.getSelectedIndex();
140: if (index >= 0) {
141: setMonth(index, false);
142: }
143: }
144:
145: /**
146: * The 2 buttons are implemented with a JScrollBar.
147: *
148: *@param e Description of the Parameter
149: */
150: public void adjustmentValueChanged(AdjustmentEvent e) {
151: boolean increase = true;
152: int newScrollBarValue = e.getValue();
153: if (newScrollBarValue > oldScrollBarValue) {
154: increase = false;
155: }
156: oldScrollBarValue = newScrollBarValue;
157: int month = getMonth();
158: if (increase) {
159: month += 1;
160: if (month == 12) {
161: month = 0;
162: if (yearChooser != null) {
163: int year = yearChooser.getYear();
164: year += 1;
165: yearChooser.setYear(year);
166: }
167: }
168: } else {
169: month -= 1;
170: if (month == -1) {
171: month = 11;
172: if (yearChooser != null) {
173: int year = yearChooser.getYear();
174: year -= 1;
175: yearChooser.setYear(year);
176: }
177: }
178: }
179: setMonth(month);
180: }
181:
182: /**
183: * Sets the month attribute of the JMonthChooser object
184: *
185: *@param newMonth The new month value
186: *@param select The new month value
187: */
188: private void setMonth(int newMonth, boolean select) {
189: if (!initialized || localInitialize) {
190: return;
191: }
192:
193: int oldMonth = month;
194: month = newMonth;
195: if (select) {
196: comboBox.setSelectedIndex(month);
197: }
198: if (dayChooser != null) {
199: dayChooser.setMonth(month);
200: }
201: firePropertyChange("month", oldMonth, month);
202: }
203:
204: /**
205: * Sets the month. This is a bound property.
206: *
207: *@param newMonth The new month value
208: *@see #getMonth
209: */
210: public void setMonth(int newMonth) {
211: setMonth(newMonth, true);
212: }
213:
214: /**
215: * Returns the month.
216: *
217: *@return The month value
218: *@see #setMonth
219: */
220: public int getMonth() {
221: return month;
222: }
223:
224: /**
225: * Convenience method set a day chooser.
226: *
227: *@param dayChooser the day chooser
228: */
229: public void setDayChooser(JDayChooser dayChooser) {
230: this .dayChooser = dayChooser;
231: }
232:
233: /**
234: * Convenience method set a year chooser. If set, the spin buttons will spin
235: * the year as well
236: *
237: *@param yearChooser The new yearChooser value
238: */
239: public void setYearChooser(JYearChooser yearChooser) {
240: this .yearChooser = yearChooser;
241: }
242:
243: /**
244: * Returns the locale.
245: *
246: *@return The locale value
247: *@see #setLocale
248: */
249: public Locale getLocale() {
250: return locale;
251: }
252:
253: /**
254: * Set the locale and initializes the new month names.
255: *
256: *@param l The new locale value
257: *@see #getLocale
258: */
259: public void setLocale(Locale l) {
260: if (!initialized) {
261: super .setLocale(l);
262: } else {
263: locale = l;
264: initNames();
265: }
266: }
267:
268: /**
269: * Enable or disable the JMonthChooser.
270: *
271: *@param enabled The new enabled value
272: */
273: public void setEnabled(boolean enabled) {
274: super .setEnabled(enabled);
275: comboBox.setEnabled(enabled);
276: if (scrollBar != null) {
277: scrollBar.setEnabled(enabled);
278: }
279: }
280:
281: /**
282: * Creates a JFrame with a JMonthChooser inside and can be used for testing.
283: *
284: *@param s The command line arguments
285: */
286: public static void main(String[] s) {
287: JFrame frame = new JFrame("MonthChooser");
288: frame.getContentPane().add(new JMonthChooser());
289: frame.pack();
290: frame.setVisible(true);
291: }
292:
293: private Locale locale;
294: private int month;
295: private int oldScrollBarValue = 0;
296: // needed for comparison
297: private JDayChooser dayChooser = null;
298: private JYearChooser yearChooser = null;
299: private JComboBox comboBox;
300: private JScrollBar scrollBar;
301: private boolean initialized = false;
302: private boolean localInitialize = false;
303: }
|