/* File: PopupHTMLbrowser.java */ package O2PlibSS.gui; import java.awt.*; import java.io.*; /** * Class PopupHTMLbrowser is the popup a web browser on * the specified URL string. First create class instance * then call the popupHTMLbrowser() method which may return * a non-null error message if there is a problem. *

*

* This code is available at the HTMLtools project on SourceForge at * http://htmltools.sourceforge.org/ * under the "Common Public License Version 1.0" * * http://www.opensource.org/licenses/cpl1.0.php.

*

* It was derived and refactored from the open source * MAExplorer (http://maexplorer.sourceforge.org/), and * Open2Dprot (http://Open2Dprot.sourceforge.net/) Table modules. *

* $Date: 2009/12/10 11:45:56 $ $Revision: 1.41 $ *
* Copyright 2008, 2009 by Peter Lemkin * E-Mail: lemkin@users.sourceforge.net * http://lemkingroup.com/ */ public class PopupHTMLbrowser extends Frame { /* PopupHTMLbrowser */ /** frame id */ final static long serialVersionUID= 0; /** * PopupHTMLbrowser() - constructor * @param urlStr to display in popup browser. It is the file's url * (the url must start with either "http://" or"file://"). * @param windowName to use for the new popup window */ public PopupHTMLbrowser() { /* PopupHTMLbrowser*/ } /* PopupHTMLbrowser*/ /** * popupHTMLbrowser() - Display URL file in system Web browser. * This assumes that you have added the proper plugin to your browser * for this data if required. *

   * Examples:
   * displayURL("http://www.javaworld.com")
   * displayURL("file://c:\\docs\\index.html")
   * displayURL("file:///user/joe/index.html");
   * displayURL("file:///user/joe/image.gif");
   * displayURL("file:///user/joe/image.pdf");
   * displayURL("file:///user/joe/image.xml");
   * displayURL("file:///user/joe/image.svg");
   *
* Under Unix, the system browser is hard-coded to be 'firefox'. * firefox must be in your PATH for this to work. *
* Under Windows, this will bring up the default browser under windows, * usually either firefox or Microsoft IE. The default browser is * determined by the OS. *
* adapted from : * http://www.javaworld.com/javaworld/javatips/jw-javatip66_p.html *
* @param urlStr to display in popup browser. It is the file's url * (the url must start with either "http://" or"file://"). * @param windowName to use for the new popup window * @return String message if error, null if ok. */ public String popupHTMLbrowser(String urlStr, String windowName) { /* popupHTMLbrowser */ /* name of this operating system: Property("os.name") */ String osName= System.getProperty("os.name"); /* [TODO] Handle the Mac better. */ String WIN_PATH= "rundll32", /* default browser under windows*/ WIN_FLAG= "url.dll,FileProtocolHandler", /* flag to display url */ UNIX_PATH= "firefox", /* default browser under UNIX */ UNIX_FLAG= "-remote openURL"; /* flag to display a url.*/ String cmd= null; try { /* try to start browser */ if(osName!=null && osName.startsWith("Windows")) { /* windows */ /* cmd= "rundll32 url.dll,FileProtocolHandler http://..." */ cmd= WIN_PATH + " " + WIN_FLAG + " " + urlStr; Process p= Runtime.getRuntime().exec(cmd); if(p==null) return("No Windows Process - Runtime.getRuntime().exec("+ cmd+") failed\n"); } /* windows */ /* Mac Code [JE-06-6-2001] * Use the MRJ from Apple to envoke the default browser */ else if(osName.startsWith("Mac")) { /* Macintosh */ try { Class C = Class.forName("com.apple.mrj.MRJFileUtils"); C.getMethod("openURL", new Class [] {Class.forName("java.lang.String")} ).invoke((Object)null, new Object [] {urlStr}); } catch(ClassNotFoundException e) { return("Can't start Web browser - missing 'com.apple.mrj.MRJFileUtils'\n"); } /* Macintosh */ catch(Exception e) { /* this catches following exceptions related to envoking MRJ * + NoSuchMethodException * + SecurityException * + IllegalAccessException * + IllegalArgumentExceptionb * + InvocationTargetException */ return("Can't start Mac Web browser, old version of MRJ "+e+"\n"); } } else { /* UNIX */ /* Under Unix, Netscape has to be running for the * "-remote" command to work. So, we try sending the * command and check for an exit value. If the exit * command is 0, it worked, otherwise we need to start * the browser. */ cmd= UNIX_PATH + " " + UNIX_FLAG + "(" + urlStr + ")"; Process p= Runtime.getRuntime().exec(cmd); try { /* wait for exit code -- if it's 0, command worked, * otherwise we need to start the browser up. */ if(p.waitFor()!=0) { /* Command failed, start up the browser*/ cmd= UNIX_PATH + " " + urlStr; p= Runtime.getRuntime().exec(cmd); } } catch(InterruptedException x) { return("Can't start Web browser under unix:"+x+"\n"); } } /* UNIX */ } /* try to start browser */ catch(IOException e) /* couldn't exec browser*/ { return("Can't start Web browser: "+e+"\n"); } return(null); } /* popupHTMLbrowser */ } /* End of class PopupHTMLbrowser */