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