JAL-1988 Added a saves in progress check for quit
[jalview.git] / src / jalview / jbgui / APQHandlers.java
index 00ce170..12ce1ba 100644 (file)
  */
 package jalview.jbgui;
 
-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
 {
-  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 setAbout = false;
 
-        // this method of raising the Jalview window is broken in java
-        // jalviewDesktop.setVisible(true);
-        // jalviewDesktop.toFront();
+  public static boolean setPreferences = false;
 
-        // a better hack which works instead
-        JFrame dialogParent = new JFrame();
-        dialogParent.setAlwaysOnTop(true);
+  public static boolean setQuit = false;
 
-        n = JOptionPane.showConfirmDialog(dialogParent,
-                MessageManager.getString("label.quit_jalview"),
-                MessageManager.getString("action.quit"),
-                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
-                null);
-
-        dialogParent.setAlwaysOnTop(false);
-        dialogParent.dispose();
-      }
-      else
-      {
-        n = JOptionPane.OK_OPTION;
-      }
-      canQuit = (n == JOptionPane.OK_OPTION);
-      if (canQuit)
-      {
-        response.performQuit();
-      }
-      else
-      {
-        response.cancelQuit();
-      }
-    });
+  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))
+    {
+      QuitHandler.setQuitHandler();
+      setQuit = true;
+    }
     // if we got to here, no exceptions occurred when we set the handlers.
-    return true;
+    return setAbout || setPreferences || setQuit;
   }
 
 }