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