You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SingleSelectionModelConnector.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2000-2014 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.client.connectors;
  17. import com.vaadin.client.ServerConnector;
  18. import com.vaadin.client.annotations.OnStateChange;
  19. import com.vaadin.client.data.DataSource.RowHandle;
  20. import com.vaadin.client.renderers.Renderer;
  21. import com.vaadin.client.widget.grid.selection.ClickSelectHandler;
  22. import com.vaadin.client.widget.grid.selection.SelectionModel;
  23. import com.vaadin.client.widget.grid.selection.SelectionModel.Single;
  24. import com.vaadin.client.widget.grid.selection.SpaceSelectHandler;
  25. import com.vaadin.shared.ui.Connect;
  26. import com.vaadin.shared.ui.grid.GridState;
  27. import com.vaadin.shared.ui.grid.selection.SingleSelectionModelServerRpc;
  28. import com.vaadin.shared.ui.grid.selection.SingleSelectionModelState;
  29. import com.vaadin.ui.Grid.SingleSelectionModel;
  30. import elemental.json.JsonObject;
  31. /**
  32. * Connector for server-side {@link SingleSelectionModel}.
  33. *
  34. * @since 7.6
  35. * @author Vaadin Ltd
  36. */
  37. @Connect(SingleSelectionModel.class)
  38. public class SingleSelectionModelConnector extends
  39. AbstractSelectionModelConnector<SelectionModel.Single<JsonObject>> {
  40. private SpaceSelectHandler<JsonObject> spaceHandler;
  41. private ClickSelectHandler<JsonObject> clickHandler;
  42. private Single<JsonObject> selectionModel = createSelectionModel();
  43. @Override
  44. protected void extend(ServerConnector target) {
  45. getGrid().setSelectionModel(selectionModel);
  46. spaceHandler = new SpaceSelectHandler<JsonObject>(getGrid());
  47. clickHandler = new ClickSelectHandler<JsonObject>(getGrid());
  48. }
  49. @Override
  50. public SingleSelectionModelState getState() {
  51. return (SingleSelectionModelState) super.getState();
  52. }
  53. @Override
  54. public void onUnregister() {
  55. spaceHandler.removeHandler();
  56. clickHandler.removeHandler();
  57. super.onUnregister();
  58. }
  59. @Override
  60. protected Single<JsonObject> createSelectionModel() {
  61. return new SingleSelectionModel();
  62. }
  63. @OnStateChange("deselectAllowed")
  64. void updateDeselectAllowed() {
  65. selectionModel.setDeselectAllowed(getState().deselectAllowed);
  66. }
  67. /**
  68. * SingleSelectionModel without a selection column renderer.
  69. */
  70. public class SingleSelectionModel extends AbstractSelectionModel implements
  71. SelectionModel.Single<JsonObject> {
  72. private RowHandle<JsonObject> selectedRow;
  73. private boolean deselectAllowed;
  74. @Override
  75. public Renderer<Boolean> getSelectionColumnRenderer() {
  76. return null;
  77. }
  78. @Override
  79. public boolean select(JsonObject row) {
  80. boolean changed = false;
  81. if ((row == null && isDeselectAllowed())
  82. || (row != null && !getRowHandle(row).equals(selectedRow))) {
  83. if (selectedRow != null) {
  84. selectedRow.getRow().remove(GridState.JSONKEY_SELECTED);
  85. selectedRow.updateRow();
  86. selectedRow.unpin();
  87. selectedRow = null;
  88. changed = true;
  89. }
  90. if (row != null) {
  91. selectedRow = getRowHandle(row);
  92. selectedRow.pin();
  93. selectedRow.getRow().put(GridState.JSONKEY_SELECTED, true);
  94. selectedRow.updateRow();
  95. changed = true;
  96. }
  97. }
  98. if (changed) {
  99. getRpcProxy(SingleSelectionModelServerRpc.class).select(
  100. getRowKey(row));
  101. }
  102. return changed;
  103. }
  104. @Override
  105. public boolean deselect(JsonObject row) {
  106. if (getRowHandle(row).equals(selectedRow)) {
  107. select(null);
  108. }
  109. return false;
  110. }
  111. @Override
  112. public JsonObject getSelectedRow() {
  113. throw new UnsupportedOperationException(
  114. "This client-side selection model "
  115. + getClass().getSimpleName()
  116. + " does not know selected row.");
  117. }
  118. @Override
  119. public void setDeselectAllowed(boolean deselectAllowed) {
  120. this.deselectAllowed = deselectAllowed;
  121. }
  122. @Override
  123. public boolean isDeselectAllowed() {
  124. return deselectAllowed;
  125. }
  126. }
  127. }