Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.v7.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.v7.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> eventType = new Type<SelectionHandler>();
  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<T>(added);
  86. } else {
  87. this.added = Collections.emptyList();
  88. }
  89. if (removed != null) {
  90. this.removed = new ArrayList<T>(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 currently selected rows.
  126. *
  127. * @return a non-null collection containing all currently selected rows.
  128. */
  129. public Collection<T> getSelected() {
  130. return grid.getSelectedRows();
  131. }
  132. /**
  133. * Gets a type identifier for this event.
  134. *
  135. * @return a {@link Type} identifier.
  136. */
  137. public static Type<SelectionHandler> getType() {
  138. return eventType;
  139. }
  140. @Override
  141. public Type<SelectionHandler> getAssociatedType() {
  142. return eventType;
  143. }
  144. @Override
  145. @SuppressWarnings("unchecked")
  146. protected void dispatch(SelectionHandler handler) {
  147. handler.onSelect(this);
  148. }
  149. /**
  150. * Checks if this selection change event is fired during a batched
  151. * selection/deselection operation.
  152. *
  153. * @return <code>true</code> if this event is fired during a batched
  154. * selection/deselection operation
  155. */
  156. public boolean isBatchedSelection() {
  157. return batched;
  158. }
  159. }