001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs.properties;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.ActionEvent;
007import java.util.Map;
008import java.util.Objects;
009import java.util.function.Function;
010
011import javax.swing.JTable;
012
013import org.openstreetmap.josm.actions.JosmAction;
014import org.openstreetmap.josm.data.osm.Relation;
015import org.openstreetmap.josm.data.preferences.StringProperty;
016import org.openstreetmap.josm.tools.OpenBrowser;
017import org.openstreetmap.josm.tools.Utils;
018
019/**
020 * Launch browser with Taginfo statistics for selected object.
021 * @since 13521
022 */
023public class TaginfoAction extends JosmAction {
024
025    final transient StringProperty TAGINFO_URL_PROP = new StringProperty("taginfo.url", "https://taginfo.openstreetmap.org/");
026
027    private final JTable tagTable;
028    private final Function<Integer, String> tagKeySupplier;
029    private final Function<Integer, Map<String, Integer>> tagValuesSupplier;
030
031    private final JTable membershipTable;
032    private final Function<Integer, Relation> memberValueSupplier;
033
034    /**
035     * Constructs a new {@code TaginfoAction}.
036     * @param tagTable The tag table. Cannot be null
037     * @param tagKeySupplier Finds the key from given row of tag table. Cannot be null
038     * @param tagValuesSupplier Finds the values from given row of tag table (map of values and number of occurrences). Cannot be null
039     * @param membershipTable The membership table. Can be null
040     * @param memberValueSupplier Finds the parent relation from given row of membership table. Can be null
041     */
042    public TaginfoAction(JTable tagTable, Function<Integer, String> tagKeySupplier, Function<Integer, Map<String, Integer>> tagValuesSupplier,
043            JTable membershipTable, Function<Integer, Relation> memberValueSupplier) {
044        super(tr("Go to Taginfo"), "dialogs/taginfo", tr("Launch browser with Taginfo statistics for selected object"), null, false);
045        this.tagTable = Objects.requireNonNull(tagTable);
046        this.tagKeySupplier = Objects.requireNonNull(tagKeySupplier);
047        this.tagValuesSupplier = Objects.requireNonNull(tagValuesSupplier);
048        this.membershipTable = membershipTable;
049        this.memberValueSupplier = memberValueSupplier;
050    }
051
052    @Override
053    public void actionPerformed(ActionEvent e) {
054        final String url;
055        if (tagTable.getSelectedRowCount() == 1) {
056            final int row = tagTable.getSelectedRow();
057            final String key = Utils.encodeUrl(tagKeySupplier.apply(row)).replaceAll("\\+", "%20");
058            Map<String, Integer> values = tagValuesSupplier.apply(row);
059            if (values.size() == 1) {
060                url = TAGINFO_URL_PROP.get() + "tags/" + key
061                        + '=' + Utils.encodeUrl(values.keySet().iterator().next()).replaceAll("\\+", "%20");
062            } else {
063                url = TAGINFO_URL_PROP.get() + "keys/" + key;
064            }
065        } else if (membershipTable != null && membershipTable.getSelectedRowCount() == 1) {
066            final String type = (memberValueSupplier.apply(membershipTable.getSelectedRow())).get("type");
067            url = TAGINFO_URL_PROP.get() + "relations/" + type;
068        } else {
069            return;
070        }
071        OpenBrowser.displayUrl(url);
072    }
073}