4ff15ff658e8745880dc77640762671f3cdb3a0b
[jalview.git] / src / jalview / util / BrowserLauncher.java
1 package jalview.util;
2
3 import java.awt.Desktop;
4 import java.io.IOException;
5 import java.net.URI;
6 import java.net.URISyntaxException;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import jalview.bin.Cache;
11 import jalview.bin.Console;
12
13 public class BrowserLauncher
14 {
15   private static BrowserLauncher INSTANCE = null;
16
17   private static String preferredBrowser = null;
18
19   public static BrowserLauncher getInstance()
20   {
21     if (INSTANCE != null)
22     {
23       return INSTANCE;
24     }
25     INSTANCE = new BrowserLauncher();
26     return INSTANCE;
27   }
28
29   public static void openURL(String url)
30   {
31     if (Platform.isJS())
32     {
33       Platform.openURL(url);
34       return;
35     }
36     else
37     /**
38      * Java only
39      * 
40      * @j2sIgnore
41      */
42     {
43       Desktop d = Desktop.getDesktop();
44       if (d != null && d.isSupported(Desktop.Action.BROWSE))
45       {
46         try
47         {
48           d.browse(new URI(url));
49         } catch (IOException e)
50         {
51           Console.warn(MessageManager.formatMessage(
52                   "exception.browser_unable_to_launch", url));
53           Console.warn(e.getMessage());
54           Console.debug(Cache.getStackTraceString(e));
55         } catch (URISyntaxException e1)
56         {
57           Console.warn(MessageManager.formatMessage(
58                   "exception.browser_unable_to_launch", url));
59           Console.warn(e1.getMessage());
60           Console.debug(Cache.getStackTraceString(e1));
61         }
62       }
63       else
64       {
65         Console.warn(MessageManager
66                 .formatMessage("exception.browser_os_not_supported", url));
67       }
68     }
69   }
70
71   public static void resetBrowser()
72   {
73     resetBrowser(false);
74   }
75
76   public static void resetBrowser(boolean removeIfNull)
77   {
78     String defaultBrowser = Cache.getProperty("DEFAULT_BROWSER");
79     preferredBrowser = defaultBrowser;
80     // System.setProperty(getBrowserSystemProperty(),
81     // Cache.getProperty("DEFAULT_BROWSER"));
82     if (defaultBrowser == null && removeIfNull)
83     {
84       // System.clearProperty(getBrowserSystemProperty());
85     }
86
87   }
88
89   public static List<String> getBrowserList()
90   {
91     return new ArrayList<String>();
92   }
93
94   public static String getBrowserSystemProperty()
95   {
96     // return IBrowserLaunching.BROWSER_SYSTEM_PROPERTY;
97     return "jalview.default.browser";
98   }
99
100 }