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

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

fix #18409 - distinct label for relation wiki link in Membership Context Menu

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