1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.Collections;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.data.osm.FilterMatcher.FilterType;
|
---|
8 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Class for applying {@link Filter}s to {@link OsmPrimitive}s.
|
---|
12 | *
|
---|
13 | * Provides a bridge between Filter GUI and the data.
|
---|
14 | *
|
---|
15 | * @author Petr_Dlouhý
|
---|
16 | */
|
---|
17 | public final class FilterWorker {
|
---|
18 |
|
---|
19 | private FilterWorker() {
|
---|
20 | // Hide default constructor for utils classes
|
---|
21 | }
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Apply the filters to the primitives of the data set.
|
---|
25 | *
|
---|
26 | * @param all the collection of primitives for that the filter state should be updated
|
---|
27 | * @param filterMatcher the FilterMatcher
|
---|
28 | * @return true, if the filter state (normal / disabled / hidden)
|
---|
29 | * of any primitive has changed in the process
|
---|
30 | */
|
---|
31 | public static boolean executeFilters(Collection<OsmPrimitive> all, FilterMatcher filterMatcher) {
|
---|
32 | boolean changed;
|
---|
33 | // first relations, then ways and nodes last; this is required to resolve dependencies
|
---|
34 | changed = doExecuteFilters(SubclassFilteredCollection.filter(all, Relation.class::isInstance), filterMatcher);
|
---|
35 | changed |= doExecuteFilters(SubclassFilteredCollection.filter(all, Way.class::isInstance), filterMatcher);
|
---|
36 | changed |= doExecuteFilters(SubclassFilteredCollection.filter(all, Node.class::isInstance), filterMatcher);
|
---|
37 | return changed;
|
---|
38 | }
|
---|
39 |
|
---|
40 | private static boolean doExecuteFilters(Collection<OsmPrimitive> all, FilterMatcher filterMatcher) {
|
---|
41 |
|
---|
42 | boolean changed = false;
|
---|
43 |
|
---|
44 | for (OsmPrimitive primitive: all) {
|
---|
45 | FilterType hiddenType = filterMatcher.isHidden(primitive);
|
---|
46 | if (hiddenType != FilterType.NOT_FILTERED) {
|
---|
47 | changed |= primitive.setDisabledState(true);
|
---|
48 | primitive.setHiddenType(hiddenType == FilterType.EXPLICIT);
|
---|
49 | } else {
|
---|
50 | FilterType disabledType = filterMatcher.isDisabled(primitive);
|
---|
51 | if (disabledType != FilterType.NOT_FILTERED) {
|
---|
52 | changed |= primitive.setDisabledState(false);
|
---|
53 | primitive.setDisabledType(disabledType == FilterType.EXPLICIT);
|
---|
54 | } else {
|
---|
55 | changed |= primitive.unsetDisabledState();
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 | return changed;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Apply the filters to a single primitive.
|
---|
64 | *
|
---|
65 | * @param primitive the primitive
|
---|
66 | * @param filterMatcher the FilterMatcher
|
---|
67 | * @return true, if the filter state (normal / disabled / hidden)
|
---|
68 | * of the primitive has changed in the process
|
---|
69 | */
|
---|
70 | public static boolean executeFilters(OsmPrimitive primitive, FilterMatcher filterMatcher) {
|
---|
71 | return doExecuteFilters(Collections.singleton(primitive), filterMatcher);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Clear all filter flags, i.e. turn off filters.
|
---|
76 | * @param prims the primitives
|
---|
77 | */
|
---|
78 | public static void clearFilterFlags(Collection<OsmPrimitive> prims) {
|
---|
79 | for (OsmPrimitive osm : prims) {
|
---|
80 | osm.unsetDisabledState();
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|