JAL-1581 Tooltip slimmed down, Help button added on panel with link to
[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");
21
22     private String id;
23
24     private HelpId(String loc)
25     {
26       this.id = loc;
27     }
28
29     @Override
30     public String toString()
31     {
32       return this.id;
33     }
34   }
35
36   private static final long HALF_A_MO = 500; // half a second
37
38   private static long lastOpenedTime = 0L;
39
40   /**
41    * Not instantiable
42    */
43   private Help()
44   {
45
46   }
47
48   /**
49    * Show help text in a new window. But do nothing if within half a second of
50    * the last invocation.
51    * 
52    * This is a workaround for issue JAL-914 - both Desktop and AlignFrame
53    * responding to F1 key, resulting in duplicate help windows opened.
54    * @param id TODO
55    * 
56    * @throws HelpSetException
57    */
58   public static void showHelpWindow(HelpId id) throws HelpSetException
59   {
60     long timeNow = System.currentTimeMillis();
61
62     if (timeNow - lastOpenedTime > HALF_A_MO)
63     {
64       lastOpenedTime = timeNow;
65       ClassLoader cl = Desktop.class.getClassLoader();
66       URL url = HelpSet.findHelpSet(cl, "help/help"); // $NON-NLS-$
67       HelpSet hs = new HelpSet(cl, url);
68
69       HelpBroker hb = hs.createHelpBroker();
70       try
71       {
72         hb.setCurrentID(id.toString());
73       } catch (BadIDException bad)
74       {
75         System.out.println("Bad help link: " + id.toString()
76                 + ": must match a target in help.jhm");
77         throw bad;
78       }
79       hb.setDisplayed(true);
80     }
81   }
82
83   public static void showHelpWindow() throws HelpSetException
84   {
85     showHelpWindow(HelpId.Home);
86   }
87 }