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