JAL-3949 Complete new abstracted logging framework in jalview.log. Updated log calls...
[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   protected static boolean setAPQHandlers(jalview.gui.Desktop jalviewDesktop)
47   {
48     // flagging this test to avoid unnecessary reflection
49     if (!setAPQHandlers)
50     {
51       // see if the Quit, About and Preferences handlers are available
52       Class desktopClass = Desktop.class;
53       Desktop hdesktop = Desktop.getDesktop();
54
55       try
56       {
57         Float specversion = Float.parseFloat(
58                 System.getProperty("java.specification.version"));
59
60         if (specversion >= 9)
61         {
62           if (Platform.isAMacAndNotJS())
63           {
64             if (desktopClass.getDeclaredMethod("setAboutHandler",
65                     new Class[]
66                     { AboutHandler.class }) != null)
67             {
68
69               hdesktop.setAboutHandler(new AboutHandler()
70               {
71                 @Override
72                 public void handleAbout(AboutEvent e)
73                 {
74                   jalviewDesktop.aboutMenuItem_actionPerformed(null);
75                 }
76               });
77
78             }
79
80             if (desktopClass.getDeclaredMethod("setPreferencesHandler",
81                     new Class[]
82                     { PreferencesHandler.class }) != null)
83             {
84
85               hdesktop.setPreferencesHandler(
86                       new PreferencesHandler()
87               {
88                         @Override
89                         public void handlePreferences(
90                                 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(
107                         QuitEvent e, QuitResponse r)
108                 {
109                   boolean confirmQuit = Cache
110                           .getDefault(
111                                   jalview.gui.Desktop.CONFIRM_KEYBOARD_QUIT,
112                                   true);
113                   int n;
114                   if (confirmQuit)
115                   {
116                     n = JOptionPane.showConfirmDialog(null,
117                             MessageManager.getString("label.quit_jalview"),
118                             MessageManager.getString("action.quit"),
119                             JOptionPane.OK_CANCEL_OPTION,
120                             JOptionPane.PLAIN_MESSAGE, null);
121                   }
122                   else
123                   {
124                     n = JOptionPane.OK_OPTION;
125                   }
126                   if (n == JOptionPane.OK_OPTION)
127                   {
128                     System.out.println("Shortcut Quit confirmed by user");
129                     jalviewDesktop.quit();
130                     r.performQuit(); // probably won't reach this line, but just
131                                      // in
132                                      // case
133                   }
134                   else
135                   {
136                     r.cancelQuit();
137                     System.out.println("Shortcut Quit cancelled by user");
138                   }
139                 }
140               });
141               hdesktop.setQuitStrategy(
142                       QuitStrategy.CLOSE_ALL_WINDOWS);
143
144             }
145           }
146           setAPQHandlers = true;
147         }
148         else
149         {
150           System.out.println(
151                   "Not going to try setting APQ Handlers as java.spec.version is "
152                           + specversion);
153         }
154
155       } catch (Exception e)
156       {
157         System.out.println(
158                 "Exception when looking for About, Preferences, Quit Handlers");
159         // e.printStackTrace();
160       } catch (Throwable t)
161       {
162         System.out.println(
163                 "Throwable when looking for About, Preferences, Quit Handlers");
164         // t.printStackTrace();
165       }
166
167     }
168     
169     return setAPQHandlers;
170   }
171
172 }