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

Last change on this file since 6268 was 6143, checked in by Don-vip, 11 years ago

see #8885 - cleanup in command line arguments and website URL handling

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