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

Last change on this file since 4153 was 3524, checked in by bastiK, 14 years ago

use java 6 features to open url in browser

  • Property svn:eol-style set to native
File size: 2.0 KB
RevLine 
[298]1// License: GPL. Copyright 2007 by Immanuel Scholz and others
[626]2package org.openstreetmap.josm.tools;
3
[2748]4import static org.openstreetmap.josm.tools.I18n.tr;
5
[3524]6import java.awt.Desktop;
[626]7import java.io.IOException;
8import java.net.MalformedURLException;
[3524]9import java.net.URI;
[626]10import java.net.URL;
11
12import javax.swing.JApplet;
13
14import org.openstreetmap.josm.Main;
15
16/**
17 * Helper to open platform web browser on different platforms
[1023]18 *
19 * This now delegates the real work to a platform specific class.
20 *
[626]21 * @author Imi
22 */
23public class OpenBrowser {
24
[1169]25 /**
26 * @return <code>null</code> for success or a string in case of an error.
[2748]27 * @throws IllegalStateException thrown if no platform is set to which opening the URL can be dispatched,
28 * {@see Main#platform}
[1169]29 */
[3524]30 public static String displayUrl(URI uri) {
[1169]31 if (Main.applet) {
32 try {
33 JApplet applet = (JApplet) Main.parent;
[3524]34 applet.getAppletContext().showDocument(uri.toURL());
[1169]35 return null;
36 } catch (MalformedURLException mue) {
37 return mue.getMessage();
38 }
39 }
[626]40
[3524]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 }
58 }
59 return null;
60 }
61
62 public static String displayUrl(String url) {
[1169]63 try {
[3524]64 return displayUrl(new URI(url));
65 } catch (Exception e) {
[1169]66 return e.getMessage();
67 }
68 }
[626]69}
Note: See TracBrowser for help on using the repository browser.