-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDefaultButtonListener.java
More file actions
120 lines (100 loc) · 3.47 KB
/
DefaultButtonListener.java
File metadata and controls
120 lines (100 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.awt.*;
import java.beans.*;
import java.util.*;
import javax.swing.*;
/*
* Keep track of the default button and the temporary default button
* for all root panes in an application.
*
* The listener is installed for the entire application by using the static
* install() method. The listener can be removed for the application
* by using the static unInstall() method.
*
* This implies you could enable the listener for specific Windows only by
* using the above methods as a Window is activated and deactivated.
*/
public class DefaultButtonListener implements PropertyChangeListener
{
private static String PERMANENT_FOCUS_OWNER = "permanentFocusOwner";
// Keep track of the default button for each root pane in the application
private HashMap<JRootPane, JButton> rootPanes = new HashMap<JRootPane, JButton>();
// Store the oldValue until the second PropertyChangeEvent is received
private Component oldValue;
/*
* Multiple property change events will be generated
*
* a) the first will contain the last component to have focus
* b) the second will contain the component that currently has focus
*
* We need information from both events in order to proceed.
*/
public void propertyChange(PropertyChangeEvent e)
{
// Wait until we have both pieces of information
if (e.getOldValue() != null)
oldValue = (Component)e.getOldValue();
if (e.getNewValue() == null) return;
// When focus remains on the same root pane and
// when leaving a button and not going to a different button
// we need to restore the original default button
Component newValue = (Component)e.getNewValue();
JRootPane oldRootPane = SwingUtilities.getRootPane(oldValue);
JRootPane newRootPane = SwingUtilities.getRootPane(newValue);
if (newRootPane == oldRootPane)
{
if ( oldValue instanceof JButton
&& ! (newValue instanceof JButton))
{
restoreDefaultButton(newRootPane);
}
}
// Make this button the new default button for the root pane
if (newValue instanceof JButton)
{
setDefaultButton(newRootPane, (JButton)newValue);
}
}
/*
* Restore the root pane to its original default button
*/
private void restoreDefaultButton(JRootPane rootPane)
{
if (rootPanes.containsKey(rootPane))
{
JButton savedDefaultButton = rootPanes.get(rootPane);
rootPane.setDefaultButton(savedDefaultButton);
}
}
/*
* Temporarily change the default button of a root pane
*/
private void setDefaultButton(JRootPane rootPane, JButton button)
{
// Save the original default button for the root pane
if (! rootPanes.containsKey(rootPane))
{
rootPanes.put(rootPane, rootPane.getDefaultButton());
}
// Set the current button to temporarily be the default button
rootPane.setDefaultButton( button );
}
/*
* Installing the listener will affect the entire application
*/
static DefaultButtonListener install()
{
DefaultButtonListener listener = new DefaultButtonListener();
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addPropertyChangeListener(PERMANENT_FOCUS_OWNER, listener);
return listener;
}
/*
* Uninstalling the listener will affect the entire application
*/
static void unInstall(DefaultButtonListener listener)
{
listener.rootPanes.clear();
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.removePropertyChangeListener(PERMANENT_FOCUS_OWNER, listener);
}
}