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