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

Last change on this file since 6890 was 6682, checked in by simon04, 10 years ago

fix #5629 #5108 #9568 - Make Unix web browsers configurable via browser.unix property

Default is ["xdg-open", "#DESKTOP#", "$BROWSER", "gnome-open", "kfmclient openURL", "firefox"].

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