From 49354006f5ccabe98ce367034cdda9c2cae16b08 Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Mon, 8 Feb 2016 17:17:54 +0200 Subject: Move key mapper to super class and provide methods to customise it Adds a note to create methods of data provider to remind the developer that the method is called from the constructor and should not depend anything initialised on init or in the subclass constructor. Change-Id: Ifb153d5e9ac382318f380e16a3552e316d8197d0 --- .../communication/data/typed/DataProvider.java | 25 +++++++++++++++++++--- .../data/typed/SimpleDataProvider.java | 13 +++++------ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/server/src/com/vaadin/server/communication/data/typed/DataProvider.java b/server/src/com/vaadin/server/communication/data/typed/DataProvider.java index 2c45c52ed5..320c1b60b7 100644 --- a/server/src/com/vaadin/server/communication/data/typed/DataProvider.java +++ b/server/src/com/vaadin/server/communication/data/typed/DataProvider.java @@ -181,6 +181,7 @@ public abstract class DataProvider extends AbstractExtension { protected DataSource dataSource; private DataChangeHandler dataChangeHandler; private DetachListener detachListener; + private DataKeyMapper keyMapper; protected DataProvider(DataSource dataSource) { addDataGenerator(handler); @@ -189,6 +190,7 @@ public abstract class DataProvider extends AbstractExtension { registerRpc(createRpc()); dataChangeHandler = createDataChangeHandler(); this.dataSource.addDataChangeHandler(dataChangeHandler); + keyMapper = createKeyMapper(); } @Override @@ -237,6 +239,17 @@ public abstract class DataProvider extends AbstractExtension { generators.remove(generator); } + /** + * Gets the {@link DataKeyMapper} used by this {@link DataProvider}. Key + * mapper can be used to map keys sent to the client-side back to their + * respective data objects. + * + * @return key mapper + */ + public DataKeyMapper getKeyMapper() { + return keyMapper; + } + /** * Sends given collection of data objects to the client-side. * @@ -313,14 +326,18 @@ public abstract class DataProvider extends AbstractExtension { } /** - * Gets the {@link DataKeyMapper} used by this {@link DataProvider}. + * Creates a {@link DataKeyMapper} to use with this {@link DataProvider}. + *

+ * This method is called from the constructor. * * @return key mapper */ - protected abstract DataKeyMapper getKeyMapper(); + protected abstract DataKeyMapper createKeyMapper(); /** - * Creates a {@link DataRequestRpc} instance. + * Creates a {@link DataRequestRpc} used with this {@link DataProvider}. + *

+ * This method is called from the constructor. * * @return data request rpc implementation */ @@ -328,6 +345,8 @@ public abstract class DataProvider extends AbstractExtension { /** * Creates a {@link DataChangeHandler} to use with the {@link DataSource}. + *

+ * This method is called from the constructor. * * @return data change handler */ diff --git a/server/src/com/vaadin/server/communication/data/typed/SimpleDataProvider.java b/server/src/com/vaadin/server/communication/data/typed/SimpleDataProvider.java index c82ed119e5..2d40730530 100644 --- a/server/src/com/vaadin/server/communication/data/typed/SimpleDataProvider.java +++ b/server/src/com/vaadin/server/communication/data/typed/SimpleDataProvider.java @@ -65,9 +65,6 @@ public class SimpleDataProvider extends DataProvider { private boolean reset = false; private final Set updatedData = new HashSet(); - // TODO: Allow customizing the used key mapper - private DataKeyMapper keyMapper = new KeyMapper(); - /** * Creates a new DataProvider with the given Collection. * @@ -105,11 +102,6 @@ public class SimpleDataProvider extends DataProvider { updatedData.clear(); } - @Override - protected DataKeyMapper getKeyMapper() { - return keyMapper; - } - /** * Informs the DataProvider that a data object has been added. It is assumed * to be the last object in the collection. @@ -160,6 +152,11 @@ public class SimpleDataProvider extends DataProvider { updatedData.add(data); } + @Override + protected DataKeyMapper createKeyMapper() { + return new KeyMapper(); + } + @Override protected DataRequestRpc createRpc() { return new SimpleDataRequestRpc(); -- cgit v1.2.3