diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-08-17 14:46:40 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-08-18 09:47:07 +0000 |
commit | ad144e26e08d69cd36ecf9fca85285755853ff15 (patch) | |
tree | 2f6ffb86950e2c5cb9e1719d777cfa7a504384a5 | |
parent | 86cee1ad6bd8e8d9ab3e9128c9b3d0059266fde6 (diff) | |
download | vaadin-framework-ad144e26e08d69cd36ecf9fca85285755853ff15.tar.gz vaadin-framework-ad144e26e08d69cd36ecf9fca85285755853ff15.zip |
Add Listing interface and minimalistic abstract implementation
Change-Id: Ie13788c2c4dc49106919d8599b4b1199395a5b8e
4 files changed, 202 insertions, 0 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java b/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java new file mode 100644 index 0000000000..92fb0f6720 --- /dev/null +++ b/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java @@ -0,0 +1,28 @@ +/* + * 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.connectors; + +import com.vaadin.client.ui.AbstractComponentConnector; +import com.vaadin.ui.AbstractListing; + +/** + * Base connector class for {@link AbstractListing}. + * + * @since + */ +public abstract class AbstractListingConnector + extends AbstractComponentConnector { +} diff --git a/server/src/main/java/com/vaadin/data/Listing.java b/server/src/main/java/com/vaadin/data/Listing.java new file mode 100644 index 0000000000..ce81490989 --- /dev/null +++ b/server/src/main/java/com/vaadin/data/Listing.java @@ -0,0 +1,65 @@ +/* + * 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.data; + +import java.io.Serializable; +import java.util.Collection; + +import com.vaadin.server.data.DataSource; + +/** + * Generic interface for Components that show a list of data. + * + * @param <T> + * data type for listing + */ +public interface Listing<T> extends Serializable { + + /** + * Sets the {@link DataSource} used by this Listing. + * + * @param data + * data source + */ + void setDataSource(DataSource<T> data); + + /** + * Sets the options available for this Listing. + * + * @param data + * collection of data + */ + default void setItems(Collection<T> data) { + setDataSource(DataSource.create(data)); + } + + /** + * Sets the options available for this Listing. + * + * @param data + * array of data + */ + default void setItems(T... data) { + setDataSource(DataSource.create(data)); + } + + /** + * Returns the {@link DataSource} of this Listing. + * + * @return data source + */ + DataSource<T> getDataSource(); +} diff --git a/server/src/main/java/com/vaadin/ui/AbstractListing.java b/server/src/main/java/com/vaadin/ui/AbstractListing.java new file mode 100644 index 0000000000..8694f5e002 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/AbstractListing.java @@ -0,0 +1,43 @@ +/* + * 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.ui; + +import com.vaadin.data.Listing; +import com.vaadin.server.data.DataSource; + +/** + * Base class for Listing components. Provides common handling for + * {@link DataCommunicator}, {@link SelectionModel} and + * {@link TypedDataGenerator}s. + * + * @param <T> + * listing data type + */ +public abstract class AbstractListing<T> extends AbstractComponent + implements Listing<T> { + + private DataSource<T> dataSource; + + @Override + public void setDataSource(DataSource<T> dataSource) { + this.dataSource = dataSource; + } + + @Override + public DataSource<T> getDataSource() { + return dataSource; + } +} diff --git a/server/src/test/java/com/vaadin/tests/server/component/abstractlisting/AbstractListingTest.java b/server/src/test/java/com/vaadin/tests/server/component/abstractlisting/AbstractListingTest.java new file mode 100644 index 0000000000..cf9fcdab37 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/server/component/abstractlisting/AbstractListingTest.java @@ -0,0 +1,66 @@ +package com.vaadin.tests.server.component.abstractlisting; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.data.Listing; +import com.vaadin.server.data.BackEndDataSource; +import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.ListDataSource; +import com.vaadin.server.data.Query; +import com.vaadin.ui.AbstractListing; + +public class AbstractListingTest { + + private static final String[] ITEM_ARRAY = new String[] { "Foo", "Bar", + "Baz" }; + + private Listing<String> listing; + private List<String> items; + + @Before + public void setUp() { + items = new ArrayList<>(Arrays.asList(ITEM_ARRAY)); + listing = new AbstractListing<String>() { + }; + } + + @Test + public void testSetItemsWithCollection() { + listing.setItems(items); + listing.getDataSource().apply(new Query()).forEach( + str -> Assert.assertTrue("Unexpected item in data source", + items.remove(str))); + Assert.assertTrue("Not all items from list were in data source", + items.isEmpty()); + } + + @Test + public void testSetItemsWithVarargs() { + listing.setItems(ITEM_ARRAY); + listing.getDataSource().apply(new Query()).forEach( + str -> Assert.assertTrue("Unexpected item in data source", + items.remove(str))); + Assert.assertTrue("Not all items from list were in data source", + items.isEmpty()); + } + + @Test + public void testSetDataSource() { + ListDataSource<String> dataSource = DataSource.create(items); + listing.setDataSource(dataSource); + Assert.assertEquals("setDataSource did not set data source", dataSource, + listing.getDataSource()); + listing.setDataSource(new BackEndDataSource<>(q -> Stream.of(ITEM_ARRAY) + .skip(q.getOffset()).limit(q.getLimit()), + q -> ITEM_ARRAY.length)); + Assert.assertNotEquals("setDataSource did not replace data source", + dataSource, listing.getDataSource()); + } +} |