1 |
alfonx |
420 |
package skrueger.swing; |
2 |
|
|
|
3 |
alfonx |
422 |
import java.awt.Component; |
4 |
|
|
import java.awt.Dialog; |
5 |
alfonx |
661 |
import java.awt.event.ActionEvent; |
6 |
alfonx |
497 |
import java.util.Locale; |
7 |
alfonx |
420 |
|
8 |
alfonx |
661 |
import javax.swing.AbstractAction; |
9 |
alfonx |
420 |
import javax.swing.JDialog; |
10 |
|
|
import javax.swing.JOptionPane; |
11 |
|
|
|
12 |
alfonx |
497 |
import schmitzm.lang.LangUtil; |
13 |
|
|
import schmitzm.lang.ResourceProvider; |
14 |
alfonx |
520 |
import schmitzm.swing.SwingUtil; |
15 |
alfonx |
497 |
|
16 |
alfonx |
422 |
/** |
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 |
alfonx |
433 |
* |
22 |
alfonx |
1267 |
* Pressing ESC or clicking the "Close X" results in asking the user whether to |
23 |
|
|
* Save/Cancel/Abort . |
24 |
alfonx |
422 |
*/ |
25 |
alfonx |
461 |
public abstract class CancellableDialogAdapter extends AtlasDialog implements |
26 |
alfonx |
420 |
CancellableDialog { |
27 |
alfonx |
1267 |
|
28 |
|
|
protected static ResourceProvider RESOURCE = ResourceProvider.newInstance( |
29 |
|
|
LangUtil.extendPackagePath(SwingUtil.class, |
30 |
alfonx |
520 |
"resource.locales.SwingResourceBundle"), Locale.ENGLISH); |
31 |
alfonx |
497 |
|
32 |
alfonx |
1267 |
/** Has this dialog been canceled ? **/ |
33 |
alfonx |
420 |
protected boolean cancelled = false; |
34 |
alfonx |
661 |
|
35 |
alfonx |
461 |
public CancellableDialogAdapter(Component parentWindow) { |
36 |
|
|
super(parentWindow); |
37 |
|
|
} |
38 |
alfonx |
420 |
|
39 |
alfonx |
461 |
public CancellableDialogAdapter(Component parentWindow, String title) { |
40 |
|
|
super(parentWindow, title); |
41 |
|
|
} |
42 |
|
|
|
43 |
alfonx |
420 |
@Override |
44 |
|
|
public boolean isCancelled() { |
45 |
|
|
return cancelled; |
46 |
|
|
} |
47 |
|
|
|
48 |
alfonx |
422 |
/** |
49 |
|
|
* Allows to close the {@link JDialog} from "outside". The user will be |
50 |
|
|
* asked and she may cancel the close process. |
51 |
|
|
*/ |
52 |
alfonx |
1267 |
@Override |
53 |
alfonx |
422 |
public boolean close() { |
54 |
alfonx |
1267 |
int showConfirmDialog = JOptionPane |
55 |
|
|
.showConfirmDialog( |
56 |
|
|
CancellableDialogAdapter.this, |
57 |
|
|
RESOURCE.getString( |
58 |
|
|
"CancellableDialogAdapter.close.save.msg", |
59 |
|
|
getTitle()), |
60 |
|
|
RESOURCE.getString("CancellableDialogAdapter.close.save.title"), |
61 |
|
|
JOptionPane.YES_NO_CANCEL_OPTION); |
62 |
alfonx |
422 |
|
63 |
|
|
if (showConfirmDialog == JOptionPane.YES_OPTION) { |
64 |
|
|
return okClose(); |
65 |
alfonx |
1267 |
} else if (showConfirmDialog == JOptionPane.NO_OPTION) { |
66 |
alfonx |
422 |
cancelClose(); |
67 |
|
|
return true; |
68 |
|
|
} |
69 |
alfonx |
1267 |
|
70 |
alfonx |
422 |
return false; |
71 |
alfonx |
420 |
} |
72 |
|
|
|
73 |
alfonx |
422 |
/** |
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 |
alfonx |
1267 |
int res = JOptionPane |
81 |
|
|
.showConfirmDialog( |
82 |
|
|
CancellableDialogAdapter.this, |
83 |
|
|
RESOURCE.getString( |
84 |
|
|
"CancellableDialogAdapter.forceClose.save.msg", |
85 |
|
|
getTitle()), |
86 |
|
|
RESOURCE.getString("CancellableDialogAdapter.close.save.title"), |
87 |
|
|
JOptionPane.YES_NO_OPTION); |
88 |
|
|
|
89 |
alfonx |
422 |
if (res == JOptionPane.YES_OPTION) { |
90 |
|
|
okClose(); |
91 |
|
|
} else { |
92 |
|
|
cancelClose(); |
93 |
|
|
} |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
/** |
97 |
|
|
* Default implementation that calls {@link #cancel()} and |
98 |
|
|
* {@link #dispose()}. |
99 |
|
|
*/ |
100 |
alfonx |
420 |
@Override |
101 |
|
|
public void cancelClose() { |
102 |
|
|
cancel(); |
103 |
alfonx |
863 |
cancelled = true; |
104 |
alfonx |
420 |
dispose(); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
@Override |
108 |
|
|
public abstract void cancel(); |
109 |
|
|
|
110 |
|
|
/** |
111 |
|
|
* This method is called when the dialog is closed and not canceled. Can be |
112 |
|
|
* overwritten to do anything when the dialog has been accepted. For example |
113 |
alfonx |
422 |
* checking for any {@link Checkable} components. <br/> |
114 |
alfonx |
1267 |
* Call super.okClose() at the end. |
115 |
alfonx |
422 |
* |
116 |
|
|
* @Return <code>false</code>, if the ok has been vetoed. |
117 |
alfonx |
420 |
*/ |
118 |
alfonx |
422 |
public boolean okClose() { |
119 |
|
|
dispose(); |
120 |
|
|
return true; |
121 |
|
|
} |
122 |
alfonx |
661 |
|
123 |
|
|
/** |
124 |
|
|
* @return a default OkButton that will call {@link #okClose()} |
125 |
|
|
*/ |
126 |
alfonx |
1270 |
@Override |
127 |
alfonx |
661 |
protected OkButton getOkButton() { |
128 |
|
|
if (okButton == null) { |
129 |
|
|
okButton = new OkButton(new AbstractAction() { |
130 |
alfonx |
1267 |
|
131 |
alfonx |
661 |
@Override |
132 |
|
|
public void actionPerformed(ActionEvent e) { |
133 |
|
|
okClose(); |
134 |
|
|
} |
135 |
|
|
}); |
136 |
|
|
} |
137 |
|
|
return okButton; |
138 |
|
|
} |
139 |
alfonx |
420 |
|
140 |
alfonx |
661 |
/** |
141 |
|
|
* @return a default CancelButton that will call {@link #cancelClose()} |
142 |
|
|
*/ |
143 |
alfonx |
1270 |
@Override |
144 |
alfonx |
661 |
protected CancelButton getCancelButton() { |
145 |
|
|
if (cancelButton == null) { |
146 |
|
|
cancelButton = new CancelButton(new AbstractAction() { |
147 |
alfonx |
1267 |
|
148 |
alfonx |
661 |
@Override |
149 |
|
|
public void actionPerformed(ActionEvent e) { |
150 |
|
|
cancelClose(); |
151 |
|
|
} |
152 |
|
|
}); |
153 |
|
|
} |
154 |
|
|
return cancelButton; |
155 |
|
|
} |
156 |
alfonx |
433 |
|
157 |
alfonx |
420 |
} |