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

Last change on this file since 1023 was 962, checked in by stoecker, 16 years ago

translate help system

  • Property svn:eol-style set to native
File size: 6.7 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;
14import java.io.StringReader;
15
16import javax.swing.AbstractAction;
17import javax.swing.AbstractButton;
18import javax.swing.Action;
19import javax.swing.JButton;
20import javax.swing.JComponent;
21import javax.swing.JEditorPane;
22import javax.swing.JFrame;
23import javax.swing.JMenu;
24import javax.swing.JOptionPane;
25import javax.swing.JPanel;
26import javax.swing.JScrollPane;
27import javax.swing.KeyStroke;
28import javax.swing.SwingUtilities;
29import javax.swing.event.HyperlinkEvent;
30import javax.swing.event.HyperlinkListener;
31
32import org.openstreetmap.josm.Main;
33import org.openstreetmap.josm.tools.ImageProvider;
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 = tr("En:");
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().startsWith(WikiReader.JOSM_EXTERN))
65 OpenBrowser.displayUrl("http://"+e.getURL().toString().substring(WikiReader.JOSM_EXTERN.length())+"?action=edit");
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 return "Menu/"+((JMenu)c).getText();
138 if (c instanceof AbstractButton) {
139 AbstractButton b = (AbstractButton)c;
140 if (b.getClientProperty("help") != null)
141 return (String)b.getClientProperty("help");
142 return contextSensitiveHelp(((AbstractButton)c).getAction());
143 }
144 if (c instanceof Action)
145 return (String)((Action)c).getValue("help");
146 if (c instanceof Component)
147 return contextSensitiveHelp(((Component)c).getParent());
148 return null;
149 }
150
151 /**
152 * Displays the help (or browse on the already open help) on the online page
153 * with the given help topic. Use this for larger help descriptions.
154 */
155 public void help(String topic) {
156 helpBrowser.setVisible(false);
157 setHelpUrl(baseurl+"/wiki/Help/"+topic);
158 }
159
160 /**
161 * Set the content of the help window to a specific text (in html format)
162 * @param url The url this content is the representation of
163 */
164 public void setHelpUrl(String url) {
165 int i = url.lastIndexOf("/")+1;
166 String title = url.substring(i);
167 if(!title.startsWith(languageCode))
168 title = languageCode + title;
169 String langurl = url.substring(0, i) + title;
170 if(langurl.equals(this.url) || langurl.equals(url))
171 {
172 this.url = url;
173 try {
174 help.read(new StringReader(reader.read(url)), help.getEditorKit().createDefaultDocument());
175 } catch (IOException ex) {
176 help.setText(tr("Error while loading page {0}",url));
177 }
178 }
179 else
180 {
181 try {
182 help.read(new StringReader(reader.read(langurl)), help.getEditorKit().createDefaultDocument());
183 String message = help.getText();
184 String le = "http://josm-extern." + langurl.substring(7);
185 if(message.indexOf("Describe &quot;") >= 0 && message.indexOf(le) >= 0)
186 throw new IOException();
187 this.url = langurl;
188 } catch (IOException e) {
189 this.url = url;
190 try {
191 help.read(new StringReader(reader.read(url)), help.getEditorKit().createDefaultDocument());
192 } catch (IOException ex) {
193 help.setText(tr("Error while loading page {0}",url));
194 }
195 }
196 }
197 helpBrowser.setVisible(true);
198 }
199
200 /**
201 * Closes the help window
202 */
203 public void closeHelp() {
204 String bounds = helpBrowser.getX()+","+helpBrowser.getY()+","+helpBrowser.getWidth()+","+helpBrowser.getHeight();
205 Main.pref.put("help.window.bounds", bounds);
206 helpBrowser.setVisible(false);
207 }
208
209 private void createButton(JPanel buttons, String name) {
210 JButton b = new JButton(tr(name));
211 b.setActionCommand(name);
212 b.addActionListener(this);
213 buttons.add(b);
214 }
215}
Note: See TracBrowser for help on using the repository browser.