1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.event.selection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import com.vaadin.data.HasValue.ValueChangeEvent;
import com.vaadin.ui.AbstractMultiSelect;
import com.vaadin.ui.Component;
import com.vaadin.ui.MultiSelect;
/**
* Event fired when the the selection changes in a
* {@link com.vaadin.data.SelectionModel.Multi}.
*
* @author Vaadin Ltd
*
* @since 8.0
*
* @param <T>
* the data type of the selection model
*/
public class MultiSelectionEvent<T> extends ValueChangeEvent<Set<T>>
implements SelectionEvent<T> {
/**
* Creates a new event.
*
* @param source
* the listing component in which the selection changed
* @param oldSelection
* the old set of selected items
* @param userOriginated
* {@code true} if this event originates from the client,
* {@code false} otherwise.
*/
public MultiSelectionEvent(AbstractMultiSelect<T> source,
Set<T> oldSelection, boolean userOriginated) {
super(source, oldSelection, userOriginated);
}
/**
* Creates a new selection change event in a multiselect component.
*
* @param component
* the component
* @param source
* the multiselect source
* @param oldSelection
* the old set of selected items
* @param userOriginated
* {@code true} if this event originates from the client,
* {@code false} otherwise.
*/
public MultiSelectionEvent(Component component, MultiSelect<T> source,
Set<T> oldSelection, boolean userOriginated) {
super(component, source, oldSelection, userOriginated);
}
/**
* Gets the new selection.
* <p>
* The result is the current selection of the source
* {@link AbstractMultiSelect} object. So it's always exactly the same as
* {@link AbstractMultiSelect#getValue()}
*
* @see #getValue()
*
* @return a set of items selected after the selection was changed
*/
public Set<T> getNewSelection() {
return getValue();
}
/**
* Gets the old selection.
*
* @return a set of items selected before the selection was changed
*/
public Set<T> getOldSelection() {
return Collections.unmodifiableSet(getOldValue());
}
/**
* Gets the items that were removed from selection.
* <p>
* This is just a convenience method for checking what was previously
* selected in {@link #getOldSelection()} but not selected anymore in
* {@link #getNewSelection()}.
*
* @return the items that were removed from selection
*/
public Set<T> getRemovedSelection() {
LinkedHashSet<T> copy = new LinkedHashSet<>(getOldValue());
copy.removeAll(getNewSelection());
return copy;
}
/**
* Gets the items that were added to selection.
* <p>
* This is just a convenience method for checking what is new selected in
* {@link #getNewSelection()} and wasn't selected in
* {@link #getOldSelection()}.
*
* @return the items that were removed from selection
*/
public Set<T> getAddedSelection() {
LinkedHashSet<T> copy = new LinkedHashSet<>(getValue());
copy.removeAll(getOldValue());
return copy;
}
@Override
public Optional<T> getFirstSelectedItem() {
return getValue().stream().findFirst();
}
/**
* The multiselect on which the Event initially occurred.
*
* @return the multiselect on which the Event initially occurred.
*/
@Override
public MultiSelect<T> getSource() {
return (MultiSelect<T>) super.getSource();
}
/**
* {@inheritDoc}
* <p>
* This is the same as {@link #getValue()}.
*/
@Override
public Set<T> getAllSelectedItems() {
return getValue();
}
}
|