source: josm/trunk/src/org/openstreetmap/josm/data/osm/DataSelectionListener.java@ 12058

Last change on this file since 12058 was 12058, checked in by Don-vip, 7 years ago

fix Java/PMD/Checkstyle warnings

File size: 9.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.util.Collections;
5import java.util.HashSet;
6import java.util.Set;
7import java.util.stream.Collectors;
8import java.util.stream.Stream;
9
10import org.openstreetmap.josm.tools.CheckParameterUtil;
11
12/**
13 * This is a listener that listens to selection change events in the data set.
14 * @author Michael Zangl
15 * @since 12048
16 */
17@FunctionalInterface
18public interface DataSelectionListener {
19
20 /**
21 * Called whenever the selection is changed.
22 * @param e The selection change event.
23 */
24 void selectionChanged(SelectionChangeEvent e);
25
26 /**
27 * The event that is fired when the selection changed.
28 * @author Michael Zangl
29 * @since 12048
30 */
31 interface SelectionChangeEvent {
32 /**
33 * Gets the previous selection
34 * <p>
35 * This collection cannot be modified and will not change.
36 * @return The old selection
37 */
38 Set<OsmPrimitive> getOldSelection();
39
40 /**
41 * Gets the new selection
42 * <p>
43 * This collection cannot be modified and will not change.
44 * @return The new selection
45 */
46 Set<OsmPrimitive> getSelection();
47
48 /**
49 * Gets the primitives that have been removed from the selection.
50 * <p>
51 * Those are the primitives contained in {@link #getOldSelection()} but not in {@link #getSelection()}
52 * <p>
53 * This collection cannot be modified and will not change.
54 * @return The primitives
55 */
56 Set<OsmPrimitive> getRemoved();
57
58 /**
59 * Gets the primitives that have been added to the selection.
60 * <p>
61 * Those are the primitives contained in {@link #getSelection()} but not in {@link #getOldSelection()}
62 * <p>
63 * This collection cannot be modified and will not change.
64 * @return The primitives
65 */
66 Set<OsmPrimitive> getAdded();
67
68 /**
69 * Gets the data set that triggered this selection event.
70 * @return The data set.
71 */
72 DataSet getSource();
73
74 /**
75 * Test if this event did not change anything.
76 * <p>
77 * Should return true for all events that are fired.
78 * @return <code>true</code> if this did not change the selection.
79 */
80 default boolean isNop() {
81 return getAdded().isEmpty() && getRemoved().isEmpty();
82 }
83 }
84
85 /**
86 * The base class for selection events
87 * @author Michael Zangl
88 * @since 12048
89 */
90 abstract class AbstractSelectionEvent implements SelectionChangeEvent {
91 private final DataSet source;
92 private final Set<OsmPrimitive> old;
93
94 public AbstractSelectionEvent(DataSet source, Set<OsmPrimitive> old) {
95 CheckParameterUtil.ensureParameterNotNull(source, "source");
96 CheckParameterUtil.ensureParameterNotNull(old, "old");
97 this.source = source;
98 this.old = Collections.unmodifiableSet(old);
99 }
100
101 @Override
102 public Set<OsmPrimitive> getOldSelection() {
103 return old;
104 }
105
106 @Override
107 public DataSet getSource() {
108 return source;
109 }
110 }
111
112
113 /**
114 * The selection is replaced by a new selection
115 * @author Michael Zangl
116 * @since 12048
117 */
118 class SelectionReplaceEvent extends AbstractSelectionEvent {
119 private final Set<OsmPrimitive> current;
120 private Set<OsmPrimitive> removed;
121 private Set<OsmPrimitive> added;
122
123 /**
124 * Create a {@link SelectionReplaceEvent}
125 * @param source The source dataset
126 * @param old The old primitves that were previously selected. The caller needs to ensure that this set is not modifed.
127 * @param newSelection The primitives of the new selection.
128 */
129 public SelectionReplaceEvent(DataSet source, Set<OsmPrimitive> old, Stream<OsmPrimitive> newSelection) {
130 super(source, old);
131 this.current = newSelection.collect(Collectors.toSet());
132 }
133
134 @Override
135 public Set<OsmPrimitive> getSelection() {
136 return current;
137 }
138
139 @Override
140 public synchronized Set<OsmPrimitive> getRemoved() {
141 if (removed == null) {
142 removed = getOldSelection().stream().filter(p -> !current.contains(p)).collect(Collectors.toSet());
143 }
144 return removed;
145 }
146
147 @Override
148 public synchronized Set<OsmPrimitive> getAdded() {
149 if (added == null) {
150 added = current.stream().filter(p -> !getOldSelection().contains(p)).collect(Collectors.toSet());
151 }
152 return added;
153 }
154 }
155
156 /**
157 * Primitives are added to the selection
158 * @author Michael Zangl
159 * @since 12048
160 */
161 class SelectionAddEvent extends AbstractSelectionEvent {
162 private final Set<OsmPrimitive> add;
163 private final Set<OsmPrimitive> current;
164
165 /**
166 * Create a {@link SelectionAddEvent}
167 * @param source The source dataset
168 * @param old The old primitves that were previously selected. The caller needs to ensure that this set is not modifed.
169 * @param toAdd The primitives to add.
170 */
171 public SelectionAddEvent(DataSet source, Set<OsmPrimitive> old, Stream<OsmPrimitive> toAdd) {
172 super(source, old);
173 this.add = toAdd.filter(p -> !old.contains(p)).collect(Collectors.toSet());
174 if (this.add.isEmpty()) {
175 this.current = this.getOldSelection();
176 } else {
177 this.current = new HashSet<>(old);
178 this.current.addAll(add);
179 }
180 }
181
182 @Override
183 public Set<OsmPrimitive> getSelection() {
184 return Collections.unmodifiableSet(current);
185 }
186
187 @Override
188 public Set<OsmPrimitive> getRemoved() {
189 return Collections.emptySet();
190 }
191
192 @Override
193 public Set<OsmPrimitive> getAdded() {
194 return Collections.unmodifiableSet(add);
195 }
196 }
197
198 /**
199 * Primitives are removed from the selection
200 * @author Michael Zangl
201 * @since 12048
202 */
203 class SelectionRemoveEvent extends AbstractSelectionEvent {
204 private final Set<OsmPrimitive> remove;
205 private final Set<OsmPrimitive> current;
206
207 /**
208 * Create a {@link SelectionRemoveEvent}
209 * @param source The source dataset
210 * @param old The old primitves that were previously selected. The caller needs to ensure that this set is not modifed.
211 * @param toRemove The primitives to remove.
212 */
213 public SelectionRemoveEvent(DataSet source, Set<OsmPrimitive> old, Stream<OsmPrimitive> toRemove) {
214 super(source, old);
215 this.remove = toRemove.filter(old::contains).collect(Collectors.toSet());
216 if (this.remove.isEmpty()) {
217 this.current = this.getOldSelection();
218 } else {
219 HashSet<OsmPrimitive> currentSet = new HashSet<>(old);
220 currentSet.removeAll(remove);
221 current = Collections.unmodifiableSet(currentSet);
222 }
223 }
224
225 @Override
226 public Set<OsmPrimitive> getSelection() {
227 return Collections.unmodifiableSet(current);
228 }
229
230 @Override
231 public Set<OsmPrimitive> getRemoved() {
232 return Collections.unmodifiableSet(remove);
233 }
234
235 @Override
236 public Set<OsmPrimitive> getAdded() {
237 return Collections.emptySet();
238 }
239 }
240
241 /**
242 * Toggle the selected state of a primitive
243 * @author Michael Zangl
244 * @since 12048
245 */
246 class SelectionToggleEvent extends AbstractSelectionEvent {
247 private final Set<OsmPrimitive> current;
248 private final Set<OsmPrimitive> remove;
249 private final Set<OsmPrimitive> add;
250
251 /**
252 * Create a {@link SelectionToggleEvent}
253 * @param source The source dataset
254 * @param old The old primitves that were previously selected. The caller needs to ensure that this set is not modifed.
255 * @param toToggle The primitives to toggle.
256 */
257 public SelectionToggleEvent(DataSet source, Set<OsmPrimitive> old, Stream<OsmPrimitive> toToggle) {
258 super(source, old);
259 HashSet<OsmPrimitive> currentSet = new HashSet<>(old);
260 HashSet<OsmPrimitive> removeSet = new HashSet<>();
261 HashSet<OsmPrimitive> addSet = new HashSet<>();
262 toToggle.forEach(p -> {
263 if (currentSet.remove(p)) {
264 removeSet.add(p);
265 } else {
266 addSet.add(p);
267 currentSet.add(p);
268 }
269 });
270 this.current = Collections.unmodifiableSet(currentSet);
271 this.remove = Collections.unmodifiableSet(removeSet);
272 this.add = Collections.unmodifiableSet(addSet);
273 }
274
275 @Override
276 public Set<OsmPrimitive> getSelection() {
277 return Collections.unmodifiableSet(current);
278 }
279
280 @Override
281 public Set<OsmPrimitive> getRemoved() {
282 return Collections.unmodifiableSet(remove);
283 }
284
285 @Override
286 public Set<OsmPrimitive> getAdded() {
287 return Collections.unmodifiableSet(add);
288 }
289 }
290}
Note: See TracBrowser for help on using the repository browser.