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