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

Last change on this file since 12504 was 11553, checked in by Don-vip, 7 years ago

refactor handling of null values - use Java 8 Optional where possible

  • Property svn:eol-style set to native
File size: 2.3 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.SwingUtilities;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.gui.help.HelpBrowser;
16import org.openstreetmap.josm.gui.help.HelpUtil;
17import org.openstreetmap.josm.io.OnlineResource;
18import org.openstreetmap.josm.tools.Shortcut;
19
20/**
21 * Open a help browser and displays lightweight online help.
22 * @since 155
23 */
24public 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}
Note: See TracBrowser for help on using the repository browser.