3e4f72b60498e441002ccda4b55797002e99b6af
[jalview.git] / src / jalview / util / BrowserLauncher.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.util;
22
23 import java.awt.Desktop;
24 import java.io.IOException;
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import jalview.bin.Cache;
31 import jalview.bin.Console;
32
33 public class BrowserLauncher
34 {
35   private static BrowserLauncher INSTANCE = null;
36
37   private static String preferredBrowser = null;
38
39   public static BrowserLauncher getInstance()
40   {
41     if (INSTANCE != null)
42     {
43       return INSTANCE;
44     }
45     INSTANCE = new BrowserLauncher();
46     return INSTANCE;
47   }
48
49   public static void openURL(String url)
50   {
51     if (Platform.isJS())
52     {
53         try {
54               Platform.openURL(url);
55         } catch (Throwable t) {
56             System.err.println("Couldn't open "+url);
57             System.err.print(t.getStackTrace());
58         }
59       return;
60     }
61     else
62     /**
63      * Java only
64      * 
65      * @j2sIgnore
66      */
67     {
68       Desktop d = Desktop.getDesktop();
69       if (d != null && d.isSupported(Desktop.Action.BROWSE))
70       {
71         try
72         {
73           d.browse(new URI(url));
74         } catch (IOException e)
75         {
76           Console.warn(MessageManager.formatMessage(
77                   "exception.browser_unable_to_launch", url));
78           Console.warn(e.getMessage());
79           Console.debug(Cache.getStackTraceString(e));
80         } catch (URISyntaxException e1)
81         {
82           Console.warn(MessageManager.formatMessage(
83                   "exception.browser_unable_to_launch", url));
84           Console.warn(e1.getMessage());
85           Console.debug(Cache.getStackTraceString(e1));
86         }
87       }
88       else
89       {
90         Console.warn(MessageManager
91                 .formatMessage("exception.browser_os_not_supported", url));
92       }
93     }
94   }
95
96   public static void resetBrowser()
97   {
98     resetBrowser(false);
99   }
100
101   public static void resetBrowser(boolean removeIfNull)
102   {
103     String defaultBrowser = Cache.getProperty("DEFAULT_BROWSER");
104     preferredBrowser = defaultBrowser;
105     // System.setProperty(getBrowserSystemProperty(),
106     // Cache.getProperty("DEFAULT_BROWSER"));
107     if (defaultBrowser == null && removeIfNull)
108     {
109       // System.clearProperty(getBrowserSystemProperty());
110     }
111
112   }
113
114   public static List<String> getBrowserList()
115   {
116     return new ArrayList<String>();
117   }
118
119   public static String getBrowserSystemProperty()
120   {
121     // return IBrowserLaunching.BROWSER_SYSTEM_PROPERTY;
122     return "jalview.default.browser";
123   }
124
125 }