1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Point;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.util.Optional;
|
---|
11 |
|
---|
12 | import javax.swing.MenuElement;
|
---|
13 | import javax.swing.MenuSelectionManager;
|
---|
14 | import javax.swing.SwingUtilities;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
17 | import org.openstreetmap.josm.gui.help.HelpBrowser;
|
---|
18 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
19 | import org.openstreetmap.josm.io.NetworkManager;
|
---|
20 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
21 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Open a help browser and displays lightweight online help.
|
---|
25 | * @since 155
|
---|
26 | */
|
---|
27 | public 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 | }
|
---|