1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.properties;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import javax.swing.AbstractAction;
|
---|
13 | import javax.swing.KeyStroke;
|
---|
14 | import javax.xml.parsers.ParserConfigurationException;
|
---|
15 | import javax.xml.xpath.XPathExpressionException;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.data.osm.IRelation;
|
---|
18 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
19 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
20 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
21 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
22 | import org.openstreetmap.josm.tools.Logging;
|
---|
23 | import org.openstreetmap.josm.tools.Mediawiki;
|
---|
24 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
25 | import org.openstreetmap.josm.tools.Utils;
|
---|
26 | import org.xml.sax.SAXException;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Launch browser with wiki help for selected object.
|
---|
30 | * @since 13521
|
---|
31 | */
|
---|
32 | public abstract class HelpAction extends AbstractAction {
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Constructs a new {@code HelpAction}.
|
---|
36 | */
|
---|
37 | protected HelpAction() {
|
---|
38 | putValue(SHORT_DESCRIPTION, tr("Launch browser with wiki help for selected object"));
|
---|
39 | new ImageProvider("help", "internet").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 (!Utils.isEmpty(type)) {
|
---|
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 | }
|
---|