Kaynağa Gözat

Move DataSource handling to DataCommunicator

Change-Id: I56702d91a5e028341d8249ad3ecf5e79dea0be00
feature/vaadin8-book-vol2
Teemu Suo-Anttila 7 yıl önce
ebeveyn
işleme
1764ad78de

+ 30
- 8
server/src/main/java/com/vaadin/tokka/server/communication/data/DataCommunicator.java Dosyayı Görüntüle

@@ -30,7 +30,6 @@ import com.vaadin.shared.data.DataRequestRpc;
import com.vaadin.shared.tokka.data.DataCommunicatorClientRpc;
import com.vaadin.shared.tokka.data.DataProviderConstants;
import com.vaadin.shared.ui.grid.Range;
import com.vaadin.tokka.event.Registration;

import elemental.json.Json;
import elemental.json.JsonArray;
@@ -172,16 +171,15 @@ public class DataCommunicator<T> extends AbstractExtension {
protected ActiveDataHandler handler = new ActiveDataHandler();
protected DataCommunicatorClientRpc rpc;

protected DataSource<T, ?> dataSource;
private DataSource<T, ?> dataSource;
private DataKeyMapper<T> keyMapper;

private boolean reset = false;
private final Set<T> updatedData = new HashSet<T>();
private Range pushRows = Range.withLength(0, 40);

public DataCommunicator(DataSource<T, ?> dataSource) {
public DataCommunicator() {
addDataGenerator(handler);
this.dataSource = dataSource;
rpc = getRpcProxy(DataCommunicatorClientRpc.class);
registerRpc(createRpc());
keyMapper = createKeyMapper();
@@ -195,13 +193,17 @@ public class DataCommunicator<T> extends AbstractExtension {
public void beforeClientResponse(boolean initial) {
super.beforeClientResponse(initial);

if (getDataSource() == null) {
return;
}

// FIXME: Sorting and Filtering with Backend
List<Object> sortOrders = Collections.emptyList();
Set<Object> filters = Collections.emptySet();

if (initial || reset) {
// FIXME: Rethink the size question.
int dataSourceSize = (int) dataSource.apply(
int dataSourceSize = (int) getDataSource().apply(
new Query(0, Integer.MAX_VALUE, sortOrders, filters))
.count();
rpc.reset(dataSourceSize);
@@ -213,14 +215,14 @@ public class DataCommunicator<T> extends AbstractExtension {

Stream<T> rowsToPush;

if (dataSource instanceof InMemoryDataSource) {
if (getDataSource() instanceof InMemoryDataSource) {
// We can safely request all the data when in memory
// FIXME: sorted and filter.
rowsToPush = dataSource.apply(new Query()).skip(offset)
rowsToPush = getDataSource().apply(new Query()).skip(offset)
.limit(limit);
} else {
Query query = new Query(offset, limit, sortOrders, filters);
rowsToPush = dataSource.apply(query);
rowsToPush = getDataSource().apply(query);
}

pushData(offset, rowsToPush);
@@ -401,4 +403,24 @@ public class DataCommunicator<T> extends AbstractExtension {
protected DataRequestRpc createRpc() {
return new SimpleDataRequestRpc();
}

/**
* Gets the current data source from this DataCommunicator.
*
* @return the data source
*/
public DataSource<T, ?> getDataSource() {
return dataSource;
}

/**
* Sets the current data source for this DataCommunicator.
*
* @param dataSource
* the data source to set
*/
public void setDataSource(DataSource<T, ?> dataSource) {
this.dataSource = dataSource;
reset();
}
}

+ 40
- 51
server/src/main/java/com/vaadin/tokka/ui/components/AbstractListing.java Dosyayı Görüntüle

@@ -15,19 +15,18 @@
*/
package com.vaadin.tokka.ui.components;

import java.util.LinkedHashSet;
import java.util.Set;

import com.vaadin.server.AbstractExtension;
import com.vaadin.tokka.server.ListingExtension;
import com.vaadin.tokka.server.communication.data.DataCommunicator;
import com.vaadin.tokka.server.communication.data.DataSource;
import com.vaadin.tokka.server.communication.data.SelectionModel;
import com.vaadin.tokka.server.communication.data.TypedDataGenerator;
import com.vaadin.ui.AbstractComponent;

/**
* Base class for Listing components. Provides common handling for
* {@link DataCommunicator}, {@link SelectionModel} and {@link TypedDataGenerator}s.
* {@link DataCommunicator}, {@link SelectionModel} and
* {@link TypedDataGenerator}s.
*
* @param <T>
* listing data type
@@ -83,7 +82,8 @@ public abstract class AbstractListing<T> extends AbstractComponent implements
* @return the data object
*/
protected T getData(String key) {
DataCommunicator<T> dataProvider = getParent().getDataCommunicator();
DataCommunicator<T> dataProvider = getParent()
.getDataCommunicator();
if (dataProvider != null) {
return dataProvider.getKeyMapper().get(key);
}
@@ -103,7 +103,8 @@ public abstract class AbstractListing<T> extends AbstractComponent implements
* data object to refresh
*/
protected void refresh(T data) {
DataCommunicator<T> dataProvider = getParent().getDataCommunicator();
DataCommunicator<T> dataProvider = getParent()
.getDataCommunicator();
if (dataProvider != null) {
dataProvider.refresh(data);
}
@@ -111,69 +112,48 @@ public abstract class AbstractListing<T> extends AbstractComponent implements
}

/* DataProvider for this Listing component */
private DataCommunicator<T> dataCommunicator;
/* TypedDataGenerators used by this Listing */
private Set<TypedDataGenerator<T>> generators = new LinkedHashSet<>();
private final DataCommunicator<T> dataCommunicator;
/* SelectionModel for this Listing */
private SelectionModel<T> selectionModel;

/**
* Adds a {@link TypedDataGenerator} for the {@link DataCommunicator} of this
* Listing component.
*
* @param generator
* typed data generator
* Constructs a new AbstractListing with a default {@link DataCommunicator}.
*/
protected void addDataGenerator(TypedDataGenerator<T> generator) {
generators.add(generator);
protected AbstractListing() {
this(new DataCommunicator<T>());
}

if (dataCommunicator != null) {
dataCommunicator.addDataGenerator(generator);
}
/**
* Constructs a new AbstractListing with given {@link DataCommunicator}.
*
* @param dataCommunicator
* data communicator to use
*/
protected AbstractListing(DataCommunicator<T> dataCommunicator) {
this.dataCommunicator = dataCommunicator;
addExtension(dataCommunicator);
}

/**
* Removed a {@link TypedDataGenerator} from the {@link DataCommunicator} of
* Adds a {@link TypedDataGenerator} for the {@link DataCommunicator} of
* this Listing component.
*
* @param generator
* typed data generator
*/
protected void removeDataGenerator(TypedDataGenerator<T> generator) {
generators.remove(generator);

if (dataCommunicator != null) {
dataCommunicator.removeDataGenerator(generator);
}
protected void addDataGenerator(TypedDataGenerator<T> generator) {
dataCommunicator.addDataGenerator(generator);
}

/**
* Extends this listing component with a data provider. This method
* reapplies all data generators to the new data provider.
* Removed a {@link TypedDataGenerator} from the {@link DataCommunicator} of
* this Listing component.
*
* @param dataProvider
* new data provider
* @param generator
* typed data generator
*/
protected void setDataCommunicator(DataCommunicator<T> dataProvider) {
if (this.dataCommunicator == dataProvider) {
return;
}

if (this.dataCommunicator != null) {
this.dataCommunicator.remove();
}

this.dataCommunicator = dataProvider;
if (dataProvider != null) {
addExtension(dataProvider);

if (dataProvider != null) {
// Reapply all data generators to the new data provider.
for (TypedDataGenerator<T> generator : generators) {
dataProvider.addDataGenerator(generator);
}
}
}
protected void removeDataGenerator(TypedDataGenerator<T> generator) {
dataCommunicator.removeDataGenerator(generator);
}

/**
@@ -185,7 +165,6 @@ public abstract class AbstractListing<T> extends AbstractComponent implements
return dataCommunicator;
}

@SuppressWarnings("unchecked")
@Override
public void setSelectionModel(SelectionModel<T> model) {
if (selectionModel != null) {
@@ -201,4 +180,14 @@ public abstract class AbstractListing<T> extends AbstractComponent implements
public SelectionModel<T> getSelectionModel() {
return selectionModel;
}

@Override
public void setDataSource(DataSource<T, ?> data) {
getDataCommunicator().setDataSource(data);
}

@Override
public DataSource<T, ?> getDataSource() {
return getDataCommunicator().getDataSource();
}
}

+ 0
- 12
server/src/main/java/com/vaadin/tokka/ui/components/grid/Grid.java Dosyayı Görüntüle

@@ -19,7 +19,6 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;

import com.vaadin.tokka.server.communication.data.DataCommunicator;
import com.vaadin.tokka.server.communication.data.DataSource;
import com.vaadin.tokka.server.communication.data.SingleSelection;
import com.vaadin.tokka.ui.components.AbstractListing;
@@ -33,17 +32,6 @@ public class Grid<T> extends AbstractListing<T> {
setSelectionModel(new SingleSelection<T>());
}

@Override
public void setDataSource(DataSource<T, ?> data) {
this.dataSource = data;
setDataCommunicator(new DataCommunicator<>(data));
}

@Override
public DataSource<T, ?> getDataSource() {
return dataSource;
}

public <V> Column<T, V> addColumn(String caption, Function<T, V> getter) {
Column<T, V> c = new Column<T, V>(caption, getter, this);
columns.put(c.getConnectorId(), c);

+ 0
- 17
server/src/main/java/com/vaadin/tokka/ui/components/nativeselect/NativeSelect.java Dosyayı Görüntüle

@@ -18,7 +18,6 @@ package com.vaadin.tokka.ui.components.nativeselect;
import java.util.function.Function;

import com.vaadin.shared.tokka.data.DataProviderConstants;
import com.vaadin.tokka.server.communication.data.DataCommunicator;
import com.vaadin.tokka.server.communication.data.DataSource;
import com.vaadin.tokka.server.communication.data.SingleSelection;
import com.vaadin.tokka.server.communication.data.TypedDataGenerator;
@@ -28,7 +27,6 @@ import elemental.json.JsonObject;

public class NativeSelect<T> extends AbstractListing<T> {

private DataSource<T, ?> dataSource;
private Function<T, String> nameProvider = T::toString;

public NativeSelect() {
@@ -51,19 +49,4 @@ public class NativeSelect<T> extends AbstractListing<T> {
this();
setDataSource(dataSource);
}

@Override
public void setDataSource(DataSource<T, ?> data) {
dataSource = data;
if (dataSource != null) {
setDataCommunicator(new DataCommunicator<>(dataSource));
} else {
setDataCommunicator(null);
}
}

@Override
public DataSource<T, ?> getDataSource() {
return dataSource;
}
}

+ 0
- 13
server/src/test/java/com/vaadin/tokka/ui/components/AbstractListingTest.java Dosyayı Görüntüle

@@ -28,19 +28,6 @@ public class AbstractListingTest {
}

AbstractListing<String> testComponent = new AbstractListing<String>() {

DataSource<String, ?> data;

@Override
public void setDataSource(DataSource<String, ?> data) {
this.data = data;
setDataCommunicator(new DataCommunicator<>(data));
}

@Override
public DataSource<String, ?> getDataSource() {
return data;
}
};

@Before

Loading…
İptal
Kaydet