1 11 package org.eclipse.ui.internal.browser; 12 13 import java.net.URL ; 14 15 import org.eclipse.osgi.util.NLS; 16 import org.eclipse.ui.PartInitException; 17 import org.eclipse.ui.browser.AbstractWebBrowser; 18 19 23 public class ExternalBrowserInstance extends AbstractWebBrowser { 24 protected IBrowserDescriptor browser; 25 26 protected Process process; 27 28 public ExternalBrowserInstance(String id, IBrowserDescriptor browser) { 29 super(id); 30 this.browser = browser; 31 } 32 33 public void openURL(URL url) throws PartInitException { 34 String urlText = null; 35 36 if (url != null) 37 urlText = url.toExternalForm(); 38 39 if (urlText != null && !WebBrowserUtil.isWindows()) { 41 int index = urlText.indexOf(" "); while (index >= 0) { 43 urlText = urlText.substring(0, index) 44 + "%20" + urlText.substring(index + 1); index = urlText.indexOf(" "); } 47 } 48 49 String location = browser.getLocation(); 50 String parameters = browser.getParameters(); 51 Trace 52 .trace( 53 Trace.FINEST, 54 "Launching external Web browser: " + location + " - " + parameters + " - " + urlText); 56 String params = parameters; 57 if (params == null) 58 params = ""; 60 if (urlText != null) { 61 int urlIndex = params.indexOf(IBrowserDescriptor.URL_PARAMETER); 62 if (urlIndex >= 0) 63 params = params.substring(0, urlIndex) 64 + " " + urlText + " " + params.substring(urlIndex + IBrowserDescriptor.URL_PARAMETER.length()); else { 66 if (!params.endsWith(" ")) params += " "; params += urlText; 69 } 70 } 71 72 try { 73 Trace.trace(Trace.FINEST, "Launching " + location + " " + params); if (params == null || params.length() == 0) 75 process = Runtime.getRuntime().exec(location); 76 else 77 process = Runtime.getRuntime().exec(location + " " + params); } catch (Exception e) { 79 Trace.trace(Trace.SEVERE, "Could not launch external browser", e); WebBrowserUtil.openError(NLS.bind( 81 Messages.errorCouldNotLaunchWebBrowser, urlText)); 82 } 83 Thread thread = new Thread () { 84 public void run() { 85 try { 86 process.waitFor(); 87 DefaultBrowserSupport.getInstance().removeBrowser( 88 ExternalBrowserInstance.this); 89 } catch (Exception e) { 90 } 92 } 93 }; 94 thread.setDaemon(true); 95 thread.start(); 96 } 97 98 public boolean close() { 99 try { 100 process.destroy(); 101 return true; 102 } catch (Exception e) { 103 return false; 104 } 105 } 106 } | Popular Tags |