JAL-1645 source formatting and organise imports
[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    * 
56    * @param id
57    *          TODO
58    * 
59    * @throws HelpSetException
60    */
61   public static void showHelpWindow(HelpId id) throws HelpSetException
62   {
63     long timeNow = System.currentTimeMillis();
64
65     if (timeNow - lastOpenedTime > HALF_A_MO)
66     {
67       lastOpenedTime = timeNow;
68       ClassLoader cl = Desktop.class.getClassLoader();
69       URL url = HelpSet.findHelpSet(cl, "help/help"); // $NON-NLS-$
70       HelpSet hs = new HelpSet(cl, url);
71
72       HelpBroker hb = hs.createHelpBroker();
73       try
74       {
75         hb.setCurrentID(id.toString());
76       } catch (BadIDException bad)
77       {
78         System.out.println("Bad help link: " + id.toString()
79                 + ": must match a target in help.jhm");
80         throw bad;
81       }
82       hb.setDisplayed(true);
83     }
84   }
85
86   public static void showHelpWindow() throws HelpSetException
87   {
88     showHelpWindow(HelpId.Home);
89   }
90 }