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

Last change on this file since 6623 was 6380, checked in by Don-vip, 10 years ago

update license/copyright information

  • 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.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 {
62 // 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
63 try {
64 Desktop.getDesktop().browse(uri);
65 } catch (IOException e) {
66 // Workaround for KDE (Desktop API is severely flawed)
67 // see https://bugs.openjdk.java.net/browse/JDK-6486393
68 Main.warn("Desktop class failed. Platform dependent fall back for open url in browser.");
69 displayUrlFallback(uri);
70 }
71 }
72 } catch (Exception e) {
73 Main.warn(e);
74 return e.getMessage();
75 }
76 } else {
77 try {
78 Main.warn("Desktop class is not supported. Platform dependent fall back for open url in browser.");
79 displayUrlFallback(uri);
80 } catch (IOException e) {
81 return e.getMessage();
82 }
83 }
84 return null;
85 }
86
87 /**
88 * Displays an external URL using platform associated software.
89 * A web resource will launch platform's browser, an audio file URL will launch audio player, etc.
90 * @param url The URL to display
91 * @return <code>null</code> for success or a string in case of an error.
92 * @throws IllegalStateException thrown if no platform is set to which opening the URL can be dispatched,
93 * {@link Main#platform}
94 */
95 public static String displayUrl(String url) {
96 try {
97 return displayUrl(new URI(url));
98 } catch (Exception e) {
99 return e.getMessage();
100 }
101 }
102}
Note: See TracBrowser for help on using the repository browser.