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

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