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