001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.help;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.event.ActionEvent;
008import java.util.Optional;
009
010import javax.swing.AbstractAction;
011
012import org.openstreetmap.josm.Main;
013import org.openstreetmap.josm.io.OnlineResource;
014import org.openstreetmap.josm.tools.ImageProvider;
015
016/**
017 * This is the standard help action to be used with help buttons for
018 * context sensitive help
019 * @since 2289
020 */
021public class ContextSensitiveHelpAction extends AbstractAction {
022
023    private String helpTopic;
024
025    /**
026     * Sets the help topic.
027     *
028     * @param relativeHelpTopic the relative help topic
029     */
030    public void setHelpTopic(String relativeHelpTopic) {
031        helpTopic = Optional.ofNullable(relativeHelpTopic).orElse("/");
032    }
033
034    /**
035     * Constructs a new {@code ContextSensitiveHelpAction} for the root help topic.
036     */
037    public ContextSensitiveHelpAction() {
038        this(ht("/"));
039    }
040
041    /**
042     * Constructs a new {@code ContextSensitiveHelpAction} for a given help topic.
043     * @param helpTopic The help topic
044     */
045    public ContextSensitiveHelpAction(String helpTopic) {
046        putValue(SHORT_DESCRIPTION, tr("Show help information"));
047        putValue(NAME, tr("Help"));
048        new ImageProvider("help").getResource().attachImageIcon(this);
049        this.helpTopic = helpTopic;
050        setEnabled(!Main.isOffline(OnlineResource.JOSM_WEBSITE));
051    }
052
053    @Override
054    public void actionPerformed(ActionEvent e) {
055        if (helpTopic != null) {
056            HelpBrowser.setUrlForHelpTopic(helpTopic);
057        }
058    }
059}