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

Last change on this file since 14397 was 14389, checked in by simon04, 5 years ago

fix #16891 - Allow to override default web browser on Windows

Set the advanced preference key browser.windows to the browser executable.

  • Property svn:eol-style set to native
File size: 2.5 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.URI;
9import java.net.URISyntaxException;
10
11/**
12 * Helper to open platform web browser on different platforms
13 *
14 * This now delegates the real work to a platform specific class.
15 *
16 * @author Imi
17 */
18public final class OpenBrowser {
19
20 private OpenBrowser() {
21 // Hide default constructor for utils classes
22 }
23
24 /**
25 * Displays an external URI using platform associated software.
26 * A web resource will launch platform's browser, an audio file URI will launch audio player, etc.
27 * @param uri The URI to display
28 * @return <code>null</code> for success or a string in case of an error.
29 * @throws IllegalStateException if no platform is set to which opening the URL can be dispatched,
30 * {@link PlatformManager#getPlatform}
31 */
32 public static String displayUrl(URI uri) {
33 CheckParameterUtil.ensureParameterNotNull(uri, "uri");
34
35 Logging.info(tr("Opening URL: {0}", uri));
36
37 try {
38 if (PlatformManager.getPlatform() != null) {
39 // see #5629 #5108 #9568
40 PlatformManager.getPlatform().openUrl(uri.toString());
41 } else if (Desktop.isDesktopSupported()) {
42 // This is not the case with some Linux environments (see below),
43 // and not sure about Mac OS X, so we need to handle API failure
44 Desktop.getDesktop().browse(uri);
45 } else {
46 Logging.warn("Neither Platform nor Desktop class is not supported. Cannot open " + uri);
47 }
48 } catch (IOException e) {
49 Logging.warn(e);
50 return e.getMessage();
51 }
52 return null;
53 }
54
55 /**
56 * Displays an external URL using platform associated software.
57 * A web resource will launch platform's browser, an audio file URL will launch audio player, etc.
58 * @param url The URL to display
59 * @return <code>null</code> for success or a string in case of an error.
60 * @throws IllegalStateException if no platform is set to which opening the URL can be dispatched,
61 * {@link PlatformManager#getPlatform}
62 */
63 public static String displayUrl(String url) {
64 try {
65 return displayUrl(new URI(url));
66 } catch (URISyntaxException e) {
67 Logging.debug(e);
68 return e.getMessage();
69 }
70 }
71}
Note: See TracBrowser for help on using the repository browser.