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.IntFunction;
010
011import javax.swing.JTable;
012
013import org.openstreetmap.josm.gui.MainApplication;
014
015/**
016 * Launch browser with wiki help for selected tag.
017 * @since 15581
018 */
019public class HelpTagAction extends HelpAction {
020    private final JTable tagTable;
021    private final IntFunction<String> tagKeySupplier;
022    private final IntFunction<Map<String, Integer>> tagValuesSupplier;
023
024    /**
025     * Constructs a new {@code HelpAction}.
026     * @param tagTable The tag table. Cannot be null
027     * @param tagKeySupplier Finds the key from given row of tag table. Cannot be null
028     * @param tagValuesSupplier Finds the values from given row of tag table (map of values and number of occurrences). Cannot be null
029     */
030    public HelpTagAction(JTable tagTable, IntFunction<String> tagKeySupplier, IntFunction<Map<String, Integer>> tagValuesSupplier) {
031        this.tagTable = Objects.requireNonNull(tagTable);
032        this.tagKeySupplier = Objects.requireNonNull(tagKeySupplier);
033        this.tagValuesSupplier = Objects.requireNonNull(tagValuesSupplier);
034        putValue(NAME, tr("Go to OSM wiki for tag help"));
035    }
036
037    @Override
038    public void actionPerformed(ActionEvent e) {
039        if (tagTable.getSelectedRowCount() == 1) {
040            int row = tagTable.getSelectedRow();
041            String key = tagKeySupplier.apply(row);
042            Map<String, Integer> m = tagValuesSupplier.apply(row);
043            if (!m.isEmpty()) {
044                String val = m.entrySet().iterator().next().getKey();
045                MainApplication.worker.execute(() -> displayTagHelp(key, val));
046            }
047        } else {
048            super.actionPerformed(e);
049        }
050    }
051}