]> source.dussan.org Git - vaadin-framework.git/blob
e75cb3230105f97b2076eaf6fc3a0ced50a40e1a
[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.widget.grid.selection;
17
18 import java.util.Collection;
19 import java.util.Collections;
20
21 import com.vaadin.client.data.DataSource.RowHandle;
22 import com.vaadin.v7.client.renderers.Renderer;
23 import com.vaadin.v7.client.widgets.Grid;
24
25 /**
26  * Single-row selection model.
27  *
28  * @author Vaadin Ltd
29  * @since 7.4
30  */
31 public class SelectionModelSingle<T> extends AbstractRowHandleSelectionModel<T>
32         implements SelectionModel.Single<T>, HasUserSelectionAllowed<T> {
33
34     private Grid<T> grid;
35     private RowHandle<T> selectedRow;
36
37     /** Event handling for selection with space key */
38     private SpaceSelectHandler<T> spaceSelectHandler;
39
40     /** Event handling for selection by clicking cells */
41     private ClickSelectHandler<T> clickSelectHandler;
42
43     private boolean deselectAllowed = true;
44     private boolean userSelectionAllowed = true;
45
46     @Override
47     public boolean isSelected(T row) {
48         return selectedRow != null
49                 && selectedRow.equals(grid.getDataSource().getHandle(row));
50     }
51
52     @Override
53     public Renderer<Boolean> getSelectionColumnRenderer() {
54         // No Selection column renderer for single selection
55         return null;
56     }
57
58     @Override
59     public void setGrid(Grid<T> grid) {
60         if (this.grid != null && grid != null) {
61             // Trying to replace grid
62             throw new IllegalStateException(
63                     "Selection model is already attached to a grid. "
64                             + "Remove the selection model first from "
65                             + "the grid and then add it.");
66         }
67
68         this.grid = grid;
69         if (this.grid != null) {
70             spaceSelectHandler = new SpaceSelectHandler<T>(grid);
71             clickSelectHandler = new ClickSelectHandler<T>(grid);
72             updateHandlerDeselectAllowed();
73         } else {
74             spaceSelectHandler.removeHandler();
75             clickSelectHandler.removeHandler();
76             spaceSelectHandler = null;
77             clickSelectHandler = null;
78         }
79     }
80
81     @Override
82     public boolean select(T row) {
83
84         if (row == null) {
85             throw new IllegalArgumentException("Row cannot be null");
86         }
87
88         T removed = getSelectedRow();
89         if (selectByHandle(grid.getDataSource().getHandle(row))) {
90             grid.fireEvent(new SelectionEvent<T>(grid, row, removed, false));
91
92             return true;
93         }
94         return false;
95     }
96
97     @Override
98     public boolean deselect(T row) {
99
100         if (row == null) {
101             throw new IllegalArgumentException("Row cannot be null");
102         }
103
104         if (isSelected(row)) {
105             deselectByHandle(selectedRow);
106             grid.fireEvent(new SelectionEvent<T>(grid, null, row, false));
107             return true;
108         }
109
110         return false;
111     }
112
113     @Override
114     public T getSelectedRow() {
115         return (selectedRow != null ? selectedRow.getRow() : null);
116     }
117
118     @Override
119     public void reset() {
120         if (selectedRow != null) {
121             deselect(getSelectedRow());
122         }
123     }
124
125     @Override
126     public Collection<T> getSelectedRows() {
127         if (getSelectedRow() != null) {
128             return Collections.singleton(getSelectedRow());
129         }
130         return Collections.emptySet();
131     }
132
133     @Override
134     protected boolean selectByHandle(RowHandle<T> handle) {
135         if (handle != null && !handle.equals(selectedRow)) {
136             deselectByHandle(selectedRow);
137             selectedRow = handle;
138             selectedRow.pin();
139             return true;
140         } else {
141             return false;
142         }
143     }
144
145     @Override
146     protected boolean deselectByHandle(RowHandle<T> handle) {
147         if (handle != null && handle.equals(selectedRow)) {
148             selectedRow.unpin();
149             selectedRow = null;
150             return true;
151         } else {
152             return false;
153         }
154     }
155
156     @Override
157     public void setDeselectAllowed(boolean deselectAllowed) {
158         this.deselectAllowed = deselectAllowed;
159         updateHandlerDeselectAllowed();
160     }
161
162     @Override
163     public boolean isDeselectAllowed() {
164         return deselectAllowed;
165     }
166
167     private void updateHandlerDeselectAllowed() {
168         if (spaceSelectHandler != null) {
169             spaceSelectHandler.setDeselectAllowed(deselectAllowed);
170         }
171         if (clickSelectHandler != null) {
172             clickSelectHandler.setDeselectAllowed(deselectAllowed);
173         }
174     }
175
176     @Override
177     public boolean isUserSelectionAllowed() {
178         return userSelectionAllowed;
179     }
180
181     @Override
182     public void setUserSelectionAllowed(boolean userSelectionAllowed) {
183         this.userSelectionAllowed = userSelectionAllowed;
184     }
185 }