JAL-914 don't open help window twice
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 3 Oct 2014 11:00:10 +0000 (12:00 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 3 Oct 2014 11:00:10 +0000 (12:00 +0100)
src/jalview/gui/Desktop.java
src/jalview/gui/Help.java [new file with mode: 0644]

index b7057d1..eb4f968 100644 (file)
@@ -89,9 +89,9 @@ import javax.swing.JPopupMenu;
 import javax.swing.JProgressBar;
 import javax.swing.SwingUtilities;
 import javax.swing.event.HyperlinkEvent;
+import javax.swing.event.HyperlinkEvent.EventType;
 import javax.swing.event.MenuEvent;
 import javax.swing.event.MenuListener;
-import javax.swing.event.HyperlinkEvent.EventType;
 
 /**
  * Jalview Desktop
@@ -317,8 +317,8 @@ public class Desktop extends jalview.jbgui.GDesktop implements
     else
     {
       Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
-      setBounds((int) (screenSize.width - 900) / 2,
-              (int) (screenSize.height - 650) / 2, 900, 650);
+      setBounds((screenSize.width - 900) / 2,
+              (screenSize.height - 650) / 2, 900, 650);
     }
     jconsole = new Console(this, showjconsole);
     // add essential build information
@@ -1183,13 +1183,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   {
     try
     {
-      ClassLoader cl = jalview.gui.Desktop.class.getClassLoader();
-      java.net.URL url = javax.help.HelpSet.findHelpSet(cl, "help/help");
-      javax.help.HelpSet hs = new javax.help.HelpSet(cl, url);
-
-      javax.help.HelpBroker hb = hs.createHelpBroker();
-      hb.setCurrentID("home");
-      hb.setDisplayed(true);
+      Help.showHelpWindow();
     } catch (Exception ex)
     {
     }
@@ -2369,7 +2363,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
               "call setProgressBar before registering the progress bar's handler.");
     }
     progressBarHandlers.put(new Long(id), handler);
-    final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
+    final JPanel progressPanel = progressBars.get(new Long(id));
     if (handler.canCancel())
     {
       JButton cancel = new JButton(
diff --git a/src/jalview/gui/Help.java b/src/jalview/gui/Help.java
new file mode 100644 (file)
index 0000000..dac17c0
--- /dev/null
@@ -0,0 +1,55 @@
+package jalview.gui;
+
+import java.net.URL;
+
+import javax.help.HelpBroker;
+import javax.help.HelpSet;
+import javax.help.HelpSetException;
+
+/**
+ * Utility class to show the help documentation window.
+ * 
+ * @author gmcarstairs
+ *
+ */
+public class Help
+{
+
+  private static final long HALF_A_MO = 500; // half a second
+
+  private static long lastOpenedTime = 0L;
+
+  /**
+   * Not instantiable
+   */
+  private Help()
+  {
+
+  }
+
+  /**
+   * Show help text in a new window. But do nothing if within half a second of
+   * the last invocation.
+   * 
+   * This is a workaround for issue JAL-914 - both Desktop and AlignFrame
+   * responding to F1 key, resulting in duplicate help windows opened.
+   * 
+   * @throws HelpSetException
+   */
+  public static void showHelpWindow() throws HelpSetException
+  {
+    long timeNow = System.currentTimeMillis();
+
+    if (timeNow - lastOpenedTime > HALF_A_MO)
+    {
+      lastOpenedTime = timeNow;
+      ClassLoader cl = Desktop.class.getClassLoader();
+      URL url = HelpSet.findHelpSet(cl, "help/help"); // $NON-NLS-$
+      HelpSet hs = new HelpSet(cl, url);
+
+      HelpBroker hb = hs.createHelpBroker();
+      hb.setCurrentID("home");
+      hb.setDisplayed(true);
+    }
+  }
+}