source: josm/trunk/src/org/openstreetmap/josm/actions/HelpAction.java@ 2017

Last change on this file since 2017 was 2017, checked in by Gubaer, 15 years ago

removed OptionPaneUtil
cleanup of deprecated Layer API
cleanup of deprecated APIs in OsmPrimitive and Way
cleanup of imports

  • Property svn:eol-style set to native
File size: 8.8 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Component;
8import java.awt.Point;
9import java.awt.event.ActionEvent;
10import java.awt.event.KeyEvent;
11import java.awt.event.WindowAdapter;
12import java.awt.event.WindowEvent;
13import java.io.IOException;
14
15import javax.swing.AbstractAction;
16import javax.swing.AbstractButton;
17import javax.swing.Action;
18import javax.swing.JButton;
19import javax.swing.JComponent;
20import javax.swing.JEditorPane;
21import javax.swing.JFrame;
22import javax.swing.JMenu;
23import javax.swing.JOptionPane;
24import javax.swing.JPanel;
25import javax.swing.JScrollPane;
26import javax.swing.KeyStroke;
27import javax.swing.SwingUtilities;
28import javax.swing.event.HyperlinkEvent;
29import javax.swing.event.HyperlinkListener;
30
31import org.openstreetmap.josm.Main;
32import org.openstreetmap.josm.tools.ImageProvider;
33import org.openstreetmap.josm.tools.LanguageInfo;
34import org.openstreetmap.josm.tools.OpenBrowser;
35import org.openstreetmap.josm.tools.WikiReader;
36
37/**
38 * Open a help browser and displays lightweight online help.
39 *
40 * @author imi
41 */
42public class HelpAction extends AbstractAction {
43
44 public interface Helpful {
45 String helpTopic();
46 }
47
48 private String languageCode = LanguageInfo.getLanguageCodeWiki();
49 private JFrame helpBrowser = new JFrame(tr("JOSM Online Help"));
50 private String baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de");
51 private String pathbase = Main.pref.get("help.pathbase", "/wiki/");
52 private String pathhelp = Main.pref.get("help.pathhelp", "Help/");
53 private String pathmenu = Main.pref.get("help.pathmenu", "Menu/");
54 private JEditorPane help = new JEditorPane();
55 private WikiReader reader = new WikiReader(baseurl);
56 private String url;
57
58 public HelpAction() {
59 super(tr("Help"), ImageProvider.get("help"));
60 help.setEditable(false);
61 help.addHyperlinkListener(new HyperlinkListener(){
62 public void hyperlinkUpdate(HyperlinkEvent e) {
63 if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED)
64 return;
65 if (e.getURL() == null) {
66 help.setText("<html>404 not found</html>");
67 } else if (e.getURL().toString().endsWith("action=edit")) {
68 OpenBrowser.displayUrl(e.getURL().toString());
69 } else {
70 setHelpUrl(e.getURL().toString());
71 }
72 }
73 });
74 help.setContentType("text/html");
75
76 JPanel p = new JPanel(new BorderLayout());
77 helpBrowser.setContentPane(p);
78
79 p.add(new JScrollPane(help), BorderLayout.CENTER);
80 String[] bounds = Main.pref.get("help.window.bounds", "0,0,800,600").split(",");
81 helpBrowser.setBounds(
82 Integer.parseInt(bounds[0]),
83 Integer.parseInt(bounds[1]),
84 Integer.parseInt(bounds[2]),
85 Integer.parseInt(bounds[3]));
86
87 JPanel buttons = new JPanel();
88 p.add(buttons, BorderLayout.SOUTH);
89 createButton(buttons, tr("Open in Browser"));
90 createButton(buttons, tr("Edit"));
91 createButton(buttons, tr("Reload"));
92
93 helpBrowser.addWindowListener(new WindowAdapter(){
94 @Override public void windowClosing(WindowEvent e) {
95 closeHelp();
96 }
97 });
98
99 help.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Close");
100 help.getActionMap().put("Close", new AbstractAction(){
101 public void actionPerformed(ActionEvent e) {
102 closeHelp();
103 }
104 });
105 }
106
107 public void actionPerformed(ActionEvent e) {
108 if (tr("Open in Browser").equals(e.getActionCommand())) {
109 OpenBrowser.displayUrl(url);
110 } else if (tr("Edit").equals(e.getActionCommand())) {
111 if (!url.startsWith(baseurl)) {
112 JOptionPane.showMessageDialog(
113 Main.parent,
114 tr("Can only edit help pages from JOSM Online Help"),
115 tr("Warning"),
116 JOptionPane.WARNING_MESSAGE
117 );
118 return;
119 }
120 OpenBrowser.displayUrl(url+"?action=edit");
121 } else if (tr("Reload").equals(e.getActionCommand())) {
122 setHelpUrl(url);
123 } else if (e.getActionCommand() == null) {
124 String topic = null;
125 Point mouse = Main.parent.getMousePosition();
126 if (mouse != null) {
127 topic = contextSensitiveHelp(SwingUtilities.getDeepestComponentAt(Main.parent, mouse.x, mouse.y));
128 }
129 if (topic == null) {
130 helpBrowser.setVisible(false);
131 setHelpUrl(baseurl+pathbase+"Help");
132 } else {
133 help(topic);
134 }
135 } else {
136 helpBrowser.setVisible(false);
137 setHelpUrl(baseurl+pathbase+"Help");
138 }
139 }
140
141 /**
142 * @return The topic of the help. <code>null</code> for "don't know"
143 */
144 private String contextSensitiveHelp(Object c) {
145 if (c instanceof Helpful)
146 return ((Helpful)c).helpTopic();
147 if (c instanceof JMenu) {
148 JMenu b = (JMenu)c;
149 if (b.getClientProperty("help") != null)
150 return (String)b.getClientProperty("help");
151 return pathmenu+b.getText();
152 }
153 if (c instanceof AbstractButton) {
154 AbstractButton b = (AbstractButton)c;
155 if (b.getClientProperty("help") != null)
156 return (String)b.getClientProperty("help");
157 return contextSensitiveHelp(((AbstractButton)c).getAction());
158 }
159 if (c instanceof Action)
160 return (String)((Action)c).getValue("help");
161 if (c instanceof Component)
162 return contextSensitiveHelp(((Component)c).getParent());
163 return null;
164 }
165
166 /**
167 * Displays the help (or browse on the already open help) on the online page
168 * with the given help topic. Use this for larger help descriptions.
169 */
170 public void help(String topic) {
171 helpBrowser.setVisible(false);
172 setHelpUrl(baseurl+pathbase+pathhelp+topic);
173 }
174
175 /**
176 * Set the content of the help window to a specific text (in html format)
177 * @param url The url this content is the representation of
178 */
179 public void setHelpUrl(String url) {
180 String langurl = url;
181 if(url.startsWith(baseurl+pathbase))
182 {
183 int i = pathbase.length()+baseurl.length();
184 String title = url.substring(i);
185 if(languageCode.length() != 0 && !title.startsWith(languageCode)) {
186 title = languageCode + title;
187 }
188 langurl = url.substring(0, i) + title;
189 }
190 boolean loaded = false;
191 if(!langurl.equals(this.url) && !langurl.equals(url))
192 {
193 loaded = loadHelpUrl(url, langurl, true);
194 }
195 if(!loaded) {
196 loaded = loadHelpUrl(url, langurl, false);
197 }
198 if(!loaded) {
199 help.setText(tr("Error while loading page {0}",url));
200 }
201 helpBrowser.setVisible(true);
202 }
203
204 private boolean loadHelpUrl(String url, String langurl, boolean lang)
205 {
206 this.url = lang ? langurl : url;
207 boolean loaded = false;
208 try {
209 String txt = reader.read(this.url);
210 if(txt.length() == 0)
211 {
212 if(lang)
213 throw new IOException();
214 else
215 {
216 if(url.equals(langurl))
217 {
218 txt = ("<HTML>"+tr("Help page missing. Create it in <A HREF=\"{0}\">english</A>.",
219 url+"?action=edit")+"</HTML>");
220 }
221 else
222 {
223 txt = ("<HTML>"+tr("Help page missing. Create it in <A HREF=\"{0}\">english</A> or <A HREF=\"{1}\">your language</A>.",
224 url+"?action=edit", langurl+"?action=edit")+"</HTML>");
225 }
226 }
227 }
228 help.setText(txt);
229 help.setCaretPosition(0);
230 loaded = true;
231 } catch (IOException ex) {
232 }
233 return loaded;
234 }
235
236 /**
237 * Closes the help window
238 */
239 public void closeHelp() {
240 String bounds = helpBrowser.getX()+","+helpBrowser.getY()+","+helpBrowser.getWidth()+","+helpBrowser.getHeight();
241 Main.pref.put("help.window.bounds", bounds);
242 helpBrowser.setVisible(false);
243 }
244
245 private void createButton(JPanel buttons, String name) {
246 JButton b = new JButton(tr(name));
247 b.setActionCommand(name);
248 b.addActionListener(this);
249 buttons.add(b);
250 }
251}
Note: See TracBrowser for help on using the repository browser.