import com.vaadin.client.connectors.data.HasDataSource;
import com.vaadin.client.data.DataSource;
import com.vaadin.client.ui.AbstractComponentConnector;
+import com.vaadin.shared.data.DataCommunicatorConstants;
import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.ui.AbstractListing;
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 {
private DataSource<JsonObject> dataSource = null;
- private SelectionModel<JsonObject> selectionModel = null;
+ private SELECTIONMODEL selectionModel = null;
@Override
public void setDataSource(DataSource<JsonObject> dataSource) {
* @param selectionModel
* the selection model or null to disable
*/
- public void setSelectionModel(SelectionModel<JsonObject> selectionModel) {
+ public void setSelectionModel(SELECTIONMODEL selectionModel) {
this.selectionModel = selectionModel;
}
- public SelectionModel<JsonObject> getSelectionModel() {
+ /**
+ * Returns the selection model instance used.
+ *
+ * @return the selection model
+ */
+ public SELECTIONMODEL getSelectionModel() {
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);
+ }
}
* @since
*/
@Connect(com.vaadin.ui.Grid.class)
-public class GridConnector extends AbstractListingConnector
+public class GridConnector
+ extends AbstractListingConnector<SelectionModel<JsonObject>>
implements HasComponentsConnector, SimpleManagedLayout, DeferredWorker {
/* Map to keep track of all added columns */
private ClickSelectHandler<JsonObject> clickSelectHandler;
@Override
+ @SuppressWarnings("unchecked")
public Grid<JsonObject> getWidget() {
return (Grid<JsonObject>) super.getWidget();
}
* The client-side connector for selection extensions.
*
* @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 {
- private SelectionModel<JsonObject> model = null;
+ private SELECTIONMODEL model = null;
@Override
+ @SuppressWarnings("unchecked")
protected void extend(ServerConnector target) {
if (!(target instanceof AbstractListingConnector)) {
throw new IllegalArgumentException(
+ AbstractListingConnector.class.getSimpleName());
}
model = createSelectionModel();
- ((AbstractListingConnector) target).setSelectionModel(model);
+ ((AbstractListingConnector<SELECTIONMODEL>) target)
+ .setSelectionModel(model);
}
/**
*
* @return created selection model
*/
- protected abstract SelectionModel<JsonObject> createSelectionModel();
+ protected abstract SELECTIONMODEL createSelectionModel();
@Override
- public AbstractListingConnector getParent() {
- return (AbstractListingConnector) super.getParent();
+ @SuppressWarnings("unchecked")
+ public AbstractListingConnector<SELECTIONMODEL> getParent() {
+ return (AbstractListingConnector<SELECTIONMODEL>) super.getParent();
}
/**
*
* @return the selection model in use
*/
- protected SelectionModel<JsonObject> getSelectionModel() {
+ protected SELECTIONMODEL getSelectionModel() {
return model;
}
* @author Vaadin Ltd.
*/
@Connect(com.vaadin.data.selection.SingleSelection.class)
-public class SingleSelectionConnector extends AbstractSelectionConnector {
+public class SingleSelectionConnector extends
+ AbstractSelectionConnector<SelectionModel.Single<JsonObject>> {
private static class SingleSelection
implements SelectionModel.Single<JsonObject> {
}
}
- private AbstractListingConnector parent;
+ private AbstractListingConnector<?> parent;
@Override
public void onUnregister() {
}
@Override
- protected SelectionModel<JsonObject> createSelectionModel() {
+ protected SingleSelection createSelectionModel() {
return new SingleSelection(getRpcProxy(SelectionServerRpc.class));
}
}
import com.vaadin.client.connectors.AbstractListingConnector;
import com.vaadin.client.data.DataSource;
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.tests.data.DummyData.DummyComponent;
import elemental.json.JsonObject;
@Connect(DummyComponent.class)
-public class DummyComponentConnector extends AbstractListingConnector {
+public class DummyComponentConnector extends
+ AbstractListingConnector<SelectionModel<?>> {
@Override
public FlowPanel getWidget() {
VLabel label = new VLabel();
getWidget().add(label);
JsonObject row = dataSource.getRow(i);
- String text = row.getString(DataCommunicatorConstants.DATA);
- if (row.hasKey(DataCommunicatorConstants.SELECTED)
- && row.getBoolean(DataCommunicatorConstants.SELECTED)) {
+ String text = getRowData(row).asString();
+ if (isRowSelected(row)) {
text = "<b>" + text + "</b>";
label.addStyleName("selected");
}