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 IPrimitive}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 <T> The primitive type
|
---|
28 | * @param all the collection of primitives for that the filter state should be updated
|
---|
29 | * @param filters the filters
|
---|
30 | * @return true, if the filter state (normal / disabled / hidden) of any primitive has changed in the process
|
---|
31 | * @throws SearchParseError if the search expression in a filter cannot be parsed
|
---|
32 | * @since 12383, xxx (generics)
|
---|
33 | */
|
---|
34 | public static <T extends IPrimitive & IFilterablePrimitive> boolean executeFilters(Collection<T> all, Filter... filters)
|
---|
35 | throws SearchParseError {
|
---|
36 | return executeFilters(all, FilterMatcher.of(filters));
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Apply the filters to the primitives of the data set.
|
---|
41 | *
|
---|
42 | * @param <T> The primitive type
|
---|
43 | * @param all the collection of primitives for that the filter state should be updated
|
---|
44 | * @param filterMatcher the FilterMatcher
|
---|
45 | * @return true, if the filter state (normal / disabled / hidden) of any primitive has changed in the process
|
---|
46 | * @since xxx (generics)
|
---|
47 | */
|
---|
48 | public static <T extends IPrimitive & IFilterablePrimitive> boolean executeFilters(Collection<T> all, FilterMatcher filterMatcher) {
|
---|
49 | boolean changed;
|
---|
50 | // first relations, then ways and nodes last; this is required to resolve dependencies
|
---|
51 | changed = doExecuteFilters(SubclassFilteredCollection.filter(all, IRelation.class::isInstance), filterMatcher);
|
---|
52 | changed |= doExecuteFilters(SubclassFilteredCollection.filter(all, IWay.class::isInstance), filterMatcher);
|
---|
53 | changed |= doExecuteFilters(SubclassFilteredCollection.filter(all, INode.class::isInstance), filterMatcher);
|
---|
54 | return changed;
|
---|
55 | }
|
---|
56 |
|
---|
57 | private static <T extends IPrimitive & IFilterablePrimitive> boolean doExecuteFilters(Collection<T> all, FilterMatcher filterMatcher) {
|
---|
58 |
|
---|
59 | boolean changed = false;
|
---|
60 |
|
---|
61 | for (T primitive : all) {
|
---|
62 | FilterType hiddenType = filterMatcher.isHidden(primitive);
|
---|
63 | if (hiddenType != FilterType.NOT_FILTERED) {
|
---|
64 | changed |= primitive.setDisabledState(true);
|
---|
65 | primitive.setHiddenType(hiddenType == FilterType.EXPLICIT);
|
---|
66 | } else {
|
---|
67 | FilterType disabledType = filterMatcher.isDisabled(primitive);
|
---|
68 | if (disabledType != FilterType.NOT_FILTERED) {
|
---|
69 | changed |= primitive.setDisabledState(false);
|
---|
70 | primitive.setDisabledType(disabledType == FilterType.EXPLICIT);
|
---|
71 | } else {
|
---|
72 | changed |= primitive.unsetDisabledState();
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | return changed;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Apply the filters to a single primitive.
|
---|
81 | *
|
---|
82 | * @param <T> the primitive type
|
---|
83 | * @param primitive the primitive
|
---|
84 | * @param filterMatcher the FilterMatcher
|
---|
85 | * @return true, if the filter state (normal / disabled / hidden)
|
---|
86 | * of the primitive has changed in the process
|
---|
87 | * @since xxx (generics)
|
---|
88 | */
|
---|
89 | public static <T extends IPrimitive & IFilterablePrimitive> boolean executeFilters(T primitive, FilterMatcher filterMatcher) {
|
---|
90 | return doExecuteFilters(Collections.singleton(primitive), filterMatcher);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Clear all filter flags, i.e. turn off filters.
|
---|
95 | * @param <T> the primitive type
|
---|
96 | * @param prims the primitives
|
---|
97 | * @return true, if the filter state (normal / disabled / hidden) of any primitive has changed in the process
|
---|
98 | * @since 12388 (signature)
|
---|
99 | */
|
---|
100 | public static <T extends IPrimitive & IFilterablePrimitive> boolean clearFilterFlags(Collection<T> prims) {
|
---|
101 | boolean changed = false;
|
---|
102 | for (T osm : prims) {
|
---|
103 | changed |= osm.unsetDisabledState();
|
---|
104 | }
|
---|
105 | return changed;
|
---|
106 | }
|
---|
107 | }
|
---|