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

Last change on this file since 3779 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
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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.MalformedURLException;
9import java.net.URI;
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
18 *
19 * This now delegates the real work to a platform specific class.
20 *
21 * @author Imi
22 */
23public class OpenBrowser {
24
25 /**
26 * @return <code>null</code> for success or a string in case of an error.
27 * @throws IllegalStateException thrown if no platform is set to which opening the URL can be dispatched,
28 * {@see Main#platform}
29 */
30 public static String displayUrl(URI uri) {
31 if (Main.applet) {
32 try {
33 JApplet applet = (JApplet) Main.parent;
34 applet.getAppletContext().showDocument(uri.toURL());
35 return null;
36 } catch (MalformedURLException mue) {
37 return mue.getMessage();
38 }
39 }
40
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) {
63 try {
64 return displayUrl(new URI(url));
65 } catch (Exception e) {
66 return e.getMessage();
67 }
68 }
69}
Note: See TracBrowser for help on using the repository browser.