1 |
package skrueger.swing; |
2 |
|
3 |
import java.awt.Component; |
4 |
import java.awt.Dialog; |
5 |
|
6 |
import javax.swing.JDialog; |
7 |
import javax.swing.JOptionPane; |
8 |
|
9 |
/** |
10 |
* An abstract {@link JDialog} that implements a basic structure of how |
11 |
* cancellable {@link JDialog}s work. The {@link JDialog} is designed to work on |
12 |
* the real object and restore it's state when the user cancels the |
13 |
* {@link JDialog}. |
14 |
* |
15 |
* Pressing ESC or clicking the "Close X" results in asking the user whether to Save/Cancel/Abort . |
16 |
*/ |
17 |
public abstract class CancellableDialogAdapter extends AtlasDialog implements |
18 |
CancellableDialog { |
19 |
|
20 |
/** Has this dialog been canceled ?**/ |
21 |
protected boolean cancelled = false; |
22 |
|
23 |
public CancellableDialogAdapter(Component parentWindow) { |
24 |
super(parentWindow); |
25 |
} |
26 |
|
27 |
public CancellableDialogAdapter(Component parentWindow, String title) { |
28 |
super(parentWindow, title); |
29 |
} |
30 |
|
31 |
@Override |
32 |
public boolean isCancelled() { |
33 |
return cancelled; |
34 |
} |
35 |
|
36 |
/** |
37 |
* Allows to close the {@link JDialog} from "outside". The user will be |
38 |
* asked and she may cancel the close process. |
39 |
*/ |
40 |
public boolean close() { |
41 |
int showConfirmDialog = JOptionPane.showConfirmDialog( |
42 |
CancellableDialogAdapter.this, "Speichern?", |
43 |
"Ă„nderungen speichern?", JOptionPane.YES_NO_CANCEL_OPTION); // i8n |
44 |
|
45 |
if (showConfirmDialog == JOptionPane.YES_OPTION) { |
46 |
return okClose(); |
47 |
} |
48 |
else if (showConfirmDialog == JOptionPane.NO_OPTION) { |
49 |
cancelClose(); |
50 |
return true; |
51 |
} |
52 |
|
53 |
return false; |
54 |
} |
55 |
|
56 |
|
57 |
/** |
58 |
* This method can be called from "outside" to force a close of this |
59 |
* {@link Dialog}. The user can't cancel the process. He may only decide to |
60 |
* save possible changes. |
61 |
*/ |
62 |
public void forceClose() { |
63 |
|
64 |
int res = JOptionPane.showConfirmDialog( |
65 |
CancellableDialogAdapter.this, "Speichern?", |
66 |
"Der Dialog muss nu gesclossen werden. Ă„nderungen speichern?", JOptionPane.YES_NO_OPTION); // i8n |
67 |
|
68 |
if (res == JOptionPane.YES_OPTION) { |
69 |
okClose(); |
70 |
} else { |
71 |
cancelClose(); |
72 |
} |
73 |
} |
74 |
|
75 |
|
76 |
/** |
77 |
* Default implementation that calls {@link #cancel()} and |
78 |
* {@link #dispose()}. |
79 |
*/ |
80 |
@Override |
81 |
public void cancelClose() { |
82 |
cancel(); |
83 |
dispose(); |
84 |
} |
85 |
|
86 |
@Override |
87 |
public abstract void cancel(); |
88 |
|
89 |
/** |
90 |
* This method is called when the dialog is closed and not canceled. Can be |
91 |
* overwritten to do anything when the dialog has been accepted. For example |
92 |
* checking for any {@link Checkable} components. <br/> |
93 |
* |
94 |
* @Return <code>false</code>, if the ok has been vetoed. |
95 |
*/ |
96 |
public boolean okClose() { |
97 |
dispose(); |
98 |
return true; |
99 |
} |
100 |
|
101 |
|
102 |
|
103 |
} |