source: josm/trunk/src/org/openstreetmap/josm/gui/help/HelpUtil.java@ 11397

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

sonar - squid:S2259 - Null pointers should not be dereferenced

  • Property svn:eol-style set to native
File size: 8.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.help;
3
4import java.awt.Component;
5import java.util.Locale;
6
7import javax.swing.AbstractButton;
8import javax.swing.Action;
9import javax.swing.JComponent;
10import javax.swing.JMenu;
11import javax.swing.KeyStroke;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.actions.HelpAction;
15import org.openstreetmap.josm.tools.LanguageInfo;
16import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
17
18/**
19 * Provides utility methods for help system.
20 * @since 2252
21 */
22public final class HelpUtil {
23
24 private HelpUtil() {
25 // Hide default constructor for utils classes
26 }
27
28 /**
29 * Replies the base wiki URL.
30 *
31 * @return the base wiki URL
32 */
33 public static String getWikiBaseUrl() {
34 return Main.pref.get("help.baseurl", Main.getJOSMWebsite());
35 }
36
37 /**
38 * Replies the base wiki URL for help pages
39 *
40 * @return the base wiki URL for help pages
41 */
42 public static String getWikiBaseHelpUrl() {
43 return getWikiBaseUrl() + "/wiki";
44 }
45
46 /**
47 * Replies the URL on the wiki for an absolute help topic. The URL is encoded in UTF-8.
48 *
49 * @param absoluteHelpTopic the absolute help topic
50 * @return the url
51 * @see #buildAbsoluteHelpTopic
52 */
53 public static String getHelpTopicUrl(String absoluteHelpTopic) {
54 if (absoluteHelpTopic == null)
55 return null;
56 String ret = getWikiBaseHelpUrl();
57 ret = ret.replaceAll("\\/+$", "");
58 absoluteHelpTopic = absoluteHelpTopic.replace(" ", "%20");
59 absoluteHelpTopic = absoluteHelpTopic.replaceAll("^\\/+", "/");
60 return ret + absoluteHelpTopic;
61 }
62
63 /**
64 * Replies the URL to the edit page for the absolute help topic.
65 *
66 * @param absoluteHelpTopic the absolute help topic
67 * @return the URL to the edit page
68 */
69 public static String getHelpTopicEditUrl(String absoluteHelpTopic) {
70 String topicUrl = getHelpTopicUrl(absoluteHelpTopic);
71 if (topicUrl != null)
72 topicUrl = topicUrl.replaceAll("#[^#]*$", ""); // remove optional fragment
73 return topicUrl + "?action=edit";
74 }
75
76 /**
77 * Extracts the relative help topic from an URL. Replies null, if
78 * no relative help topic is found.
79 *
80 * @param url the url
81 * @return the relative help topic in the URL, i.e. "/Action/New"
82 */
83 public static String extractRelativeHelpTopic(String url) {
84 String topic = extractAbsoluteHelpTopic(url);
85 if (topic == null)
86 return null;
87 String topicPrefix = getHelpTopicPrefix(LocaleType.ENGLISH);
88 if (topicPrefix != null)
89 topicPrefix = topicPrefix.replaceAll("^\\/+", "");
90 String pattern = "/[A-Z][a-z]{1,2}(_[A-Z]{2})?:" + topicPrefix;
91 if (url.matches(pattern)) {
92 return topic.substring(pattern.length());
93 }
94 return null;
95 }
96
97 /**
98 * Extracts the absolute help topic from an URL. Replies null, if
99 * no absolute help topic is found.
100 *
101 * @param url the url
102 * @return the absolute help topic in the URL, i.e. "/De:Help/Action/New"
103 */
104 public static String extractAbsoluteHelpTopic(String url) {
105 if (url == null || !url.startsWith(getWikiBaseHelpUrl())) return null;
106 String topic = url.substring(getWikiBaseHelpUrl().length());
107 String prefix = getHelpTopicPrefix(LocaleType.ENGLISH);
108 if (prefix == null || topic.startsWith(prefix))
109 return topic;
110
111 String pattern = "/[A-Z][a-z]{1,2}(_[A-Z]{2})?:" + prefix.replaceAll("^\\/+", "");
112 if (topic.matches(pattern))
113 return topic;
114
115 return null;
116 }
117
118 /**
119 * Replies the help topic prefix for the given locale. Examples:
120 * <ul>
121 * <li>/Help if the locale is a locale with language "en"</li>
122 * <li>/De:Help if the locale is a locale with language "de"</li>
123 * </ul>
124 *
125 * @param type the type of the locale to use
126 * @return the help topic prefix
127 * @since 5915
128 */
129 private static String getHelpTopicPrefix(LocaleType type) {
130 String ret = LanguageInfo.getWikiLanguagePrefix(type);
131 if (ret == null)
132 return ret;
133 ret = '/' + ret + Main.pref.get("help.pathhelp", "/Help").replaceAll("^\\/+", ""); // remove leading /
134 return ret.replaceAll("\\/+", "\\/"); // collapse sequences of //
135 }
136
137 /**
138 * Replies the absolute, localized help topic for the given topic.
139 *
140 * Example: for a topic "/Dialog/RelationEditor" and the locale "de", this method
141 * replies "/De:Help/Dialog/RelationEditor"
142 *
143 * @param topic the relative help topic. Home help topic assumed, if null.
144 * @param type the locale. {@link Locale#ENGLISH} assumed, if null.
145 * @return the absolute, localized help topic
146 * @since 5915
147 */
148 public static String buildAbsoluteHelpTopic(String topic, LocaleType type) {
149 String prefix = getHelpTopicPrefix(type);
150 if (prefix == null || topic == null || topic.trim().isEmpty() || "/".equals(topic.trim()))
151 return prefix;
152 prefix += '/' + topic;
153 return prefix.replaceAll("\\/+", "\\/"); // collapse sequences of //
154 }
155
156 /**
157 * Replies the context specific help topic configured for <code>context</code>.
158 * @param context The UI object used as context
159 *
160 * @return the help topic. null, if no context specific help topic is found
161 */
162 public static String getContextSpecificHelpTopic(Object context) {
163 if (context == null)
164 return null;
165 if (context instanceof Helpful)
166 return ((Helpful) context).helpTopic();
167 if (context instanceof JMenu) {
168 JMenu b = (JMenu) context;
169 if (b.getClientProperty("help") != null)
170 return (String) b.getClientProperty("help");
171 return null;
172 }
173 if (context instanceof AbstractButton) {
174 AbstractButton b = (AbstractButton) context;
175 if (b.getClientProperty("help") != null)
176 return (String) b.getClientProperty("help");
177 return getContextSpecificHelpTopic(b.getAction());
178 }
179 if (context instanceof Action)
180 return (String) ((Action) context).getValue("help");
181 if (context instanceof JComponent && ((JComponent) context).getClientProperty("help") != null)
182 return (String) ((JComponent) context).getClientProperty("help");
183 if (context instanceof Component)
184 return getContextSpecificHelpTopic(((Component) context).getParent());
185 return null;
186 }
187
188 /**
189 * Replies the global help action, if available. Otherwise, creates an instance
190 * of {@link HelpAction}.
191 *
192 * @return instance of help action
193 */
194 private static Action getHelpAction() {
195 if (Main.main != null && Main.main.menu != null) {
196 return Main.main.menu.help;
197 }
198 return HelpAction.createWithoutShortcut();
199 }
200
201 /**
202 * Makes a component aware of context sensitive help.
203 *
204 * A relative help topic doesn't start with /Help and doesn't include a locale
205 * code. Example: /Dialog/RelationEditor is a relative help topic, /De:Help/Dialog/RelationEditor
206 * is not.
207 *
208 * @param component the component
209 * @param relativeHelpTopic the help topic. Set to the default help topic if null.
210 */
211 public static void setHelpContext(JComponent component, String relativeHelpTopic) {
212 component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("F1"), "help");
213 component.getActionMap().put("help", getHelpAction());
214 component.putClientProperty("help", relativeHelpTopic == null ? "/" : relativeHelpTopic);
215 }
216
217 /**
218 * This is a simple marker method for help topic literals. If you declare a help
219 * topic literal in the source you should enclose it in ht(...).
220 *
221 * <strong>Example</strong>
222 * <pre>
223 * String helpTopic = ht("/Dialog/RelationEditor");
224 * or
225 * putValue("help", ht("/Dialog/RelationEditor"));
226 * </pre>
227 *
228 * @param helpTopic Help topic to mark
229 * @return {@code helpTopic}
230 */
231 public static String ht(String helpTopic) {
232 // this is just a marker method
233 return helpTopic;
234 }
235}
Note: See TracBrowser for help on using the repository browser.