1 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 24 public class MozillaBrowserAdapter implements IBrowser { 25 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 executable; 42 43 protected String executableName; 44 45 protected Thread uiThread; 46 47 53 MozillaBrowserAdapter(String executable, String executableName) { 54 this.uiThread = Thread.currentThread(); 55 this.executable = executable; 56 this.executableName = executableName; 57 } 58 59 62 public void close() { 63 } 64 65 68 public void displayURL(String 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 83 public boolean isCloseSupported() { 84 return false; 85 } 86 87 90 public boolean isSetLocationSupported() { 91 return true; 92 } 93 94 97 public boolean isSetSizeSupported() { 98 return true; 99 } 100 101 104 public void setLocation(int x, int y) { 105 this.x = x; 106 this.y = y; 107 setLocationPending = true; 108 } 109 110 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 createPositioningURL(String url) { 120 IPath pluginPath = HelpBasePlugin.getDefault().getStateLocation(); 121 File outFile = pluginPath.append("mozillaPositon") .append("position.html") .toFile(); 124 try { 125 outFile.getParentFile().mkdirs(); 126 PrintWriter writer = new PrintWriter(new BufferedWriter( 127 new OutputStreamWriter(new FileOutputStream(outFile), 128 "UTF8")), false); 130 writer 131 .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"); writer.println("<html><head>"); writer 134 .println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">"); writer.print("<title></title><script language=\"JavaScript\">"); if (setSizePending) 137 writer.print("window.resizeTo(" + width + "," + height + ");"); if (setLocationPending) 139 writer.print("window.moveTo(" + x + "," + y + ");"); writer.print("location.replace(\"" + url + "\");"); writer.print("</script></head><body>"); writer.print("<a HREF=\"" + url + "\">--></a>"); writer.print("</body></html>"); writer.close(); 145 return "file://" + outFile.getAbsolutePath(); } catch (IOException ioe) { 147 return url; 149 } 150 } 151 152 private class BrowserThread extends Thread { 153 public boolean exitRequested = false; 154 155 private String url; 156 157 public BrowserThread(String urlName) { 158 this.url = urlName; 159 } 160 161 165 private int openBrowser(String browserCmd) { 166 try { 167 Process 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 e) { 180 } catch (IOException e) { 181 HelpBasePlugin.logError("Launching " + executableName + " has failed.", e); String msg = NLS.bind(HelpBaseResources.MozillaBrowserAdapter_executeFailed, executableName); 184 BaseHelpSystem.getDefaultErrorUtil() 185 .displayError(msg, uiThread); 186 return 0; 188 } 189 return -1; 190 } 191 192 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") >= 0 || outputs.getLastLine().indexOf( 209 "not running on display") >= 0)) { 211 return true; 212 } 213 errors.join(1000); 214 if (errors.getLastLine() != null 215 && (errors.getLastLine().indexOf( 216 "No running window found") >= 0 || errors.getLastLine().indexOf( 218 "not running on display") >= 0)) { 220 return true; 221 } 222 } catch (InterruptedException ie) { 223 } 225 return false; 226 } 227 228 public void run() { 229 waitForBrowser(); 231 if (exitRequested) 232 return; 233 if (openBrowser(executable + " -remote openURL(" + url + ")") == 0) { 235 return; 236 } 237 if (exitRequested) 238 return; 239 browserFullyOpenedAt = System.currentTimeMillis() + DELAY; 240 openBrowser(executable + " " + url); } 242 243 private void waitForBrowser() { 244 while (System.currentTimeMillis() < browserFullyOpenedAt) 245 try { 246 if (exitRequested) 247 return; 248 Thread.sleep(100); 249 } catch (InterruptedException ie) { 250 } 251 } 252 } 253 } 254 | Popular Tags |