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 JFrame helpBrowser = new JFrame(tr("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, tr("Open in Browser"));
|
---|
85 | createButton(buttons, tr("Edit"));
|
---|
86 | createButton(buttons, tr("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 (tr("Open in Browser").equals(e.getActionCommand())) {
|
---|
104 | OpenBrowser.displayUrl(url);
|
---|
105 | } else if (tr("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 (tr("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) on 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(tr("Error while loading page {0}",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 | }
|
---|