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

Last change on this file since 14146 was 14146, checked in by Don-vip, 6 years ago

see #15229 - don't import Main just for javadoc

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