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