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