1 |
package skrueger.swing; |
2 |
|
3 |
import java.awt.Window; |
4 |
import java.awt.event.WindowAdapter; |
5 |
import java.awt.event.WindowEvent; |
6 |
|
7 |
import javax.swing.JDialog; |
8 |
import javax.swing.JOptionPane; |
9 |
|
10 |
public abstract class CancellableDialogAdapter extends JDialog implements |
11 |
CancellableDialog { |
12 |
|
13 |
protected boolean cancelled = false; |
14 |
|
15 |
@Override |
16 |
public boolean isCancelled() { |
17 |
return cancelled; |
18 |
} |
19 |
|
20 |
public CancellableDialogAdapter(final Window parentWindow) { |
21 |
super(parentWindow); |
22 |
initDialog(); |
23 |
} |
24 |
|
25 |
private void initDialog() { |
26 |
|
27 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
28 |
|
29 |
addWindowListener(new WindowAdapter() { |
30 |
|
31 |
@Override |
32 |
public void windowClosing(WindowEvent e) { |
33 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
34 |
|
35 |
int showConfirmDialog = JOptionPane.showConfirmDialog( |
36 |
CancellableDialogAdapter.this, "Speichern?", |
37 |
"Ă„nderungen speichern?", JOptionPane.YES_NO_CANCEL_OPTION); // i8n |
38 |
|
39 |
if (showConfirmDialog == JOptionPane.YES_OPTION) |
40 |
okClose(); |
41 |
else if (showConfirmDialog == JOptionPane.NO_OPTION) |
42 |
cancelClose(); |
43 |
} |
44 |
|
45 |
}); |
46 |
} |
47 |
|
48 |
public CancellableDialogAdapter(final Window parentWindow, String title) { |
49 |
super(parentWindow, title); |
50 |
initDialog(); |
51 |
} |
52 |
|
53 |
@Override |
54 |
public void cancelClose() { |
55 |
cancel(); |
56 |
dispose(); |
57 |
} |
58 |
|
59 |
@Override |
60 |
public abstract void cancel(); |
61 |
|
62 |
/** |
63 |
* This method is called when the dialog is closed and not canceled. Can be |
64 |
* overwritten to do anything when the dialog has been accepted. For example |
65 |
* cheking for any {@link Checkable} components. Returns false, if the ok |
66 |
* has been vetoed. |
67 |
*/ |
68 |
public abstract boolean okClose(); |
69 |
|
70 |
} |