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