JAL-3130 Added flagged runtime 'Reflection' check on Java9 Quit/About/Preferences...
authorBen Soares <bsoares@dundee.ac.uk>
Mon, 3 Dec 2018 12:20:57 +0000 (12:20 +0000)
committerBen Soares <bsoares@dundee.ac.uk>
Mon, 3 Dec 2018 12:20:57 +0000 (12:20 +0000)
src/jalview/gui/Desktop.java

index c2eb598..40e6ccf 100644 (file)
@@ -152,6 +152,8 @@ public class Desktop extends jalview.jbgui.GDesktop
 
   private static final String EXPERIMENTAL_FEATURES = "EXPERIMENTAL_FEATURES";
 
+  private static final String CONFIRM_KEYBOARD_QUIT = "CONFIRM_KEYBOARD_QUIT";
+
   public static HashMap<String, FileWriter> savingFiles = new HashMap<>();
 
   private JalviewChangeSupport changeSupport = new JalviewChangeSupport();
@@ -163,6 +165,8 @@ public class Desktop extends jalview.jbgui.GDesktop
 
   private File projectFile;
 
+  private static boolean setAPQHandlers = false;
+
   /**
    * @param listener
    * @see jalview.gui.JalviewChangeSupport#addJalviewPropertyChangeListener(java.beans.PropertyChangeListener)
@@ -361,53 +365,105 @@ public class Desktop extends jalview.jbgui.GDesktop
       // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     }
     else
+    {
+     this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
+    }
     */
-    // {
-    // this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
-    java.awt.Desktop hdesktop = java.awt.Desktop.getDesktop();
 
-    hdesktop.setAboutHandler(new AboutHandler()
+    // flagging this test to avoid unnecessary reflection
+    if (!setAPQHandlers)
     {
-      @Override
-      public void handleAbout(AboutEvent e)
-      {
-        aboutMenuItem_actionPerformed(null);
-      }
-    });
-    hdesktop.setPreferencesHandler(new PreferencesHandler()
-    {
-      @Override
-      public void handlePreferences(PreferencesEvent e)
+      // see if the Quit, About and Preferences handlers are available
+      Class desktopClass = java.awt.Desktop.class;
+      java.awt.Desktop hdesktop = java.awt.Desktop.getDesktop();
+
+      try
       {
-        preferences_actionPerformed(null);
-      }
-    });
-    hdesktop.setQuitHandler(new QuitHandler()
-    {
-      @Override
-      public void handleQuitRequestWith(QuitEvent e, QuitResponse r)
-      {
-        int n = JOptionPane.showConfirmDialog(null,
-                MessageManager.getString("label.quit_jalview"),
-                MessageManager.getString("action.quit"),
-                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
-                null);
-        if (n == JOptionPane.OK_OPTION)
-        {
-          System.out.println("Shortcut Quit confirmed by user");
-          quit();
-          r.performQuit(); // probably won't reach this line, but just in case
+
+        if (desktopClass.getDeclaredMethod("setAboutHandler",
+                new Class[]
+                { AboutHandler.class }) != null)
+        {
+
+          hdesktop.setAboutHandler(new AboutHandler()
+          {
+            @Override
+            public void handleAbout(AboutEvent e)
+            {
+              aboutMenuItem_actionPerformed(null);
+            }
+          });
+
         }
-        else
+
+        if (desktopClass.getDeclaredMethod("setPreferencesHandler",
+                new Class[]
+                { PreferencesHandler.class }) != null)
+        {
+
+          hdesktop.setPreferencesHandler(new PreferencesHandler()
+          {
+            @Override
+            public void handlePreferences(PreferencesEvent e)
+            {
+              preferences_actionPerformed(null);
+            }
+          });
+
+        }
+
+        if (desktopClass.getDeclaredMethod("setQuitHandler",
+                new Class[]
+                { QuitHandler.class }) != null)
         {
-          r.cancelQuit();
-          System.out.println("Shortcut Quit cancelled by user");
+
+          hdesktop.setQuitHandler(new QuitHandler()
+          {
+            @Override
+            public void handleQuitRequestWith(QuitEvent e, QuitResponse r)
+            {
+              boolean confirmQuit = jalview.bin.Cache
+                      .getDefault(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");
+                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);
+
         }
+
+      } catch (NoSuchMethodException e)
+      {
+        System.out.println(
+                "NoSuchMethodException when looking for About, Preferences, Quit Handlers");
+        e.printStackTrace();
       }
-    });
-    hdesktop.setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);
 
-    // }
+      setAPQHandlers = true;
+    }
 
     addWindowListener(new WindowAdapter()
     {