JAL-1988 JAL-3416 Using the flatlaf-extras About, Preferences and Quit handler system...
[jalview.git] / src / jalview / gui / APQHandlers.java
index fb9b707..3d4c91a 100644 (file)
  */
 package jalview.gui;
 
-import jalview.util.MessageManager;
-import jalview.util.Platform;
+import javax.swing.JOptionPane;
 
-import java.awt.Desktop;
-import java.awt.desktop.AboutEvent;
-import java.awt.desktop.AboutHandler;
-import java.awt.desktop.PreferencesEvent;
-import java.awt.desktop.PreferencesHandler;
-import java.awt.desktop.QuitEvent;
-import java.awt.desktop.QuitHandler;
-import java.awt.desktop.QuitResponse;
-import java.awt.desktop.QuitStrategy;
+import com.formdev.flatlaf.extras.FlatDesktop;
 
-import javax.swing.JOptionPane;
+import jalview.util.MessageManager;
 
 public class APQHandlers
 {
   private static boolean setAPQHandlers = false;
 
-  public APQHandlers() {
-  }
-
-  protected static boolean setAPQHandlers(jalview.gui.Desktop jalviewDesktop)
+  protected static boolean setAPQHandlers(
+          jalview.gui.Desktop jalviewDesktop)
   {
-    // flagging this test to avoid unnecessary reflection
-    if (!setAPQHandlers)
-    {
-      // see if the Quit, About and Preferences handlers are available
-      Class desktopClass = Desktop.class;
-      Desktop hdesktop = Desktop.getDesktop();
-
-      try
+    FlatDesktop.setAboutHandler(() -> {
+      jalviewDesktop.aboutMenuItem_actionPerformed(null);
+    });
+    FlatDesktop.setPreferencesHandler(() -> {
+      jalviewDesktop.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)
       {
-        Float specversion = Float.parseFloat(
-                System.getProperty("java.specification.version"));
-
-        if (specversion >= 9)
-        {
-          if (Platform.isAMacAndNotJS())
-          {
-            if (desktopClass.getDeclaredMethod("setAboutHandler",
-                    new Class[]
-                    { AboutHandler.class }) != null)
-            {
-
-              hdesktop.setAboutHandler(new AboutHandler()
-              {
-                @Override
-                public void handleAbout(AboutEvent e)
-                {
-                  jalviewDesktop.aboutMenuItem_actionPerformed(null);
-                }
-              });
-
-            }
-
-            if (desktopClass.getDeclaredMethod("setPreferencesHandler",
-                    new Class[]
-                    { PreferencesHandler.class }) != null)
-            {
-
-              hdesktop.setPreferencesHandler(
-                      new PreferencesHandler()
-              {
-                        @Override
-                        public void handlePreferences(
-                                PreferencesEvent e)
-                        {
-                          jalviewDesktop.preferences_actionPerformed(null);
-                        }
-                      });
-
-            }
-
-            if (desktopClass.getDeclaredMethod("setQuitHandler",
-                    new Class[]
-                    { QuitHandler.class }) != null)
-            {
-
-              hdesktop.setQuitHandler(new QuitHandler()
-              {
-                @Override
-                public void handleQuitRequestWith(
-                        QuitEvent e, QuitResponse r)
-                {
-                  boolean confirmQuit = jalview.bin.Cache
-                          .getDefault(
-                                  jalview.gui.Desktop.CONFIRM_KEYBOARD_QUIT,
-                                  true);
-                  int n;
-                  if (confirmQuit)
-                  {
-                    n = JOptionPane.showConfirmDialog(null,
-                            MessageManager.getString("label.quit_jalview"),
-                            MessageManager.getString("action.quit"),
-                            JOptionPane.OK_CANCEL_OPTION,
-                            JOptionPane.PLAIN_MESSAGE, null);
-                  }
-                  else
-                  {
-                    n = JOptionPane.OK_OPTION;
-                  }
-                  if (n == JOptionPane.OK_OPTION)
-                  {
-                    System.out.println("Shortcut Quit confirmed by user");
-                    jalviewDesktop.quit();
-                    r.performQuit(); // probably won't reach this line, but just
-                                     // in
-                                     // case
-                  }
-                  else
-                  {
-                    r.cancelQuit();
-                    System.out.println("Shortcut Quit cancelled by user");
-                  }
-                }
-              });
-              hdesktop.setQuitStrategy(
-                      QuitStrategy.CLOSE_ALL_WINDOWS);
-
-            }
-          }
-          setAPQHandlers = true;
-        }
-        else
-        {
-          System.out.println(
-                  "Not going to try setting APQ Handlers as java.spec.version is "
-                          + specversion);
-        }
-
-      } catch (Exception e)
+        n = JOptionPane.showConfirmDialog(null,
+                MessageManager.getString("label.quit_jalview"),
+                MessageManager.getString("action.quit"),
+                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
+                null);
+      }
+      else
       {
-        System.out.println(
-                "Exception when looking for About, Preferences, Quit Handlers");
-        // e.printStackTrace();
-      } catch (Throwable t)
+        n = JOptionPane.OK_OPTION;
+      }
+      canQuit = (n == JOptionPane.OK_OPTION);
+      if (canQuit)
       {
-        System.out.println(
-                "Throwable when looking for About, Preferences, Quit Handlers");
-        // t.printStackTrace();
+        response.performQuit();
       }
+      else
+      {
+        response.cancelQuit();
+      }
+    });
+
+    setAPQHandlers = true;
 
-    }
-    
     return setAPQHandlers;
   }