1 |
alfonx |
420 |
package skrueger.swing; |
2 |
|
|
|
3 |
alfonx |
422 |
import java.awt.Component; |
4 |
|
|
import java.awt.Dialog; |
5 |
alfonx |
497 |
import java.util.Locale; |
6 |
alfonx |
420 |
|
7 |
|
|
import javax.swing.JDialog; |
8 |
|
|
import javax.swing.JOptionPane; |
9 |
|
|
|
10 |
alfonx |
497 |
import schmitzm.lang.LangUtil; |
11 |
|
|
import schmitzm.lang.ResourceProvider; |
12 |
alfonx |
520 |
import schmitzm.swing.SwingUtil; |
13 |
alfonx |
497 |
|
14 |
alfonx |
422 |
/** |
15 |
|
|
* An abstract {@link JDialog} that implements a basic structure of how |
16 |
|
|
* cancellable {@link JDialog}s work. The {@link JDialog} is designed to work on |
17 |
|
|
* the real object and restore it's state when the user cancels the |
18 |
|
|
* {@link JDialog}. |
19 |
alfonx |
433 |
* |
20 |
|
|
* Pressing ESC or clicking the "Close X" results in asking the user whether to Save/Cancel/Abort . |
21 |
alfonx |
422 |
*/ |
22 |
alfonx |
461 |
public abstract class CancellableDialogAdapter extends AtlasDialog implements |
23 |
alfonx |
420 |
CancellableDialog { |
24 |
alfonx |
497 |
|
25 |
|
|
protected static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
26 |
alfonx |
520 |
.extendPackagePath(SwingUtil.class, |
27 |
|
|
"resource.locales.SwingResourceBundle"), Locale.ENGLISH); |
28 |
alfonx |
497 |
|
29 |
alfonx |
520 |
|
30 |
alfonx |
461 |
/** Has this dialog been canceled ?**/ |
31 |
alfonx |
420 |
protected boolean cancelled = false; |
32 |
alfonx |
461 |
|
33 |
|
|
public CancellableDialogAdapter(Component parentWindow) { |
34 |
|
|
super(parentWindow); |
35 |
|
|
} |
36 |
alfonx |
420 |
|
37 |
alfonx |
461 |
public CancellableDialogAdapter(Component parentWindow, String title) { |
38 |
|
|
super(parentWindow, title); |
39 |
|
|
} |
40 |
|
|
|
41 |
alfonx |
420 |
@Override |
42 |
|
|
public boolean isCancelled() { |
43 |
|
|
return cancelled; |
44 |
|
|
} |
45 |
|
|
|
46 |
alfonx |
422 |
/** |
47 |
|
|
* Allows to close the {@link JDialog} from "outside". The user will be |
48 |
|
|
* asked and she may cancel the close process. |
49 |
|
|
*/ |
50 |
|
|
public boolean close() { |
51 |
|
|
int showConfirmDialog = JOptionPane.showConfirmDialog( |
52 |
alfonx |
497 |
CancellableDialogAdapter.this, |
53 |
|
|
RESOURCE.getString("CancellableDialogAdapter.close.save.msg",getTitle()), RESOURCE.getString("CancellableDialogAdapter.close.save.title"), JOptionPane.YES_NO_CANCEL_OPTION); |
54 |
alfonx |
422 |
|
55 |
|
|
if (showConfirmDialog == JOptionPane.YES_OPTION) { |
56 |
|
|
return okClose(); |
57 |
|
|
} |
58 |
|
|
else if (showConfirmDialog == JOptionPane.NO_OPTION) { |
59 |
|
|
cancelClose(); |
60 |
|
|
return true; |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
return false; |
64 |
alfonx |
420 |
} |
65 |
alfonx |
422 |
|
66 |
alfonx |
420 |
|
67 |
alfonx |
422 |
/** |
68 |
|
|
* This method can be called from "outside" to force a close of this |
69 |
|
|
* {@link Dialog}. The user can't cancel the process. He may only decide to |
70 |
|
|
* save possible changes. |
71 |
|
|
*/ |
72 |
|
|
public void forceClose() { |
73 |
|
|
|
74 |
|
|
int res = JOptionPane.showConfirmDialog( |
75 |
alfonx |
497 |
CancellableDialogAdapter.this, |
76 |
|
|
RESOURCE.getString("CancellableDialogAdapter.forceClose.save.msg",getTitle()), RESOURCE.getString("CancellableDialogAdapter.close.save.title"), JOptionPane.YES_NO_OPTION); |
77 |
alfonx |
422 |
|
78 |
|
|
if (res == JOptionPane.YES_OPTION) { |
79 |
|
|
okClose(); |
80 |
|
|
} else { |
81 |
|
|
cancelClose(); |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
/** |
87 |
|
|
* Default implementation that calls {@link #cancel()} and |
88 |
|
|
* {@link #dispose()}. |
89 |
|
|
*/ |
90 |
alfonx |
420 |
@Override |
91 |
|
|
public void cancelClose() { |
92 |
|
|
cancel(); |
93 |
|
|
dispose(); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
@Override |
97 |
|
|
public abstract void cancel(); |
98 |
|
|
|
99 |
|
|
/** |
100 |
|
|
* This method is called when the dialog is closed and not canceled. Can be |
101 |
|
|
* overwritten to do anything when the dialog has been accepted. For example |
102 |
alfonx |
422 |
* checking for any {@link Checkable} components. <br/> |
103 |
|
|
* |
104 |
|
|
* @Return <code>false</code>, if the ok has been vetoed. |
105 |
alfonx |
420 |
*/ |
106 |
alfonx |
422 |
public boolean okClose() { |
107 |
|
|
dispose(); |
108 |
|
|
return true; |
109 |
|
|
} |
110 |
alfonx |
433 |
|
111 |
alfonx |
420 |
|
112 |
alfonx |
433 |
|
113 |
alfonx |
420 |
} |