source: josm/trunk/src/org/openstreetmap/josm/tools/OpenBrowser.java@ 9634

Last change on this file since 9634 was 8540, checked in by Don-vip, 9 years ago

fix remaining checkstyle issues

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Desktop;
7import java.io.IOException;
8import java.net.URI;
9
10import org.openstreetmap.josm.Main;
11
12/**
13 * Helper to open platform web browser on different platforms
14 *
15 * This now delegates the real work to a platform specific class.
16 *
17 * @author Imi
18 */
19public final class OpenBrowser {
20
21 private OpenBrowser() {
22 // Hide default constructor for utils classes
23 }
24
25 private static void displayUrlFallback(URI uri) throws IOException {
26 if (Main.platform == null)
27 throw new IllegalStateException(tr("Failed to open URL. There is currently no platform set. Please set a platform first."));
28 Main.platform.openUrl(uri.toString());
29 }
30
31 /**
32 * Displays an external URI using platform associated software.
33 * A web resource will launch platform's browser, an audio file URI will launch audio player, etc.
34 * @param uri The URI to display
35 * @return <code>null</code> for success or a string in case of an error.
36 * @throws IllegalStateException if no platform is set to which opening the URL can be dispatched,
37 * {@link Main#platform}
38 */
39 public static String displayUrl(URI uri) {
40 CheckParameterUtil.ensureParameterNotNull(uri, "uri");
41
42 Main.info(tr("Opening URL: {0}", uri));
43
44 if (Desktop.isDesktopSupported()) {
45 try {
46 if (Main.isPlatformWindows()) {
47 // Desktop API works fine under Windows, so we don't try any fallback in case of I/O exceptions because it's not API's fault
48 Desktop.getDesktop().browse(uri);
49 } else if (Main.platform instanceof PlatformHookUnixoid) {
50 // see #5629 #5108 #9568
51 Main.platform.openUrl(uri.toString());
52 } else {
53 // This is not the case with some Linux environments (see below),
54 // and not sure about Mac OS X, so we need to handle API failure
55 try {
56 Desktop.getDesktop().browse(uri);
57 } catch (IOException e) {
58 // Workaround for KDE (Desktop API is severely flawed)
59 // see https://bugs.openjdk.java.net/browse/JDK-6486393
60 Main.warn("Desktop class failed. Platform dependent fall back for open url in browser.");
61 displayUrlFallback(uri);
62 }
63 }
64 } catch (Exception e) {
65 Main.warn(e);
66 return e.getMessage();
67 }
68 } else {
69 try {
70 Main.warn("Desktop class is not supported. Platform dependent fall back for open url in browser.");
71 displayUrlFallback(uri);
72 } catch (IOException e) {
73 return e.getMessage();
74 }
75 }
76 return null;
77 }
78
79 /**
80 * Displays an external URL using platform associated software.
81 * A web resource will launch platform's browser, an audio file URL will launch audio player, etc.
82 * @param url The URL to display
83 * @return <code>null</code> for success or a string in case of an error.
84 * @throws IllegalStateException if no platform is set to which opening the URL can be dispatched,
85 * {@link Main#platform}
86 */
87 public static String displayUrl(String url) {
88 try {
89 return displayUrl(new URI(url));
90 } catch (Exception e) {
91 return e.getMessage();
92 }
93 }
94}
Note: See TracBrowser for help on using the repository browser.