|
22 | 22 | import java.awt.*;
|
23 | 23 | import java.awt.image.BufferedImage;
|
24 | 24 | import java.io.ByteArrayInputStream;
|
| 25 | +import java.io.IOException; |
| 26 | +import java.io.InputStream; |
25 | 27 | import java.lang.reflect.Method;
|
| 28 | +import java.net.URI; |
26 | 29 | import java.util.Arrays;
|
27 | 30 | import java.util.Set;
|
28 | 31 | import java.util.stream.Collectors;
|
@@ -310,4 +313,41 @@ private static void animate(Node node, long millis, int r, int g, int b) {
|
310 | 313 | timeline.getKeyFrames().add(kf);
|
311 | 314 | timeline.play();
|
312 | 315 | }
|
| 316 | + |
| 317 | + /** |
| 318 | + * Attempts to launch a browser to display a {@link URI}. |
| 319 | + * |
| 320 | + * @param uri |
| 321 | + * URI to display. |
| 322 | + * |
| 323 | + * @throws IOException |
| 324 | + * If the browser is not found, or it fails |
| 325 | + * to be launched. |
| 326 | + */ |
| 327 | + public static void showDocument(URI uri) throws IOException { |
| 328 | + switch (OSUtil.getOSType()) { |
| 329 | + case MAC: |
| 330 | + Desktop.getDesktop().browse(uri); |
| 331 | + break; |
| 332 | + case WINDOWS: |
| 333 | + Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + uri); |
| 334 | + break; |
| 335 | + case LINUX: |
| 336 | + Runtime rt = Runtime.getRuntime(); |
| 337 | + String[] browsers = new String[]{"xdg-open", "google-chrome", "firefox", "opera", |
| 338 | + "konqueror", "mozilla"}; |
| 339 | + |
| 340 | + for (String browser : browsers) { |
| 341 | + try (InputStream in = rt.exec(new String[]{"which", browser}).getInputStream()) { |
| 342 | + if (in.read() != -1) { |
| 343 | + rt.exec(new String[]{browser, uri.toString()}); |
| 344 | + return; |
| 345 | + } |
| 346 | + } |
| 347 | + } |
| 348 | + throw new IOException("No browser found"); |
| 349 | + default: |
| 350 | + throw new IllegalStateException("Unsupported OS"); |
| 351 | + } |
| 352 | + } |
313 | 353 | }
|
0 commit comments