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