|
1 | | -// /////////////////////////////////////////////////////// |
2 | | -// Bare Bones Browser Launch // |
3 | | -// Version 1.5 // |
4 | | -// December 10, 2005 // |
5 | | -// Supports: Mac OS X, GNU/Linux, Unix, Windows XP // |
6 | | -// Example Usage: // |
7 | | -// String url = "http://www.centerkey.com/"; // |
8 | | -// BareBonesBrowserLaunch.openURL(url); // |
9 | | -// Public Domain Software -- Free to Use as You Like // |
10 | | -// /////////////////////////////////////////////////////// |
| 1 | +// SPDX-License-Identifier: MPL-2.0 |
| 2 | +// SPDX-FileCopyrightText: 2025 Tony Germano <[email protected]> |
| 3 | +// SPDX-Contributor: Original Author Dem Pilafian (Original work licensed under WTFPL) |
| 4 | + |
11 | 5 | package com.mirth.connect.client.ui; |
12 | 6 |
|
13 | | -import java.lang.reflect.Method; |
| 7 | +import java.util.Arrays; |
14 | 8 |
|
| 9 | +/** |
| 10 | + * Utility class to open a web page from a Swing application |
| 11 | + * in the user's default browser. |
| 12 | + * <p> |
| 13 | + * Supports: Mac OS X, Linux, Unix, Windows |
| 14 | + * <p> |
| 15 | + * Example usage:<br> |
| 16 | + * <code> |
| 17 | + * String url = "https://dna-engine.org/";<br> |
| 18 | + * BareBonesBrowserLaunch.openURL(url);</code> |
| 19 | + * <p> |
| 20 | + * Latest Version: <a href=https://centerkey.com/java/browser> |
| 21 | + * https://centerkey.com/java/browser</a> |
| 22 | + * <p> |
| 23 | + * Published: October 24, 2010 |
| 24 | + * Modified: 2025 |
| 25 | + * <p> |
| 26 | + * |
| 27 | + * @author Dem Pilafian |
| 28 | + * @version 3.2 |
| 29 | + */ |
15 | 30 | public class BareBonesBrowserLaunch { |
16 | 31 |
|
| 32 | + static final String[] browsers = { "xdg-open", "x-www-browser", "google-chrome", |
| 33 | + "firefox", "opera", "epiphany", "konqueror", "conkeror", "midori", |
| 34 | + "kazehakase", "mozilla" }; |
| 35 | + |
| 36 | + /** |
| 37 | + * Open the specified web page in the user's default browser |
| 38 | + * |
| 39 | + * @param url A web address (URL) of a web page (example: |
| 40 | + * <code>"https://dna-engine.org/"</code>) |
| 41 | + */ |
17 | 42 | public static void openURL(String url) { |
18 | | - String osName = System.getProperty("os.name"); |
19 | | - try { |
20 | | - if (osName.startsWith("Mac OS")) { |
21 | | - Class<?> fileMgr = Class.forName("com.apple.eio.FileManager"); |
22 | | - Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class }); |
23 | | - openURL.invoke(null, new Object[] { url }); |
24 | | - } else if (osName.startsWith("Windows")) { |
25 | | - Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); |
26 | | - } else { |
27 | | - // assume Unix or Linux |
28 | | - String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", |
29 | | - "netscape" }; |
30 | | - String browser = null; |
31 | | - for (int count = 0; count < browsers.length && browser == null; count++) { |
32 | | - if (Runtime.getRuntime().exec(new String[] { "which", |
33 | | - browsers[count] }).waitFor() == 0) { |
34 | | - browser = browsers[count]; |
35 | | - } |
36 | | - } |
37 | | - if (browser == null) { |
38 | | - throw new Exception("Could not find web browser"); |
39 | | - } else { |
40 | | - Runtime.getRuntime().exec(new String[] { browser, url }); |
| 43 | + try { // attempt to use Desktop library from JDK 1.6+ |
| 44 | + Class<?> d = Class.forName("java.awt.Desktop"); |
| 45 | + d.getDeclaredMethod("browse", |
| 46 | + new Class<?>[] { java.net.URI.class }).invoke( |
| 47 | + d.getDeclaredMethod("getDesktop").invoke(null), |
| 48 | + new Object[] { java.net.URI.create(url) }); |
| 49 | + } catch (Exception ignore) { // library not available or failed |
| 50 | + String osName = System.getProperty("os.name"); |
| 51 | + try { |
| 52 | + if (osName.startsWith("Mac OS")) { |
| 53 | + Class.forName("com.apple.eio.FileManager").getDeclaredMethod( |
| 54 | + "openURL", new Class<?>[] { String.class }).invoke(null, |
| 55 | + new Object[] { url }); |
| 56 | + } else if (osName.startsWith("Windows")) |
| 57 | + Runtime.getRuntime().exec(new String[] { |
| 58 | + "rundll32", "url.dll,FileProtocolHandler", url }); |
| 59 | + else { // assume Unix or Linux |
| 60 | + String browser = null; |
| 61 | + for (String b : browsers) |
| 62 | + if (browser == null |
| 63 | + && Runtime.getRuntime().exec(new String[] { "which", b }).getInputStream().read() != -1) |
| 64 | + Runtime.getRuntime().exec(new String[] { browser = b, url }); |
| 65 | + if (browser == null) |
| 66 | + throw new Exception(Arrays.toString(browsers)); |
41 | 67 | } |
| 68 | + } catch (Exception e) { |
| 69 | + PlatformUI.MIRTH_FRAME.alertThrowable(PlatformUI.MIRTH_FRAME, e); |
42 | 70 | } |
43 | | - } catch (Exception e) { |
44 | | - PlatformUI.MIRTH_FRAME.alertThrowable(PlatformUI.MIRTH_FRAME, e); |
45 | 71 | } |
46 | 72 | } |
47 | 73 | } |
0 commit comments