3 |
import java.awt.Component; |
import java.awt.Component; |
4 |
import java.awt.Dialog; |
import java.awt.Dialog; |
5 |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
import java.util.Locale; |
|
import java.awt.event.KeyEvent; |
|
|
import java.awt.event.WindowAdapter; |
|
|
import java.awt.event.WindowEvent; |
|
7 |
|
|
8 |
import javax.swing.JComponent; |
import javax.swing.AbstractAction; |
9 |
import javax.swing.JDialog; |
import javax.swing.JDialog; |
10 |
import javax.swing.JOptionPane; |
import javax.swing.JOptionPane; |
|
import javax.swing.JRootPane; |
|
|
import javax.swing.KeyStroke; |
|
11 |
|
|
12 |
|
import schmitzm.lang.LangUtil; |
13 |
|
import schmitzm.lang.ResourceProvider; |
14 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
15 |
|
|
16 |
/** |
/** |
21 |
* |
* |
22 |
* Pressing ESC or clicking the "Close X" results in asking the user whether to Save/Cancel/Abort . |
* Pressing ESC or clicking the "Close X" results in asking the user whether to Save/Cancel/Abort . |
23 |
*/ |
*/ |
24 |
public abstract class CancellableDialogAdapter extends JDialog implements |
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 |
|
|
|
protected boolean cancelled = false; |
|
31 |
|
|
32 |
@Override |
/** Has this dialog been canceled ?**/ |
33 |
public boolean isCancelled() { |
protected boolean cancelled = false; |
|
return cancelled; |
|
|
} |
|
34 |
|
|
35 |
public CancellableDialogAdapter(final Component parentWindow, String title) { |
private OkButton okButton; |
36 |
super(SwingUtil.getParentWindow(parentWindow), title); |
private CancelButton cancelButton; |
37 |
initDialog(); |
|
38 |
|
public CancellableDialogAdapter(Component parentWindow) { |
39 |
|
super(parentWindow); |
40 |
} |
} |
41 |
|
|
42 |
public CancellableDialogAdapter(final Component parentWindow) { |
public CancellableDialogAdapter(Component parentWindow, String title) { |
43 |
this(parentWindow, null); |
super(parentWindow, title); |
44 |
} |
} |
45 |
|
|
46 |
private void initDialog() { |
@Override |
47 |
|
public boolean isCancelled() { |
48 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
return cancelled; |
|
|
|
|
addWindowListener(new WindowAdapter() { |
|
|
|
|
|
@Override |
|
|
public void windowClosing(WindowEvent e) { |
|
|
close(); |
|
|
} |
|
|
|
|
|
}); |
|
49 |
} |
} |
50 |
|
|
51 |
/** |
/** |
54 |
*/ |
*/ |
55 |
public boolean close() { |
public boolean close() { |
56 |
int showConfirmDialog = JOptionPane.showConfirmDialog( |
int showConfirmDialog = JOptionPane.showConfirmDialog( |
57 |
CancellableDialogAdapter.this, "Speichern?", |
CancellableDialogAdapter.this, |
58 |
"Ă„nderungen speichern?", JOptionPane.YES_NO_CANCEL_OPTION); // i8n |
RESOURCE.getString("CancellableDialogAdapter.close.save.msg",getTitle()), RESOURCE.getString("CancellableDialogAdapter.close.save.title"), JOptionPane.YES_NO_CANCEL_OPTION); |
59 |
|
|
60 |
if (showConfirmDialog == JOptionPane.YES_OPTION) { |
if (showConfirmDialog == JOptionPane.YES_OPTION) { |
61 |
return okClose(); |
return okClose(); |
77 |
public void forceClose() { |
public void forceClose() { |
78 |
|
|
79 |
int res = JOptionPane.showConfirmDialog( |
int res = JOptionPane.showConfirmDialog( |
80 |
CancellableDialogAdapter.this, "Speichern?", |
CancellableDialogAdapter.this, |
81 |
"Der Dialog muss nu gesclossen werden. Ă„nderungen speichern?", JOptionPane.YES_NO_OPTION); // i8n |
RESOURCE.getString("CancellableDialogAdapter.forceClose.save.msg",getTitle()), RESOURCE.getString("CancellableDialogAdapter.close.save.title"), JOptionPane.YES_NO_OPTION); |
82 |
|
|
83 |
if (res == JOptionPane.YES_OPTION) { |
if (res == JOptionPane.YES_OPTION) { |
84 |
okClose(); |
okClose(); |
112 |
dispose(); |
dispose(); |
113 |
return true; |
return true; |
114 |
} |
} |
|
|
|
115 |
|
|
116 |
/** |
/** |
117 |
* Since the registerKeyboardAction() method is part of the JComponent class |
* @return a default OkButton that will call {@link #okClose()} |
|
* definition, you must define the Escape keystroke and register the |
|
|
* keyboard action with a JComponent, not with a JDialog. The JRootPane for |
|
|
* the JDialog serves as an excellent choice to associate the registration, |
|
|
* as this will always be visible. If you override the protected |
|
|
* createRootPane() method of JDialog, you can return your custom JRootPane |
|
|
* with the keystroke enabled: |
|
118 |
*/ |
*/ |
119 |
@Override |
protected OkButton getOkButton() { |
120 |
protected JRootPane createRootPane() { |
if (okButton == null) { |
121 |
final KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
okButton = new OkButton(new AbstractAction() { |
122 |
final JRootPane rootPane = new JRootPane(); |
|
123 |
rootPane.registerKeyboardAction(new ActionListener() { |
@Override |
124 |
|
public void actionPerformed(ActionEvent e) { |
125 |
public void actionPerformed(final ActionEvent e) { |
okClose(); |
126 |
close(); |
} |
127 |
} |
}); |
128 |
|
} |
129 |
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW); |
return okButton; |
|
|
|
|
return rootPane; |
|
130 |
} |
} |
131 |
|
|
132 |
|
|
133 |
|
/** |
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 |
|
|
149 |
} |
} |