001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.tagging.presets;
003
004import org.openstreetmap.josm.data.osm.OsmPrimitive;
005import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
006
007/**
008 * Enumeration of OSM primitive types associated with names and icons
009 * @since 6068
010 */
011public enum TaggingPresetType {
012    /** Node */
013    NODE(/* ICON */ "Mf_node", "node"),
014    /** Way */
015    WAY(/* ICON */ "Mf_way", "way"),
016    /** Relation */
017    RELATION(/* ICON */ "Mf_relation", "relation"),
018    /** Closed way */
019    CLOSEDWAY(/* ICON */ "Mf_closedway", "closedway");
020    private final String iconName;
021    private final String name;
022
023    TaggingPresetType(String iconName, String name) {
024        this.iconName = iconName + ".svg";
025        this.name = name;
026    }
027
028    /**
029     * Replies the SVG icon name.
030     * @return the SVG icon name
031     */
032    public String getIconName() {
033        return iconName;
034    }
035
036    /**
037     * Replies the name, as used in XML presets.
038     * @return the name: "node", "way", "relation" or "closedway"
039     */
040    public String getName() {
041        return name;
042    }
043
044    /**
045     * Determines the {@code TaggingPresetType} of a given primitive.
046     * @param p The OSM primitive
047     * @return the {@code TaggingPresetType} of {@code p}
048     */
049    public static TaggingPresetType forPrimitive(OsmPrimitive p) {
050        return forPrimitiveType(p.getDisplayType());
051    }
052
053    /**
054     * Determines the {@code TaggingPresetType} of a given primitive type.
055     * @param type The OSM primitive type
056     * @return the {@code TaggingPresetType} of {@code type}
057     */
058    public static TaggingPresetType forPrimitiveType(OsmPrimitiveType type) {
059        if (type == OsmPrimitiveType.NODE) return NODE;
060        if (type == OsmPrimitiveType.WAY) return WAY;
061        if (type == OsmPrimitiveType.CLOSEDWAY) return CLOSEDWAY;
062        if (type == OsmPrimitiveType.RELATION || type == OsmPrimitiveType.MULTIPOLYGON)
063                return RELATION;
064        throw new IllegalArgumentException("Unexpected primitive type: " + type);
065    }
066
067    /**
068     * Determines the {@code TaggingPresetType} from a given string.
069     * @param type The OSM primitive type as string ("node", "way", "relation" or "closedway")
070     * @return the {@code TaggingPresetType} from {@code type}
071     */
072    public static TaggingPresetType fromString(String type) {
073        for (TaggingPresetType t : TaggingPresetType.values()) {
074            if (t.getName().equals(type)) {
075                return t;
076            }
077        }
078        return null;
079    }
080}