package jalview.jbgui; import java.io.File; import javax.swing.JFrame; import javax.swing.JOptionPane; import com.formdev.flatlaf.extras.FlatDesktop; import jalview.io.BackupFiles; import jalview.util.MessageManager; public class QuitHandler { public static void setQuitHandler() { FlatDesktop.setQuitHandler(response -> { // confirm quit if needed and wanted boolean confirmQuit = jalview.bin.Cache .getDefault(jalview.gui.Desktop.CONFIRM_KEYBOARD_QUIT, true); /* if undostack is empty confirmQuit = false */ int n = confirmQuit ? JOptionPane.CANCEL_OPTION : JOptionPane.OK_OPTION; // if going to confirm, do it before the save in progress check to give // the save time to finish! if (confirmQuit) { n = frameOnTop(MessageManager.getString("label.quit_jalview"), MessageManager.getString("action.quit"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); } if (BackupFiles.hasSavesInProgress()) { // sleep 1 // ... StringBuilder messageSB = new StringBuilder( MessageManager.getString("label.save_in_progress")); for (File file : BackupFiles.savesInProgressFiles()) { messageSB.append("\n"); messageSB.append(file.getName()); } n = frameOnTop(messageSB.toString(), MessageManager.getString("action.force_quit"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); } boolean canQuit = (n == JOptionPane.OK_OPTION); if (canQuit) { response.performQuit(); } else { response.cancelQuit(); } }); } public static int frameOnTop(String label, String actionString, int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE) { // ensure Jalview window is brought to front for Quit confirmation // window to be visible // this method of raising the Jalview window is broken in java // jalviewDesktop.setVisible(true); // jalviewDesktop.toFront(); // a better hack which works instead JFrame dialogParent = new JFrame(); dialogParent.setAlwaysOnTop(true); int n = JOptionPane.showConfirmDialog(dialogParent, label, actionString, JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE); dialogParent.setAlwaysOnTop(false); dialogParent.dispose(); return n; } }