1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.help;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.util.logging.Logger;
|
---|
10 |
|
---|
11 | import javax.swing.SwingUtilities;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Listens to commands on an input stream and delegates them to the help browser.
|
---|
15 | *
|
---|
16 | *
|
---|
17 | */
|
---|
18 | public class HelpBrowserCommandProcessor implements Runnable {
|
---|
19 | private static final Logger logger = Logger.getLogger(HelpBrowserCommandProcessor.class.getName());
|
---|
20 |
|
---|
21 | /** the controlled help browser*/
|
---|
22 | private HelpBrowser browser;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | *
|
---|
26 | * @param browser the controlled help browser
|
---|
27 | */
|
---|
28 | public HelpBrowserCommandProcessor(HelpBrowser browser) {
|
---|
29 | this.browser = browser;
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Show the help page for help topic <code>helpTopic</code>.
|
---|
34 | *
|
---|
35 | * @param helpTopic the help topic
|
---|
36 | */
|
---|
37 | protected void setUrlForHelpTopic(final String helpTopic) {
|
---|
38 | Runnable r = new Runnable() {
|
---|
39 | public void run() {
|
---|
40 | browser.openHelpTopic(helpTopic);
|
---|
41 | browser.setVisible(true);
|
---|
42 | browser.toFront();
|
---|
43 | }
|
---|
44 | };
|
---|
45 | SwingUtilities.invokeLater(r);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Exit the help browser
|
---|
50 | */
|
---|
51 | protected void exit() {
|
---|
52 | Runnable r = new Runnable() {
|
---|
53 | public void run() {
|
---|
54 | browser.setVisible(false);
|
---|
55 | System.exit(0);
|
---|
56 | }
|
---|
57 | };
|
---|
58 | SwingUtilities.invokeLater(r);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void run() {
|
---|
62 | BufferedReader reader = new BufferedReader(
|
---|
63 | new InputStreamReader(
|
---|
64 | System.in
|
---|
65 | )
|
---|
66 | );
|
---|
67 | while(true) {
|
---|
68 | String cmd = null;
|
---|
69 |
|
---|
70 | try {
|
---|
71 | cmd = reader.readLine();
|
---|
72 | } catch(IOException e) {
|
---|
73 | System.out.println(tr("Failed to read command. Exiting help browser. Exception was:" + e.toString()));
|
---|
74 | System.exit(1);
|
---|
75 | }
|
---|
76 | if (cmd.startsWith("exit")) {
|
---|
77 | exit();
|
---|
78 | } else if (cmd.startsWith("setUrlForHelpTopic ")) {
|
---|
79 | String helpTopic = cmd.substring("setUrlForHelpTopic ".length());
|
---|
80 | setUrlForHelpTopic(helpTopic);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|