uk.co.mullsoft.jean.components
Class Browser

java.lang.Object
  extended by uk.co.mullsoft.jean.components.Browser

public class Browser
extends java.lang.Object

open URLs in the system browser on Windows and Unix.


Field Summary
protected static uk.co.mullsoft.jean.components.Browser.BrowserDialog dialog
          The dialog that allows user configuration of the options for this class.
static java.lang.String[] exec
          A list of commands to try in order to display the url.
protected static java.util.ResourceBundle labels
          Locale specific strings displayed to the user.
 
Constructor Summary
Browser()
           
 
Method Summary
static java.lang.String checkURL(java.lang.String url)
           
static java.lang.String[] checkURLs(java.lang.String[] url)
           
static java.lang.String[] defaultCommands()
          Retrieve the default commands to open a browser for this system.
static boolean dialogConfiguration(java.awt.Frame owner)
          Show a dialog that allows the user to configure the command lines used for starting a browser on their system.
static boolean dialogConfiguration(java.awt.Frame owner, java.util.Properties props)
          Deprecated. Use the uk.co.mullsoft.jean.components.Browser resource bundle to set strings for the given locale.
static void displayURL(java.lang.String url1)
          Display a URL in the system browser.
static void displayURL(java.lang.String url, java.lang.String namedWindow)
          Display the URL in the named window.
static void displayURLinNew(java.lang.String url)
          Display the URL in a new window.
static void displayURLs(java.lang.String[] urls1)
          Display the URLs, each in their own window, in the system browser.
static void displayURLs(java.lang.String[] urls, java.lang.String namedWindow)
          Display the URLs the first in the given named window.
static void displayURLs(java.lang.String[] urls1, java.lang.String[] namedWindows)
          Display the URLs in the named windows.
static void displayURLsinNew(java.lang.String[] urls1)
          Display the URLs, each in their own window, in the system browser and the first in the named window.
static void init()
          Determine appropriate commands to start a browser on the current operating system.
static void load(java.util.Properties props)
          Load the options for this class from the given properties file.
static void main(java.lang.String[] args)
          Open the url(s) specified on the command line in your browser.
static void save(java.util.Properties props)
          Save the options used to the given properties file.
static void setLocale(java.util.Locale locale)
          Set the locale used for getting localized strings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dialog

protected static uk.co.mullsoft.jean.components.Browser.BrowserDialog dialog
The dialog that allows user configuration of the options for this class.


labels

protected static java.util.ResourceBundle labels
Locale specific strings displayed to the user.


exec

public static java.lang.String[] exec
A list of commands to try in order to display the url. The url is put into the command using MessageFormat, so the URL will be specified as {0} in the command. Some examples of commands to try might be:
rundll32 url.dll,FileProtocolHandler {0}
netscape {0}
These commands are passed in order to exec until something works when displayURL is used.

Constructor Detail

Browser

public Browser()
Method Detail

setLocale

public static void setLocale(java.util.Locale locale)
Set the locale used for getting localized strings.

Parameters:
locale - Locale used for i18n.

init

public static void init()
Determine appropriate commands to start a browser on the current operating system. On windows:
rundll32 url.dll,FileProtocolHandler {0}
On other operating systems, the "which" command is used to test if Mozilla, netscape, and lynx(xterm) are available (in that order).


defaultCommands

public static java.lang.String[] defaultCommands()
Retrieve the default commands to open a browser for this system.


save

public static void save(java.util.Properties props)
Save the options used to the given properties file. Property names used will all start with uk.co.mullsoft.jean.components.Browser Properties are saved in such a way that a call to load(props); will restore the state of this class. If the default commands to open a browser are being used then they are not saved in the properties file, assuming that the user will want to use the defaults next time even if the defaults change.

Parameters:
props - properties file to which configuration is saved.

load

public static void load(java.util.Properties props)
Load the options for this class from the given properties file. This method is designed to work with the save(props) method. All properties used will start with uk.co.mullsoft.jean.components.Browser. If no configuration is found, the default configuration will be used. If this method is used, a call to Browser.init(); is not needed.

Parameters:
props - properties file from which configuration is loaded.

checkURLs

public static java.lang.String[] checkURLs(java.lang.String[] url)

checkURL

public static java.lang.String checkURL(java.lang.String url)

displayURL

public static void displayURL(java.lang.String url1)
                       throws java.io.IOException
Display a URL in the system browser. Browser.init() should be called before calling this function or Browser.exec should be set explicitly. For security reasons, the URL will may not be passed directly to the browser as it is passed to this method. The URL may be made safe for the exec command by URLEncoding the URL before passing it.

