Package com.Ostermiller.util
Class Browser
java.lang.Object
com.Ostermiller.util.Browser
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String[]Retrieve the default commands to open a browser for this system.static booleandialogConfiguration(Frame owner) Show a dialog that allows the user to configure the command lines used for starting a browser on their system.static booleandialogConfiguration(Frame owner, Properties props) Deprecated.Use the com.Ostermiller.util.Browser resource bundle to set strings for the given locale.static voiddisplayURL(String url) Display a URL in the system browser.static voiddisplayURL(String url, String namedWindow) Display the URL in the named window.static voiddisplayURLinNew(String url) Display the URL in a new window.static voiddisplayURLs(String[] urls) Display the URLs, each in their own window, in the system browser.static voiddisplayURLs(String[] urls, String namedWindow) Display the URLs the first in the given named window.static voiddisplayURLs(String[] urls, String[] namedWindows) Display the URLs in the named windows.static voiddisplayURLsinNew(String[] urls) Display the URLs, each in their own window, in the system browser and the first in the named window.static JPanelgetDialogPanel(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.static voidinit()Determine appropriate commands to start a browser on the current operating system.static voidIf you are using the getDialogPanel() method to create your own dialog, this method should be called every time before you display the dialog.static voidload(Properties props) Load the options for this class from the given properties file.static voidOpen the url(s) specified on the command line in your browser.static voidsave(Properties props) Save the options used to the given properties file.static voidSet the locale used for getting localized strings.static voidIf 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.
-
Field Details
-
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
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
Retrieve the default commands to open a browser for this system.- Returns:
- list of commands
- Since:
- ostermillerutils 1.00.00
-
save
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
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
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
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
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
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
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 displaynamedWindow- 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
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 displaynamedWindows- 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
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 displaynamedWindow- 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
Open the url(s) specified on the command line in your browser.- Parameters:
args- Command line arguments (URLs)
-
dialogConfiguration
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.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
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
-