Changeset 3524 in josm


Ignore:
Timestamp:
Sep 13, 2010 9:40:18 PM (3 years ago)
Author:
bastiK
Message:

use java 6 features to open url in browser

File:
1 edited

Legend:

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

    r2748 r3524  
    44import static org.openstreetmap.josm.tools.I18n.tr; 
    55 
     6import java.awt.Desktop; 
    67import java.io.IOException; 
    78import java.net.MalformedURLException; 
     9import java.net.URI; 
    810import java.net.URL; 
    911 
     
    2628     * {@see Main#platform} 
    2729     */ 
    28     public static String displayUrl(String url) throws IllegalStateException { 
     30    public static String displayUrl(URI uri) { 
    2931        if (Main.applet) { 
    3032            try { 
    3133                JApplet applet = (JApplet) Main.parent; 
    32                 applet.getAppletContext().showDocument(new URL(url)); 
     34                applet.getAppletContext().showDocument(uri.toURL()); 
    3335                return null; 
    3436            } catch (MalformedURLException mue) { 
     
    3739        } 
    3840 
    39         if (Main.platform == null) 
    40             throw new IllegalStateException(tr("Failed to open URL. There is currently no platform set. Please set a platform first.")); 
    41         try { 
    42             Main.platform.openUrl(url); 
    43         } catch (IOException e) { 
    44             return e.getMessage(); 
     41        if (Desktop.isDesktopSupported()) { 
     42            try { 
     43                Desktop.getDesktop().browse(uri); 
     44            } catch (Exception e) { 
     45                e.printStackTrace(); 
     46                return e.getMessage(); 
     47            } 
     48        } else { 
     49            System.err.println("Warning: Desktop class is not supported. Platform dependent fall back for open url in browser."); 
     50 
     51            if (Main.platform == null) 
     52                throw new IllegalStateException(tr("Failed to open URL. There is currently no platform set. Please set a platform first.")); 
     53            try { 
     54                Main.platform.openUrl(uri.toString()); 
     55            } catch (IOException e) { 
     56                return e.getMessage(); 
     57            } 
    4558        } 
    4659        return null; 
    4760    } 
    4861 
     62    public static String displayUrl(String url) { 
     63        try { 
     64            return displayUrl(new URI(url)); 
     65        } catch (Exception e) { 
     66            return e.getMessage(); 
     67        } 
     68    } 
    4969} 
Note: See TracChangeset for help on using the changeset viewer.