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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.event;
  17. import java.io.Serializable;
  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.EventObject;
  21. import java.util.LinkedHashSet;
  22. import java.util.Set;
  23. /**
  24. * An event that specifies what in a selection has changed, and where the
  25. * selection took place.
  26. *
  27. * @since 7.4
  28. * @author Vaadin Ltd
  29. */
  30. @Deprecated
  31. public class SelectionEvent extends EventObject {
  32. private LinkedHashSet<Object> oldSelection;
  33. private LinkedHashSet<Object> newSelection;
  34. public SelectionEvent(Object source, Collection<Object> oldSelection,
  35. Collection<Object> newSelection) {
  36. super(source);
  37. this.oldSelection = new LinkedHashSet<Object>(oldSelection);
  38. this.newSelection = new LinkedHashSet<Object>(newSelection);
  39. }
  40. /**
  41. * A {@link Collection} of all the itemIds that became selected.
  42. * <p>
  43. * <em>Note:</em> this excludes all itemIds that might have been previously
  44. * selected.
  45. *
  46. * @return a Collection of the itemIds that became selected
  47. */
  48. public Set<Object> getAdded() {
  49. return setDifference(newSelection, oldSelection);
  50. }
  51. /**
  52. * A {@link Collection} of all the itemIds that became deselected.
  53. * <p>
  54. * <em>Note:</em> this excludes all itemIds that might have been previously
  55. * deselected.
  56. *
  57. * @return a Collection of the itemIds that became deselected
  58. */
  59. public Set<Object> getRemoved() {
  60. return setDifference(oldSelection, newSelection);
  61. }
  62. /**
  63. * Slightly optimized set difference that can return the original set or a
  64. * modified one.
  65. *
  66. * @param set1
  67. * original set
  68. * @param set2
  69. * the set to subtract
  70. * @return the difference set
  71. */
  72. private static <T> Set<T> setDifference(Set<T> set1, Set<T> set2) {
  73. if (set2.isEmpty()) {
  74. return set1;
  75. } else {
  76. LinkedHashSet<T> set = new LinkedHashSet<T>(set1);
  77. set.removeAll(set2);
  78. return set;
  79. }
  80. }
  81. /**
  82. * A {@link Collection} of all the itemIds that are currently selected.
  83. *
  84. * @return a Collection of the itemIds that are currently selected
  85. */
  86. public Set<Object> getSelected() {
  87. return Collections.unmodifiableSet(newSelection);
  88. }
  89. /**
  90. * The listener interface for receiving {@link SelectionEvent
  91. * SelectionEvents}.
  92. */
  93. @Deprecated
  94. public interface SelectionListener extends Serializable {
  95. /**
  96. * Notifies the listener that the selection state has changed.
  97. *
  98. * @param event
  99. * the selection change event
  100. */
  101. void select(SelectionEvent event);
  102. }
  103. /**
  104. * The interface for adding and removing listeners for {@link SelectionEvent
  105. * SelectionEvents}.
  106. */
  107. @Deprecated
  108. public interface SelectionNotifier extends Serializable {
  109. /**
  110. * Registers a new selection listener
  111. *
  112. * @param listener
  113. * the listener to register
  114. */
  115. void addSelectionListener(SelectionListener listener);
  116. /**
  117. * Removes a previously registered selection change listener
  118. *
  119. * @param listener
  120. * the listener to remove
  121. */
  122. void removeSelectionListener(SelectionListener listener);
  123. }
  124. }