From 8f750561f9693fbe9bbc7deb48b3d94815d49073 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Mon, 3 Dec 2018 12:20:57 +0000 Subject: [PATCH] JAL-3130 Added flagged runtime 'Reflection' check on Java9 Quit/About/Preferences Handlers --- src/jalview/gui/Desktop.java | 132 ++++++++++++++++++++++++++++++------------ 1 file changed, 94 insertions(+), 38 deletions(-) diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index c2eb598..40e6ccf 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -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 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() { -- 1.7.10.2