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.

AbstractSelectionModel.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.ui.components.grid;
  17. import com.vaadin.shared.data.DataCommunicatorConstants;
  18. import com.vaadin.shared.ui.grid.AbstractSelectionModelState;
  19. import com.vaadin.ui.AbstractListing;
  20. import com.vaadin.ui.Grid;
  21. import com.vaadin.ui.Grid.AbstractGridExtension;
  22. import elemental.json.JsonObject;
  23. /**
  24. * Abstract selection model for grid.
  25. *
  26. * @author Vaadin Ltd
  27. *
  28. * @since 8.0
  29. *
  30. * @param <T>
  31. * the type of the items in grid.
  32. */
  33. public abstract class AbstractSelectionModel<T> extends AbstractGridExtension<T>
  34. implements GridSelectionModel<T> {
  35. @Override
  36. public void generateData(T item, JsonObject jsonObject) {
  37. if (isSelected(item)) {
  38. // Pre-emptive update in case used a stale element in selection.
  39. refreshData(item);
  40. jsonObject.put(DataCommunicatorConstants.SELECTED, true);
  41. }
  42. }
  43. @Override
  44. public void destroyAllData() {
  45. deselectAll();
  46. }
  47. @Override
  48. protected AbstractSelectionModelState getState() {
  49. return (AbstractSelectionModelState) super.getState();
  50. }
  51. @Override
  52. protected AbstractSelectionModelState getState(boolean markAsDirty) {
  53. return (AbstractSelectionModelState) super.getState(markAsDirty);
  54. }
  55. /**
  56. * Returns the grid this selection model is attached to using, or throws
  57. * {@link IllegalStateException} if not attached to any selection model.
  58. *
  59. * @return the grid this selection model is attached to
  60. * @throws IllegalStateException
  61. * if this selection mode is not attached to any grid
  62. */
  63. protected Grid<T> getGrid() throws IllegalStateException {
  64. Grid<T> parent = getParent();
  65. if (parent == null) {
  66. throw new IllegalStateException(
  67. "This selection model is no currently attached to any grid.");
  68. }
  69. return parent;
  70. }
  71. @Override
  72. public void extend(AbstractListing<T> grid) {
  73. if (getParent() != null) {
  74. throw new IllegalStateException(
  75. "This selection model has been bound to a grid already. Please remove the existing binding with model.remove() first.");
  76. }
  77. // type is verified in parent
  78. super.extend(grid);
  79. init();
  80. }
  81. /**
  82. * Initializes the selection model after it has been attached to a grid.
  83. */
  84. protected abstract void init();
  85. @Override
  86. public void remove() {
  87. deselectAll();
  88. super.remove();
  89. }
  90. @Override
  91. public boolean isUserSelectionAllowed() {
  92. return getState(false).selectionAllowed;
  93. }
  94. @Override
  95. public void setUserSelectionAllowed(boolean allowed) {
  96. getState().selectionAllowed = allowed;
  97. }
  98. }