1 |
package skrueger.swing; |
2 |
|
3 |
import java.awt.Component; |
4 |
import java.awt.Dialog; |
5 |
import java.awt.event.ActionEvent; |
6 |
import java.util.Locale; |
7 |
|
8 |
import javax.swing.AbstractAction; |
9 |
import javax.swing.JDialog; |
10 |
import javax.swing.JOptionPane; |
11 |
|
12 |
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 |
23 |
* Save/Cancel/Abort . |
24 |
*/ |
25 |
public abstract class CancellableDialogAdapter extends AtlasDialog implements |
26 |
CancellableDialog { |
27 |
|
28 |
protected static ResourceProvider RESOURCE = ResourceProvider.newInstance( |
29 |
LangUtil.extendPackagePath(SwingUtil.class, |
30 |
"resource.locales.SwingResourceBundle"), Locale.ENGLISH); |
31 |
|
32 |
/** Has this dialog been canceled ? **/ |
33 |
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 |
48 |
public boolean isCancelled() { |
49 |
return cancelled; |
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 |
@Override |
57 |
public boolean close() { |
58 |
int showConfirmDialog = JOptionPane |
59 |
.showConfirmDialog( |
60 |
CancellableDialogAdapter.this, |
61 |
RESOURCE.getString( |
62 |
"CancellableDialogAdapter.close.save.msg", |
63 |
getTitle()), |
64 |
RESOURCE.getString("CancellableDialogAdapter.close.save.title"), |
65 |
JOptionPane.YES_NO_CANCEL_OPTION); |
66 |
|
67 |
if (showConfirmDialog == JOptionPane.YES_OPTION) { |
68 |
return okClose(); |
69 |
} else if (showConfirmDialog == JOptionPane.NO_OPTION) { |
70 |
cancelClose(); |
71 |
return true; |
72 |
} |
73 |
|
74 |
return false; |
75 |
} |
76 |
|
77 |
/** |
78 |
* This method can be called from "outside" to force a close of this |
79 |
* {@link Dialog}. The user can't cancel the process. He may only decide to |
80 |
* save possible changes. |
81 |
*/ |
82 |
public void forceClose() { |
83 |
|
84 |
int res = JOptionPane |
85 |
.showConfirmDialog( |
86 |
CancellableDialogAdapter.this, |
87 |
RESOURCE.getString( |
88 |
"CancellableDialogAdapter.forceClose.save.msg", |
89 |
getTitle()), |
90 |
RESOURCE.getString("CancellableDialogAdapter.close.save.title"), |
91 |
JOptionPane.YES_NO_OPTION); |
92 |
|
93 |
if (res == JOptionPane.YES_OPTION) { |
94 |
okClose(); |
95 |
} else { |
96 |
cancelClose(); |
97 |
} |
98 |
} |
99 |
|
100 |
/** |
101 |
* Default implementation that calls {@link #cancel()} and |
102 |
* {@link #dispose()}. |
103 |
*/ |
104 |
@Override |
105 |
public void cancelClose() { |
106 |
cancel(); |
107 |
cancelled = true; |
108 |
dispose(); |
109 |
} |
110 |
|
111 |
@Override |
112 |
public abstract void cancel(); |
113 |
|
114 |
/** |
115 |
* This method is called when the dialog is closed and not canceled. Can be |
116 |
* overwritten to do anything when the dialog has been accepted. For example |
117 |
* checking for any {@link Checkable} components. <br/> |
118 |
* Call super.okClose() at the end. |
119 |
* |
120 |
* @Return <code>false</code>, if the ok has been vetoed. |
121 |
*/ |
122 |
public boolean okClose() { |
123 |
dispose(); |
124 |
return true; |
125 |
} |
126 |
|
127 |
/** |
128 |
* @return a default OkButton that will call {@link #okClose()} |
129 |
*/ |
130 |
protected OkButton getOkButton() { |
131 |
if (okButton == null) { |
132 |
okButton = new OkButton(new AbstractAction() { |
133 |
|
134 |
@Override |
135 |
public void actionPerformed(ActionEvent e) { |
136 |
okClose(); |
137 |
} |
138 |
}); |
139 |
} |
140 |
return okButton; |
141 |
} |
142 |
|
143 |
/** |
144 |
* @return a default CancelButton that will call {@link #cancelClose()} |
145 |
*/ |
146 |
protected CancelButton getCancelButton() { |
147 |
if (cancelButton == null) { |
148 |
cancelButton = new CancelButton(new AbstractAction() { |
149 |
|
150 |
@Override |
151 |
public void actionPerformed(ActionEvent e) { |
152 |
cancelClose(); |
153 |
} |
154 |
}); |
155 |
} |
156 |
return cancelButton; |
157 |
} |
158 |
|
159 |
} |