PageRenderTime 33ms CodeModel.GetById 15ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  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. */
  21. package mpv5.ui.panels.calendar;
  22. import java.awt.BorderLayout;
  23. import java.awt.Font;
  24. import java.beans.PropertyChangeEvent;
  25. import java.beans.PropertyChangeListener;
  26. import java.util.Calendar;
  27. import java.util.Date;
  28. import java.util.Locale;
  29. import javax.swing.BorderFactory;
  30. import javax.swing.JPanel;
  31. import mpv5.globals.LocalSettings;
  32. /**
  33. * JCalendar is a bean for entering a date by choosing the year, month and day.
  34. *
  35. * @author Kai Toedter
  36. * @version $LastChangedRevision: 95 $
  37. * @version $LastChangedDate: 2006-05-05 18:43:15 +0200 (Fr, 05 Mai 2006) $
  38. */
  39. public final class ScheduleCalendar extends JPanel implements PropertyChangeListener {
  40. private static final long serialVersionUID = 8913369762644440133L;
  41. private Calendar calendar;
  42. /** the day chooser */
  43. protected ScheduleCalendarDayChooser dayChooser;
  44. /** indicates if weeks of year shall be visible */
  45. protected boolean weekOfYearVisible = true;
  46. /** the locale */
  47. protected Locale locale;
  48. /** the month chooser */
  49. protected ScheduleMonthChooser monthChooser;
  50. private JPanel monthYearPanel;
  51. /** the year chhoser */
  52. protected ScheduleYearChooser yearChooser;
  53. protected Date minSelectableDate;
  54. protected Date maxSelectableDate;
  55. private static ScheduleCalendar icke;
  56. /**
  57. * The jc instance
  58. * @return
  59. */
  60. public static ScheduleCalendar instanceOf() {
  61. if (icke == null) {
  62. icke = new ScheduleCalendar(null, null, true, true);
  63. }
  64. return icke;
  65. }
  66. /**
  67. * JCalendar constructor with month spinner parameter.
  68. *
  69. * @param date
  70. * the date
  71. * @param locale
  72. * the locale
  73. * @param monthSpinner
  74. * false, if no month spinner should be used
  75. * @param weekOfYearVisible
  76. * true, if weeks of year shall be visible
  77. */
  78. private ScheduleCalendar(Date date, Locale locale, boolean monthSpinner, boolean weekOfYearVisible) {
  79. // needed for setFont() etc.
  80. dayChooser = null;
  81. monthChooser = null;
  82. yearChooser = null;
  83. this.weekOfYearVisible = weekOfYearVisible;
  84. if (locale == null) {
  85. this.locale = Locale.getDefault();
  86. } else {
  87. this.locale = locale;
  88. }
  89. calendar = Calendar.getInstance();
  90. setLayout(new BorderLayout());
  91. monthYearPanel = new JPanel();
  92. monthYearPanel.setLayout(new BorderLayout());
  93. monthChooser = new ScheduleMonthChooser(monthSpinner);
  94. yearChooser = new ScheduleYearChooser();
  95. yearChooser.setSize(yearChooser.getWidth(), yearChooser.getHeight() + 20);
  96. monthChooser.setSize(monthChooser.getWidth(), monthChooser.getHeight() + 20);
  97. monthChooser.setYearChooser(yearChooser);
  98. monthYearPanel.add(monthChooser, BorderLayout.WEST);
  99. monthYearPanel.add(yearChooser, BorderLayout.CENTER);
  100. monthYearPanel.setBorder(BorderFactory.createEmptyBorder());
  101. dayChooser = ScheduleCalendarDayChooser.instanceOf();
  102. dayChooser.addPropertyChangeListener(this);
  103. monthChooser.setDayChooser(dayChooser);
  104. monthChooser.addPropertyChangeListener(this);
  105. yearChooser.setDayChooser(dayChooser);
  106. yearChooser.addPropertyChangeListener(this);
  107. add(monthYearPanel, BorderLayout.NORTH);
  108. add(dayChooser, BorderLayout.CENTER);
  109. add(new ScheduleNavigator(monthChooser, yearChooser), BorderLayout.SOUTH);
  110. // Set the initialized flag before setting the calendar. This will
  111. // cause the other components to be updated properly.
  112. if (date != null) {
  113. calendar.setTime(date);
  114. }
  115. setCalendar(calendar);
  116. yearChooser.setFont(Font.decode(LocalSettings.getProperty(LocalSettings.DEFAULT_FONT)).deriveFont(Font.BOLD, 14));
  117. monthChooser.setFont(Font.decode(LocalSettings.getProperty(LocalSettings.DEFAULT_FONT)).deriveFont(Font.PLAIN, 14));
  118. }
  119. /**
  120. * Gets the dayChooser attribute of the JCalendar object
  121. *
  122. * @return the dayChooser value
  123. */
  124. public ScheduleCalendarDayChooser getDayChooser() {
  125. return dayChooser;
  126. }
  127. /**
  128. * Gets the monthChooser attribute of the JCalendar object
  129. *
  130. * @return the monthChooser value
  131. */
  132. public ScheduleMonthChooser getMonthChooser() {
  133. return monthChooser;
  134. }
  135. /**
  136. * Gets the yearChooser attribute of the JCalendar object
  137. *
  138. * @return the yearChooser value
  139. */
  140. public ScheduleYearChooser getYearChooser() {
  141. return yearChooser;
  142. }
  143. /**
  144. * JCalendar is a PropertyChangeListener, for its day, month and year
  145. * chooser.
  146. *
  147. * @param evt
  148. * the property change event
  149. */
  150. public void propertyChange(PropertyChangeEvent evt) {
  151. if (calendar != null) {
  152. Calendar c = (Calendar) calendar.clone();
  153. if (evt.getPropertyName().equals("day")) {
  154. c.set(Calendar.DAY_OF_MONTH, ((Integer) evt.getNewValue()).intValue());
  155. setCalendar(c, false);
  156. } else if (evt.getPropertyName().equals("month")) {
  157. c.set(Calendar.MONTH, ((Integer) evt.getNewValue()).intValue());
  158. setCalendar(c, false);
  159. } else if (evt.getPropertyName().equals("year")) {
  160. c.set(Calendar.YEAR, ((Integer) evt.getNewValue()).intValue());
  161. setCalendar(c, false);
  162. } else if (evt.getPropertyName().equals("date")) {
  163. c.setTime((Date) evt.getNewValue());
  164. setCalendar(c, true);
  165. }
  166. }
  167. }
  168. /**
  169. * Sets the calendar property. This is a bound property.
  170. *
  171. * @param c
  172. * the new calendar
  173. * @throws NullPointerException -
  174. * if c is null;
  175. * @see #getCalendar
  176. */
  177. public void setCalendar(Calendar c) {
  178. setCalendar(c, true);
  179. }
  180. /**
  181. * Sets the calendar attribute of the JCalendar object
  182. *
  183. * @param c
  184. * the new calendar value
  185. * @param update
  186. * the new calendar value
  187. * @throws NullPointerException -
  188. * if c is null;
  189. */
  190. private void setCalendar(Calendar c, boolean update) {
  191. if (c == null) {
  192. setDate(null);
  193. }
  194. Calendar oldCalendar = calendar;
  195. calendar = c;
  196. if (update) {
  197. // Thanks to Jeff Ulmer for correcting a bug in the sequence :)
  198. yearChooser.setYear(c.get(Calendar.YEAR));
  199. monthChooser.setMonth(c.get(Calendar.MONTH));
  200. dayChooser.setDay(c.get(Calendar.DATE));
  201. }
  202. firePropertyChange("calendar", oldCalendar, calendar);
  203. }
  204. /**
  205. * Returns a Date object.
  206. *
  207. * @return a date object constructed from the calendar property.
  208. */
  209. public Date getDate() {
  210. return new Date(calendar.getTimeInMillis());
  211. }
  212. /**
  213. * Sets the date. Fires the property change "date".
  214. *
  215. * @param date
  216. * the new date.
  217. * @throws NullPointerException -
  218. * if tha date is null
  219. */
  220. public void setDate(Date date) {
  221. Date oldDate = calendar.getTime();
  222. calendar.setTime(date);
  223. int year = calendar.get(Calendar.YEAR);
  224. int month = calendar.get(Calendar.MONTH);
  225. int day = calendar.get(Calendar.DAY_OF_MONTH);
  226. yearChooser.setYear(year);
  227. monthChooser.setMonth(month);
  228. dayChooser.setCalendar(calendar);
  229. dayChooser.setDay(day);
  230. firePropertyChange("date", oldDate, date);
  231. }
  232. }