1 |
package skrueger.swing; |
2 |
|
3 |
import java.awt.Component; |
4 |
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; |
9 |
import java.awt.event.WindowEvent; |
10 |
|
11 |
import javax.swing.JComponent; |
12 |
import javax.swing.JDialog; |
13 |
import javax.swing.JOptionPane; |
14 |
import javax.swing.JRootPane; |
15 |
import javax.swing.KeyStroke; |
16 |
|
17 |
import schmitzm.swing.SwingUtil; |
18 |
|
19 |
/** |
20 |
* An abstract {@link JDialog} that implements a basic structure of how |
21 |
* 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 |
23 |
* {@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 |
28 |
CancellableDialog { |
29 |
|
30 |
protected boolean cancelled = false; |
31 |
|
32 |
@Override |
33 |
public boolean isCancelled() { |
34 |
return cancelled; |
35 |
} |
36 |
|
37 |
public CancellableDialogAdapter(final Component parentWindow, String title) { |
38 |
super(SwingUtil.getParentWindow(parentWindow), title); |
39 |
initDialog(); |
40 |
} |
41 |
|
42 |
public CancellableDialogAdapter(final Component parentWindow) { |
43 |
this(parentWindow, null); |
44 |
} |
45 |
|
46 |
private void initDialog() { |
47 |
|
48 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
49 |
|
50 |
addWindowListener(new WindowAdapter() { |
51 |
|
52 |
@Override |
53 |
public void windowClosing(WindowEvent e) { |
54 |
close(); |
55 |
} |
56 |
|
57 |
}); |
58 |
} |
59 |
|
60 |
/** |
61 |
* Allows to close the {@link JDialog} from "outside". The user will be |
62 |
* asked and she may cancel the close process. |
63 |
*/ |
64 |
public boolean close() { |
65 |
int showConfirmDialog = JOptionPane.showConfirmDialog( |
66 |
CancellableDialogAdapter.this, "Speichern?", |
67 |
"Ă„nderungen speichern?", JOptionPane.YES_NO_CANCEL_OPTION); // i8n |
68 |
|
69 |
if (showConfirmDialog == JOptionPane.YES_OPTION) { |
70 |
return okClose(); |
71 |
} |
72 |
else if (showConfirmDialog == JOptionPane.NO_OPTION) { |
73 |
cancelClose(); |
74 |
return true; |
75 |
} |
76 |
|
77 |
return false; |
78 |
} |
79 |
|
80 |
|
81 |
/** |
82 |
* This method can be called from "outside" to force a close of this |
83 |
* {@link Dialog}. The user can't cancel the process. He may only decide to |
84 |
* save possible changes. |
85 |
*/ |
86 |
public void forceClose() { |
87 |
|
88 |
int res = JOptionPane.showConfirmDialog( |
89 |
CancellableDialogAdapter.this, "Speichern?", |
90 |
"Der Dialog muss nu gesclossen werden. Ă„nderungen speichern?", JOptionPane.YES_NO_OPTION); // i8n |
91 |
|
92 |
if (res == JOptionPane.YES_OPTION) { |
93 |
okClose(); |
94 |
} else { |
95 |
cancelClose(); |
96 |
} |
97 |
} |
98 |
|
99 |
|
100 |
/** |
101 |
* Default implementation that calls {@link #cancel()} and |
102 |
* {@link #dispose()}. |
103 |
*/ |
104 |
@Override |
105 |
public void cancelClose() { |
106 |
cancel(); |
107 |
dispose(); |
108 |
} |
109 |
|
110 |
@Override |
111 |
public abstract void cancel(); |
112 |
|
113 |
/** |
114 |
* This method is called when the dialog is closed and not canceled. Can be |
115 |
* overwritten to do anything when the dialog has been accepted. For example |
116 |
* checking for any {@link Checkable} components. <br/> |
117 |
* |
118 |
* @Return <code>false</code>, if the ok has been vetoed. |
119 |
*/ |
120 |
public boolean okClose() { |
121 |
dispose(); |
122 |
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 |
} |