JAL-1988 Check for setHandler capability to avoid About and Preferences menu options...
authorBen Soares <b.soares@dundee.ac.uk>
Thu, 23 Jun 2022 14:28:46 +0000 (15:28 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Thu, 23 Jun 2022 14:28:46 +0000 (15:28 +0100)
src/jalview/jbgui/APQHandlers.java
src/jalview/jbgui/GDesktop.java

index e18ec44..02e7083 100644 (file)
@@ -24,67 +24,87 @@ import javax.swing.JFrame;
 import javax.swing.JOptionPane;
 
 import com.formdev.flatlaf.extras.FlatDesktop;
+import com.formdev.flatlaf.extras.FlatDesktop.Action;
 
 import jalview.util.MessageManager;
+import jalview.util.Platform;
 
 public class APQHandlers
 {
-  protected static boolean setAPQHandlers = false;
+  public static boolean setAbout = false;
 
-  public static boolean setAPQHandlers(GDesktop desktop)
-  {
-    FlatDesktop.setAboutHandler(() -> {
-      desktop.aboutMenuItem_actionPerformed(null);
-    });
-    FlatDesktop.setPreferencesHandler(() -> {
-      desktop.preferences_actionPerformed(null);
-    });
-    FlatDesktop.setQuitHandler(response -> {
-      boolean confirmQuit = jalview.bin.Cache
-              .getDefault(jalview.gui.Desktop.CONFIRM_KEYBOARD_QUIT, true);
-      boolean canQuit = !confirmQuit;
-      int n;
-      if (confirmQuit)
-      {
-        // ensure Jalview window is brought to front for Quit confirmation
-        // window to be
-        // visible
+  public static boolean setPreferences = false;
 
-        // this method of raising the Jalview window is broken in java
-        // jalviewDesktop.setVisible(true);
-        // jalviewDesktop.toFront();
+  public static boolean setQuit = false;
 
-        // a better hack which works instead
-        JFrame dialogParent = new JFrame();
-        dialogParent.setAlwaysOnTop(true);
+  public static boolean setAPQHandlers(GDesktop desktop)
+  {
+    if (Platform.isJS())
+    {
+      return false;
+    }
+    if (FlatDesktop.isSupported(Action.APP_ABOUT))
+    {
+      FlatDesktop.setAboutHandler(() -> {
+        desktop.aboutMenuItem_actionPerformed(null);
+      });
+      setAbout = true;
+    }
+    if (FlatDesktop.isSupported(Action.APP_PREFERENCES))
+    {
+      FlatDesktop.setPreferencesHandler(() -> {
+        desktop.preferences_actionPerformed(null);
+      });
+      setPreferences = true;
+    }
+    if (FlatDesktop.isSupported(Action.APP_QUIT_HANDLER))
+    {
+      FlatDesktop.setQuitHandler(response -> {
+        boolean confirmQuit = jalview.bin.Cache.getDefault(
+                jalview.gui.Desktop.CONFIRM_KEYBOARD_QUIT, true);
+        boolean canQuit = !confirmQuit;
+        int n;
+        if (confirmQuit)
+        {
+          // ensure Jalview window is brought to front for Quit confirmation
+          // window to be
+          // visible
 
-        n = JOptionPane.showConfirmDialog(dialogParent,
-                MessageManager.getString("label.quit_jalview"),
-                MessageManager.getString("action.quit"),
-                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
-                null);
+          // this method of raising the Jalview window is broken in java
+          // jalviewDesktop.setVisible(true);
+          // jalviewDesktop.toFront();
 
-        dialogParent.setAlwaysOnTop(false);
-        dialogParent.dispose();
-      }
-      else
-      {
-        n = JOptionPane.OK_OPTION;
-      }
-      canQuit = (n == JOptionPane.OK_OPTION);
-      if (canQuit)
-      {
-        response.performQuit();
-      }
-      else
-      {
-        response.cancelQuit();
-      }
-    });
+          // a better hack which works instead
+          JFrame dialogParent = new JFrame();
+          dialogParent.setAlwaysOnTop(true);
 
-    setAPQHandlers = true;
+          n = JOptionPane.showConfirmDialog(dialogParent,
+                  MessageManager.getString("label.quit_jalview"),
+                  MessageManager.getString("action.quit"),
+                  JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
+                  null);
 
-    return setAPQHandlers;
+          dialogParent.setAlwaysOnTop(false);
+          dialogParent.dispose();
+        }
+        else
+        {
+          n = JOptionPane.OK_OPTION;
+        }
+        canQuit = (n == JOptionPane.OK_OPTION);
+        if (canQuit)
+        {
+          response.performQuit();
+        }
+        else
+        {
+          response.cancelQuit();
+        }
+      });
+      setQuit = true;
+    }
+    // if we got to here, no exceptions occurred when we set the handlers.
+    return setAbout || setPreferences || setQuit;
   }
 
 }
index f3d9a65..996cb1f 100755 (executable)
@@ -405,12 +405,12 @@ public class GDesktop extends JFrame
     FileMenu.add(loadState);
     FileMenu.addSeparator();
     FileMenu.add(quit);
-    if (!APQHandlers.setAPQHandlers)
+    if (!APQHandlers.setAbout)
     {
       HelpMenu.add(aboutMenuItem);
     }
     HelpMenu.add(documentationMenuItem);
-    if (!APQHandlers.setAPQHandlers)
+    if (!APQHandlers.setPreferences)
     {
       toolsMenu.add(preferences);
     }