1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Point;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.WindowAdapter;
|
---|
12 | import java.awt.event.WindowEvent;
|
---|
13 | import java.io.IOException;
|
---|
14 |
|
---|
15 | import javax.swing.AbstractAction;
|
---|
16 | import javax.swing.AbstractButton;
|
---|
17 | import javax.swing.Action;
|
---|
18 | import javax.swing.JButton;
|
---|
19 | import javax.swing.JComponent;
|
---|
20 | import javax.swing.JEditorPane;
|
---|
21 | import javax.swing.JFrame;
|
---|
22 | import javax.swing.JMenu;
|
---|
23 | import javax.swing.JOptionPane;
|
---|
24 | import javax.swing.JPanel;
|
---|
25 | import javax.swing.JScrollPane;
|
---|
26 | import javax.swing.KeyStroke;
|
---|
27 | import javax.swing.SwingUtilities;
|
---|
28 | import javax.swing.event.HyperlinkEvent;
|
---|
29 | import javax.swing.event.HyperlinkListener;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.Main;
|
---|
32 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
33 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
34 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
35 | import org.openstreetmap.josm.tools.WikiReader;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Open a help browser and displays lightweight online help.
|
---|
39 | *
|
---|
40 | * @author imi
|
---|
41 | */
|
---|
42 | public 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 | }
|
---|