001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.tools; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.Desktop; 007import java.io.IOException; 008import java.net.MalformedURLException; 009import java.net.URI; 010import java.net.URISyntaxException; 011 012/** 013 * Helper to open platform web browser on different platforms 014 * 015 * This now delegates the real work to a platform specific class. 016 * 017 * @author Imi 018 */ 019public final class OpenBrowser { 020 021 private OpenBrowser() { 022 // Hide default constructor for utils classes 023 } 024 025 /** 026 * Displays an external URI using platform associated software. 027 * A web resource will launch platform's browser, an audio file URI will launch audio player, etc. 028 * @param uri The URI to display 029 * @return <code>null</code> for success or a string in case of an error. 030 * @throws IllegalStateException if no platform is set to which opening the URL can be dispatched, 031 * {@link PlatformManager#getPlatform} 032 */ 033 public static String displayUrl(URI uri) { 034 CheckParameterUtil.ensureParameterNotNull(uri, "uri"); 035 036 Logging.info(tr("Opening URL: {0}", uri)); 037 038 try { 039 if (PlatformManager.getPlatform() != null) { 040 // see #5629 #5108 #9568 041 PlatformManager.getPlatform().openUrl(uri.toString()); 042 } else if (Desktop.isDesktopSupported()) { 043 // This is not the case with some Linux environments (see below), 044 // and not sure about Mac OS X, so we need to handle API failure 045 Desktop.getDesktop().browse(uri); 046 } else { 047 Logging.warn("Neither Platform nor Desktop class is not supported. Cannot open " + uri); 048 } 049 } catch (IOException e) { 050 Logging.warn(e); 051 return e.getMessage(); 052 } 053 return null; 054 } 055 056 /** 057 * Displays an external URL using platform associated software. 058 * A web resource will launch platform's browser, an audio file URL will launch audio player, etc. 059 * @param url The URL to display 060 * @return <code>null</code> for success or a string in case of an error. 061 * @throws IllegalStateException if no platform is set to which opening the URL can be dispatched, 062 * {@link PlatformManager#getPlatform} 063 */ 064 public static String displayUrl(String url) { 065 try { 066 return displayUrl(Utils.urlToURI(url)); 067 } catch (URISyntaxException | MalformedURLException e) { 068 Logging.debug(e); 069 return e.getMessage(); 070 } 071 } 072}