1 |
package skrueger.swing; |
package skrueger.swing; |
2 |
|
|
3 |
import java.awt.Window; |
import java.awt.Component; |
4 |
|
import java.awt.Dialog; |
5 |
import java.awt.event.WindowAdapter; |
import java.awt.event.WindowAdapter; |
6 |
import java.awt.event.WindowEvent; |
import java.awt.event.WindowEvent; |
7 |
|
|
8 |
import javax.swing.JDialog; |
import javax.swing.JDialog; |
9 |
import javax.swing.JOptionPane; |
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 |
public abstract class CancellableDialogAdapter extends JDialog implements |
20 |
CancellableDialog { |
CancellableDialog { |
21 |
|
|
26 |
return cancelled; |
return cancelled; |
27 |
} |
} |
28 |
|
|
29 |
public CancellableDialogAdapter(final Window parentWindow) { |
public CancellableDialogAdapter(final Component parentWindow, String title) { |
30 |
super(parentWindow); |
super(SwingUtil.getParentWindow(parentWindow), title); |
31 |
initDialog(); |
initDialog(); |
32 |
} |
} |
33 |
|
|
34 |
|
public CancellableDialogAdapter(final Component parentWindow) { |
35 |
|
this(parentWindow, null); |
36 |
|
} |
37 |
|
|
38 |
private void initDialog() { |
private void initDialog() { |
39 |
|
|
40 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
43 |
|
|
44 |
@Override |
@Override |
45 |
public void windowClosing(WindowEvent e) { |
public void windowClosing(WindowEvent e) { |
46 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
close(); |
|
|
|
|
int showConfirmDialog = JOptionPane.showConfirmDialog( |
|
|
CancellableDialogAdapter.this, "Speichern?", |
|
|
"Änderungen speichern?", JOptionPane.YES_NO_CANCEL_OPTION); // i8n |
|
|
|
|
|
if (showConfirmDialog == JOptionPane.YES_OPTION) |
|
|
okClose(); |
|
|
else if (showConfirmDialog == JOptionPane.NO_OPTION) |
|
|
cancelClose(); |
|
47 |
} |
} |
48 |
|
|
49 |
}); |
}); |
50 |
} |
} |
51 |
|
|
52 |
public CancellableDialogAdapter(final Window parentWindow, String title) { |
/** |
53 |
super(parentWindow, title); |
* Allows to close the {@link JDialog} from "outside". The user will be |
54 |
initDialog(); |
* 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 |
@Override |
97 |
public void cancelClose() { |
public void cancelClose() { |
98 |
cancel(); |
cancel(); |
105 |
/** |
/** |
106 |
* This method is called when the dialog is closed and not canceled. Can be |
* 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 |
* overwritten to do anything when the dialog has been accepted. For example |
108 |
* cheking for any {@link Checkable} components. Returns false, if the ok |
* checking for any {@link Checkable} components. <br/> |
109 |
* has been vetoed. |
* |
110 |
|
* @Return <code>false</code>, if the ok has been vetoed. |
111 |
*/ |
*/ |
112 |
public abstract boolean okClose(); |
public boolean okClose() { |
113 |
|
dispose(); |
114 |
|
return true; |
115 |
|
} |
116 |
|
|
117 |
} |
} |