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;
009
010import javax.swing.AbstractAction;
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.ImageProvider;
018
019/**
020 * Open a help browser and displays lightweight online help.
021 * @since 155
022 */
023public class HelpAction extends AbstractAction {
024
025    /**
026     * Constructs a new {@code HelpAction}.
027     */
028    public HelpAction() {
029        super(tr("Help"), ImageProvider.get("help"));
030        putValue("toolbar", "help");
031        setEnabled(!Main.isOffline(OnlineResource.JOSM_WEBSITE));
032    }
033
034    @Override
035    public void actionPerformed(ActionEvent e) {
036        if (e.getActionCommand() == null) {
037            String topic;
038            if (e.getSource() instanceof Component) {
039                Component c = SwingUtilities.getRoot((Component)e.getSource());
040                Point mouse = c.getMousePosition();
041                if (mouse != null) {
042                    c = SwingUtilities.getDeepestComponentAt(c, mouse.x, mouse.y);
043                    topic = HelpUtil.getContextSpecificHelpTopic(c);
044                } else {
045                    topic = null;
046                }
047            } else {
048                Point mouse = Main.parent.getMousePosition();
049                topic = HelpUtil.getContextSpecificHelpTopic(SwingUtilities.getDeepestComponentAt(Main.parent, mouse.x, mouse.y));
050            }
051            if (topic == null) {
052                HelpBrowser.setUrlForHelpTopic("/");
053            } else {
054                HelpBrowser.setUrlForHelpTopic(topic);
055            }
056        } else {
057            HelpBrowser.setUrlForHelpTopic("/");
058        }
059    }
060}