Author: rwhitcomb
Date: Mon Sep 14 22:06:20 2015
New Revision: 1703071

URL: http://svn.apache.org/r1703071
Log:
PIVOT-978: Fix Calendar control to only allow valid years.

Two changes:
1. Add two static methods to CalendarDate to get the min and max supported
   years for dates.
2. Then, set the Spinner data for the year spinner in the Calendar control
   to have these values as the bounds.  Have to change the code to use
   getSelectedItem instead of getSelectedIndex, because the index no longer
   starts at zero, to get/set the year value from the Spinner.

There is still a small problem: when you try to display January 1583, it
needs to reach back to 1582 to get the start of the week, and then you will
get the exception.  Other than that, everything seems fine.

Modified:
    pivot/branches/2.0.x/core/src/org/apache/pivot/util/CalendarDate.java
    
pivot/branches/2.0.x/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraCalendarSkin.java

Modified: pivot/branches/2.0.x/core/src/org/apache/pivot/util/CalendarDate.java
URL: 
http://svn.apache.org/viewvc/pivot/branches/2.0.x/core/src/org/apache/pivot/util/CalendarDate.java?rev=1703071&r1=1703070&r2=1703071&view=diff
==============================================================================
--- pivot/branches/2.0.x/core/src/org/apache/pivot/util/CalendarDate.java 
(original)
+++ pivot/branches/2.0.x/core/src/org/apache/pivot/util/CalendarDate.java Mon 
Sep 14 22:06:20 2015
@@ -205,6 +205,26 @@ public final class CalendarDate implemen
     private static final Pattern PATTERN = 
Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$");
 
     /**
+     * Return the minimum supported year. This is to help the {@link 
org.apache.pivot.wtk.CalendarButton}
+     * get the right range of years, which should be this value up to and 
including
+     * {@link #getMaximumSupportedYear}.
+     */
+    public static int getMinimumSupportedYear() {
+        // The test in the constructor is year <= GREGORIAN_CUTOVER_YEAR, so 
we must
+        // return +1 here as the minimum legal year.
+        return GREGORIAN_CUTOVER_YEAR + 1;
+    }
+
+    /**
+     * Return the maximum supported year. This is to help the {@link 
org.apache.pivot.wtk.CalendarButton}
+     * get the right range of years, which should be {@link 
#getMinimumSupportedYear}
+     * up to and including this value.
+     */
+    public static int getMaximumSupportedYear() {
+        return 9999;
+    }
+
+    /**
      * Creates a new <tt>CalendarDate</tt> representing the current day in the
      * default timezone and the default locale.
      */

Modified: 
pivot/branches/2.0.x/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraCalendarSkin.java
URL: 
http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraCalendarSkin.java?rev=1703071&r1=1703070&r2=1703071&view=diff
==============================================================================
--- 
pivot/branches/2.0.x/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraCalendarSkin.java
 (original)
+++ 
pivot/branches/2.0.x/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraCalendarSkin.java
 Mon Sep 14 22:06:20 2015
@@ -431,7 +431,8 @@ public class TerraCalendarSkin extends C
 
         // Year spinner
         yearSpinner = new Spinner();
-        yearSpinner.setSpinnerData(new NumericSpinnerData(0, Short.MAX_VALUE));
+        yearSpinner.setSpinnerData(new 
NumericSpinnerData(CalendarDate.getMinimumSupportedYear(),
+                                                          
CalendarDate.getMaximumSupportedYear()));
 
         yearSpinner.getSpinnerSelectionListeners().add(new 
SpinnerSelectionListener.Adapter() {
             @Override
@@ -502,7 +503,7 @@ public class TerraCalendarSkin extends C
                     // result of the user toggling the date button (as opposed
                     // to changing the month or year), clear the selection
                     if (selectedDate == null
-                        || (selectedDate.year == yearSpinner.getSelectedIndex()
+                        || (selectedDate.year == 
((Integer)yearSpinner.getSelectedItem())
                             && selectedDate.month == 
monthSpinner.getSelectedIndex())) {
                         calendar.setSelectedDate((CalendarDate)null);
                     }
@@ -545,7 +546,7 @@ public class TerraCalendarSkin extends C
         Calendar calendar = (Calendar)component;
         calendar.add(calendarTablePane);
 
-        yearSpinner.setSelectedIndex(calendar.getYear());
+        yearSpinner.setSelectedItem(calendar.getYear());
         monthSpinner.setSelectedIndex(calendar.getMonth());
         updateLabels();
         updateCalendar();
@@ -620,7 +621,7 @@ public class TerraCalendarSkin extends C
         Filter<CalendarDate> disabledDateFilter = 
calendar.getDisabledDateFilter();
 
         monthSpinner.setSelectedIndex(month);
-        yearSpinner.setSelectedIndex(year);
+        yearSpinner.setSelectedItem(year);
 
         // Determine the first and last days of the month
         Locale locale = calendar.getLocale();
@@ -938,7 +939,7 @@ public class TerraCalendarSkin extends C
     // Calendar events
     @Override
     public void yearChanged(Calendar calendar, int previousYear) {
-        yearSpinner.setSelectedIndex(calendar.getYear());
+        yearSpinner.setSelectedItem(calendar.getYear());
         updateCalendar();
     }
 


Reply via email to