1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.help;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.OutputStreamWriter;
|
---|
8 | import java.io.PrintWriter;
|
---|
9 | import java.util.ArrayList;
|
---|
10 |
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * This is the proxy class for the help browser running in its own process.
|
---|
17 | *
|
---|
18 | *
|
---|
19 | */
|
---|
20 | public class HelpBrowserProxy {
|
---|
21 |
|
---|
22 | /** the unique instance of the proxy */
|
---|
23 | private static HelpBrowserProxy instance;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * replies the unique instance of the proxy
|
---|
27 | *
|
---|
28 | * @return the unique instance of the proxy
|
---|
29 | */
|
---|
30 | static public HelpBrowserProxy getInstance() {
|
---|
31 | if (instance == null) {
|
---|
32 | instance = new HelpBrowserProxy();
|
---|
33 | }
|
---|
34 | return instance;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /** the process running the help browser */
|
---|
38 | private Process helpBrowserProcess;
|
---|
39 | /** the print writer to the input stream of the help browser process */
|
---|
40 | private PrintWriter pw;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * launches the help browser in its own process
|
---|
44 | *
|
---|
45 | */
|
---|
46 | protected void launch() {
|
---|
47 | ArrayList<String> cmdLine = new ArrayList<String>();
|
---|
48 | String javaBin = null;
|
---|
49 | if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
|
---|
50 | javaBin = "javaw.exe";
|
---|
51 | } else {
|
---|
52 | javaBin = "java";
|
---|
53 | }
|
---|
54 | cmdLine.add(new File(new File(System.getProperty("java.home"), "bin"), javaBin).toString());
|
---|
55 | cmdLine.add("-classpath");
|
---|
56 | cmdLine.add(System.getProperty("java.class.path"));
|
---|
57 | cmdLine.add("org.openstreetmap.josm.gui.help.HelpApplication");
|
---|
58 | if (System.getProperty("josm.home") != null) {
|
---|
59 | cmdLine.add("-Djosm.home="+System.getProperty("josm.home"));
|
---|
60 | }
|
---|
61 | String[] cmds = new String[cmdLine.size()];
|
---|
62 | cmdLine.toArray(cmds);
|
---|
63 | try {
|
---|
64 | helpBrowserProcess = Runtime.getRuntime().exec(cmds);
|
---|
65 | } catch(IOException e) {
|
---|
66 | e.printStackTrace();
|
---|
67 | }
|
---|
68 | if (helpBrowserProcess != null) {
|
---|
69 | pw = new PrintWriter(
|
---|
70 | new OutputStreamWriter(
|
---|
71 | helpBrowserProcess.getOutputStream()
|
---|
72 | )
|
---|
73 | );
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Direct the help browser to the help page for help topic
|
---|
79 | * <code>relativeHelpTopic</code>
|
---|
80 | *
|
---|
81 | * @param relativeHelpTopic the help topic
|
---|
82 | */
|
---|
83 | public void setUrlForHelpTopic(String relativeHelpTopic) {
|
---|
84 | if (helpBrowserProcess == null) {
|
---|
85 | launch();
|
---|
86 | }
|
---|
87 | if (helpBrowserProcess == null) {
|
---|
88 | JOptionPane.showMessageDialog(
|
---|
89 | Main.parent,
|
---|
90 | tr("Failed to launch the external help browser"),
|
---|
91 | tr("Error"),
|
---|
92 | JOptionPane.ERROR_MESSAGE
|
---|
93 | );
|
---|
94 | System.err.println("Failed to launch browser");
|
---|
95 | return;
|
---|
96 | }
|
---|
97 | pw.println("setUrlForHelpTopic " + relativeHelpTopic);
|
---|
98 | pw.flush();
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Exit the help browser
|
---|
103 | */
|
---|
104 | public void exit() {
|
---|
105 | if (helpBrowserProcess == null)
|
---|
106 | return;
|
---|
107 | pw.println("exit");
|
---|
108 | pw.flush();
|
---|
109 | pw.close();
|
---|
110 | helpBrowserProcess.destroy();
|
---|
111 | }
|
---|
112 | }
|
---|