Skip to content

Commit 07e537f

Browse files
committed
Combine almost identical methods; clearer name
1 parent 9ad021a commit 07e537f

21 files changed

Lines changed: 33 additions & 76 deletions

src/main/java/edu/umich/soar/visualsoar/dialogs/AboutDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public AboutDialog(final Frame owner) {
5353
pack();
5454
getRootPane().setDefaultButton(buttonPanel.greatButton);
5555

56-
DialogUtils.closeOnEscapeKey(this, owner);
56+
DialogUtils.setUpDialogFocus(this, owner, null);
5757

5858
addWindowListener(new WindowAdapter() {
5959
public void windowActivated(WindowEvent we) {

src/main/java/edu/umich/soar/visualsoar/dialogs/CommentDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public CommentDialog(final Frame owner, String oldComment) {
5050
pack();
5151
getRootPane().setDefaultButton(buttonPanel.okButton);
5252

53-
DialogUtils.closeOnEscapeKey(this, owner);
53+
DialogUtils.setUpDialogFocus(this, owner, null);
5454
addWindowListener(new WindowAdapter() {
5555
public void windowOpened(WindowEvent we) {
5656
setLocationRelativeTo(owner);

src/main/java/edu/umich/soar/visualsoar/dialogs/DialogUtils.java

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,23 @@
77
import java.awt.event.WindowEvent;
88

99
public class DialogUtils {
10-
/**
11-
* Register a listener for the {@link KeyEvent#VK_ESCAPE escape key} to close {@code dialog}. The listener is global, so
12-
* even if the dialog has lost focus, it will close when the key is pressed.
13-
* <p>
14-
* The dialog's default close operation will be set to {@link JDialog#DISPOSE_ON_CLOSE}
15-
*
16-
* @param dialog to close
17-
* @param owner of the dialog box
18-
*/
19-
public static void closeOnEscapeKey(final JDialog dialog, final Component owner) {
20-
// Add a global KeyEventDispatcher for ESC key
21-
KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
22-
KeyEventDispatcher escDispatcher =
23-
e -> {
24-
if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_ESCAPE) {
25-
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
26-
return true; // Consume the event
27-
}
28-
return false; // Let other events proceed
29-
};
30-
31-
// Ensure the dialog's default close operation is set to dispose; without this, the key handler
32-
// would work once but then never work again for subsequent dialog opens
33-
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
34-
// Register the dispatcher when the dialog is shown
35-
dialog.addWindowListener(
36-
new WindowAdapter() {
37-
@Override
38-
public void windowOpened(WindowEvent we) {
39-
dialog.setLocationRelativeTo(owner);
40-
// Use SwingUtilities.invokeLater for better Windows compatibility
41-
SwingUtilities.invokeLater(() -> {
42-
dialog.toFront();
43-
dialog.requestFocusInWindow();
44-
});
45-
focusManager.addKeyEventDispatcher(escDispatcher);
46-
}
47-
48-
@Override
49-
public void windowClosed(WindowEvent we) {
50-
// Remove the dispatcher when the dialog is closed
51-
focusManager.removeKeyEventDispatcher(escDispatcher);
52-
}
53-
});
54-
}
5510

5611
/**
57-
* Register a listener for the {@link KeyEvent#VK_ESCAPE escape key} to close {@code dialog} and
58-
* automatically focus a specific component when the dialog opens. This provides better Windows
59-
* compatibility by using proper focus management techniques.
12+
* Bring the dialog to the front and give it focus. If {@code focusComponent} is not null, give it
13+
* the focus within the dialog.
14+
*
15+
* <p>Register a listener for the {@link KeyEvent#VK_ESCAPE escape key} to close {@code dialog}.
16+
* The listener is global, so even if the dialog has lost focus, it will close when the key is
17+
* pressed.
18+
*
19+
* <p>Set the dialog's default close operation to {@link JDialog#DISPOSE_ON_CLOSE}.
6020
*
6121
* @param dialog to close
62-
* @param owner of the dialog box
22+
* @param owner of the dialog box
6323
* @param focusComponent component to receive focus when dialog opens (can be null)
6424
*/
65-
public static void closeOnEscapeKeyWithFocus(final JDialog dialog, final Component owner, final Component focusComponent) {
25+
public static void setUpDialogFocus(
26+
final JDialog dialog, final Component owner, final Component focusComponent) {
6627
// Add a global KeyEventDispatcher for ESC key
6728
KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
6829
KeyEventDispatcher escDispatcher =
@@ -74,7 +35,8 @@ public static void closeOnEscapeKeyWithFocus(final JDialog dialog, final Compone
7435
return false; // Let other events proceed
7536
};
7637

77-
// Ensure the dialog's default close operation is set to dispose
38+
// Ensure the dialog's default close operation is set to dispose; without this, the key handler
39+
// would work once but then never work again for subsequent dialog opens
7840
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
7941
// Register the dispatcher when the dialog is shown
8042
dialog.addWindowListener(

src/main/java/edu/umich/soar/visualsoar/dialogs/DirectorySelectionDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public DirectorySelectionDialog(Frame owner) {
4848
createButtonPanel();
4949
contentPane.add(buttonPanel, BorderLayout.SOUTH);
5050

51-
DialogUtils.closeOnEscapeKey(this, owner);
51+
DialogUtils.setUpDialogFocus(this, owner, null);
5252

5353
pack();
5454
}

src/main/java/edu/umich/soar/visualsoar/dialogs/EditCustomTemplatesDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void windowActivated(WindowEvent we) {
9595
dirButton.addActionListener(this);
9696
closeButton.addActionListener(this);
9797

98-
DialogUtils.closeOnEscapeKey(this, owner);
98+
DialogUtils.setUpDialogFocus(this, owner, null);
9999

100100
}//ctor
101101

src/main/java/edu/umich/soar/visualsoar/dialogs/EditEnumerationDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public EditEnumerationDialog(final Frame owner, Vector<String> theSet) {
5050
pack();
5151
getRootPane().setDefaultButton(buttonPanel.addButton);
5252

53-
DialogUtils.closeOnEscapeKey(this, owner);
53+
DialogUtils.setUpDialogFocus(this, owner, null);
5454

5555
//Special handling for the Enter key
5656
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);

src/main/java/edu/umich/soar/visualsoar/dialogs/EditNumberDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public EditNumberDialog(final Frame owner, String type) {
7474
pack();
7575
getRootPane().setDefaultButton(buttonPanel.okButton);
7676

77-
DialogUtils.closeOnEscapeKey(this, owner);
77+
DialogUtils.setUpDialogFocus(this, owner, null);
7878

7979
addWindowListener(new WindowAdapter() {
8080
public void windowActivated(WindowEvent we) {

src/main/java/edu/umich/soar/visualsoar/dialogs/EnumerationDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public EnumerationDialog(final Frame owner) {
6565
contentPane.add(buttonPanel, c);
6666
pack();
6767

68-
DialogUtils.closeOnEscapeKey(this, owner);
68+
DialogUtils.setUpDialogFocus(this, owner, null);
6969

7070
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
7171

src/main/java/edu/umich/soar/visualsoar/dialogs/FileAlreadyExistsDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public FileAlreadyExistsDialog(final Frame owner, String file) {
4242
contentPane.add(buttonPanel, c);
4343
pack();
4444

45-
DialogUtils.closeOnEscapeKey(this, owner);
45+
DialogUtils.setUpDialogFocus(this, owner, null);
4646

4747
addWindowListener(new WindowAdapter() {
4848
public void windowOpened(WindowEvent we) {

src/main/java/edu/umich/soar/visualsoar/dialogs/IdentifierDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public IdentifierDialog(final Frame owner) {
5353
pack();
5454
getRootPane().setDefaultButton(buttonPanel.okButton);
5555

56-
DialogUtils.closeOnEscapeKeyWithFocus(this, owner, namePanel);
56+
DialogUtils.setUpDialogFocus(this, owner, namePanel);
5757

5858
addWindowListener(new WindowAdapter() {
5959
public void windowOpened(WindowEvent we) {

0 commit comments

Comments
 (0)