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

Revision 4578, 2.5 KB checked in by Don-vip, 7 months ago (diff)

see #5805 - Java Desktop API Fallback on unsupported-but-said-supported platforms such as KDE

  • Property svn:eol-style set to native
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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 class OpenBrowser {
23
24    private static void displayUrlFallback(URI uri) throws IOException {
25        if (Main.platform == null)
26            throw new IllegalStateException(tr("Failed to open URL. There is currently no platform set. Please set a platform first."));
27        Main.platform.openUrl(uri.toString());
28    }
29   
30    /**
31     * @return <code>null</code> for success or a string in case of an error.
32     * @throws IllegalStateException thrown if no platform is set to which opening the URL can be dispatched,
33     * {@see Main#platform}
34     */
35    public static String displayUrl(URI uri) {
36        if (Main.applet) {
37            try {
38                JApplet applet = (JApplet) Main.parent;
39                applet.getAppletContext().showDocument(uri.toURL());
40                return null;
41            } catch (MalformedURLException mue) {
42                return mue.getMessage();
43            }
44        }
45
46        if (Desktop.isDesktopSupported()) {
47            try {
48                try {
49                    Desktop.getDesktop().browse(uri);
50                } catch (IOException e) {
51                    // Workaround for KDE (Desktop API is severely flawed)
52                    // see http://bugs.sun.com/view_bug.do?bug_id=6486393
53                    System.err.println("Warning: Desktop class failed. Platform dependent fall back for open url in browser.");
54                    displayUrlFallback(uri);
55                }
56            } catch (Exception e) {
57                e.printStackTrace();
58                return e.getMessage();
59            }
60        } else {
61            try {
62                System.err.println("Warning: Desktop class is not supported. Platform dependent fall back for open url in browser.");
63                displayUrlFallback(uri);
64            } catch (IOException e) {
65                return e.getMessage();
66            }
67        }
68        return null;
69    }
70
71    public static String displayUrl(String url) {
72        try {
73            return displayUrl(new URI(url));
74        } catch (Exception e) {
75            return e.getMessage();
76        }
77    }
78}
Note: See TracBrowser for help on using the repository browser.