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.

SelectionEvent.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import com.google.gwt.event.shared.GwtEvent;
  22. import com.vaadin.client.widgets.Grid;
  23. /**
  24. * Event object describing a change in Grid row selection state.
  25. *
  26. * @since 7.4
  27. * @author Vaadin Ltd
  28. */
  29. @SuppressWarnings("rawtypes")
  30. public class SelectionEvent<T> extends GwtEvent<SelectionHandler> {
  31. private static final Type<SelectionHandler> EVENT_TYPE = new Type<>();
  32. private final Grid<T> grid;
  33. private final List<T> added;
  34. private final List<T> removed;
  35. private final boolean batched;
  36. /**
  37. * Creates an event with a single added or removed row.
  38. *
  39. * @param grid
  40. * grid reference, used for getSource
  41. * @param added
  42. * the added row, or <code>null</code> if a row was not added
  43. * @param removed
  44. * the removed row, or <code>null</code> if a row was not removed
  45. * @param batched
  46. * whether or not this selection change event is triggered during
  47. * a batched selection/deselection action
  48. * @see SelectionModel.Multi.Batched
  49. */
  50. public SelectionEvent(Grid<T> grid, T added, T removed, boolean batched) {
  51. this.grid = grid;
  52. this.batched = batched;
  53. if (added != null) {
  54. this.added = Collections.singletonList(added);
  55. } else {
  56. this.added = Collections.emptyList();
  57. }
  58. if (removed != null) {
  59. this.removed = Collections.singletonList(removed);
  60. } else {
  61. this.removed = Collections.emptyList();
  62. }
  63. }
  64. /**
  65. * Creates an event where several rows have been added or removed.
  66. *
  67. * @param grid
  68. * Grid reference, used for getSource
  69. * @param added
  70. * a collection of added rows, or <code>null</code> if no rows
  71. * were added
  72. * @param removed
  73. * a collection of removed rows, or <code>null</code> if no rows
  74. * were removed
  75. * @param batched
  76. * whether or not this selection change event is triggered during
  77. * a batched selection/deselection action
  78. * @see SelectionModel.Multi.Batched
  79. */
  80. public SelectionEvent(Grid<T> grid, Collection<T> added,
  81. Collection<T> removed, boolean batched) {
  82. this.grid = grid;
  83. this.batched = batched;
  84. if (added != null) {
  85. this.added = new ArrayList<>(added);
  86. } else {
  87. this.added = Collections.emptyList();
  88. }
  89. if (removed != null) {
  90. this.removed = new ArrayList<>(removed);
  91. } else {
  92. this.removed = Collections.emptyList();
  93. }
  94. }
  95. /**
  96. * Gets a reference to the Grid object that fired this event.
  97. *
  98. * @return a grid reference
  99. */
  100. @Override
  101. public Grid<T> getSource() {
  102. return grid;
  103. }
  104. /**
  105. * Gets all rows added to the selection since the last
  106. * {@link SelectionEvent} .
  107. *
  108. * @return a collection of added rows. Empty collection if no rows were
  109. * added.
  110. */
  111. public Collection<T> getAdded() {
  112. return Collections.unmodifiableCollection(added);
  113. }
  114. /**
  115. * Gets all rows removed from the selection since the last
  116. * {@link SelectionEvent}.
  117. *
  118. * @return a collection of removed rows. Empty collection if no rows were
  119. * removed.
  120. */
  121. public Collection<T> getRemoved() {
  122. return Collections.unmodifiableCollection(removed);
  123. }
  124. /**
  125. * Gets a type identifier for this event.
  126. *
  127. * @return a {@link Type} identifier.
  128. */
  129. public static Type<SelectionHandler> getType() {
  130. return EVENT_TYPE;
  131. }
  132. @Override
  133. public Type<SelectionHandler> getAssociatedType() {
  134. return EVENT_TYPE;
  135. }
  136. @Override
  137. @SuppressWarnings("unchecked")
  138. protected void dispatch(SelectionHandler handler) {
  139. handler.onSelect(this);
  140. }
  141. /**
  142. * Checks if this selection change event is fired during a batched
  143. * selection/deselection operation.
  144. *
  145. * @return <code>true</code> if this event is fired during a batched
  146. * selection/deselection operation
  147. */
  148. public boolean isBatchedSelection() {
  149. return batched;
  150. }
  151. }