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

Last change on this file since 2347 was 2347, checked in by Gubaer, 14 years ago

fixed #3802: Buttons dont't react to enter in download dialog

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