[3719] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[3356] | 2 | package org.openstreetmap.josm.data.osm;
|
---|
| 3 |
|
---|
| 4 | import java.util.Collection;
|
---|
[5437] | 5 | import java.util.Collections;
|
---|
[3356] | 6 |
|
---|
[5437] | 7 | import org.openstreetmap.josm.data.osm.FilterMatcher.FilterType;
|
---|
[12656] | 8 | import org.openstreetmap.josm.data.osm.search.SearchParseError;
|
---|
[10657] | 9 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
[5437] | 10 |
|
---|
[3356] | 11 | /**
|
---|
[9113] | 12 | * Class for applying {@link Filter}s to {@link OsmPrimitive}s.
|
---|
[9665] | 13 | *
|
---|
[9113] | 14 | * Provides a bridge between Filter GUI and the data.
|
---|
[9665] | 15 | *
|
---|
[3356] | 16 | * @author Petr_Dlouhý
|
---|
| 17 | */
|
---|
[6362] | 18 | public final class FilterWorker {
|
---|
[7509] | 19 |
|
---|
[6360] | 20 | private FilterWorker() {
|
---|
| 21 | // Hide default constructor for utils classes
|
---|
| 22 | }
|
---|
[7509] | 23 |
|
---|
[3356] | 24 | /**
|
---|
| 25 | * Apply the filters to the primitives of the data set.
|
---|
| 26 | *
|
---|
[10308] | 27 | * @param all the collection of primitives for that the filter state should be updated
|
---|
[12383] | 28 | * @param filters the filters
|
---|
| 29 | * @return true, if the filter state (normal / disabled / hidden) of any primitive has changed in the process
|
---|
[12656] | 30 | * @throws SearchParseError if the search expression in a filter cannot be parsed
|
---|
[12383] | 31 | * @since 12383
|
---|
| 32 | */
|
---|
[12656] | 33 | public static boolean executeFilters(Collection<OsmPrimitive> all, Filter... filters) throws SearchParseError {
|
---|
[12383] | 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
|
---|
[5423] | 41 | * @param filterMatcher the FilterMatcher
|
---|
[12388] | 42 | * @return true, if the filter state (normal / disabled / hidden) of any primitive has changed in the process
|
---|
[3356] | 43 | */
|
---|
[3471] | 44 | public static boolean executeFilters(Collection<OsmPrimitive> all, FilterMatcher filterMatcher) {
|
---|
[10308] | 45 | boolean changed;
|
---|
[5437] | 46 | // first relations, then ways and nodes last; this is required to resolve dependencies
|
---|
[10716] | 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);
|
---|
[5437] | 50 | return changed;
|
---|
| 51 | }
|
---|
[3367] | 52 |
|
---|
[5437] | 53 | private static boolean doExecuteFilters(Collection<OsmPrimitive> all, FilterMatcher filterMatcher) {
|
---|
| 54 |
|
---|
[3471] | 55 | boolean changed = false;
|
---|
| 56 |
|
---|
[3356] | 57 | for (OsmPrimitive primitive: all) {
|
---|
[5437] | 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);
|
---|
[5442] | 66 | primitive.setDisabledType(disabledType == FilterType.EXPLICIT);
|
---|
[3367] | 67 | } else {
|
---|
[5437] | 68 | changed |= primitive.unsetDisabledState();
|
---|
[3367] | 69 | }
|
---|
[3356] | 70 | }
|
---|
| 71 | }
|
---|
[3471] | 72 | return changed;
|
---|
[3356] | 73 | }
|
---|
| 74 |
|
---|
[9113] | 75 | /**
|
---|
| 76 | * Apply the filters to a single primitive.
|
---|
[9665] | 77 | *
|
---|
| 78 | * @param primitive the primitive
|
---|
[9113] | 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 | */
|
---|
[3371] | 83 | public static boolean executeFilters(OsmPrimitive primitive, FilterMatcher filterMatcher) {
|
---|
[5437] | 84 | return doExecuteFilters(Collections.singleton(primitive), filterMatcher);
|
---|
[3371] | 85 | }
|
---|
| 86 |
|
---|
[9113] | 87 | /**
|
---|
| 88 | * Clear all filter flags, i.e. turn off filters.
|
---|
| 89 | * @param prims the primitives
|
---|
[12388] | 90 | * @return true, if the filter state (normal / disabled / hidden) of any primitive has changed in the process
|
---|
| 91 | * @since 12388 (signature)
|
---|
[9113] | 92 | */
|
---|
[12388] | 93 | public static boolean clearFilterFlags(Collection<OsmPrimitive> prims) {
|
---|
| 94 | boolean changed = false;
|
---|
[3356] | 95 | for (OsmPrimitive osm : prims) {
|
---|
[12388] | 96 | changed |= osm.unsetDisabledState();
|
---|
[3356] | 97 | }
|
---|
[12388] | 98 | return changed;
|
---|
[3356] | 99 | }
|
---|
| 100 | }
|
---|