JAL-1988 added a saveInProgress stack to BackupFiles
[jalview.git] / src / jalview / jbgui / QuitHandler.java
1 package jalview.jbgui;
2
3 import javax.swing.JFrame;
4 import javax.swing.JOptionPane;
5
6 import com.formdev.flatlaf.extras.FlatDesktop;
7
8 import jalview.util.MessageManager;
9
10 public class QuitHandler
11 {
12   public static void setQuitHandler()
13   {
14     FlatDesktop.setQuitHandler(response -> {
15       boolean confirmQuit = jalview.bin.Cache
16               .getDefault(jalview.gui.Desktop.CONFIRM_KEYBOARD_QUIT, true);
17       boolean canQuit = !confirmQuit;
18       int n;
19       if (confirmQuit)
20       {
21         // ensure Jalview window is brought to front for Quit confirmation
22         // window to be visible
23
24         // this method of raising the Jalview window is broken in java
25         // jalviewDesktop.setVisible(true);
26         // jalviewDesktop.toFront();
27
28         // a better hack which works instead
29         JFrame dialogParent = new JFrame();
30         dialogParent.setAlwaysOnTop(true);
31
32         n = JOptionPane.showConfirmDialog(dialogParent,
33                 MessageManager.getString("label.quit_jalview"),
34                 MessageManager.getString("action.quit"),
35                 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
36                 null);
37
38         dialogParent.setAlwaysOnTop(false);
39         dialogParent.dispose();
40       }
41       else
42       {
43         n = JOptionPane.OK_OPTION;
44       }
45       canQuit = (n == JOptionPane.OK_OPTION);
46       if (canQuit)
47       {
48         response.performQuit();
49       }
50       else
51       {
52         response.cancelQuit();
53       }
54     });
55   }
56
57 }