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