package com.ts3client.ui; import javax.swing.AbstractAction; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JRootPane; import javax.swing.KeyStroke; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; /** Behaviour shared by the client's dialogs. */ final class Dialogs { private Dialogs() { } /** * Makes Escape close a dialog, the way Swing's own option panes do. * *

The binding lives on the root pane's window-wide input map, so it fires * wherever the focus sits — but only where nothing nearer to the focused * component claims Escape first, which is what leaves a table's or combo * box's own "cancel the edit" behaviour intact. */ static void closeOnEscape(JDialog dialog) { closeOnEscape(dialog, dialog::dispose); } /** * Binds Escape to the dialog's own way of closing, for dialogs that have to * tidy up (or discard edits) on the way out. */ static void closeOnEscape(JDialog dialog, Runnable close) { JRootPane root = dialog.getRootPane(); root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "ts3-close"); root.getActionMap().put("ts3-close", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { close.run(); } }); } }