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.

SelectionModel.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2000-2021 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.widget.grid.selection;
  17. import com.vaadin.shared.data.DataCommunicatorConstants;
  18. import elemental.json.JsonObject;
  19. /**
  20. * Models the selection logic of a {@code Grid} component. Determines how items
  21. * can be selected and deselected.
  22. *
  23. * @author Vaadin Ltd.
  24. *
  25. * @param <T>
  26. * the type of the items in the Grid
  27. * @since 8.0
  28. */
  29. public interface SelectionModel<T> {
  30. /**
  31. * Selection model that does not allow any selection.
  32. *
  33. * @param <T>
  34. * the type of the items to select
  35. */
  36. public static class NoSelectionModel<T> implements SelectionModel<T> {
  37. @Override
  38. public void select(T item) {
  39. }
  40. @Override
  41. public void deselect(T item) {
  42. }
  43. @Override
  44. public boolean isSelected(T item) {
  45. return false;
  46. }
  47. @Override
  48. public void deselectAll() {
  49. }
  50. @Override
  51. public void setSelectionAllowed(boolean selectionAllowed) {
  52. }
  53. @Override
  54. public boolean isSelectionAllowed() {
  55. return false;
  56. }
  57. @Override
  58. public boolean isMultiSelectionAllowed() {
  59. return false;
  60. }
  61. }
  62. /**
  63. * Selects the given item. If another item was already selected, that item
  64. * is deselected.
  65. *
  66. * @param item
  67. * the item to select, not null
  68. */
  69. void select(T item);
  70. /**
  71. * Deselects the given item. If the item is not currently selected, does
  72. * nothing.
  73. *
  74. * @param item
  75. * the item to deselect, not null
  76. */
  77. void deselect(T item);
  78. /**
  79. * Returns whether the given item is currently selected.
  80. *
  81. * @param item
  82. * the item to check, not null
  83. * @return {@code true} if the item is selected, {@code false} otherwise
  84. */
  85. boolean isSelected(T item);
  86. /**
  87. * Deselects all currently selected items.
  88. */
  89. void deselectAll();
  90. /**
  91. * Sets whether the user is allowed to change the selection.
  92. * <p>
  93. * The check is done only for the client side actions. It doesn't affect
  94. * selection requests sent from the server side.
  95. *
  96. * @param selectionAllowed
  97. * <code>true</code> if the user is allowed to change the
  98. * selection, <code>false</code> otherwise
  99. */
  100. void setSelectionAllowed(boolean selectionAllowed);
  101. /**
  102. * Checks if the user is allowed to change the selection.
  103. * <p>
  104. * The check is done only for the client side actions. It doesn't affect
  105. * selection requests sent from the server side.
  106. *
  107. * @return <code>true</code> if the user is allowed to change the selection,
  108. * <code>false</code> otherwise
  109. */
  110. boolean isSelectionAllowed();
  111. /**
  112. * Checks if the user is allowed to have more than on item selected.
  113. * <p>
  114. *
  115. * @return <code>true</code> if the user is allowed to select multiple
  116. * items, <code>false</code> otherwise
  117. * @since 8.2
  118. */
  119. boolean isMultiSelectionAllowed();
  120. /**
  121. * Gets the selected state from a given grid row json object. This is a
  122. * helper method for grid selection models.
  123. *
  124. * @param item
  125. * a json object
  126. * @return {@code true} if the json object is marked as selected;
  127. * {@code false} if not
  128. */
  129. public static boolean isItemSelected(JsonObject item) {
  130. return item.hasKey(DataCommunicatorConstants.SELECTED)
  131. && item.getBoolean(DataCommunicatorConstants.SELECTED);
  132. }
  133. }