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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.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 to select
  27. * @since 8.0
  28. */
  29. public interface SelectionModel<T> {
  30. public static class NoSelectionModel<T> implements SelectionModel<T> {
  31. @Override
  32. public void select(T item) {
  33. }
  34. @Override
  35. public void deselect(T item) {
  36. }
  37. @Override
  38. public boolean isSelected(T item) {
  39. return false;
  40. }
  41. @Override
  42. public void deselectAll() {
  43. }
  44. @Override
  45. public void setSelectionAllowed(boolean selectionAllowed) {
  46. }
  47. @Override
  48. public boolean isSelectionAllowed() {
  49. return false;
  50. }
  51. }
  52. /**
  53. * Selects the given item. If another item was already selected, that item
  54. * is deselected.
  55. *
  56. * @param item
  57. * the item to select, not null
  58. */
  59. void select(T item);
  60. /**
  61. * Deselects the given item. If the item is not currently selected, does
  62. * nothing.
  63. *
  64. * @param item
  65. * the item to deselect, not null
  66. */
  67. void deselect(T item);
  68. /**
  69. * Returns whether the given item is currently selected.
  70. *
  71. * @param item
  72. * the item to check, not null
  73. * @return {@code true} if the item is selected, {@code false} otherwise
  74. */
  75. boolean isSelected(T item);
  76. /**
  77. * Deselects all currently selected items.
  78. */
  79. void deselectAll();
  80. /**
  81. * Sets whether the user is allowed to change the selection.
  82. * <p>
  83. * The check is done only for the client side actions. It doesn't affect
  84. * selection requests sent from the server side.
  85. *
  86. * @param selectionAllowed
  87. * <code>true</code> if the user is allowed to change the
  88. * selection, <code>false</code> otherwise
  89. */
  90. void setSelectionAllowed(boolean selectionAllowed);
  91. /**
  92. * Checks if the user is allowed to change the selection.
  93. * <p>
  94. * The check is done only for the client side actions. It doesn't affect
  95. * selection requests sent from the server side.
  96. *
  97. * @return <code>true</code> if the user is allowed to change the selection,
  98. * <code>false</code> otherwise
  99. */
  100. boolean isSelectionAllowed();
  101. /**
  102. * Gets the selected state from a given grid row json object. This is a
  103. * helper method for grid selection models.
  104. *
  105. * @param item
  106. * a json object
  107. * @return {@code true} if the json object is marked as selected;
  108. * {@code false} if not
  109. */
  110. public static boolean isItemSelected(JsonObject item) {
  111. return item.hasKey(DataCommunicatorConstants.SELECTED)
  112. && item.getBoolean(DataCommunicatorConstants.SELECTED);
  113. }
  114. }