5 import javax.swing.JFrame;
6 import javax.swing.JOptionPane;
8 import com.formdev.flatlaf.extras.FlatDesktop;
10 import jalview.io.BackupFiles;
11 import jalview.util.MessageManager;
13 public class QuitHandler
15 public static void setQuitHandler()
17 FlatDesktop.setQuitHandler(response -> {
18 // confirm quit if needed and wanted
19 boolean confirmQuit = jalview.bin.Cache
20 .getDefault(jalview.gui.Desktop.CONFIRM_KEYBOARD_QUIT, true);
25 int n = confirmQuit ? JOptionPane.CANCEL_OPTION
26 : JOptionPane.OK_OPTION;
28 // if going to confirm, do it before the save in progress check to give
29 // the save time to finish!
32 n = frameOnTop(MessageManager.getString("label.quit_jalview"),
33 MessageManager.getString("action.quit"),
34 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
38 if (BackupFiles.hasSavesInProgress())
43 StringBuilder messageSB = new StringBuilder(
44 MessageManager.getString("label.save_in_progress"));
45 for (File file : BackupFiles.savesInProgressFiles())
47 messageSB.append("\n");
48 messageSB.append(file.getName());
50 n = frameOnTop(messageSB.toString(),
51 MessageManager.getString("action.force_quit"),
52 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
55 boolean canQuit = (n == JOptionPane.OK_OPTION);
58 response.performQuit();
62 response.cancelQuit();
67 public static int frameOnTop(String label, String actionString,
68 int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE)
70 // ensure Jalview window is brought to front for Quit confirmation
71 // window to be visible
73 // this method of raising the Jalview window is broken in java
74 // jalviewDesktop.setVisible(true);
75 // jalviewDesktop.toFront();
77 // a better hack which works instead
79 JFrame dialogParent = new JFrame();
80 dialogParent.setAlwaysOnTop(true);
82 int n = JOptionPane.showConfirmDialog(dialogParent, label, actionString,
83 JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE);
85 dialogParent.setAlwaysOnTop(false);
86 dialogParent.dispose();