source: josm/trunk/src/org/openstreetmap/josm/actions/HelpAction.java

Last change on this file was 18111, checked in by Don-vip, 3 years ago

fix #21148 - fix context specific help (F1) usage in menus (patch by DevCharly)

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.Point;
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.util.Optional;
11
12import javax.swing.MenuElement;
13import javax.swing.MenuSelectionManager;
14import javax.swing.SwingUtilities;
15
16import org.openstreetmap.josm.gui.MainApplication;
17import org.openstreetmap.josm.gui.help.HelpBrowser;
18import org.openstreetmap.josm.gui.help.HelpUtil;
19import org.openstreetmap.josm.io.NetworkManager;
20import org.openstreetmap.josm.io.OnlineResource;
21import org.openstreetmap.josm.tools.Shortcut;
22
23/**
24 * Open a help browser and displays lightweight online help.
25 * @since 155
26 */
27public class HelpAction extends JosmAction {
28
29 /**
30 * Constructs a new {@code HelpAction}.
31 */
32 public HelpAction() {
33 this(true);
34 }
35
36 private HelpAction(boolean shortcut) {
37 super(tr("Help"), "help", null,
38 shortcut ? Shortcut.registerShortcut("system:help", tr("Help: {0}", tr("Help")), KeyEvent.VK_F1, Shortcut.DIRECT) : null,
39 true, false);
40 setEnabled(!NetworkManager.isOffline(OnlineResource.JOSM_WEBSITE));
41 }
42
43 /**
44 * Constructs a new {@code HelpAction} without assigning a shortcut.
45 * @return a new {@code HelpAction}
46 */
47 public static HelpAction createWithoutShortcut() {
48 return new HelpAction(false);
49 }
50
51 @Override
52 public void actionPerformed(ActionEvent e) {
53 if (e.getActionCommand() == null) {
54 String topic;
55 MenuElement[] menuPath = MenuSelectionManager.defaultManager().getSelectedPath();
56 if (menuPath.length > 0) {
57 // Get help topic from last element in selected menu path (usually a JMenuItem).
58 // If a JMenu is selected, which shows a JPopupMenu, then the last path element
59 // is a JPopupMenu and it is necessary to look also into previous path elements.
60 topic = null;
61 for (int i = menuPath.length - 1; i >= 0; i--) {
62 Component c = menuPath[i].getComponent();
63 topic = HelpUtil.getContextSpecificHelpTopic(c);
64 if (topic != null) {
65 break;
66 }
67 }
68 } else if (e.getSource() instanceof Component) {
69 Component c = SwingUtilities.getRoot((Component) e.getSource());
70 Point mouse = c.getMousePosition();
71 if (mouse != null) {
72 c = SwingUtilities.getDeepestComponentAt(c, mouse.x, mouse.y);
73 topic = HelpUtil.getContextSpecificHelpTopic(c);
74 } else {
75 topic = null;
76 }
77 } else {
78 Point mouse = MainApplication.getMainFrame().getMousePosition();
79 topic = HelpUtil.getContextSpecificHelpTopic(
80 SwingUtilities.getDeepestComponentAt(MainApplication.getMainFrame(), mouse.x, mouse.y));
81 }
82 HelpBrowser.setUrlForHelpTopic(Optional.ofNullable(topic).orElse("/"));
83 } else {
84 HelpBrowser.setUrlForHelpTopic("/");
85 }
86 }
87}
Note: See TracBrowser for help on using the repository browser.