001    // License: GPL. Copyright 2007 by Immanuel Scholz and others
002    package org.openstreetmap.josm.tools;
003    
004    import static org.openstreetmap.josm.tools.I18n.tr;
005    
006    import java.awt.Desktop;
007    import java.io.IOException;
008    import java.net.MalformedURLException;
009    import java.net.URI;
010    
011    import javax.swing.JApplet;
012    
013    import org.openstreetmap.josm.Main;
014    
015    /**
016     * Helper to open platform web browser on different platforms
017     *
018     * This now delegates the real work to a platform specific class.
019     *
020     * @author Imi
021     */
022    public class OpenBrowser {
023    
024        private static void displayUrlFallback(URI uri) throws IOException {
025            if (Main.platform == null)
026                throw new IllegalStateException(tr("Failed to open URL. There is currently no platform set. Please set a platform first."));
027            Main.platform.openUrl(uri.toString());
028        }
029        
030        /**
031         * @return <code>null</code> for success or a string in case of an error.
032         * @throws IllegalStateException thrown if no platform is set to which opening the URL can be dispatched,
033         * {@link Main#platform}
034         */
035        public static String displayUrl(URI uri) {
036            if (Main.applet) {
037                try {
038                    JApplet applet = (JApplet) Main.parent;
039                    applet.getAppletContext().showDocument(uri.toURL());
040                    return null;
041                } catch (MalformedURLException mue) {
042                    return mue.getMessage();
043                }
044            }
045    
046            if (Desktop.isDesktopSupported()) {
047                try {
048                    try {
049                        Desktop.getDesktop().browse(uri);
050                    } catch (IOException e) {
051                        // Workaround for KDE (Desktop API is severely flawed)
052                        // see http://bugs.sun.com/view_bug.do?bug_id=6486393
053                        System.err.println("Warning: Desktop class failed. Platform dependent fall back for open url in browser.");
054                        displayUrlFallback(uri);
055                    }
056                } catch (Exception e) {
057                    e.printStackTrace();
058                    return e.getMessage();
059                }
060            } else {
061                try {
062                    System.err.println("Warning: Desktop class is not supported. Platform dependent fall back for open url in browser.");
063                    displayUrlFallback(uri);
064                } catch (IOException e) {
065                    return e.getMessage();
066                }
067            }
068            return null;
069        }
070    
071        public static String displayUrl(String url) {
072            try {
073                return displayUrl(new URI(url));
074            } catch (Exception e) {
075                return e.getMessage();
076            }
077        }
078    }