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

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

sonar - place Strings literals on the left side when checking for equality (can avoid NPEs)

  • Property svn:eol-style set to native
File size: 8.1 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 topicUrl = topicUrl.replaceAll("#[^#]*$", ""); // remove optional fragment
72 return topicUrl + "?action=edit";
73 }
74
75 /**
76 * Extracts the relative help topic from an URL. Replies null, if
77 * no relative help topic is found.
78 *
79 * @param url the url
80 * @return the relative help topic in the URL, i.e. "/Action/New"
81 */
82 public static String extractRelativeHelpTopic(String url) {
83 String topic = extractAbsoluteHelpTopic(url);
84 if (topic == null)
85 return null;
86 String pattern = "/[A-Z][a-z]{1,2}(_[A-Z]{2})?:" + getHelpTopicPrefix(LocaleType.ENGLISH).replaceAll("^\\/+", "");
87 if (url.matches(pattern)) {
88 return topic.substring(pattern.length());
89 }
90 return null;
91 }
92
93 /**
94 * Extracts the absolute help topic from an URL. Replies null, if
95 * no absolute help topic is found.
96 *
97 * @param url the url
98 * @return the absolute help topic in the URL, i.e. "/De:Help/Action/New"
99 */
100 public static String extractAbsoluteHelpTopic(String url) {
101 if (!url.startsWith(getWikiBaseHelpUrl())) return null;
102 url = url.substring(getWikiBaseHelpUrl().length());
103 String prefix = getHelpTopicPrefix(LocaleType.ENGLISH);
104 if (url.startsWith(prefix))
105 return url;
106
107 String pattern = "/[A-Z][a-z]{1,2}(_[A-Z]{2})?:" + prefix.replaceAll("^\\/+", "");
108 if (url.matches(pattern))
109 return url;
110
111 return null;
112 }
113
114 /**
115 * Replies the help topic prefix for the given locale. Examples:
116 * <ul>
117 * <li>/Help if the locale is a locale with language "en"</li>
118 * <li>/De:Help if the locale is a locale with language "de"</li>
119 * </ul>
120 *
121 * @param type the type of the locale to use
122 * @return the help topic prefix
123 * @since 5915
124 */
125 private static String getHelpTopicPrefix(LocaleType type) {
126 String ret = LanguageInfo.getWikiLanguagePrefix(type);
127 if(ret == null)
128 return ret;
129 ret = "/" + ret + Main.pref.get("help.pathhelp", "/Help").replaceAll("^\\/+", ""); // remove leading /
130 return ret.replaceAll("\\/+", "\\/"); // collapse sequences of //
131 }
132
133 /**
134 * Replies the absolute, localized help topic for the given topic.
135 *
136 * Example: for a topic "/Dialog/RelationEditor" and the locale "de", this method
137 * replies "/De:Help/Dialog/RelationEditor"
138 *
139 * @param topic the relative help topic. Home help topic assumed, if null.
140 * @param type the locale. {@link Locale#ENGLISH} assumed, if null.
141 * @return the absolute, localized help topic
142 * @since 5915
143 */
144 public static String buildAbsoluteHelpTopic(String topic, LocaleType type) {
145 String prefix = getHelpTopicPrefix(type);
146 if (prefix == null || topic == null || topic.trim().length() == 0 || "/".equals(topic.trim()))
147 return prefix;
148 prefix += "/" + topic;
149 return prefix.replaceAll("\\/+", "\\/"); // collapse sequences of //
150 }
151
152 /**
153 * Replies the context specific help topic configured for <code>context</code>.
154 *
155 * @return the help topic. null, if no context specific help topic is found
156 */
157 public static String getContextSpecificHelpTopic(Object context) {
158 if (context == null)
159 return null;
160 if (context instanceof Helpful)
161 return ((Helpful)context).helpTopic();
162 if (context instanceof JMenu) {
163 JMenu b = (JMenu)context;
164 if (b.getClientProperty("help") != null)
165 return (String)b.getClientProperty("help");
166 return null;
167 }
168 if (context instanceof AbstractButton) {
169 AbstractButton b = (AbstractButton)context;
170 if (b.getClientProperty("help") != null)
171 return (String)b.getClientProperty("help");
172 return getContextSpecificHelpTopic(b.getAction());
173 }
174 if (context instanceof Action)
175 return (String)((Action)context).getValue("help");
176 if (context instanceof JComponent && ((JComponent)context).getClientProperty("help") != null)
177 return (String)((JComponent)context).getClientProperty("help");
178 if (context instanceof Component)
179 return getContextSpecificHelpTopic(((Component)context).getParent());
180 return null;
181 }
182
183 /**
184 * Replies the global help action, if available. Otherwise, creates an instance
185 * of {@link HelpAction}.
186 *
187 * @return instance of help action
188 */
189 private static Action getHelpAction() {
190 if (Main.main != null && Main.main.menu != null) {
191 return Main.main.menu.help;
192 }
193 return new HelpAction();
194 }
195
196 /**
197 * Makes a component aware of context sensitive help.
198 *
199 * A relative help topic doesn't start with /Help and doesn't include a locale
200 * code. Example: /Dialog/RelationEditor is a relative help topic, /De:Help/Dialog/RelationEditor
201 * is not.
202 *
203 * @param component the component the component
204 * @param relativeHelpTopic the help topic. Set to the default help topic if null.
205 */
206 public static void setHelpContext(JComponent component, String relativeHelpTopic) {
207 if (relativeHelpTopic == null) {
208 relativeHelpTopic = "/";
209 }
210 component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("F1"), "help");
211 component.getActionMap().put("help", getHelpAction());
212 component.putClientProperty("help", relativeHelpTopic);
213 }
214
215 /**
216 * This is a simple marker method for help topic literals. If you declare a help
217 * topic literal in the source you should enclose it in ht(...).
218 *
219 * <strong>Example</strong>
220 * <pre>
221 * String helpTopic = ht("/Dialog/RelationEditor");
222 * or
223 * putValue("help", ht("/Dialog/RelationEditor"));
224 * </pre>
225 *
226 * @param helpTopic Help topic to mark
227 * @return {@code helpTopic}
228 */
229 public static String ht(String helpTopic) {
230 // this is just a marker method
231 return helpTopic;
232 }
233}
Note: See TracBrowser for help on using the repository browser.