]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add method to auto scale calendar time range (#17715)
authorSteven Spungin <steven@spungin.tv>
Fri, 1 May 2015 15:44:42 +0000 (11:44 -0400)
committerPekka Hyvönen <pekka@vaadin.com>
Fri, 4 Nov 2016 12:51:36 +0000 (14:51 +0200)
Change-Id: I193fe9e8a0ee03d66e4b63d5f85c0e9a9233c0de

compatibility-server/src/main/java/com/vaadin/v7/ui/Calendar.java
compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/calendar/ContainerDataSourceTest.java

index da6443da29ca53303dc814c5823c75b3b89bfbf3..a33cce6eef7f4905d5286e38ed48be3db44450ac 100644 (file)
@@ -223,6 +223,18 @@ public class Calendar extends AbstractLegacyComponent
      */
     private CalendarServerRpcImpl rpc = new CalendarServerRpcImpl();
 
+    /**
+     * The cached minimum minute shown when using
+     * {@link #autoScaleVisibleHoursOfDay()}.
+     */
+    private Integer minTimeInMinutes;
+
+    /**
+     * The cached maximum minute shown when using
+     * {@link #autoScaleVisibleHoursOfDay()}.
+     */
+    private Integer maxTimeInMinutes;
+
     private Integer customFirstDayOfWeek;
 
     /**
@@ -452,6 +464,7 @@ public class Calendar extends AbstractLegacyComponent
 
         currentCalendar.setTime(firstDateToShow);
         events = getEventProvider().getEvents(firstDateToShow, lastDateToShow);
+        cacheMinMaxTimeOfDay(events);
 
         List<CalendarState.Event> calendarStateEvents = new ArrayList<>();
         if (events != null) {
@@ -475,6 +488,76 @@ public class Calendar extends AbstractLegacyComponent
         getState().events = calendarStateEvents;
     }
 
+    /**
+     * Stores the minimum and maximum time-of-day in minutes for the events.
+     *
+     * @param events
+     *            A list of calendar events. Can be <code>null</code>.
+     */
+    private void cacheMinMaxTimeOfDay(List<CalendarEvent> events) {
+        minTimeInMinutes = null;
+        maxTimeInMinutes = null;
+        if (events != null) {
+            for (CalendarEvent event : events) {
+                int minuteOfDayStart = getMinuteOfDay(event.getStart());
+                int minuteOfDayEnd = getMinuteOfDay(event.getEnd());
+                if (minTimeInMinutes == null) {
+                    minTimeInMinutes = minuteOfDayStart;
+                    maxTimeInMinutes = minuteOfDayEnd;
+                } else {
+                    if (minuteOfDayStart < minTimeInMinutes) {
+                        minTimeInMinutes = minuteOfDayStart;
+                    }
+                    if (minuteOfDayEnd > maxTimeInMinutes) {
+                        maxTimeInMinutes = minuteOfDayEnd;
+                    }
+                }
+            }
+        }
+    }
+
+    private static int getMinuteOfDay(Date date) {
+        java.util.Calendar calendar = java.util.Calendar.getInstance();
+        calendar.setTime(date);
+        return calendar.get(java.util.Calendar.HOUR_OF_DAY) * 60
+                + calendar.get(java.util.Calendar.MINUTE);
+    }
+
+    /**
+     * Sets the displayed start and end time to fit all current events that were
+     * retrieved from the last call to getEvents().
+     * <p>
+     * If no events exist, nothing happens.
+     * <p>
+     * <b>NOTE: triggering this method only does this once for the current
+     * events - events that are not in the current visible range, are
+     * ignored!</b>
+     *
+     * @see #setFirstVisibleHourOfDay(int)
+     * @see #setLastVisibleHourOfDay(int)
+     */
+    public void autoScaleVisibleHoursOfDay() {
+        if (minTimeInMinutes != null) {
+            setFirstVisibleHourOfDay(minTimeInMinutes / 60);
+            // Do not show the final hour if last minute ends on it
+            setLastVisibleHourOfDay((maxTimeInMinutes - 1) / 60);
+        }
+    }
+
+    /**
+     * Resets the {@link #setFirstVisibleHourOfDay(int)} and
+     * {@link #setLastVisibleHourOfDay(int)} to the default values, 0 and 23
+     * respectively.
+     *
+     * @see #autoScaleVisibleHoursOfDay()
+     * @see #setFirstVisibleHourOfDay(int)
+     * @see #setLastVisibleHourOfDay(int)
+     */
+    public void resetVisibleHoursOfDay() {
+        setFirstVisibleHourOfDay(0);
+        setLastVisibleHourOfDay(23);
+    }
+
     private void setupDaysAndActions() {
         // Make sure we have a up-to-date locale
         initCalendarWithLocale();
@@ -832,9 +915,12 @@ public class Calendar extends AbstractLegacyComponent
      * requested by the dates set by {@link #setStartDate(Date)} and
      * {@link #setEndDate(Date)}.
      * </p>
+     * You can use {@link #autoScaleVisibleHoursOfDay()} for automatic scaling
+     * of the visible hours based on current events.
      *
      * @param firstHour
      *            the first hour of the day to show, between 0 and 23
+     * @see #autoScaleVisibleHoursOfDay()
      */
     public void setFirstVisibleHourOfDay(int firstHour) {
         if (this.firstHour != firstHour && firstHour >= 0 && firstHour <= 23
@@ -864,9 +950,12 @@ public class Calendar extends AbstractLegacyComponent
      * requested by the dates set by {@link #setStartDate(Date)} and
      * {@link #setEndDate(Date)}.
      * </p>
+     * You can use {@link #autoScaleVisibleHoursOfDay()} for automatic scaling
+     * of the visible hours based on current events.
      *
      * @param lastHour
      *            the first hour of the day to show, between 0 and 23
+     * @see #autoScaleVisibleHoursOfDay()
      */
     public void setLastVisibleHourOfDay(int lastHour) {
         if (this.lastHour != lastHour && lastHour >= 0 && lastHour <= 23
@@ -1612,7 +1701,10 @@ public class Calendar extends AbstractLegacyComponent
      */
     @Override
     public List<CalendarEvent> getEvents(Date startDate, Date endDate) {
-        return getEventProvider().getEvents(startDate, endDate);
+        List<CalendarEvent> events = getEventProvider().getEvents(startDate,
+                endDate);
+        cacheMinMaxTimeOfDay(events);
+        return events;
     }
 
     /*
index 311cb8f6cc9787f2667e93361157dae68b6d67e3..d7076800826535d62fd5d8a2c7f4e52617d0a115 100644 (file)
@@ -20,7 +20,10 @@ import static org.junit.Assert.assertTrue;
 
 import java.util.Date;
 import java.util.List;
+import java.util.Locale;
+import java.util.TimeZone;
 
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -276,7 +279,7 @@ public class ContainerDataSourceTest {
 
         // Use a container data source
         calendar.setEventProvider(new ContainerEventProvider(
-                new BeanItemContainer<BasicEvent>(BasicEvent.class)));
+                new BeanItemContainer<>(BasicEvent.class)));
 
         // Start and end dates to query for
         java.util.Calendar cal = java.util.Calendar.getInstance();
@@ -304,7 +307,7 @@ public class ContainerDataSourceTest {
 
         // Use a container data source
         calendar.setEventProvider(new ContainerEventProvider(
-                new BeanItemContainer<BasicEvent>(BasicEvent.class)));
+                new BeanItemContainer<>(BasicEvent.class)));
 
         // Start and end dates to query for
         java.util.Calendar cal = java.util.Calendar.getInstance();
@@ -361,8 +364,59 @@ public class ContainerDataSourceTest {
         }
     }
 
+    @Test
+    public void testAutomaticScaleVisibleHoursOfDay() {
+        BeanItemContainer<CalendarEvent> container = new BeanItemContainer<>(
+                CalendarEvent.class);
+        java.util.Calendar start = java.util.Calendar.getInstance();
+        java.util.Calendar end = java.util.Calendar.getInstance();
+
+        start.set(java.util.Calendar.HOUR_OF_DAY, 8);
+        start.set(java.util.Calendar.MINUTE, 10);
+        // same start and end time
+        container.addBean(
+                new BasicEvent("8:00", "Description 1", start.getTime()));
+
+        start.set(java.util.Calendar.HOUR_OF_DAY, 16);
+        end.set(java.util.Calendar.HOUR_OF_DAY, 18);
+        end.set(java.util.Calendar.MINUTE, 10);
+
+        container.addBean(new BasicEvent("16-18", "Description 2",
+                start.getTime(), end.getTime())); // 16-18
+
+        calendar.setContainerDataSource(container);
+        calendar.setTimeZone(TimeZone.getDefault());
+        calendar.setLocale(Locale.getDefault());
+        calendar.beforeClientResponse(true); // simulate adding to UI
+
+        Assert.assertEquals(0, calendar.getFirstVisibleHourOfDay());
+        Assert.assertEquals(23, calendar.getLastVisibleHourOfDay());
+
+        calendar.autoScaleVisibleHoursOfDay();
+        Assert.assertEquals(8, calendar.getFirstVisibleHourOfDay());
+        Assert.assertEquals(18, calendar.getLastVisibleHourOfDay());
+
+        // reset visible timing to something else, so that the added event is
+        // not filtered out
+        calendar.resetVisibleHoursOfDay();
+        calendar.beforeClientResponse(false); // simulate being attached
+
+        Assert.assertEquals(0, calendar.getFirstVisibleHourOfDay());
+        Assert.assertEquals(23, calendar.getLastVisibleHourOfDay());
+
+        start.set(java.util.Calendar.HOUR_OF_DAY, 5);
+        end.set(java.util.Calendar.HOUR_OF_DAY, 21);
+        container.addBean(new BasicEvent("05-21", "Description 3",
+                start.getTime(), end.getTime())); // 05-21
+
+        calendar.beforeClientResponse(false); // simulate being attached
+        calendar.autoScaleVisibleHoursOfDay();
+        Assert.assertEquals(5, calendar.getFirstVisibleHourOfDay());
+        Assert.assertEquals(21, calendar.getLastVisibleHourOfDay());
+    }
+
     private static Indexed createTestBeanItemContainer() {
-        BeanItemContainer<CalendarEvent> eventContainer = new BeanItemContainer<CalendarEvent>(
+        BeanItemContainer<CalendarEvent> eventContainer = new BeanItemContainer<>(
                 CalendarEvent.class);
         java.util.Calendar cal = java.util.Calendar.getInstance();
         for (int i = 1; i <= 10; i++) {