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 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2000-2016 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.grid;
  17. import com.vaadin.client.widget.grid.events.GridSelectionAllowedEvent;
  18. import com.vaadin.client.widget.grid.selection.ClickSelectHandler;
  19. import com.vaadin.client.widget.grid.selection.SelectionModel;
  20. import com.vaadin.shared.data.DataCommunicatorConstants;
  21. import com.vaadin.shared.data.selection.SelectionServerRpc;
  22. import com.vaadin.shared.ui.Connect;
  23. import elemental.json.JsonObject;
  24. /**
  25. * Client side connector for grid single selection model.
  26. *
  27. * @author Vaadin Ltd.
  28. *
  29. * @since 8.0
  30. */
  31. @Connect(com.vaadin.ui.components.grid.SingleSelectionModelImpl.class)
  32. public class SingleSelectionModelConnector
  33. extends AbstractSelectionModelConnector {
  34. private ClickSelectHandler<JsonObject> clickSelectHandler;
  35. /**
  36. * Single selection model for grid.
  37. */
  38. protected class SingleSelectionModel implements SelectionModel<JsonObject> {
  39. private boolean isSelectionAllowed = true;
  40. @Override
  41. public void select(JsonObject item) {
  42. getRpcProxy(SelectionServerRpc.class)
  43. .select(item.getString(DataCommunicatorConstants.KEY));
  44. }
  45. @Override
  46. public void deselect(JsonObject item) {
  47. getRpcProxy(SelectionServerRpc.class)
  48. .deselect(item.getString(DataCommunicatorConstants.KEY));
  49. }
  50. @Override
  51. public boolean isSelected(JsonObject item) {
  52. return SingleSelectionModelConnector.this.isSelected(item);
  53. }
  54. @Override
  55. public void deselectAll() {
  56. getRpcProxy(SelectionServerRpc.class).select(null);
  57. }
  58. @Override
  59. public void setSelectionAllowed(boolean selectionAllowed) {
  60. isSelectionAllowed = selectionAllowed;
  61. getGrid()
  62. .fireEvent(new GridSelectionAllowedEvent(selectionAllowed));
  63. }
  64. @Override
  65. public boolean isSelectionAllowed() {
  66. return isSelectionAllowed;
  67. }
  68. }
  69. @Override
  70. protected void initSelectionModel() {
  71. getGrid().setSelectionModel(new SingleSelectionModel());
  72. clickSelectHandler = new ClickSelectHandler<>(getParent().getWidget());
  73. }
  74. @Override
  75. public void onUnregister() {
  76. super.onUnregister();
  77. if (clickSelectHandler != null) {
  78. clickSelectHandler.removeHandler();
  79. }
  80. }
  81. }