2 * Copyright 2000-2018 Vaadin Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.vaadin.v7.client.connectors;
18 import java.util.logging.Logger;
20 import com.vaadin.client.ServerConnector;
21 import com.vaadin.client.annotations.OnStateChange;
22 import com.vaadin.client.data.DataSource.RowHandle;
23 import com.vaadin.shared.ui.Connect;
24 import com.vaadin.v7.client.renderers.Renderer;
25 import com.vaadin.v7.client.widget.grid.selection.ClickSelectHandler;
26 import com.vaadin.v7.client.widget.grid.selection.HasUserSelectionAllowed;
27 import com.vaadin.v7.client.widget.grid.selection.SelectionModel;
28 import com.vaadin.v7.client.widget.grid.selection.SelectionModel.Single;
29 import com.vaadin.v7.client.widget.grid.selection.SpaceSelectHandler;
30 import com.vaadin.v7.shared.ui.grid.GridState;
31 import com.vaadin.v7.shared.ui.grid.selection.SingleSelectionModelServerRpc;
32 import com.vaadin.v7.shared.ui.grid.selection.SingleSelectionModelState;
33 import com.vaadin.v7.ui.Grid.SingleSelectionModel;
35 import elemental.json.JsonObject;
38 * Connector for server-side {@link SingleSelectionModel}.
43 @Connect(SingleSelectionModel.class)
44 public class SingleSelectionModelConnector extends
45 AbstractSelectionModelConnector<SelectionModel.Single<JsonObject>> {
47 private SpaceSelectHandler<JsonObject> spaceHandler;
48 private ClickSelectHandler<JsonObject> clickHandler;
49 private Single<JsonObject> selectionModel = createSelectionModel();
52 protected void extend(ServerConnector target) {
53 getGrid().setSelectionModel(selectionModel);
54 spaceHandler = new SpaceSelectHandler<JsonObject>(getGrid());
55 clickHandler = new ClickSelectHandler<JsonObject>(getGrid());
59 public SingleSelectionModelState getState() {
60 return (SingleSelectionModelState) super.getState();
64 public void onUnregister() {
65 spaceHandler.removeHandler();
66 clickHandler.removeHandler();
72 protected Single<JsonObject> createSelectionModel() {
73 return new SingleSelectionModel();
76 @OnStateChange("deselectAllowed")
77 void updateDeselectAllowed() {
78 selectionModel.setDeselectAllowed(getState().deselectAllowed);
81 @OnStateChange("userSelectionAllowed")
82 void updateUserSelectionAllowed() {
84 if (selectionModel instanceof HasUserSelectionAllowed) {
85 ((HasUserSelectionAllowed) selectionModel)
86 .setUserSelectionAllowed(getState().userSelectionAllowed);
88 getLogger().warning("userSelectionAllowed set to "
89 + getState().userSelectionAllowed
90 + " but the selection model does not implement "
91 + HasUserSelectionAllowed.class.getSimpleName());
95 private static Logger getLogger() {
96 return Logger.getLogger(SingleSelectionModelConnector.class.getName());
100 * SingleSelectionModel without a selection column renderer.
102 public class SingleSelectionModel extends AbstractSelectionModel
103 implements SelectionModel.Single<JsonObject>,
104 HasUserSelectionAllowed<JsonObject> {
106 private RowHandle<JsonObject> selectedRow;
107 private boolean deselectAllowed;
108 private boolean userSelectionAllowed = true;
111 public Renderer<Boolean> getSelectionColumnRenderer() {
116 public void reset() {
119 // Clean up selected row
120 if (selectedRow != null) {
126 public boolean select(JsonObject row) {
127 boolean changed = false;
129 if (row == null && !isDeselectAllowed()) {
130 // Attempting to deselect, even though it's not allowed.
132 if (selectedRow != null) {
133 // Check if currently re-selected row was deselected from
135 if (row != null && getRowHandle(row).equals(selectedRow)) {
136 if (selectedRow.getRow()
137 .hasKey(GridState.JSONKEY_SELECTED)) {
138 // Everything is OK, no need to do anything.
143 // Remove old selected row
149 // Select the new row.
156 getRpcProxy(SingleSelectionModelServerRpc.class)
157 .select(getRowKey(row));
163 private void setSelectedRow(JsonObject row) {
164 selectedRow = getRowHandle(row);
166 selectedRow.getRow().put(GridState.JSONKEY_SELECTED, true);
167 selectedRow.updateRow();
170 private void clearSelectedRow() {
171 selectedRow.getRow().remove(GridState.JSONKEY_SELECTED);
172 selectedRow.updateRow();
178 public boolean deselect(JsonObject row) {
179 if (isSelected(row)) {
180 // If no selection has happened client side, then selectedRow is
181 // null but must be set so that a deselection event with the
182 // correct key can be sent to the server
183 selectedRow = getRowHandle(row);
192 public JsonObject getSelectedRow() {
193 throw new UnsupportedOperationException(
194 "This client-side selection model "
195 + getClass().getSimpleName()
196 + " does not know selected row.");
200 public void setDeselectAllowed(boolean deselectAllowed) {
201 this.deselectAllowed = deselectAllowed;
205 public boolean isDeselectAllowed() {
206 return deselectAllowed;
210 public boolean isUserSelectionAllowed() {
211 return userSelectionAllowed;
215 public void setUserSelectionAllowed(boolean userSelectionAllowed) {
216 this.userSelectionAllowed = userSelectionAllowed;