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.awt.event.KeyEvent;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Objects;
|
---|
14 | import java.util.function.IntFunction;
|
---|
15 |
|
---|
16 | import javax.swing.AbstractAction;
|
---|
17 | import javax.swing.JTable;
|
---|
18 | import javax.swing.KeyStroke;
|
---|
19 | import javax.xml.parsers.ParserConfigurationException;
|
---|
20 | import javax.xml.xpath.XPathExpressionException;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.data.osm.IRelation;
|
---|
23 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
24 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
25 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
26 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
27 | import org.openstreetmap.josm.tools.Logging;
|
---|
28 | import org.openstreetmap.josm.tools.Mediawiki;
|
---|
29 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
30 | import org.openstreetmap.josm.tools.Utils;
|
---|
31 | import org.xml.sax.SAXException;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Launch browser with wiki help for selected object.
|
---|
35 | * @since 13521
|
---|
36 | */
|
---|
37 | public class HelpAction extends AbstractAction {
|
---|
38 | private final JTable tagTable;
|
---|
39 | private final IntFunction<String> tagKeySupplier;
|
---|
40 | private final IntFunction<Map<String, Integer>> tagValuesSupplier;
|
---|
41 |
|
---|
42 | private final JTable membershipTable;
|
---|
43 | private final IntFunction<IRelation<?>> memberValueSupplier;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Constructs a new {@code HelpAction}.
|
---|
47 | * @param tagTable The tag table. Cannot be null
|
---|
48 | * @param tagKeySupplier Finds the key from given row of tag table. Cannot be null
|
---|
49 | * @param tagValuesSupplier Finds the values from given row of tag table (map of values and number of occurrences). Cannot be null
|
---|
50 | * @param membershipTable The membership table. Can be null
|
---|
51 | * @param memberValueSupplier Finds the parent relation from given row of membership table. Can be null
|
---|
52 | * @since 13959 (signature)
|
---|
53 | */
|
---|
54 | public HelpAction(JTable tagTable, IntFunction<String> tagKeySupplier, IntFunction<Map<String, Integer>> tagValuesSupplier,
|
---|
55 | JTable membershipTable, IntFunction<IRelation<?>> memberValueSupplier) {
|
---|
56 | this.tagTable = Objects.requireNonNull(tagTable);
|
---|
57 | this.tagKeySupplier = Objects.requireNonNull(tagKeySupplier);
|
---|
58 | this.tagValuesSupplier = Objects.requireNonNull(tagValuesSupplier);
|
---|
59 | this.membershipTable = membershipTable;
|
---|
60 | this.memberValueSupplier = memberValueSupplier;
|
---|
61 | putValue(NAME, tr("Go to OSM wiki for tag help"));
|
---|
62 | putValue(SHORT_DESCRIPTION, tr("Launch browser with wiki help for selected object"));
|
---|
63 | new ImageProvider("dialogs", "search").getResource().attachImageIcon(this, true);
|
---|
64 | putValue(ACCELERATOR_KEY, getKeyStroke());
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Returns the keystroke launching this action (F1).
|
---|
69 | * @return the keystroke launching this action
|
---|
70 | */
|
---|
71 | public KeyStroke getKeyStroke() {
|
---|
72 | return KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public void actionPerformed(ActionEvent e) {
|
---|
77 | if (tagTable.getSelectedRowCount() == 1) {
|
---|
78 | int row = tagTable.getSelectedRow();
|
---|
79 | String key = Utils.encodeUrl(tagKeySupplier.apply(row));
|
---|
80 | Map<String, Integer> m = tagValuesSupplier.apply(row);
|
---|
81 | if (!m.isEmpty()) {
|
---|
82 | String val = Utils.encodeUrl(m.entrySet().iterator().next().getKey());
|
---|
83 | MainApplication.worker.execute(() -> displayTagHelp(key, val));
|
---|
84 | }
|
---|
85 | } else if (membershipTable != null && membershipTable.getSelectedRowCount() == 1) {
|
---|
86 | int row = membershipTable.getSelectedRow();
|
---|
87 | final IRelation<?> relation = memberValueSupplier.apply(row);
|
---|
88 | MainApplication.worker.execute(() -> displayRelationHelp(relation));
|
---|
89 | } else {
|
---|
90 | // give the generic help page, if more than one element is selected
|
---|
91 | MainApplication.worker.execute(HelpAction::displayGenericHelp);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Displays the most specific wiki page for the given key/value.
|
---|
97 | * @param key Key
|
---|
98 | * @param val Value
|
---|
99 | * @since 14208
|
---|
100 | */
|
---|
101 | public static void displayTagHelp(String key, String val) {
|
---|
102 | final String lang = LanguageInfo.getWikiLanguagePrefix(LanguageInfo.LocaleType.OSM_WIKI);
|
---|
103 | final List<String> pages = Arrays.asList(
|
---|
104 | String.format("%sTag:%s=%s", lang, key, val),
|
---|
105 | String.format("Tag:%s=%s", key, val),
|
---|
106 | String.format("%sKey:%s", lang, key),
|
---|
107 | String.format("Key:%s", key),
|
---|
108 | String.format("%sMap_Features", lang),
|
---|
109 | "Map_Features"
|
---|
110 | );
|
---|
111 | displayHelp(pages);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Displays the most specific wiki page for the given relation.
|
---|
116 | * @param rel Relation
|
---|
117 | * @since 14208
|
---|
118 | */
|
---|
119 | public static void displayRelationHelp(IRelation<?> rel) {
|
---|
120 | final String lang = LanguageInfo.getWikiLanguagePrefix(LanguageInfo.LocaleType.OSM_WIKI);
|
---|
121 | final List<String> pages = new ArrayList<>();
|
---|
122 | String type = rel.get("type");
|
---|
123 | if (type != null) {
|
---|
124 | type = Utils.encodeUrl(type);
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (type != null && !type.isEmpty()) {
|
---|
128 | pages.add(String.format("%sRelation:%s", lang, type));
|
---|
129 | pages.add(String.format("Relation:%s", type));
|
---|
130 | }
|
---|
131 |
|
---|
132 | pages.add(String.format("%sRelations", lang));
|
---|
133 | pages.add("Relations");
|
---|
134 | displayHelp(pages);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Displays the localized Map Features.
|
---|
139 | * @since 14208
|
---|
140 | */
|
---|
141 | public static void displayGenericHelp() {
|
---|
142 | final String lang = LanguageInfo.getWikiLanguagePrefix(LanguageInfo.LocaleType.OSM_WIKI);
|
---|
143 | final List<String> pages = Arrays.asList(
|
---|
144 | String.format("%sMap_Features", lang),
|
---|
145 | "Map_Features"
|
---|
146 | );
|
---|
147 | displayHelp(pages);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Display help by opening the first existing wiki page in the given list.
|
---|
152 | * @param pages list of wiki page names to test
|
---|
153 | * @since 14208
|
---|
154 | */
|
---|
155 | public static void displayHelp(final List<String> pages) {
|
---|
156 | try {
|
---|
157 | new Mediawiki(Config.getUrls().getOSMWiki())
|
---|
158 | .findExistingPage(pages)
|
---|
159 | .ifPresent(page -> OpenBrowser.displayUrl(Config.getUrls().getOSMWiki() + "/wiki/" + page));
|
---|
160 | } catch (IOException | ParserConfigurationException | XPathExpressionException | SAXException e1) {
|
---|
161 | Logging.error(e1);
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|