1 |
package skrueger.swing; |
package skrueger.swing; |
2 |
|
|
3 |
import java.awt.Window; |
import java.awt.Component; |
4 |
import java.awt.event.WindowAdapter; |
import java.awt.Dialog; |
5 |
import java.awt.event.WindowEvent; |
import java.awt.event.ActionEvent; |
6 |
|
import java.util.Locale; |
7 |
|
|
8 |
|
import javax.swing.AbstractAction; |
9 |
import javax.swing.JDialog; |
import javax.swing.JDialog; |
10 |
import javax.swing.JOptionPane; |
import javax.swing.JOptionPane; |
11 |
|
|
12 |
public abstract class CancellableDialogAdapter extends JDialog implements |
import schmitzm.lang.LangUtil; |
13 |
|
import schmitzm.lang.ResourceProvider; |
14 |
|
import schmitzm.swing.SwingUtil; |
15 |
|
|
16 |
|
/** |
17 |
|
* An abstract {@link JDialog} that implements a basic structure of how |
18 |
|
* cancellable {@link JDialog}s work. The {@link JDialog} is designed to work on |
19 |
|
* the real object and restore it's state when the user cancels the |
20 |
|
* {@link JDialog}. |
21 |
|
* |
22 |
|
* Pressing ESC or clicking the "Close X" results in asking the user whether to Save/Cancel/Abort . |
23 |
|
*/ |
24 |
|
public abstract class CancellableDialogAdapter extends AtlasDialog implements |
25 |
CancellableDialog { |
CancellableDialog { |
26 |
|
|
27 |
|
protected static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
28 |
|
.extendPackagePath(SwingUtil.class, |
29 |
|
"resource.locales.SwingResourceBundle"), Locale.ENGLISH); |
30 |
|
|
31 |
|
|
32 |
|
/** Has this dialog been canceled ?**/ |
33 |
protected boolean cancelled = false; |
protected boolean cancelled = false; |
34 |
|
|
35 |
|
private OkButton okButton; |
36 |
|
|
37 |
|
private CancelButton cancelButton; |
38 |
|
|
39 |
|
public CancellableDialogAdapter(Component parentWindow) { |
40 |
|
super(parentWindow); |
41 |
|
} |
42 |
|
|
43 |
|
public CancellableDialogAdapter(Component parentWindow, String title) { |
44 |
|
super(parentWindow, title); |
45 |
|
} |
46 |
|
|
47 |
@Override |
@Override |
48 |
public boolean isCancelled() { |
public boolean isCancelled() { |
49 |
return cancelled; |
return cancelled; |
50 |
} |
} |
51 |
|
|
52 |
public CancellableDialogAdapter(final Window parentWindow) { |
/** |
53 |
super(parentWindow); |
* 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, |
59 |
|
RESOURCE.getString("CancellableDialogAdapter.close.save.msg",getTitle()), RESOURCE.getString("CancellableDialogAdapter.close.save.title"), JOptionPane.YES_NO_CANCEL_OPTION); |
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 |
private void initDialog() { |
/** |
74 |
|
* This method can be called from "outside" to force a close of this |
75 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
* {@link Dialog}. The user can't cancel the process. He may only decide to |
76 |
|
* save possible changes. |
77 |
addWindowListener(new WindowAdapter() { |
*/ |
78 |
|
public void forceClose() { |
|
@Override |
|
|
public void windowClosing(WindowEvent e) { |
|
|
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_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(); |
|
|
} |
|
|
|
|
|
}); |
|
|
} |
|
79 |
|
|
80 |
public CancellableDialogAdapter(final Window parentWindow, String title) { |
int res = JOptionPane.showConfirmDialog( |
81 |
super(parentWindow, title); |
CancellableDialogAdapter.this, |
82 |
initDialog(); |
RESOURCE.getString("CancellableDialogAdapter.forceClose.save.msg",getTitle()), RESOURCE.getString("CancellableDialogAdapter.close.save.title"), JOptionPane.YES_NO_OPTION); |
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(); |
99 |
|
cancelled = true; |
100 |
dispose(); |
dispose(); |
101 |
} |
} |
102 |
|
|
106 |
/** |
/** |
107 |
* 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 |
108 |
* overwritten to do anything when the dialog has been accepted. For example |
* overwritten to do anything when the dialog has been accepted. For example |
109 |
* cheking for any {@link Checkable} components. Returns false, if the ok |
* checking for any {@link Checkable} components. <br/> |
110 |
* has been vetoed. |
* |
111 |
|
* @Return <code>false</code>, if the ok has been vetoed. |
112 |
|
*/ |
113 |
|
public boolean okClose() { |
114 |
|
dispose(); |
115 |
|
return true; |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
|
* @return a default OkButton that will call {@link #okClose()} |
120 |
*/ |
*/ |
121 |
public abstract boolean okClose(); |
protected OkButton getOkButton() { |
122 |
|
if (okButton == null) { |
123 |
|
okButton = new OkButton(new AbstractAction() { |
124 |
|
|
125 |
|
@Override |
126 |
|
public void actionPerformed(ActionEvent e) { |
127 |
|
okClose(); |
128 |
|
} |
129 |
|
}); |
130 |
|
} |
131 |
|
return okButton; |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
|
/** |
136 |
|
* @return a default CancelButton that will call {@link #cancelClose()} |
137 |
|
*/ |
138 |
|
protected CancelButton getCancelButton() { |
139 |
|
if (cancelButton == null) { |
140 |
|
cancelButton = new CancelButton(new AbstractAction() { |
141 |
|
|
142 |
|
@Override |
143 |
|
public void actionPerformed(ActionEvent e) { |
144 |
|
cancelClose(); |
145 |
|
} |
146 |
|
}); |
147 |
|
} |
148 |
|
return cancelButton; |
149 |
|
} |
150 |
|
|
151 |
} |
} |