Skip to content

Commit 23685fa

Browse files
authored
Add files via upload
1 parent 3af55e4 commit 23685fa

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

source/DefaultButtonListener.java

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

0 commit comments

Comments
 (0)