Skip to content

Commit

Permalink
Parameterize listing and selection connectors by selection model type
Browse files Browse the repository at this point in the history
Change-Id: I9c31582242b0b37b8a732e41bc73c59881dcf68b
  • Loading branch information
jdahlstrom authored and Vaadin Code Review committed Sep 7, 2016
1 parent 400818b commit d856282
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 24 deletions.
Expand Up @@ -18,22 +18,29 @@
import com.vaadin.client.connectors.data.HasDataSource; import com.vaadin.client.connectors.data.HasDataSource;
import com.vaadin.client.data.DataSource; import com.vaadin.client.data.DataSource;
import com.vaadin.client.ui.AbstractComponentConnector; import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.shared.data.DataCommunicatorConstants;
import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.ui.AbstractListing; import com.vaadin.ui.AbstractListing;


import elemental.json.JsonObject; import elemental.json.JsonObject;
import elemental.json.JsonValue;


/** /**
* Base connector class for {@link AbstractListing}. * A base connector class for {@link AbstractListing}.
* *
* @since * @author Vaadin Ltd.
*
* @param <SELECTIONMODEL>
* the client-side selection model type
*
* @since 8.0
*/ */
public abstract class AbstractListingConnector public abstract class AbstractListingConnector<SELECTIONMODEL extends SelectionModel<?>>
extends AbstractComponentConnector implements HasDataSource { extends AbstractComponentConnector implements HasDataSource {


private DataSource<JsonObject> dataSource = null; private DataSource<JsonObject> dataSource = null;


private SelectionModel<JsonObject> selectionModel = null; private SELECTIONMODEL selectionModel = null;


@Override @Override
public void setDataSource(DataSource<JsonObject> dataSource) { public void setDataSource(DataSource<JsonObject> dataSource) {
Expand All @@ -51,11 +58,49 @@ public DataSource<JsonObject> getDataSource() {
* @param selectionModel * @param selectionModel
* the selection model or null to disable * the selection model or null to disable
*/ */
public void setSelectionModel(SelectionModel<JsonObject> selectionModel) { public void setSelectionModel(SELECTIONMODEL selectionModel) {
this.selectionModel = selectionModel; this.selectionModel = selectionModel;
} }


public SelectionModel<JsonObject> getSelectionModel() { /**
* Returns the selection model instance used.
*
* @return the selection model
*/
public SELECTIONMODEL getSelectionModel() {
return selectionModel; return selectionModel;
} }

/**
* Returns the key of the given data row.
*
* @param row
* the row
* @return the row key
*/
protected static String getRowKey(JsonObject row) {
return row.getString(DataCommunicatorConstants.KEY);
}

/**
* Returns the data of the given data row.
*
* @param row
* the row
* @return the row data
*/
protected static JsonValue getRowData(JsonObject row) {
return row.get(DataCommunicatorConstants.DATA);
}

/**
* Returns whether the given row is selected.
*
* @param row
* the row
* @return {@code true} if the row is selected, {@code false} otherwise
*/
protected boolean isRowSelected(JsonObject row) {
return row.hasKey(DataCommunicatorConstants.SELECTED);
}
} }
Expand Up @@ -52,7 +52,8 @@
* @since * @since
*/ */
@Connect(com.vaadin.ui.Grid.class) @Connect(com.vaadin.ui.Grid.class)
public class GridConnector extends AbstractListingConnector public class GridConnector
extends AbstractListingConnector<SelectionModel<JsonObject>>
implements HasComponentsConnector, SimpleManagedLayout, DeferredWorker { implements HasComponentsConnector, SimpleManagedLayout, DeferredWorker {


/* Map to keep track of all added columns */ /* Map to keep track of all added columns */
Expand All @@ -63,6 +64,7 @@ public class GridConnector extends AbstractListingConnector
private ClickSelectHandler<JsonObject> clickSelectHandler; private ClickSelectHandler<JsonObject> clickSelectHandler;


@Override @Override
@SuppressWarnings("unchecked")
public Grid<JsonObject> getWidget() { public Grid<JsonObject> getWidget() {
return (Grid<JsonObject>) super.getWidget(); return (Grid<JsonObject>) super.getWidget();
} }
Expand Down
Expand Up @@ -27,43 +27,48 @@
* The client-side connector for selection extensions. * The client-side connector for selection extensions.
* *
* @author Vaadin Ltd. * @author Vaadin Ltd.
* *
* @since * @param <SELECTIONMODEL>
* the supported client-side selection model
* @since 8.0
*/ */
public abstract class AbstractSelectionConnector public abstract class AbstractSelectionConnector<SELECTIONMODEL extends SelectionModel<?>>
extends AbstractExtensionConnector { extends AbstractExtensionConnector {


private SelectionModel<JsonObject> model = null; private SELECTIONMODEL model = null;


@Override @Override
@SuppressWarnings("unchecked")
protected void extend(ServerConnector target) { protected void extend(ServerConnector target) {
if (!(target instanceof AbstractListingConnector)) { if (!(target instanceof AbstractListingConnector)) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Cannot extend a connector that is not an " "Cannot extend a connector that is not an "
+ AbstractListingConnector.class.getSimpleName()); + AbstractListingConnector.class.getSimpleName());
} }
model = createSelectionModel(); model = createSelectionModel();
((AbstractListingConnector) target).setSelectionModel(model); ((AbstractListingConnector<SELECTIONMODEL>) target)
.setSelectionModel(model);
} }


/** /**
* Creates a selection model object to be used by the Connector. * Creates a selection model object to be used by the Connector.
* *
* @return created selection model * @return created selection model
*/ */
protected abstract SelectionModel<JsonObject> createSelectionModel(); protected abstract SELECTIONMODEL createSelectionModel();


@Override @Override
public AbstractListingConnector getParent() { @SuppressWarnings("unchecked")
return (AbstractListingConnector) super.getParent(); public AbstractListingConnector<SELECTIONMODEL> getParent() {
return (AbstractListingConnector<SELECTIONMODEL>) super.getParent();
} }


/** /**
* Returns the client-side selection model associated with this connector. * Returns the client-side selection model associated with this connector.
* *
* @return the selection model in use * @return the selection model in use
*/ */
protected SelectionModel<JsonObject> getSelectionModel() { protected SELECTIONMODEL getSelectionModel() {
return model; return model;
} }


Expand Down
Expand Up @@ -31,7 +31,8 @@
* @author Vaadin Ltd. * @author Vaadin Ltd.
*/ */
@Connect(com.vaadin.data.selection.SingleSelection.class) @Connect(com.vaadin.data.selection.SingleSelection.class)
public class SingleSelectionConnector extends AbstractSelectionConnector { public class SingleSelectionConnector extends
AbstractSelectionConnector<SelectionModel.Single<JsonObject>> {


private static class SingleSelection private static class SingleSelection
implements SelectionModel.Single<JsonObject> { implements SelectionModel.Single<JsonObject> {
Expand Down Expand Up @@ -68,7 +69,7 @@ public Optional<JsonObject> getSelectedItem() {
} }
} }


private AbstractListingConnector parent; private AbstractListingConnector<?> parent;


@Override @Override
public void onUnregister() { public void onUnregister() {
Expand All @@ -85,7 +86,7 @@ protected void extend(ServerConnector target) {
} }


@Override @Override
protected SelectionModel<JsonObject> createSelectionModel() { protected SingleSelection createSelectionModel() {
return new SingleSelection(getRpcProxy(SelectionServerRpc.class)); return new SingleSelection(getRpcProxy(SelectionServerRpc.class));
} }
} }
Expand Up @@ -4,14 +4,15 @@
import com.vaadin.client.connectors.AbstractListingConnector; import com.vaadin.client.connectors.AbstractListingConnector;
import com.vaadin.client.data.DataSource; import com.vaadin.client.data.DataSource;
import com.vaadin.client.ui.VLabel; import com.vaadin.client.ui.VLabel;
import com.vaadin.shared.data.DataCommunicatorConstants; import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.Connect;
import com.vaadin.tests.data.DummyData.DummyComponent; import com.vaadin.tests.data.DummyData.DummyComponent;


import elemental.json.JsonObject; import elemental.json.JsonObject;


@Connect(DummyComponent.class) @Connect(DummyComponent.class)
public class DummyComponentConnector extends AbstractListingConnector { public class DummyComponentConnector extends
AbstractListingConnector<SelectionModel<?>> {


@Override @Override
public FlowPanel getWidget() { public FlowPanel getWidget() {
Expand All @@ -30,9 +31,8 @@ public void setDataSource(DataSource<JsonObject> dataSource) {
VLabel label = new VLabel(); VLabel label = new VLabel();
getWidget().add(label); getWidget().add(label);
JsonObject row = dataSource.getRow(i); JsonObject row = dataSource.getRow(i);
String text = row.getString(DataCommunicatorConstants.DATA); String text = getRowData(row).asString();
if (row.hasKey(DataCommunicatorConstants.SELECTED) if (isRowSelected(row)) {
&& row.getBoolean(DataCommunicatorConstants.SELECTED)) {
text = "<b>" + text + "</b>"; text = "<b>" + text + "</b>";
label.addStyleName("selected"); label.addStyleName("selected");
} }
Expand Down

0 comments on commit d856282

Please sign in to comment.