Merge: 497958b 68dcaa7
[jalview.git] / src / jalview / gui / Help.java
1 package jalview.gui;
2
3 import java.net.URL;
4
5 import javax.help.BadIDException;
6 import javax.help.HelpBroker;
7 import javax.help.HelpSet;
8 import javax.help.HelpSetException;
9
10 /**
11  * Utility class to show the help documentation window.
12  * 
13  * @author gmcarstairs
14  *
15  */
16 public class Help
17 {
18   public enum HelpId
19   {
20     Home("home"), SequenceFeatureSettings("seqfeatures.settings"), StructureViewer(
21             "viewingpdbs");
22
23     private String id;
24
25     private HelpId(String loc)
26     {
27       this.id = loc;
28     }
29
30     @Override
31     public String toString()
32     {
33       return this.id;
34     }
35   }
36
37   private static final long HALF_A_MO = 500; // half a second
38
39   private static long lastOpenedTime = 0L;
40
41   /**
42    * Not instantiable
43    */
44   private Help()
45   {
46
47   }
48
49   /**
50    * Show help text in a new window. But do nothing if within half a second of
51    * the last invocation.
52    * 
53    * This is a workaround for issue JAL-914 - both Desktop and AlignFrame
54    * responding to F1 key, resulting in duplicate help windows opened.
55    * @param id TODO
56    * 
57    * @throws HelpSetException
58    */
59   public static void showHelpWindow(HelpId id) throws HelpSetException
60   {
61     long timeNow = System.currentTimeMillis();
62
63     if (timeNow - lastOpenedTime > HALF_A_MO)
64     {
65       lastOpenedTime = timeNow;
66       ClassLoader cl = Desktop.class.getClassLoader();
67       URL url = HelpSet.findHelpSet(cl, "help/help"); // $NON-NLS-$
68       HelpSet hs = new HelpSet(cl, url);
69
70       HelpBroker hb = hs.createHelpBroker();
71       try
72       {
73         hb.setCurrentID(id.toString());
74       } catch (BadIDException bad)
75       {
76         System.out.println("Bad help link: " + id.toString()
77                 + ": must match a target in help.jhm");
78         throw bad;
79       }
80       hb.setDisplayed(true);
81     }
82   }
83
84   public static void showHelpWindow() throws HelpSetException
85   {
86     showHelpWindow(HelpId.Home);
87   }
88 }