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;
010import java.util.function.Supplier;
011
012import javax.swing.JTable;
013
014import org.openstreetmap.josm.data.osm.Tag;
015import org.openstreetmap.josm.gui.MainApplication;
016
017/**
018 * Launch browser with wiki help for selected tag.
019 * @since 15581
020 */
021public class HelpTagAction extends HelpAction {
022    private final Supplier<Tag> tagSupplier;
023
024    /**
025     * Constructs a new {@code HelpAction}.
026     * @param tagSupplier Supplies the tag for which the help should be shown
027     * @since 16274
028     */
029    public HelpTagAction(Supplier<Tag> tagSupplier) {
030        this.tagSupplier = Objects.requireNonNull(tagSupplier);
031        putValue(NAME, tr("Go to OSM wiki for tag help"));
032    }
033
034    /**
035     * Constructs a new {@code HelpAction}.
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     */
040    public HelpTagAction(JTable tagTable, IntFunction<String> tagKeySupplier, IntFunction<Map<String, Integer>> tagValuesSupplier) {
041        this.tagSupplier = () -> {
042            if (tagTable.getSelectedRowCount() == 1) {
043                int row = tagTable.getSelectedRow();
044                String key = tagKeySupplier.apply(row);
045                Map<String, Integer> m = tagValuesSupplier.apply(row);
046                if (!m.isEmpty()) {
047                    String val = m.entrySet().iterator().next().getKey();
048                    return new Tag(key, val);
049                }
050            }
051            return null;
052        };
053        putValue(NAME, tr("Go to OSM wiki for tag help"));
054    }
055
056    @Override
057    public void actionPerformed(ActionEvent e) {
058        Tag tag = tagSupplier.get();
059        if (tag != null) {
060            MainApplication.worker.execute(() -> displayTagHelp(tag.getKey(), tag.getValue()));
061        } else {
062            super.actionPerformed(e);
063        }
064    }
065}