summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <teemusa@vaadin.com>2016-08-24 14:04:02 +0300
committerVaadin Code Review <review@vaadin.com>2016-08-24 15:31:22 +0000
commit08450203e750bfd4b7a6b2e55842f185aef2b8e5 (patch)
treeabd5f49469e7a64b8b799f0d5ad1cbbf79a6652c /server
parentdd1b81a2c25cc4413eab462c3724e2dba6b87f84 (diff)
downloadvaadin-framework-08450203e750bfd4b7a6b2e55842f185aef2b8e5.tar.gz
vaadin-framework-08450203e750bfd4b7a6b2e55842f185aef2b8e5.zip
Add listing extension to allow access to data communicator
Change-Id: I4eca9884cf0195bab68d282bf1a6fd8c8b48a0fb
Diffstat (limited to 'server')
-rw-r--r--server/src/main/java/com/vaadin/ui/AbstractListing.java71
-rw-r--r--server/src/test/java/com/vaadin/tests/server/component/abstractlisting/AbstractListingTest.java56
2 files changed, 110 insertions, 17 deletions
diff --git a/server/src/main/java/com/vaadin/ui/AbstractListing.java b/server/src/main/java/com/vaadin/ui/AbstractListing.java
index 05f31d7b81..b5780f57e9 100644
--- a/server/src/main/java/com/vaadin/ui/AbstractListing.java
+++ b/server/src/main/java/com/vaadin/ui/AbstractListing.java
@@ -15,7 +15,10 @@
*/
package com.vaadin.ui;
+import java.util.Objects;
+
import com.vaadin.data.Listing;
+import com.vaadin.server.AbstractExtension;
import com.vaadin.server.data.DataCommunicator;
import com.vaadin.server.data.DataSource;
import com.vaadin.server.data.TypedDataGenerator;
@@ -30,6 +33,72 @@ import com.vaadin.server.data.TypedDataGenerator;
public abstract class AbstractListing<T> extends AbstractComponent
implements Listing<T> {
+ /**
+ * Helper base class for creating extensions for Listing components. This
+ * class provides helpers for accessing the underlying parts of the
+ * component and its communicational mechanism.
+ *
+ * @param <T>
+ * listing data type
+ */
+ public abstract static class AbstractListingExtension<T>
+ extends AbstractExtension implements TypedDataGenerator<T> {
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * Note: AbstractListingExtensions need parent to be of type
+ * AbstractListing.
+ *
+ * @throws IllegalArgument
+ * if parent is not an AbstractListing
+ */
+ public void extend(Listing<T> listing) {
+ if (listing instanceof AbstractListing) {
+ AbstractListing<T> parent = (AbstractListing<T>) listing;
+ super.extend(parent);
+ parent.addDataGenerator(this);
+ } else {
+ throw new IllegalArgumentException(
+ "Parent needs to extend AbstractListing");
+ }
+ }
+
+ @Override
+ public void remove() {
+ getParent().removeDataGenerator(this);
+
+ super.remove();
+ }
+
+ /**
+ * Gets a data object based on its client-side identifier key.
+ *
+ * @param key
+ * key for data object
+ * @return the data object
+ */
+ protected T getData(String key) {
+ return getParent().getDataCommunicator().getKeyMapper().get(key);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public AbstractListing<T> getParent() {
+ return (AbstractListing<T>) super.getParent();
+ }
+
+ /**
+ * Helper method for refreshing a single data object.
+ *
+ * @param data
+ * data object to refresh
+ */
+ protected void refresh(T data) {
+ getParent().getDataCommunicator().refresh(data);
+ }
+ }
+
/* DataCommunicator for this Listing component */
private final DataCommunicator<T> dataCommunicator;
@@ -53,6 +122,8 @@ public abstract class AbstractListing<T> extends AbstractComponent
* a customized data communicator instance
*/
protected AbstractListing(DataCommunicator<T> dataCommunicator) {
+ Objects.requireNonNull(dataCommunicator,
+ "The data communicator can't be null");
this.dataCommunicator = dataCommunicator;
addExtension(dataCommunicator);
}
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
index b3d606cc30..2bc13dc4f3 100644
--- 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
@@ -13,25 +13,14 @@ 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.server.data.TypedDataGenerator;
import com.vaadin.ui.AbstractListing;
+import com.vaadin.ui.AbstractListing.AbstractListingExtension;
import elemental.json.JsonObject;
public class AbstractListingTest {
private final class TestListing extends AbstractListing<String> {
-
- @Override
- public void addDataGenerator(TypedDataGenerator<String> generator) {
- super.addDataGenerator(generator);
- }
-
- @Override
- public void removeDataGenerator(TypedDataGenerator<String> generator) {
- super.removeDataGenerator(generator);
- }
-
/**
* Used to execute data generation
*/
@@ -40,7 +29,8 @@ public class AbstractListingTest {
}
}
- private final class CountGenerator implements TypedDataGenerator<String> {
+ private final class CountGenerator
+ extends AbstractListingExtension<String> {
int callCount = 0;
@@ -52,6 +42,11 @@ public class AbstractListingTest {
@Override
public void destroyData(String data) {
}
+
+ @Override
+ public void refresh(String data) {
+ super.refresh(data);
+ }
}
private static final String[] ITEM_ARRAY = new String[] { "Foo", "Bar",
@@ -102,7 +97,7 @@ public class AbstractListingTest {
@Test
public void testAddDataGeneartorBeforeDataSource() {
CountGenerator generator = new CountGenerator();
- listing.addDataGenerator(generator);
+ generator.extend(listing);
listing.setItems("Foo");
listing.runDataGeneration();
Assert.assertEquals("Generator should have been called once", 1,
@@ -113,20 +108,47 @@ public class AbstractListingTest {
public void testAddDataGeneartorAfterDataSource() {
CountGenerator generator = new CountGenerator();
listing.setItems("Foo");
- listing.addDataGenerator(generator);
+ generator.extend(listing);
+ listing.runDataGeneration();
+ Assert.assertEquals("Generator should have been called once", 1,
+ generator.callCount);
+ }
+
+ @Test
+ public void testDataNotGeneratedTwice() {
+ listing.setItems("Foo");
+ CountGenerator generator = new CountGenerator();
+ generator.extend(listing);
listing.runDataGeneration();
Assert.assertEquals("Generator should have been called once", 1,
generator.callCount);
+ listing.runDataGeneration();
+ Assert.assertEquals("Generator should not have been called again", 1,
+ generator.callCount);
}
@Test
public void testRemoveDataGeneartor() {
listing.setItems("Foo");
CountGenerator generator = new CountGenerator();
- listing.addDataGenerator(generator);
- listing.removeDataGenerator(generator);
+ generator.extend(listing);
+ generator.remove();
listing.runDataGeneration();
Assert.assertEquals("Generator should not have been called", 0,
generator.callCount);
}
+
+ @Test
+ public void testDataRefresh() {
+ listing.setItems("Foo");
+ CountGenerator generator = new CountGenerator();
+ generator.extend(listing);
+ listing.runDataGeneration();
+ Assert.assertEquals("Generator should have been called once", 1,
+ generator.callCount);
+ generator.refresh("Foo");
+ listing.runDataGeneration();
+ Assert.assertEquals("Generator should have been called again", 2,
+ generator.callCount);
+ }
}