dsto.dfc.util
Class BrowserControl

java.lang.Object
  extended by dsto.dfc.util.BrowserControl

public class BrowserControl
extends Object

(mpp) Note: this code is based on 1.4b1 of BrowserLauncher.java from http://browserlauncher.sourceforge.net/. Various mods have been made eg to support use in an applet context.

Original documentation:

BrowserLauncher is a class that provides one static method, openURL, which opens the default web browser for the current user of the system to the given URL. It may support other protocols depending on the system -- mailto, ftp, etc. -- but that has not been rigorously tested and is not guaranteed to work.

Yes, this is platform-specific code, and yes, it may rely on classes on certain platforms that are not part of the standard JDK. What we're trying to do, though, is to take something that's frequently desirable but inherently platform-specific -- opening a default browser -- and allow programmers (you, for example) to do so without worrying about dropping into native code or doing anything else similarly evil.

Anyway, this code is completely in Java and will run on all JDK 1.1-compliant systems without modification or a need for additional libraries. All classes that are required on certain platforms to allow this to run are dynamically loaded at runtime via reflection and, if not found, will not cause this to do anything other than returning an error when opening the browser.

There are certain system requirements for this class, as it's running through Runtime.exec(), which is Java's way of making a native system call. Currently, this requires that a Macintosh have a Finder which supports the GURL event, which is true for Mac OS 8.0 and 8.1 systems that have the Internet Scripting AppleScript dictionary installed in the Scripting Additions folder in the Extensions folder (which is installed by default as far as I know under Mac OS 8.0 and 8.1), and for all Mac OS 8.5 and later systems. On Windows, it only runs under Win32 systems (Windows 95, 98, and NT 4.0, as well as later versions of all). On other systems, this drops back from the inherently platform-sensitive concept of a default browser and simply attempts to launch Netscape via a shell command.

This code is Copyright 1999-2001 by Eric Albert (ejalbert@cs.stanford.edu) and may be redistributed or modified in any form without restrictions as long as the portion of this comment from this paragraph through the end of the comment is not removed. The author requests that he be notified of any application, applet, or other binary that makes use of this code, but that's more out of curiosity than anything and is not required. This software includes no warranty. The author is not repsonsible for any loss of data or functionality or any adverse or unexpected effects of using this software.

Credits:
Steven Spencer, JavaWorld magazine (Java Tip 66)
Thanks also to Ron B. Yeh, Eric Shapiro, Ben Engber, Paul Teitlebaum, Andrea Cantatore, Larry Barowski, Trevor Bedzek, Frank Miedrich, and Ron Rabakukk

Version:
1.4b1 (Released June 20, 2001)
Author:
Eric Albert (ejalbert@cs.stanford.edu)

Field Summary
protected static String JDirect_MacOSX
          The framework to reference on Mac OS X
protected static Object linkage
          The linkage object required for JDirect 3 on Mac OS X.
 
Method Summary
static void displayURL(String url)
          Attempts to open the default web browser to the given URL.
static void displayURL(String command, String url)
          Open a URL using a shell command.
static AppletContext getAppletContext()
           
static String getBrowserCommand()
           
static void setAppletContext(AppletContext context)
           
static void setBrowserCommand(String browserCommand)
          Sets the command used to invoke the browser.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

linkage

protected static Object linkage
The linkage object required for JDirect 3 on Mac OS X.


JDirect_MacOSX

protected static final String JDirect_MacOSX
The framework to reference on Mac OS X

See Also:
Constant Field Values
Method Detail

setAppletContext

public static void setAppletContext(AppletContext context)

getAppletContext

public static AppletContext getAppletContext()

getBrowserCommand

public static String getBrowserCommand()
Returns:
the command used to invoke the browser.
See Also:
setBrowserCommand(String)

setBrowserCommand

public static void setBrowserCommand(String browserCommand)
Sets the command used to invoke the browser. The command is in the format specified by http://tuxedo.org/~esr/BROWSER/ eg
   netscape -raise -remote \"openURL(%s,new-window)\":lynx"
 

Parameters:
browserCommand - The browserCommand to set

displayURL

public static void displayURL(String url)
                       throws IOException
Attempts to open the default web browser to the given URL.

Parameters:
url - The URL to open
Throws:
IOException - If the web browser could not be located or does not run

displayURL

public static void displayURL(String command,
                              String url)
                       throws IOException
Open a URL using a shell command.

Throws:
IOException


Copyright © 2008 Commonwealth of Australia