org/homelinux/largo/utils/BrowserLaunch.java
changeset 16 55b0d5006e7b
parent 13 f83884cc7d2f
equal deleted inserted replaced
15:d4b2b9a87d80 16:55b0d5006e7b
    11 
    11 
    12 /**
    12 /**
    13  * Cross-Platform Browserlaunch
    13  * Cross-Platform Browserlaunch
    14  */
    14  */
    15 public class BrowserLaunch {
    15 public class BrowserLaunch {
    16     private static final String errMsg = "Error attempting to launch web browser";
    16 	private static final String errMsg = "Error attempting to launch web browser";
    17 
    17 
    18     /**
    18 	/**
    19      * This method opens the URL in a native platform browser
    19 	 * This method opens the URL in a native platform browser
    20      */
    20 	 */
    21     public void openURL(String url) {
    21 	public void openURL(String url) {
    22         String osName = System.getProperty("os.name");
    22 		String osName = System.getProperty("os.name");
    23         try {
    23 		try {
    24             if (osName.startsWith("Mac OS")) {
    24 			if (osName.startsWith("Mac OS")) {
    25                 Class<?> fileMgr = Class.forName("com.apple.eio.FileManager");
    25 				Class<?> fileMgr = Class.forName("com.apple.eio.FileManager");
    26                 Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] {
    26 				Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
    27                     String.class
    27 				openURL.invoke(null, new Object[] { url });
    28                 });
    28 			} else if (osName.startsWith("Windows"))
    29                 openURL.invoke(null, new Object[] {
    29 				Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
    30                     url
    30 			else {
    31                 });
    31 				String[] browsers = { "chome", "chromium", "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
    32             } else if (osName.startsWith("Windows"))
    32 				String browser = null;
    33                 Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
    33 				for (int count = 0; count < browsers.length && browser == null; count++)
    34             else {
    34 					if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0)
    35                 String[] browsers = {
    35 						browser = browsers[count];
    36                         "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"
    36 				if (browser == null)
    37                 };
    37 					throw new Exception("Could not find web browser");
    38                 String browser = null;
    38 				else
    39                 for (int count = 0; count < browsers.length && browser == null; count++)
    39 					Runtime.getRuntime().exec(new String[] { browser, url });
    40                     if (Runtime.getRuntime().exec(new String[] {
    40 			}
    41                             "which", browsers[count]
    41 		} catch (Exception e) {
    42                     }).waitFor() == 0)
    42 			JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
    43                         browser = browsers[count];
    43 		}
    44                 if (browser == null)
    44 	}
    45                     throw new Exception("Could not find web browser");
       
    46                 else
       
    47                     Runtime.getRuntime().exec(new String[] {
       
    48                             browser, url
       
    49                     });
       
    50             }
       
    51         } catch (Exception e) {
       
    52             JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
       
    53         }
       
    54     }
       
    55 };
    45 };