KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > browser > MozillaBrowserAdapter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.browser;
12
13 import java.io.*;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.help.browser.*;
17 import org.eclipse.help.internal.base.*;
18 import org.eclipse.osgi.util.NLS;
19
20 /**
21  * Browser adapter for browsers supporting -remote openURL command line option
22  * i.e. Mozilla and Netscape.
23  */

24 public class MozillaBrowserAdapter implements IBrowser {
25     // delay that it takes mozilla to start responding
26
// to remote command after mozilla has been called
27
protected static final int DELAY = 5000;
28
29     protected long browserFullyOpenedAt = 0;
30
31     private BrowserThread lastBrowserThread = null;
32
33     private int x, y;
34
35     private int width, height;
36
37     private boolean setLocationPending = false;
38
39     private boolean setSizePending = false;
40
41     protected String JavaDoc executable;
42
43     protected String JavaDoc executableName;
44
45     protected Thread JavaDoc uiThread;
46
47     /**
48      * Constructor
49      *
50      * @executable executable filename to launch
51      * @executableName name of the program to display when error occurs
52      */

53     MozillaBrowserAdapter(String JavaDoc executable, String JavaDoc executableName) {
54         this.uiThread = Thread.currentThread();
55         this.executable = executable;
56         this.executableName = executableName;
57     }
58
59     /*
60      * @see IBrowser#close()
61      */

62     public void close() {
63     }
64
65     /*
66      * @see IBrowser#displayURL(String)
67      */

68     public void displayURL(String JavaDoc url) {
69         if (lastBrowserThread != null)
70             lastBrowserThread.exitRequested = true;
71         if (setLocationPending || setSizePending) {
72             url = createPositioningURL(url);
73         }
74         lastBrowserThread = new BrowserThread(url);
75         lastBrowserThread.start();
76         setLocationPending = false;
77         setSizePending = false;
78     }
79
80     /*
81      * @see IBrowser#isCloseSupported()
82      */

83     public boolean isCloseSupported() {
84         return false;
85     }
86
87     /*
88      * @see IBrowser#isSetLocationSupported()
89      */

90     public boolean isSetLocationSupported() {
91         return true;
92     }
93
94     /*
95      * @see IBrowser#isSetSizeSupported()
96      */

97     public boolean isSetSizeSupported() {
98         return true;
99     }
100
101     /*
102      * @see IBrowser#setLocation(int, int)
103      */

104     public void setLocation(int x, int y) {
105         this.x = x;
106         this.y = y;
107         setLocationPending = true;
108     }
109
110     /*
111      * @see IBrowser#setSize(int, int)
112      */

113     public void setSize(int width, int height) {
114         this.width = width;
115         this.height = height;
116         setSizePending = true;
117     }
118
119     private synchronized String JavaDoc createPositioningURL(String JavaDoc url) {
120         IPath pluginPath = HelpBasePlugin.getDefault().getStateLocation();
121         File outFile = pluginPath.append("mozillaPositon") //$NON-NLS-1$
122
.append("position.html") //$NON-NLS-1$
123
.toFile();
124         try {
125             outFile.getParentFile().mkdirs();
126             PrintWriter writer = new PrintWriter(new BufferedWriter(
127                     new OutputStreamWriter(new FileOutputStream(outFile),
128                             "UTF8")), //$NON-NLS-1$
129
false);
130             writer
131                     .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"); //$NON-NLS-1$
132
writer.println("<html><head>"); //$NON-NLS-1$
133
writer
134                     .println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">"); //$NON-NLS-1$
135
writer.print("<title></title><script language=\"JavaScript\">"); //$NON-NLS-1$
136
if (setSizePending)
137                 writer.print("window.resizeTo(" + width + "," + height + ");"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
138
if (setLocationPending)
139                 writer.print("window.moveTo(" + x + "," + y + ");"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
140
writer.print("location.replace(\"" + url + "\");"); //$NON-NLS-1$ //$NON-NLS-2$
141
writer.print("</script></head><body>"); //$NON-NLS-1$
142
writer.print("<a HREF=\"" + url + "\">--&gt;</a>"); //$NON-NLS-1$ //$NON-NLS-2$
143
writer.print("</body></html>"); //$NON-NLS-1$
144
writer.close();
145             return "file://" + outFile.getAbsolutePath(); //$NON-NLS-1$
146
} catch (IOException ioe) {
147             // return the original url
148
return url;
149         }
150     }
151
152     private class BrowserThread extends Thread JavaDoc {
153         public boolean exitRequested = false;
154
155         private String JavaDoc url;
156
157         public BrowserThread(String JavaDoc urlName) {
158             this.url = urlName;
159         }
160
161         /**
162          * @param browserCmd
163          * @return int 0 if success
164          */

165         private int openBrowser(String JavaDoc browserCmd) {
166             try {
167                 Process JavaDoc pr = Runtime.getRuntime().exec(browserCmd);
168                 StreamConsumer outputs = new StreamConsumer(pr.getInputStream());
169                 (outputs).start();
170                 StreamConsumer errors = new StreamConsumer(pr.getErrorStream());
171                 (errors).start();
172                 pr.waitFor();
173                 int ret = pr.exitValue();
174
175                 if (ret == 0 && errorsInOutput(outputs, errors)) {
176                     return -1;
177                 }
178                 return ret;
179             } catch (InterruptedException JavaDoc e) {
180             } catch (IOException e) {
181                 HelpBasePlugin.logError("Launching " + executableName //$NON-NLS-1$
182
+ " has failed.", e); //$NON-NLS-1$
183
String JavaDoc msg = NLS.bind(HelpBaseResources.MozillaBrowserAdapter_executeFailed, executableName);
184                 BaseHelpSystem.getDefaultErrorUtil()
185                         .displayError(msg, uiThread);
186                 // return success, so second command does not execute
187
return 0;
188             }
189             return -1;
190         }
191
192         /**
193          * On some OSes 0 is always returned by netscape -remote. It is
194          * necessary to examine ouput to find out failure
195          *
196          * @param outputs
197          * @param errors
198          * @return @throws
199          * InterruptedException
200          */

201         private boolean errorsInOutput(StreamConsumer outputs,
202                 StreamConsumer errors) {
203             try {
204                 outputs.join(1000);
205                 if (outputs.getLastLine() != null
206                         && (outputs.getLastLine().indexOf(
207                                 "No running window found") //$NON-NLS-1$
208
>= 0 || outputs.getLastLine().indexOf(
209                                 "not running on display") //$NON-NLS-1$
210
>= 0)) {
211                     return true;
212                 }
213                 errors.join(1000);
214                 if (errors.getLastLine() != null
215                         && (errors.getLastLine().indexOf(
216                                 "No running window found") //$NON-NLS-1$
217
>= 0 || errors.getLastLine().indexOf(
218                                 "not running on display") //$NON-NLS-1$
219
>= 0)) {
220                     return true;
221                 }
222             } catch (InterruptedException JavaDoc ie) {
223                 // ignore
224
}
225             return false;
226         }
227
228         public void run() {
229             // If browser is opening, wait until it fully opens,
230
waitForBrowser();
231             if (exitRequested)
232                 return;
233             if (openBrowser(executable + " -remote openURL(" + url + ")") //$NON-NLS-1$ //$NON-NLS-2$
234
== 0) {
235                 return;
236             }
237             if (exitRequested)
238                 return;
239             browserFullyOpenedAt = System.currentTimeMillis() + DELAY;
240             openBrowser(executable + " " + url); //$NON-NLS-1$
241
}
242
243         private void waitForBrowser() {
244             while (System.currentTimeMillis() < browserFullyOpenedAt)
245                 try {
246                     if (exitRequested)
247                         return;
248                     Thread.sleep(100);
249                 } catch (InterruptedException JavaDoc ie) {
250                 }
251         }
252     }
253 }
254
Popular Tags