Class Browser

java.lang.Object
com.Ostermiller.util.Browser

public class Browser extends Object
Allows URLs to be opened in the system browser on Windows and Unix. More information about this class is available from ostermiller.org.
Since:
ostermillerutils 1.00.00
Author:
Stephen Ostermiller https://ostermiller.org/contact.pl?regarding=Java+Utilities
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static String[]
    A list of commands to try in order to display the url.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String[]
    Retrieve the default commands to open a browser for this system.
    static boolean
    Show a dialog that allows the user to configure the command lines used for starting a browser on their system.
    static boolean
    Deprecated.
    Use the com.Ostermiller.util.Browser resource bundle to set strings for the given locale.
    static void
    Display a URL in the system browser.
    static void
    displayURL(String url, String namedWindow)
    Display the URL in the named window.
    static void
    Display the URL in a new window.
    static void
    Display the URLs, each in their own window, in the system browser.
    static void
    displayURLs(String[] urls, String namedWindow)
    Display the URLs the first in the given named window.
    static void
    displayURLs(String[] urls, String[] namedWindows)
    Display the URLs in the named windows.
    static void
    Display the URLs, each in their own window, in the system browser and the first in the named window.
    static JPanel
    If you wish to add to your own dialog box rather than have a separate one just for the browser, use this method to get a JPanel that can be added to your own dialog.
    static void
    Determine appropriate commands to start a browser on the current operating system.
    static void
    If you are using the getDialogPanel() method to create your own dialog, this method should be called every time before you display the dialog.
    static void
    Load the options for this class from the given properties file.
    static void
    main(String[] args)
    Open the url(s) specified on the command line in your browser.
    static void
    Save the options used to the given properties file.
    static void
    setLocale(Locale locale)
    Set the locale used for getting localized strings.
    static void
    If you are using the getDialogPanel() method to create your own dialog, this method should be called after you display the dialog if the user pressed ok.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • exec

      public static 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.
      Since:
      ostermillerutils 1.00.00
  • Constructor Details

    • Browser

      public Browser()
  • Method Details

    • setLocale

      public static void setLocale(Locale locale)
      Set the locale used for getting localized strings.
      Parameters:
      locale - Locale used to for i18n.
      Since:
      ostermillerutils 1.00.00
    • 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).
      Since:
      ostermillerutils 1.00.00
    • defaultCommands

      public static String[] defaultCommands()
      Retrieve the default commands to open a browser for this system.
      Returns:
      list of commands
      Since:
      ostermillerutils 1.00.00
    • save

      public static void save(Properties props)
      Save the options used to the given properties file. Property names used will all start with com.Ostermiller.util.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.
      Since:
      ostermillerutils 1.00.00
    • load

      public static void load(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 com.Ostermiller.util.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.
      Since:
      ostermillerutils 1.00.00
    • displayURL

      public static void displayURL(String url) throws 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:
      IOException - if the url is not valid or the browser fails to star
      Since:
      ostermillerutils 1.00.00
    • displayURLs

      public static void displayURLs(String[] urls) throws 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:
      IOException - if the url is not valid or the browser fails to star
      Since:
      ostermillerutils 1.00.00
    • displayURLinNew

      public static void displayURLinNew(String url) throws 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:
      IOException - if the url is not valid or the browser fails to star
      Since:
      ostermillerutils 1.00.00
    • displayURLsinNew

      public static void displayURLsinNew(String[] urls) throws 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:
      IOException - if the url is not valid or the browser fails to star
      Since:
      ostermillerutils 1.00.00
    • displayURL

      public static void displayURL(String url, String namedWindow) throws 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:
      IOException - if the url is not valid or the browser fails to star
      Since:
      ostermillerutils 1.00.00
    • displayURLs

      public static void displayURLs(String[] urls, String[] namedWindows) throws 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:
      IOException - if the url is not valid or the browser fails to star
      Since:
      ostermillerutils 1.00.00
    • displayURLs

      public static void displayURLs(String[] urls, String namedWindow) throws 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:
      IOException - if the url is not valid or the browser fails to star
      Since:
      ostermillerutils 1.00.00
    • main

      public static void main(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(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.
      Returns:
      whether or not the dialog has changed
      Since:
      ostermillerutils 1.00.00
    • dialogConfiguration

      @Deprecated public static boolean dialogConfiguration(Frame owner, Properties props)
      Deprecated.
      Use the com.Ostermiller.util.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: com.Ostermiller.util.BrowserDialog.title
      com.Ostermiller.util.BrowserDialog.description
      com.Ostermiller.util.BrowserDialog.label
      com.Ostermiller.util.BrowserDialog.defaults
      com.Ostermiller.util.BrowserDialog.browse
      com.Ostermiller.util.BrowserDialog.ok
      com.Ostermiller.util.BrowserDialog.cancel

      Parameters:
      owner - The frame that owns this dialog.
      props - contains the strings used in the dialog.
      Returns:
      whether or not the dialog has changed
      Since:
      ostermillerutils 1.00.00
    • getDialogPanel

      public static JPanel getDialogPanel(Window parent)
      If you wish to add to your own dialog box rather than have a separate one just for the browser, use this method to get a JPanel that can be added to your own dialog. mydialog.add(Browser.getDialogPanel(mydialog)); Browser.initPanel(); mydialog.show(); if (ok_pressed){   Browser.userOKedPanelChanges(); }
      Parameters:
      parent - window into which panel with eventually be placed.
      Returns:
      the dialog
      Since:
      ostermillerutils 1.02.22
    • initPanel

      public static void initPanel()
      If you are using the getDialogPanel() method to create your own dialog, this method should be called every time before you display the dialog. mydialog.add(Browser.getDialogPanel(mydialog)); Browser.initPanel(); mydialog.show(); if (ok_pressed){   Browser.userOKedPanelChanges(); }
      Since:
      ostermillerutils 1.02.22
    • userOKedPanelChanges

      public static void userOKedPanelChanges()
      If you are using the getDialogPanel() method to create your own dialog, this method should be called after you display the dialog if the user pressed ok. mydialog.add(Browser.getDialogPanel(mydialog)); Browser.initPanel(); mydialog.show(); if (ok_pressed){   Browser.userOKedPanelChanges(); }
      Since:
      ostermillerutils 1.02.22