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

Last change on this file since 14091 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

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