Parameters:
url - the url to display
Throws:
java.io.IOException - if the url is not valid or the browser fails to star

displayURLs

public static void displayURLs(java.lang.String[] urls1)
                        throws java.io.IOException
Display the URLs, each in their own window, in the system browser. Browser.init() should be called before calling this function or Browser.exec should be set explicitly. If more than one URL is given an HTML page containing JavaScript will be written to the local drive, that page will be opened, and it will open the rest of the URLs.

Parameters:
urls - the list of urls to display
Throws:
java.io.IOException - if the url is not valid or the browser fails to star

displayURLinNew

public static void displayURLinNew(java.lang.String url)
                            throws java.io.IOException
Display the URL in a new window. Uses javascript to check history.length to determine if the browser opened a new window already. If it did, the url is shown in that window, if not, it is shown in new window. Some browsers do not allow the length of history to be viewed by a web page. In that case, the url will be displayed in the current window. Browser.init() should be called before calling this function or Browser.exec should be set explicitly.

Parameters:
url - the url to display in a new window.
Throws:
java.io.IOException - if the url is not valid or the browser fails to star

displayURLsinNew

public static void displayURLsinNew(java.lang.String[] urls1)
                             throws java.io.IOException
Display the URLs, each in their own window, in the system browser and the first in the named window. The first URL will only be opened in the named window if the browser did no open it in a new window to begin with. Browser.init() should be called before calling this function or Browser.exec should be set explicitly. An html page containing javascript will be written to the local drive, that page will be opened, and it will open all the urls.

Parameters:
urls - the list of urls to display
Throws:
java.io.IOException - if the url is not valid or the browser fails to star

displayURL

public static void displayURL(java.lang.String url,
                              java.lang.String namedWindow)
                       throws java.io.IOException
Display the URL in the named window. If the browser opens a new window by default, this will likely cause a duplicate window to be opened. Browser.init() should be called before calling this function or Browser.exec should be set explicitly.

Parameters:
url - the url to display
namedWindow - the name of the desired window.
Throws:
java.io.IOException - if the url is not valid or the browser fails to star

displayURLs

public static void displayURLs(java.lang.String[] urls1,
                               java.lang.String[] namedWindows)
                        throws java.io.IOException
Display the URLs in the named windows. If the browser opens a new window by default, this will likely cause a duplicate window to be opened. This method relies on the browser to support javascript. Browser.init() should be called before calling this function or Browser.exec should be set explicitly. Extra names for windows will be ignored, and if there are too few names, the remaining windows will be named "_blank".

Parameters:
urls - the list of urls to display
namedWindows - the list of names for the windows.
Throws:
java.io.IOException - if the url is not valid or the browser fails to star

displayURLs

public static void displayURLs(java.lang.String[] urls,
                               java.lang.String namedWindow)
                        throws java.io.IOException
Display the URLs the first in the given named window. If the browser opens a new window by default, this will likely cause a duplicate window to be opened. This method relies on the browser to support javascript. Browser.init() should be called before calling this function or Browser.exec should be set explicitly.

Parameters:
urls - the list of urls to display
namedWindow - the name of the first window to use.
Throws:
java.io.IOException - if the url is not valid or the browser fails to star

main

public static void main(java.lang.String[] args)
Open the url(s) specified on the command line in your browser.

Parameters:
args - Command line arguments (URLs)

dialogConfiguration

public static boolean dialogConfiguration(java.awt.Frame owner)
Show a dialog that allows the user to configure the command lines used for starting a browser on their system.

Parameters:
owner - The frame that owns the dialog.

dialogConfiguration

public static boolean dialogConfiguration(java.awt.Frame owner,
                                          java.util.Properties props)
Deprecated. Use the uk.co.mullsoft.jean.components.Browser resource bundle to set strings for the given locale.

Show a dialog that allows the user to configure the command lines used for starting a browser on their system. String used in the dialog are taken from the given properties. This dialog can be customized or displayed in multiple languages.

Properties that are used: uk.co.mullsoft.jean.components.BrowserDialog.title
uk.co.mullsoft.jean.components.BrowserDialog.description
uk.co.mullsoft.jean.components.BrowserDialog.label
uk.co.mullsoft.jean.components.BrowserDialog.defaults
uk.co.mullsoft.jean.components.BrowserDialog.browse
uk.co.mullsoft.jean.components.BrowserDialog.ok
uk.co.mullsoft.jean.components.BrowserDialog.cancel

Parameters:
owner - The frame that owns this dialog.
props - contains the strings used in the dialog.