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

Last change on this file since 163 was 163, checked in by imi, 18 years ago
  • fixed Help so that it starts top-scrolled
File size: 5.7 KB
Line 
1package org.openstreetmap.josm.actions;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.BorderLayout;
6import java.awt.Component;
7import java.awt.Point;
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.awt.event.WindowAdapter;
11import java.awt.event.WindowEvent;
12import java.io.IOException;
13import java.io.StringReader;
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.OpenBrowser;
34import org.openstreetmap.josm.tools.WikiReader;
35
36/**
37 * Open a help browser and displays lightweight online help.
38 *
39 * @author imi
40 */
41public class HelpAction extends AbstractAction {
42
43 public interface Helpful {
44 String helpTopic();
45 }
46
47 private JFrame helpBrowser = new JFrame("JOSM Online Help");
48 private String baseurl = Main.pref.get("help.baseurl", "http://josm.eigenheimstrasse.de");
49 private JEditorPane help = new JEditorPane();
50 private WikiReader reader = new WikiReader(baseurl);
51 private String url;
52
53 public HelpAction() {
54 super(tr("Help"), ImageProvider.get("help"));
55 help.setEditable(false);
56 help.addHyperlinkListener(new HyperlinkListener(){
57 public void hyperlinkUpdate(HyperlinkEvent e) {
58 if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED)
59 return;
60 if (e.getURL() == null)
61 help.setText("<html>404 not found</html>");
62 else if (e.getURL().toString().startsWith(WikiReader.JOSM_EXTERN))
63 OpenBrowser.displayUrl("http://"+e.getURL().toString().substring(WikiReader.JOSM_EXTERN.length())+"?action=edit");
64 else
65 setHelpUrl(e.getURL().toString());
66 }
67 });
68 help.setContentType("text/html");
69
70 JPanel p = new JPanel(new BorderLayout());
71 helpBrowser.setContentPane(p);
72
73 p.add(new JScrollPane(help), BorderLayout.CENTER);
74 String[] bounds = Main.pref.get("help.window.bounds", "0,0,800,600").split(",");
75 helpBrowser.setBounds(
76 Integer.parseInt(bounds[0]),
77 Integer.parseInt(bounds[1]),
78 Integer.parseInt(bounds[2]),
79 Integer.parseInt(bounds[3]));
80
81 JPanel buttons = new JPanel();
82 p.add(buttons, BorderLayout.SOUTH);
83 createButton(buttons, "Open in Browser");
84 createButton(buttons, "Edit");
85 createButton(buttons, "Reload");
86
87 helpBrowser.addWindowListener(new WindowAdapter(){
88 @Override public void windowClosing(WindowEvent e) {
89 closeHelp();
90 }
91 });
92
93 help.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Close");
94 help.getActionMap().put("Close", new AbstractAction(){
95 public void actionPerformed(ActionEvent e) {
96 closeHelp();
97 }
98 });
99 }
100
101 public void actionPerformed(ActionEvent e) {
102 if ("Open in Browser".equals(e.getActionCommand())) {
103 OpenBrowser.displayUrl(url);
104 } else if ("Edit".equals(e.getActionCommand())) {
105 if (!url.startsWith(baseurl)) {
106 JOptionPane.showMessageDialog(Main.parent, tr("Can only edit help pages from JOSM Online Help"));
107 return;
108 }
109 OpenBrowser.displayUrl(url+"?action=edit");
110 } else if ("Reload".equals(e.getActionCommand())) {
111 setHelpUrl(url);
112 } else if (e.getActionCommand() == null) {
113 String topic = null;
114 Point mouse = Main.parent.getMousePosition();
115 if (mouse != null)
116 topic = contextSensitiveHelp(SwingUtilities.getDeepestComponentAt(Main.parent, mouse.x, mouse.y));
117 if (topic == null) {
118 helpBrowser.setVisible(false);
119 setHelpUrl(baseurl+"/wiki/Help");
120 } else
121 help(topic);
122 } else {
123 helpBrowser.setVisible(false);
124 setHelpUrl(baseurl+"/wiki/Help");
125 }
126 }
127
128 /**
129 * @return The topic of the help. <code>null</code> for "don't know"
130 */
131 private String contextSensitiveHelp(Object c) {
132 if (c instanceof Helpful)
133 return ((Helpful)c).helpTopic();
134 if (c instanceof JMenu)
135 return "Menu/"+((JMenu)c).getText();
136 if (c instanceof AbstractButton) {
137 AbstractButton b = (AbstractButton)c;
138 if (b.getClientProperty("help") != null)
139 return (String)b.getClientProperty("help");
140 return contextSensitiveHelp(((AbstractButton)c).getAction());
141 }
142 if (c instanceof Action)
143 return (String)((Action)c).getValue("help");
144 if (c instanceof Component)
145 return contextSensitiveHelp(((Component)c).getParent());
146 return null;
147 }
148
149 /**
150 * Displays the help (or browse on the already open help) to the online page
151 * with the given help topic. Use this for larger help descriptions.
152 */
153 public void help(String topic) {
154 helpBrowser.setVisible(false);
155 setHelpUrl(baseurl+"/wiki/Help/"+topic);
156 }
157
158 /**
159 * Set the content of the help window to a specific text (in html format)
160 * @param url The url this content is the representation of
161 */
162 public void setHelpUrl(String url) {
163 this.url = url;
164 try {
165 help.read(new StringReader(reader.read(url)), help.getEditorKit().createDefaultDocument());
166 } catch (IOException e) {
167 help.setText("Error while loading page "+url);
168 }
169 helpBrowser.setVisible(true);
170 }
171
172 /**
173 * Closes the help window
174 */
175 public void closeHelp() {
176 String bounds = helpBrowser.getX()+","+helpBrowser.getY()+","+helpBrowser.getWidth()+","+helpBrowser.getHeight();
177 Main.pref.put("help.window.bounds", bounds);
178 helpBrowser.setVisible(false);
179 }
180
181 private void createButton(JPanel buttons, String name) {
182 JButton b = new JButton(tr(name));
183 b.setActionCommand(name);
184 b.addActionListener(this);
185 buttons.add(b);
186 }
187}
Note: See TracBrowser for help on using the repository browser.