JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / gui / Help.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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.gui;
22
23 import java.net.URL;
24
25 import javax.help.BadIDException;
26 import javax.help.HelpBroker;
27 import javax.help.HelpSet;
28 import javax.help.HelpSetException;
29
30 /**
31  * Utility class to show the help documentation window.
32  * 
33  * @author gmcarstairs
34  *
35  */
36 public class Help
37 {
38   public enum HelpId
39   {
40     Home("home"), SequenceFeatureSettings("seqfeatures.settings"), StructureViewer(
41             "viewingpdbs");
42
43     private String id;
44
45     private HelpId(String loc)
46     {
47       this.id = loc;
48     }
49
50     @Override
51     public String toString()
52     {
53       return this.id;
54     }
55   }
56
57   private static final long HALF_A_MO = 500; // half a second
58
59   private static long lastOpenedTime = 0L;
60
61   /**
62    * Not instantiable
63    */
64   private Help()
65   {
66
67   }
68
69   /**
70    * Show help text in a new window. But do nothing if within half a second of
71    * the last invocation.
72    * 
73    * This is a workaround for issue JAL-914 - both Desktop and AlignFrame
74    * responding to F1 key, resulting in duplicate help windows opened.
75    * 
76    * @param id
77    *          TODO
78    * 
79    * @throws HelpSetException
80    */
81   public static void showHelpWindow(HelpId id) throws HelpSetException
82   {
83     long timeNow = System.currentTimeMillis();
84
85     if (timeNow - lastOpenedTime > HALF_A_MO)
86     {
87       lastOpenedTime = timeNow;
88       ClassLoader cl = Desktop.class.getClassLoader();
89       URL url = HelpSet.findHelpSet(cl, "help/help"); // $NON-NLS-$
90       HelpSet hs = new HelpSet(cl, url);
91
92       HelpBroker hb = hs.createHelpBroker();
93       try
94       {
95         hb.setCurrentID(id.toString());
96       } catch (BadIDException bad)
97       {
98         System.out.println("Bad help link: " + id.toString()
99                 + ": must match a target in help.jhm");
100         throw bad;
101       }
102       hb.setDisplayed(true);
103     }
104   }
105
106   public static void showHelpWindow() throws HelpSetException
107   {
108     showHelpWindow(HelpId.Home);
109   }
110 }