2 |
|
|
3 |
import java.awt.Component; |
import java.awt.Component; |
4 |
import java.awt.Dialog; |
import java.awt.Dialog; |
5 |
|
import java.awt.event.ActionEvent; |
6 |
|
import java.awt.event.ActionListener; |
7 |
|
import java.awt.event.KeyEvent; |
8 |
import java.awt.event.WindowAdapter; |
import java.awt.event.WindowAdapter; |
9 |
import java.awt.event.WindowEvent; |
import java.awt.event.WindowEvent; |
10 |
|
|
11 |
|
import javax.swing.JComponent; |
12 |
import javax.swing.JDialog; |
import javax.swing.JDialog; |
13 |
import javax.swing.JOptionPane; |
import javax.swing.JOptionPane; |
14 |
|
import javax.swing.JRootPane; |
15 |
|
import javax.swing.KeyStroke; |
16 |
|
|
17 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
18 |
|
|
21 |
* cancellable {@link JDialog}s work. The {@link JDialog} is designed to work on |
* cancellable {@link JDialog}s work. The {@link JDialog} is designed to work on |
22 |
* the real object and restore it's state when the user cancels the |
* the real object and restore it's state when the user cancels the |
23 |
* {@link JDialog}. |
* {@link JDialog}. |
24 |
|
* |
25 |
|
* Pressing ESC or clicking the "Close X" results in asking the user whether to Save/Cancel/Abort . |
26 |
*/ |
*/ |
27 |
public abstract class CancellableDialogAdapter extends JDialog implements |
public abstract class CancellableDialogAdapter extends JDialog implements |
28 |
CancellableDialog { |
CancellableDialog { |
121 |
dispose(); |
dispose(); |
122 |
return true; |
return true; |
123 |
} |
} |
124 |
|
|
125 |
|
|
126 |
|
/** |
127 |
|
* Since the registerKeyboardAction() method is part of the JComponent class |
128 |
|
* definition, you must define the Escape keystroke and register the |
129 |
|
* keyboard action with a JComponent, not with a JDialog. The JRootPane for |
130 |
|
* the JDialog serves as an excellent choice to associate the registration, |
131 |
|
* as this will always be visible. If you override the protected |
132 |
|
* createRootPane() method of JDialog, you can return your custom JRootPane |
133 |
|
* with the keystroke enabled: |
134 |
|
*/ |
135 |
|
@Override |
136 |
|
protected JRootPane createRootPane() { |
137 |
|
final KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
138 |
|
final JRootPane rootPane = new JRootPane(); |
139 |
|
rootPane.registerKeyboardAction(new ActionListener() { |
140 |
|
|
141 |
|
public void actionPerformed(final ActionEvent e) { |
142 |
|
close(); |
143 |
|
} |
144 |
|
|
145 |
|
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW); |
146 |
|
|
147 |
|
return rootPane; |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
} |
} |