source: josm/trunk/src/org/openstreetmap/josm/data/osm/FilterWorker.java@ 12383

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

see #14929 - new methods to ease the direct handling of filters

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.util.Collection;
5import java.util.Collections;
6
7import org.openstreetmap.josm.actions.search.SearchCompiler.ParseError;
8import org.openstreetmap.josm.data.osm.FilterMatcher.FilterType;
9import 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 */
18public 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 ParseError 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 ParseError {
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)
43 * of any primitive has changed in the process
44 */
45 public static boolean executeFilters(Collection<OsmPrimitive> all, FilterMatcher filterMatcher) {
46 boolean changed;
47 // first relations, then ways and nodes last; this is required to resolve dependencies
48 changed = doExecuteFilters(SubclassFilteredCollection.filter(all, Relation.class::isInstance), filterMatcher);
49 changed |= doExecuteFilters(SubclassFilteredCollection.filter(all, Way.class::isInstance), filterMatcher);
50 changed |= doExecuteFilters(SubclassFilteredCollection.filter(all, Node.class::isInstance), filterMatcher);
51 return changed;
52 }
53
54 private static boolean doExecuteFilters(Collection<OsmPrimitive> all, FilterMatcher filterMatcher) {
55
56 boolean changed = false;
57
58 for (OsmPrimitive primitive: all) {
59 FilterType hiddenType = filterMatcher.isHidden(primitive);
60 if (hiddenType != FilterType.NOT_FILTERED) {
61 changed |= primitive.setDisabledState(true);
62 primitive.setHiddenType(hiddenType == FilterType.EXPLICIT);
63 } else {
64 FilterType disabledType = filterMatcher.isDisabled(primitive);
65 if (disabledType != FilterType.NOT_FILTERED) {
66 changed |= primitive.setDisabledState(false);
67 primitive.setDisabledType(disabledType == FilterType.EXPLICIT);
68 } else {
69 changed |= primitive.unsetDisabledState();
70 }
71 }
72 }
73 return changed;
74 }
75
76 /**
77 * Apply the filters to a single primitive.
78 *
79 * @param primitive the primitive
80 * @param filterMatcher the FilterMatcher
81 * @return true, if the filter state (normal / disabled / hidden)
82 * of the primitive has changed in the process
83 */
84 public static boolean executeFilters(OsmPrimitive primitive, FilterMatcher filterMatcher) {
85 return doExecuteFilters(Collections.singleton(primitive), filterMatcher);
86 }
87
88 /**
89 * Clear all filter flags, i.e.&nbsp;turn off filters.
90 * @param prims the primitives
91 */
92 public static void clearFilterFlags(Collection<OsmPrimitive> prims) {
93 for (OsmPrimitive osm : prims) {
94 osm.unsetDisabledState();
95 }
96 }
97}
Note: See TracBrowser for help on using the repository browser.