JAL-4004 Fix missing whatsNew.html when not building with RELEASE channel set
[jalview.git] / src / jalview / gui / APQHandlers.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.bin.Cache;
24 import jalview.util.MessageManager;
25 import jalview.util.Platform;
26
27 import java.awt.Desktop;
28 import java.awt.desktop.AboutEvent;
29 import java.awt.desktop.AboutHandler;
30 import java.awt.desktop.PreferencesEvent;
31 import java.awt.desktop.PreferencesHandler;
32 import java.awt.desktop.QuitEvent;
33 import java.awt.desktop.QuitHandler;
34 import java.awt.desktop.QuitResponse;
35 import java.awt.desktop.QuitStrategy;
36
37 import javax.swing.JOptionPane;
38
39 public class APQHandlers
40 {
41   private static boolean setAPQHandlers = false;
42
43   public APQHandlers()
44   {
45   }
46
47   protected static boolean setAPQHandlers(
48           jalview.gui.Desktop jalviewDesktop)
49   {
50     // flagging this test to avoid unnecessary reflection
51     if (!setAPQHandlers)
52     {
53       // see if the Quit, About and Preferences handlers are available
54       Class desktopClass = Desktop.class;
55       Desktop hdesktop = Desktop.getDesktop();
56
57       try
58       {
59         Float specversion = Float.parseFloat(
60                 System.getProperty("java.specification.version"));
61
62         if (specversion >= 9)
63         {
64           if (Platform.isAMacAndNotJS())
65           {
66             if (desktopClass.getDeclaredMethod("setAboutHandler",
67                     new Class[]
68                     { AboutHandler.class }) != null)
69             {
70
71               hdesktop.setAboutHandler(new AboutHandler()
72               {
73                 @Override
74                 public void handleAbout(AboutEvent e)
75                 {
76                   jalviewDesktop.aboutMenuItem_actionPerformed(null);
77                 }
78               });
79
80             }
81
82             if (desktopClass.getDeclaredMethod("setPreferencesHandler",
83                     new Class[]
84                     { PreferencesHandler.class }) != null)
85             {
86
87               hdesktop.setPreferencesHandler(new PreferencesHandler()
88               {
89                 @Override
90                 public void handlePreferences(PreferencesEvent e)
91                 {
92                   jalviewDesktop.preferences_actionPerformed(null);
93                 }
94               });
95
96             }
97
98             if (desktopClass.getDeclaredMethod("setQuitHandler",
99                     new Class[]
100                     { QuitHandler.class }) != null)
101             {
102
103               hdesktop.setQuitHandler(new QuitHandler()
104               {
105                 @Override
106                 public void handleQuitRequestWith(QuitEvent e,
107                         QuitResponse r)
108                 {
109                   boolean confirmQuit = Cache.getDefault(
110                           jalview.gui.Desktop.CONFIRM_KEYBOARD_QUIT, true);
111                   int n;
112                   if (confirmQuit)
113                   {
114                     n = JOptionPane.showConfirmDialog(null,
115                             MessageManager.getString("label.quit_jalview"),
116                             MessageManager.getString("action.quit"),
117                             JOptionPane.OK_CANCEL_OPTION,
118                             JOptionPane.PLAIN_MESSAGE, null);
119                   }
120                   else
121                   {
122                     n = JOptionPane.OK_OPTION;
123                   }
124                   if (n == JOptionPane.OK_OPTION)
125                   {
126                     System.out.println("Shortcut Quit confirmed by user");
127                     jalviewDesktop.quit();
128                     r.performQuit(); // probably won't reach this line, but just
129                                      // in
130                                      // case
131                   }
132                   else
133                   {
134                     r.cancelQuit();
135                     System.out.println("Shortcut Quit cancelled by user");
136                   }
137                 }
138               });
139               hdesktop.setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);
140
141             }
142           }
143           setAPQHandlers = true;
144         }
145         else
146         {
147           System.out.println(
148                   "Not going to try setting APQ Handlers as java.spec.version is "
149                           + specversion);
150         }
151
152       } catch (Exception e)
153       {
154         System.out.println(
155                 "Exception when looking for About, Preferences, Quit Handlers");
156         // e.printStackTrace();
157       } catch (Throwable t)
158       {
159         System.out.println(
160                 "Throwable when looking for About, Preferences, Quit Handlers");
161         // t.printStackTrace();
162       }
163
164     }
165
166     return setAPQHandlers;
167   }
168
169 }