source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/properties/HelpAction.java@ 15644

Last change on this file since 15644 was 15644, checked in by Don-vip, 4 years ago

fix #18091 - reuse Help shortcut inside tag/membership dialog

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.properties;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.io.IOException;
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.List;
11
12import javax.swing.AbstractAction;
13import javax.swing.KeyStroke;
14import javax.xml.parsers.ParserConfigurationException;
15import javax.xml.xpath.XPathExpressionException;
16
17import org.openstreetmap.josm.data.osm.IRelation;
18import org.openstreetmap.josm.gui.MainApplication;
19import org.openstreetmap.josm.spi.preferences.Config;
20import org.openstreetmap.josm.tools.ImageProvider;
21import org.openstreetmap.josm.tools.LanguageInfo;
22import org.openstreetmap.josm.tools.Logging;
23import org.openstreetmap.josm.tools.Mediawiki;
24import org.openstreetmap.josm.tools.OpenBrowser;
25import org.openstreetmap.josm.tools.Utils;
26import org.xml.sax.SAXException;
27
28/**
29 * Launch browser with wiki help for selected object.
30 * @since 13521
31 */
32public abstract class HelpAction extends AbstractAction {
33
34 /**
35 * Constructs a new {@code HelpAction}.
36 */
37 public HelpAction() {
38 putValue(SHORT_DESCRIPTION, tr("Launch browser with wiki help for selected object"));
39 new ImageProvider("dialogs", "search").getResource().attachImageIcon(this, true);
40 putValue(ACCELERATOR_KEY, getKeyStroke());
41 }
42
43 /**
44 * Returns the keystroke launching this action (F1 by default).
45 * @return the keystroke launching this action
46 */
47 public static KeyStroke getKeyStroke() {
48 return MainApplication.getMenu().help.getShortcut().getKeyStroke();
49 }
50
51 @Override
52 public void actionPerformed(ActionEvent e) {
53 // give the generic help page, if more than one element is selected
54 MainApplication.worker.execute(HelpAction::displayGenericHelp);
55 }
56
57 /**
58 * Displays the most specific wiki page for the given key/value.
59 * @param key Key
60 * @param val Value
61 * @since 14208
62 */
63 public static void displayTagHelp(String key, String val) {
64 final String lang = LanguageInfo.getWikiLanguagePrefix(LanguageInfo.LocaleType.OSM_WIKI);
65 final List<String> pages = Arrays.asList(
66 String.format("%sTag:%s=%s", lang, key, val),
67 String.format("Tag:%s=%s", key, val),
68 String.format("%sKey:%s", lang, key),
69 String.format("Key:%s", key),
70 String.format("%sMap_Features", lang),
71 "Map_Features"
72 );
73 displayHelp(pages);
74 }
75
76 /**
77 * Displays the most specific wiki page for the given relation.
78 * @param rel Relation
79 * @since 14208
80 */
81 public static void displayRelationHelp(IRelation<?> rel) {
82 final String lang = LanguageInfo.getWikiLanguagePrefix(LanguageInfo.LocaleType.OSM_WIKI);
83 final List<String> pages = new ArrayList<>();
84 String type = rel.get("type");
85 if (type != null) {
86 type = Utils.encodeUrl(type);
87 }
88
89 if (type != null && !type.isEmpty()) {
90 pages.add(String.format("%sRelation:%s", lang, type));
91 pages.add(String.format("Relation:%s", type));
92 }
93
94 pages.add(String.format("%sRelations", lang));
95 pages.add("Relations");
96 displayHelp(pages);
97 }
98
99 /**
100 * Displays the localized Map Features.
101 * @since 14208
102 */
103 public static void displayGenericHelp() {
104 final String lang = LanguageInfo.getWikiLanguagePrefix(LanguageInfo.LocaleType.OSM_WIKI);
105 final List<String> pages = Arrays.asList(
106 String.format("%sMap_Features", lang),
107 "Map_Features"
108 );
109 displayHelp(pages);
110 }
111
112 /**
113 * Display help by opening the first existing wiki page in the given list.
114 * @param pages list of wiki page names to test
115 * @since 14208
116 */
117 public static void displayHelp(final List<String> pages) {
118 try {
119 new Mediawiki(Config.getUrls().getOSMWiki())
120 .findExistingPage(pages)
121 .ifPresent(page -> OpenBrowser.displayUrl(Config.getUrls().getOSMWiki() + "/wiki/" + page));
122 } catch (IOException | ParserConfigurationException | XPathExpressionException | SAXException e1) {
123 Logging.error(e1);
124 }
125 }
126}
Note: See TracBrowser for help on using the repository browser.