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;
008
009import javax.swing.AbstractAction;
010
011import org.openstreetmap.josm.Main;
012import org.openstreetmap.josm.io.OnlineResource;
013import org.openstreetmap.josm.tools.ImageProvider;
014
015/**
016 * This is the standard help action to be used with help buttons for
017 * context sensitive help
018 * @since 2289
019 */
020public class ContextSensitiveHelpAction extends AbstractAction {
021
022    private String helpTopic;
023
024    /**
025     * Sets the help topic.
026     *
027     * @param relativeHelpTopic the relative help topic
028     */
029    public void setHelpTopic(String relativeHelpTopic) {
030        if (relativeHelpTopic == null)
031            relativeHelpTopic = "/";
032        this.helpTopic = relativeHelpTopic;
033    }
034
035    /**
036     * Constructs a new {@code ContextSensitiveHelpAction} for the root help topic.
037     */
038    public ContextSensitiveHelpAction() {
039        this(ht("/"));
040    }
041
042    /**
043     * Constructs a new {@code ContextSensitiveHelpAction} for a given help topic.
044     * @param helpTopic The help topic
045     */
046    public ContextSensitiveHelpAction(String helpTopic) {
047        putValue(SHORT_DESCRIPTION, tr("Show help information"));
048        putValue(NAME, tr("Help"));
049        new ImageProvider("help").getResource().attachImageIcon(this);
050        this.helpTopic = helpTopic;
051        setEnabled(!Main.isOffline(OnlineResource.JOSM_WEBSITE));
052    }
053
054    @Override
055    public void actionPerformed(ActionEvent e) {
056        if (helpTopic != null) {
057            HelpBrowser.setUrlForHelpTopic(helpTopic);
058        }
059    }
060}