Changeset 14389 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2018-10-30T23:07:31+01:00 (6 years ago)
Author:
simon04
Message:

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

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

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/OpenBrowser.java

    r14146 r14389  
    2222    }
    2323
    24     private static void displayUrlFallback(URI uri) throws IOException {
    25         if (PlatformManager.getPlatform() == null)
    26             throw new IllegalStateException(tr("Failed to open URL. There is currently no platform set. Please set a platform first."));
    27         PlatformManager.getPlatform().openUrl(uri.toString());
    28     }
    29 
    3024    /**
    3125     * Displays an external URI using platform associated software.
     
    4135        Logging.info(tr("Opening URL: {0}", uri));
    4236
    43         if (Desktop.isDesktopSupported()) {
    44             try {
    45                 if (PlatformManager.isPlatformWindows()) {
    46                     // Desktop API works fine under Windows, so we don't try any fallback in case of I/O exceptions because it's not API's fault
    47                     Desktop.getDesktop().browse(uri);
    48                 } else if (PlatformManager.isPlatformUnixoid() || PlatformManager.isPlatformOsx()) {
    49                     // see #5629 #5108 #9568
    50                     PlatformManager.getPlatform().openUrl(uri.toString());
    51                 } else {
    52                     // This is not the case with some Linux environments (see below),
    53                     // and not sure about Mac OS X, so we need to handle API failure
    54                     try {
    55                         Desktop.getDesktop().browse(uri);
    56                     } catch (IOException e) {
    57                         // Workaround for KDE (Desktop API is severely flawed)
    58                         // see https://bugs.openjdk.java.net/browse/JDK-6486393
    59                         Logging.log(Logging.LEVEL_WARN, "Desktop class failed. Platform dependent fall back for open url in browser.", e);
    60                         displayUrlFallback(uri);
    61                     }
    62                 }
    63             } catch (IOException e) {
    64                 Logging.warn(e);
    65                 return e.getMessage();
     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);
    6647            }
    67         } else {
    68             try {
    69                 Logging.warn("Desktop class is not supported. Platform dependent fall back for open url in browser.");
    70                 displayUrlFallback(uri);
    71             } catch (IOException e) {
    72                 Logging.debug(e);
    73                 return e.getMessage();
    74             }
     48        } catch (IOException e) {
     49            Logging.warn(e);
     50            return e.getMessage();
    7551        }
    7652        return null;
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookWindows.java

    r14368 r14389  
    3131import static org.openstreetmap.josm.tools.WinRegistry.HKEY_LOCAL_MACHINE;
    3232
     33import java.awt.Desktop;
    3334import java.awt.GraphicsEnvironment;
    3435import java.io.BufferedWriter;
     
    4041import java.io.Writer;
    4142import java.lang.reflect.InvocationTargetException;
     43import java.net.URI;
     44import java.net.URISyntaxException;
    4245import java.nio.charset.StandardCharsets;
    4346import java.nio.file.DirectoryIteratorException;
     
    188191    @Override
    189192    public void openUrl(String url) throws IOException {
    190         Runtime.getRuntime().exec(new String[]{"rundll32", "url.dll,FileProtocolHandler", url});
     193        final String customBrowser = Config.getPref().get("browser.windows", null);
     194        if (customBrowser != null) {
     195            Runtime.getRuntime().exec(new String[]{customBrowser, url});
     196            return;
     197        }
     198        try {
     199            // Desktop API works fine under Windows
     200            Desktop.getDesktop().browse(new URI(url));
     201        } catch (IOException | URISyntaxException e) {
     202            Logging.log(Logging.LEVEL_WARN, "Desktop class failed. Platform dependent fall back for open url in browser.", e);
     203            Runtime.getRuntime().exec(new String[]{"rundll32", "url.dll,FileProtocolHandler", url});
     204        }
    191205    }
    192206
Note: See TracChangeset for help on using the changeset viewer.