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.SwingUtilities;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.gui.help.HelpBrowser;
|
---|
16 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
17 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
18 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Open a help browser and displays lightweight online help.
|
---|
22 | * @since 155
|
---|
23 | */
|
---|
24 | public class HelpAction extends JosmAction {
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Constructs a new {@code HelpAction}.
|
---|
28 | */
|
---|
29 | public HelpAction() {
|
---|
30 | this(true);
|
---|
31 | }
|
---|
32 |
|
---|
33 | private HelpAction(boolean shortcut) {
|
---|
34 | super(tr("Help"), "help", null,
|
---|
35 | shortcut ? Shortcut.registerShortcut("system:help", tr("Help"), KeyEvent.VK_F1, Shortcut.DIRECT) : null,
|
---|
36 | true);
|
---|
37 | setEnabled(!Main.isOffline(OnlineResource.JOSM_WEBSITE));
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Constructs a new {@code HelpAction} without assigning a shortcut.
|
---|
42 | * @return a new {@code HelpAction}
|
---|
43 | */
|
---|
44 | public static HelpAction createWithoutShortcut() {
|
---|
45 | return new HelpAction(false);
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public void actionPerformed(ActionEvent e) {
|
---|
50 | if (e.getActionCommand() == null) {
|
---|
51 | String topic;
|
---|
52 | if (e.getSource() instanceof Component) {
|
---|
53 | Component c = SwingUtilities.getRoot((Component) e.getSource());
|
---|
54 | Point mouse = c.getMousePosition();
|
---|
55 | if (mouse != null) {
|
---|
56 | c = SwingUtilities.getDeepestComponentAt(c, mouse.x, mouse.y);
|
---|
57 | topic = HelpUtil.getContextSpecificHelpTopic(c);
|
---|
58 | } else {
|
---|
59 | topic = null;
|
---|
60 | }
|
---|
61 | } else {
|
---|
62 | Point mouse = Main.parent.getMousePosition();
|
---|
63 | topic = HelpUtil.getContextSpecificHelpTopic(SwingUtilities.getDeepestComponentAt(Main.parent, mouse.x, mouse.y));
|
---|
64 | }
|
---|
65 | HelpBrowser.setUrlForHelpTopic(Optional.ofNullable(topic).orElse("/"));
|
---|
66 | } else {
|
---|
67 | HelpBrowser.setUrlForHelpTopic("/");
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|