Merge branch 'task/JAL-3247_JAL-3246_JAL-3254_JAL-3236_merge' into task/JAL-3141_JAL...
[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(
90                                   jalview.gui.Desktop.CONFIRM_KEYBOARD_QUIT,
91                                   true);
92                   int n;
93                   if (confirmQuit)
94                   {
95                     n = JOptionPane.showConfirmDialog(null,
96                             MessageManager.getString("label.quit_jalview"),
97                             MessageManager.getString("action.quit"),
98                             JOptionPane.OK_CANCEL_OPTION,
99                             JOptionPane.PLAIN_MESSAGE, null);
100                   }
101                   else
102                   {
103                     n = JOptionPane.OK_OPTION;
104                   }
105                   if (n == JOptionPane.OK_OPTION)
106                   {
107                     System.out.println("Shortcut Quit confirmed by user");
108                     jalviewDesktop.quit();
109                     r.performQuit(); // probably won't reach this line, but just
110                                      // in
111                                      // case
112                   }
113                   else
114                   {
115                     r.cancelQuit();
116                     System.out.println("Shortcut Quit cancelled by user");
117                   }
118                 }
119               });
120               hdesktop.setQuitStrategy(
121                       QuitStrategy.CLOSE_ALL_WINDOWS);
122
123             }
124           }
125           setAPQHandlers = true;
126         }
127         else
128         {
129           System.out.println(
130                   "Not going to try setting APQ Handlers as java.spec.version is "
131                           + specversion);
132         }
133
134       } catch (Exception e)
135       {
136         System.out.println(
137                 "Exception when looking for About, Preferences, Quit Handlers");
138         // e.printStackTrace();
139       } catch (Throwable t)
140       {
141         System.out.println(
142                 "Throwable when looking for About, Preferences, Quit Handlers");
143         // t.printStackTrace();
144       }
145
146     }
147     
148     return setAPQHandlers;
149   }
150
151 }