001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Component;
007import java.awt.Point;
008import java.awt.event.ActionEvent;
009import java.awt.event.KeyEvent;
010import java.util.Optional;
011
012import javax.swing.SwingUtilities;
013
014import org.openstreetmap.josm.Main;
015import org.openstreetmap.josm.gui.help.HelpBrowser;
016import org.openstreetmap.josm.gui.help.HelpUtil;
017import org.openstreetmap.josm.io.OnlineResource;
018import org.openstreetmap.josm.tools.Shortcut;
019
020/**
021 * Open a help browser and displays lightweight online help.
022 * @since 155
023 */
024public class HelpAction extends JosmAction {
025
026    /**
027     * Constructs a new {@code HelpAction}.
028     */
029    public HelpAction() {
030        this(true);
031    }
032
033    private HelpAction(boolean shortcut) {
034        super(tr("Help"), "help", null,
035                shortcut ? Shortcut.registerShortcut("system:help", tr("Help"), KeyEvent.VK_F1, Shortcut.DIRECT) : null,
036                true);
037        setEnabled(!Main.isOffline(OnlineResource.JOSM_WEBSITE));
038    }
039
040    /**
041     * Constructs a new {@code HelpAction} without assigning a shortcut.
042     * @return a new {@code HelpAction}
043     */
044    public static HelpAction createWithoutShortcut() {
045        return new HelpAction(false);
046    }
047
048    @Override
049    public void actionPerformed(ActionEvent e) {
050        if (e.getActionCommand() == null) {
051            String topic;
052            if (e.getSource() instanceof Component) {
053                Component c = SwingUtilities.getRoot((Component) e.getSource());
054                Point mouse = c.getMousePosition();
055                if (mouse != null) {
056                    c = SwingUtilities.getDeepestComponentAt(c, mouse.x, mouse.y);
057                    topic = HelpUtil.getContextSpecificHelpTopic(c);
058                } else {
059                    topic = null;
060                }
061            } else {
062                Point mouse = Main.parent.getMousePosition();
063                topic = HelpUtil.getContextSpecificHelpTopic(SwingUtilities.getDeepestComponentAt(Main.parent, mouse.x, mouse.y));
064            }
065            HelpBrowser.setUrlForHelpTopic(Optional.ofNullable(topic).orElse("/"));
066        } else {
067            HelpBrowser.setUrlForHelpTopic("/");
068        }
069    }
070}