diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-08-31 16:03:20 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-09-01 08:50:59 +0000 |
commit | 6d0204cafe8c95acd72728b3d1be616d87a31db1 (patch) | |
tree | ffd603f512c4f516ae02fc124f78f5575ddb4075 /client | |
parent | 84fbff9e6645d0133c7b9f214389e215c79c818f (diff) | |
download | vaadin-framework-6d0204cafe8c95acd72728b3d1be616d87a31db1.tar.gz vaadin-framework-6d0204cafe8c95acd72728b3d1be616d87a31db1.zip |
Add simple data change handling for Selects
Change-Id: I16f9577ea4091fb4febe167d76e141b5945f53ab
Diffstat (limited to 'client')
9 files changed, 131 insertions, 8 deletions
diff --git a/client/src/main/java/com/vaadin/client/data/AbstractRemoteDataSource.java b/client/src/main/java/com/vaadin/client/data/AbstractRemoteDataSource.java index 95e013ed28..71568cac7a 100644 --- a/client/src/main/java/com/vaadin/client/data/AbstractRemoteDataSource.java +++ b/client/src/main/java/com/vaadin/client/data/AbstractRemoteDataSource.java @@ -28,8 +28,8 @@ import com.google.gwt.core.client.Duration; import com.google.gwt.core.client.Scheduler; import com.google.gwt.core.client.Scheduler.ScheduledCommand; import com.vaadin.client.Profiler; +import com.vaadin.shared.Range; import com.vaadin.shared.Registration; -import com.vaadin.shared.ui.grid.Range; /** * Base implementation for data sources that fetch data from a remote system. diff --git a/client/src/main/java/com/vaadin/client/data/CacheStrategy.java b/client/src/main/java/com/vaadin/client/data/CacheStrategy.java index 9410223ab2..acfadcbcf7 100644 --- a/client/src/main/java/com/vaadin/client/data/CacheStrategy.java +++ b/client/src/main/java/com/vaadin/client/data/CacheStrategy.java @@ -16,7 +16,7 @@ package com.vaadin.client.data; -import com.vaadin.shared.ui.grid.Range; +import com.vaadin.shared.Range; /** * Determines what data an {@link AbstractRemoteDataSource} should fetch and diff --git a/client/src/main/java/com/vaadin/client/data/DataSource.java b/client/src/main/java/com/vaadin/client/data/DataSource.java index 3457b7b9fc..fdec4b9a38 100644 --- a/client/src/main/java/com/vaadin/client/data/DataSource.java +++ b/client/src/main/java/com/vaadin/client/data/DataSource.java @@ -16,6 +16,9 @@ package com.vaadin.client.data; +import java.util.function.Consumer; + +import com.vaadin.shared.Range; import com.vaadin.shared.Registration; /** @@ -104,7 +107,7 @@ public interface DataSource<T> { * override of an existing method, we're defining a new method for that * instead. * - * @param rowHandle + * @param obj * the reference object with which to compare * @return {@code true} if this object is the same as the obj argument; * {@code false} otherwise. @@ -182,11 +185,28 @@ public interface DataSource<T> { * * @param dataChangeHandler * the data change handler + * + * @return registration for removing the handler */ public Registration addDataChangeHandler( DataChangeHandler dataChangeHandler); /** + * Sets a simple data change handler for a widget without lazy loading. + * Refresh method should reset all the data in the widget. + * + * @param refreshMethod + * a method to refresh all data in the widget + * + * @return registration for removing the handler + */ + public default Registration addDataChangeHandler( + Consumer<Range> refreshMethod) { + return addDataChangeHandler( + new SimpleDataChangeHandler(this, refreshMethod)); + } + + /** * Gets a {@link RowHandle} of a row object in the cache. * * @param row diff --git a/client/src/main/java/com/vaadin/client/data/SimpleDataChangeHandler.java b/client/src/main/java/com/vaadin/client/data/SimpleDataChangeHandler.java new file mode 100644 index 0000000000..46fb019f3c --- /dev/null +++ b/client/src/main/java/com/vaadin/client/data/SimpleDataChangeHandler.java @@ -0,0 +1,103 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.client.data; + +import java.util.function.Consumer; + +import com.google.gwt.core.client.Scheduler; +import com.vaadin.shared.Range; + +/** + * Helper class for creating a {@link DataChangeHandler} for a Widget that does + * not support lazy loading. + * + * @author Vaadin Ltd + * @since + */ +public class SimpleDataChangeHandler implements DataChangeHandler { + + /** + * Class to request the data source to get the full data set. + */ + private static class DelayedResetScheduler { + + private final DataSource<?> dataSource; + private boolean scheduled = false; + + public DelayedResetScheduler(DataSource<?> dataSource) { + this.dataSource = dataSource; + } + + public void schedule() { + if (scheduled) { + return; + } + Scheduler.get().scheduleFinally(() -> { + dataSource.ensureAvailability(0, dataSource.size()); + scheduled = false; + }); + scheduled = true; + } + + public int getExpectedSize() { + return dataSource.size(); + } + + public boolean isScheduled() { + return scheduled; + } + } + + private final DelayedResetScheduler scheduler; + private final Consumer<Range> refreshMethod; + + SimpleDataChangeHandler(DataSource<?> dataSource, + Consumer<Range> refreshMethod) { + scheduler = new DelayedResetScheduler(dataSource); + this.refreshMethod = refreshMethod; + } + + @Override + public void dataUpdated(int firstRowIndex, int numberOfRows) { + scheduler.schedule(); + } + + @Override + public void dataRemoved(int firstRowIndex, int numberOfRows) { + scheduler.schedule(); + } + + @Override + public void dataAdded(int firstRowIndex, int numberOfRows) { + scheduler.schedule(); + } + + @Override + public void dataAvailable(int firstRowIndex, int numberOfRows) { + if (!scheduler.isScheduled() && firstRowIndex == 0 + && numberOfRows == scheduler.getExpectedSize()) { + // All data should now be available. + refreshMethod.accept(Range.withLength(firstRowIndex, numberOfRows)); + } else { + scheduler.schedule(); + } + } + + @Override + public void resetDataAndSize(int newSize) { + scheduler.schedule(); + } +} diff --git a/client/src/main/java/com/vaadin/client/widget/escalator/RowVisibilityChangeEvent.java b/client/src/main/java/com/vaadin/client/widget/escalator/RowVisibilityChangeEvent.java index ce4be72b68..a36b591fd0 100644 --- a/client/src/main/java/com/vaadin/client/widget/escalator/RowVisibilityChangeEvent.java +++ b/client/src/main/java/com/vaadin/client/widget/escalator/RowVisibilityChangeEvent.java @@ -17,7 +17,7 @@ package com.vaadin.client.widget.escalator; import com.google.gwt.event.shared.GwtEvent; -import com.vaadin.shared.ui.grid.Range; +import com.vaadin.shared.Range; /** * Event fired when the range of visible rows changes e.g. because of scrolling. diff --git a/client/src/main/java/com/vaadin/client/widget/grid/DataAvailableEvent.java b/client/src/main/java/com/vaadin/client/widget/grid/DataAvailableEvent.java index f4ecf11677..8441a16bc5 100644 --- a/client/src/main/java/com/vaadin/client/widget/grid/DataAvailableEvent.java +++ b/client/src/main/java/com/vaadin/client/widget/grid/DataAvailableEvent.java @@ -16,7 +16,7 @@ package com.vaadin.client.widget.grid; import com.google.gwt.event.shared.GwtEvent; -import com.vaadin.shared.ui.grid.Range; +import com.vaadin.shared.Range; /** * Event object describing a change of row availability in DataSource of a Grid. diff --git a/client/src/main/java/com/vaadin/client/widgets/Escalator.java b/client/src/main/java/com/vaadin/client/widgets/Escalator.java index a5e95a26a5..35978b7cb0 100644 --- a/client/src/main/java/com/vaadin/client/widgets/Escalator.java +++ b/client/src/main/java/com/vaadin/client/widgets/Escalator.java @@ -89,8 +89,8 @@ import com.vaadin.client.widget.escalator.events.RowHeightChangedEvent; import com.vaadin.client.widget.grid.events.ScrollEvent; import com.vaadin.client.widget.grid.events.ScrollHandler; import com.vaadin.client.widgets.Escalator.JsniUtil.TouchHandlerBundle; +import com.vaadin.shared.Range; import com.vaadin.shared.ui.grid.HeightMode; -import com.vaadin.shared.ui.grid.Range; import com.vaadin.shared.ui.grid.ScrollDestination; import com.vaadin.shared.util.SharedUtil; diff --git a/client/src/main/java/com/vaadin/client/widgets/Grid.java b/client/src/main/java/com/vaadin/client/widgets/Grid.java index 990c85dd1c..85c6d48942 100644 --- a/client/src/main/java/com/vaadin/client/widgets/Grid.java +++ b/client/src/main/java/com/vaadin/client/widgets/Grid.java @@ -174,13 +174,13 @@ import com.vaadin.client.widgets.Escalator.SubPartArguments; import com.vaadin.client.widgets.Grid.Editor.State; import com.vaadin.client.widgets.Grid.StaticSection.StaticCell; import com.vaadin.client.widgets.Grid.StaticSection.StaticRow; +import com.vaadin.shared.Range; import com.vaadin.shared.Registration; import com.vaadin.shared.data.sort.SortDirection; import com.vaadin.shared.ui.grid.GridConstants; import com.vaadin.shared.ui.grid.GridConstants.Section; import com.vaadin.shared.ui.grid.GridStaticCellType; import com.vaadin.shared.ui.grid.HeightMode; -import com.vaadin.shared.ui.grid.Range; import com.vaadin.shared.ui.grid.ScrollDestination; import com.vaadin.shared.util.SharedUtil; diff --git a/client/src/test/java/com/vaadin/client/ui/grid/PartitioningTest.java b/client/src/test/java/com/vaadin/client/ui/grid/PartitioningTest.java index ac4d39006a..8be729e7c4 100644 --- a/client/src/test/java/com/vaadin/client/ui/grid/PartitioningTest.java +++ b/client/src/test/java/com/vaadin/client/ui/grid/PartitioningTest.java @@ -21,7 +21,7 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; -import com.vaadin.shared.ui.grid.Range; +import com.vaadin.shared.Range; @SuppressWarnings("static-method") public class PartitioningTest { |