Ignore:
Timestamp:
2017-07-10T23:12:16+02:00 (9 years ago)
Author:
michael2402
Message:

Apply #14923: Adjust the search dialog to allow to search for primitives that use a preset. Patch by bafonins

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java

    r11978 r12464  
    2121import java.util.regex.Pattern;
    2222import java.util.regex.PatternSyntaxException;
     23import java.util.stream.Collectors;
    2324
    2425import org.openstreetmap.josm.Main;
     
    3940import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
    4041import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
     42import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
     43import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetMenu;
     44import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetSeparator;
     45import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
    4146import org.openstreetmap.josm.tools.AlphanumComparator;
    4247import org.openstreetmap.josm.tools.Geometry;
     
    116121                "changeset", "nodes", "ways", "tags", "areasize", "waylength", "modified", "deleted", "selected",
    117122                "incomplete", "untagged", "closed", "new", "indownloadedarea",
    118                 "allindownloadedarea", "inview", "allinview", "timestamp", "nth", "nth%", "hasRole");
     123                "allindownloadedarea", "inview", "allinview", "timestamp", "nth", "nth%", "hasRole", "preset");
    119124
    120125        @Override
     
    152157                    case "type":
    153158                        return new ExactType(tokenizer.readTextOrNumber());
     159                    case "preset":
     160                        return new Preset(tokenizer.readTextOrNumber());
    154161                    case "user":
    155162                        return new UserMatch(tokenizer.readTextOrNumber());
     
    15551562    }
    15561563
     1564    /**
     1565     * Matches presets.
     1566     * @since 12464
     1567     */
     1568    private static class Preset extends Match {
     1569        private final List<TaggingPreset> presets;
     1570
     1571        Preset(String presetName) throws ParseError {
     1572
     1573            if (presetName == null || presetName.equals("")) {
     1574                throw new ParseError("The name of the preset is required");
     1575            }
     1576
     1577            int wildCardIdx = presetName.lastIndexOf("*");
     1578            int length = presetName.length() - 1;
     1579
     1580            /*
     1581             * Match strictly (simply comparing the names) if there is no '*' symbol
     1582             * at the end of the name or '*' is a part of the preset name.
     1583             */
     1584            boolean matchStrictly = wildCardIdx == -1 || wildCardIdx != length;
     1585
     1586            this.presets = TaggingPresets.getTaggingPresets()
     1587                    .stream()
     1588                    .filter(preset -> !(preset instanceof TaggingPresetMenu || preset instanceof TaggingPresetSeparator))
     1589                    .filter(preset -> this.presetNameMatch(presetName, preset, matchStrictly))
     1590                    .collect(Collectors.toList());
     1591
     1592            if (this.presets.isEmpty()) {
     1593                throw new ParseError(tr("Unknown preset name: ") + presetName);
     1594            }
     1595        }
     1596
     1597        @Override
     1598        public boolean match(OsmPrimitive osm) {
     1599            for (TaggingPreset preset : this.presets) {
     1600                if (preset.test(osm)) {
     1601                    return true;
     1602                }
     1603            }
     1604
     1605            return false;
     1606        }
     1607
     1608        private boolean presetNameMatch(String name, TaggingPreset preset, boolean matchStrictly) {
     1609            if (matchStrictly) {
     1610                return name.equalsIgnoreCase(preset.getRawName());
     1611            }
     1612
     1613            try {
     1614                String groupSuffix = name.substring(0, name.length() - 2); // try to remove '/*'
     1615                TaggingPresetMenu group = preset.group;
     1616
     1617                return group != null && groupSuffix.equalsIgnoreCase(group.getRawName());
     1618            } catch (StringIndexOutOfBoundsException ex) {
     1619                return false;
     1620            }
     1621        }
     1622    }
     1623
    15571624    public static class ParseError extends Exception {
    15581625        public ParseError(String msg) {
     
    18051872    }
    18061873}
     1874
Note: See TracChangeset for help on using the changeset viewer.