]> source.dussan.org Git - vaadin-framework.git/blob
773392d0b47187ca6a1df4ca9d890ee81b7f15f2
[vaadin-framework.git] /
1 /*
2  * Copyright 2000-2018 Vaadin Ltd.
3  *
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
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
14  * the License.
15  */
16 package com.vaadin.v7.client.connectors;
17
18 import java.util.logging.Logger;
19
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;
34
35 import elemental.json.JsonObject;
36
37 /**
38  * Connector for server-side {@link SingleSelectionModel}.
39  *
40  * @since 7.6
41  * @author Vaadin Ltd
42  */
43 @Connect(SingleSelectionModel.class)
44 public class SingleSelectionModelConnector extends
45         AbstractSelectionModelConnector<SelectionModel.Single<JsonObject>> {
46
47     private SpaceSelectHandler<JsonObject> spaceHandler;
48     private ClickSelectHandler<JsonObject> clickHandler;
49     private Single<JsonObject> selectionModel = createSelectionModel();
50
51     @Override
52     protected void extend(ServerConnector target) {
53         getGrid().setSelectionModel(selectionModel);
54         spaceHandler = new SpaceSelectHandler<JsonObject>(getGrid());
55         clickHandler = new ClickSelectHandler<JsonObject>(getGrid());
56     }
57
58     @Override
59     public SingleSelectionModelState getState() {
60         return (SingleSelectionModelState) super.getState();
61     }
62
63     @Override
64     public void onUnregister() {
65         spaceHandler.removeHandler();
66         clickHandler.removeHandler();
67
68         super.onUnregister();
69     }
70
71     @Override
72     protected Single<JsonObject> createSelectionModel() {
73         return new SingleSelectionModel();
74     }
75
76     @OnStateChange("deselectAllowed")
77     void updateDeselectAllowed() {
78         selectionModel.setDeselectAllowed(getState().deselectAllowed);
79     }
80
81     @OnStateChange("userSelectionAllowed")
82     void updateUserSelectionAllowed() {
83
84         if (selectionModel instanceof HasUserSelectionAllowed) {
85             ((HasUserSelectionAllowed) selectionModel)
86                     .setUserSelectionAllowed(getState().userSelectionAllowed);
87         } else {
88             getLogger().warning("userSelectionAllowed set to "
89                     + getState().userSelectionAllowed
90                     + " but the selection model does not implement "
91                     + HasUserSelectionAllowed.class.getSimpleName());
92         }
93     }
94
95     private static Logger getLogger() {
96         return Logger.getLogger(SingleSelectionModelConnector.class.getName());
97     }
98
99     /**
100      * SingleSelectionModel without a selection column renderer.
101      */
102     public class SingleSelectionModel extends AbstractSelectionModel
103             implements SelectionModel.Single<JsonObject>,
104             HasUserSelectionAllowed<JsonObject> {
105
106         private RowHandle<JsonObject> selectedRow;
107         private boolean deselectAllowed;
108         private boolean userSelectionAllowed = true;
109
110         @Override
111         public Renderer<Boolean> getSelectionColumnRenderer() {
112             return null;
113         }
114
115         @Override
116         public void reset() {
117             super.reset();
118
119             // Clean up selected row
120             if (selectedRow != null) {
121                 clearSelectedRow();
122             }
123         }
124
125         @Override
126         public boolean select(JsonObject row) {
127             boolean changed = false;
128
129             if (row == null && !isDeselectAllowed()) {
130                 // Attempting to deselect, even though it's not allowed.
131             } else {
132                 if (selectedRow != null) {
133                     // Check if currently re-selected row was deselected from
134                     // the server.
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.
139                             return false;
140                         }
141                     }
142
143                     // Remove old selected row
144                     clearSelectedRow();
145                     changed = true;
146                 }
147
148                 if (row != null) {
149                     // Select the new row.
150                     setSelectedRow(row);
151                     changed = true;
152                 }
153             }
154
155             if (changed) {
156                 getRpcProxy(SingleSelectionModelServerRpc.class)
157                         .select(getRowKey(row));
158             }
159
160             return changed;
161         }
162
163         private void setSelectedRow(JsonObject row) {
164             selectedRow = getRowHandle(row);
165             selectedRow.pin();
166             selectedRow.getRow().put(GridState.JSONKEY_SELECTED, true);
167             selectedRow.updateRow();
168         }
169
170         private void clearSelectedRow() {
171             selectedRow.getRow().remove(GridState.JSONKEY_SELECTED);
172             selectedRow.updateRow();
173             selectedRow.unpin();
174             selectedRow = null;
175         }
176
177         @Override
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);
184                 selectedRow.pin();
185
186                 return select(null);
187             }
188             return false;
189         }
190
191         @Override
192         public JsonObject getSelectedRow() {
193             throw new UnsupportedOperationException(
194                     "This client-side selection model "
195                             + getClass().getSimpleName()
196                             + " does not know selected row.");
197         }
198
199         @Override
200         public void setDeselectAllowed(boolean deselectAllowed) {
201             this.deselectAllowed = deselectAllowed;
202         }
203
204         @Override
205         public boolean isDeselectAllowed() {
206             return deselectAllowed;
207         }
208
209         @Override
210         public boolean isUserSelectionAllowed() {
211             return userSelectionAllowed;
212         }
213
214         @Override
215         public void setUserSelectionAllowed(boolean userSelectionAllowed) {
216             this.userSelectionAllowed = userSelectionAllowed;
217         }
218     }
219 }