JAL-3130 Helper classes to take Class Exceptions when run in java 1.8 JRE out of...
[jalview.git] / src / jalview / gui / APQHandlers.java
1 package jalview.gui;
2
3 import jalview.util.MessageManager;
4 import jalview.util.Platform;
5
6 import java.awt.Desktop;
7 import java.awt.desktop.AboutEvent;
8 import java.awt.desktop.AboutHandler;
9 import java.awt.desktop.PreferencesEvent;
10 import java.awt.desktop.PreferencesHandler;
11 import java.awt.desktop.QuitEvent;
12 import java.awt.desktop.QuitHandler;
13 import java.awt.desktop.QuitResponse;
14 import java.awt.desktop.QuitStrategy;
15
16 import javax.swing.JOptionPane;
17
18 public class APQHandlers
19 {
20   private static boolean setAPQHandlers = false;
21
22   public APQHandlers() {
23   }
24
25   protected static boolean setAPQHandlers(jalview.gui.Desktop jalviewDesktop)
26   {
27     // flagging this test to avoid unnecessary reflection
28     if (!setAPQHandlers)
29     {
30       // see if the Quit, About and Preferences handlers are available
31       Class desktopClass = Desktop.class;
32       Desktop hdesktop = Desktop.getDesktop();
33
34       try
35       {
36         Float specversion = Float.parseFloat(
37                 System.getProperty("java.specification.version"));
38
39         if (specversion >= 9)
40         {
41           if (Platform.isAMac())
42           {
43             if (desktopClass.getDeclaredMethod("setAboutHandler",
44                     new Class[]
45                     { AboutHandler.class }) != null)
46             {
47
48               hdesktop.setAboutHandler(new AboutHandler()
49               {
50                 @Override
51                 public void handleAbout(AboutEvent e)
52                 {
53                   jalviewDesktop.aboutMenuItem_actionPerformed(null);
54                 }
55               });
56
57             }
58
59             if (desktopClass.getDeclaredMethod("setPreferencesHandler",
60                     new Class[]
61                     { PreferencesHandler.class }) != null)
62             {
63
64               hdesktop.setPreferencesHandler(
65                       new PreferencesHandler()
66               {
67                         @Override
68                         public void handlePreferences(
69                                 PreferencesEvent e)
70                         {
71                           jalviewDesktop.preferences_actionPerformed(null);
72                         }
73                       });
74
75             }
76
77             if (desktopClass.getDeclaredMethod("setQuitHandler",
78                     new Class[]
79                     { QuitHandler.class }) != null)
80             {
81
82               hdesktop.setQuitHandler(new QuitHandler()
83               {
84                 @Override
85                 public void handleQuitRequestWith(
86                         QuitEvent e, QuitResponse r)
87                 {
88                   boolean confirmQuit = jalview.bin.Cache
89                           .getDefault(jalviewDesktop.CONFIRM_KEYBOARD_QUIT,
90                                   true);
91                   int n;
92                   if (confirmQuit)
93                   {
94                     n = JOptionPane.showConfirmDialog(null,
95                             MessageManager.getString("label.quit_jalview"),
96                             MessageManager.getString("action.quit"),
97                             JOptionPane.OK_CANCEL_OPTION,
98                             JOptionPane.PLAIN_MESSAGE, null);
99                   }
100                   else
101                   {
102                     n = JOptionPane.OK_OPTION;
103                   }
104                   if (n == JOptionPane.OK_OPTION)
105                   {
106                     System.out.println("Shortcut Quit confirmed by user");
107                     jalviewDesktop.quit();
108                     r.performQuit(); // probably won't reach this line, but just
109                                      // in
110                                      // case
111                   }
112                   else
113                   {
114                     r.cancelQuit();
115                     System.out.println("Shortcut Quit cancelled by user");
116                   }
117                 }
118               });
119               hdesktop.setQuitStrategy(
120                       QuitStrategy.CLOSE_ALL_WINDOWS);
121
122             }
123           }
124           setAPQHandlers = true;
125         }
126         else
127         {
128           System.out.println(
129                   "Not going to try setting APQ Handlers as java.spec.version is "
130                           + specversion);
131         }
132
133       } catch (Exception e)
134       {
135         System.out.println(
136                 "Exception when looking for About, Preferences, Quit Handlers");
137         e.printStackTrace();
138       } catch (Throwable t)
139       {
140         System.out.println(
141                 "Throwable when looking for About, Preferences, Quit Handlers");
142         t.printStackTrace();
143       }
144
145     }
146     
147     return setAPQHandlers;
148   }
149
150 }