2 |
|
|
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.util.Locale; |
|
import java.awt.event.ActionListener; |
|
|
import java.awt.event.KeyEvent; |
|
|
import java.awt.event.WindowAdapter; |
|
|
import java.awt.event.WindowEvent; |
|
6 |
|
|
|
import javax.swing.JComponent; |
|
7 |
import javax.swing.JDialog; |
import javax.swing.JDialog; |
8 |
import javax.swing.JOptionPane; |
import javax.swing.JOptionPane; |
|
import javax.swing.JRootPane; |
|
|
import javax.swing.KeyStroke; |
|
9 |
|
|
10 |
|
import schmitzm.geotools.gui.GeotoolsGUIUtil; |
11 |
|
import schmitzm.lang.LangUtil; |
12 |
|
import schmitzm.lang.ResourceProvider; |
13 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
14 |
|
|
15 |
/** |
/** |
20 |
* |
* |
21 |
* 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 . |
22 |
*/ |
*/ |
23 |
public abstract class CancellableDialogAdapter extends JDialog implements |
public abstract class CancellableDialogAdapter extends AtlasDialog implements |
24 |
CancellableDialog { |
CancellableDialog { |
25 |
|
|
26 |
|
protected static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
27 |
|
.extendPackagePath(SwingUtil.class, |
28 |
|
"resource.locales.SwingResourceBundle"), Locale.ENGLISH); |
29 |
|
|
|
protected boolean cancelled = false; |
|
|
|
|
|
@Override |
|
|
public boolean isCancelled() { |
|
|
return cancelled; |
|
|
} |
|
30 |
|
|
31 |
public CancellableDialogAdapter(final Component parentWindow, String title) { |
/** Has this dialog been canceled ?**/ |
32 |
super(SwingUtil.getParentWindow(parentWindow), title); |
protected boolean cancelled = false; |
33 |
initDialog(); |
|
34 |
|
public CancellableDialogAdapter(Component parentWindow) { |
35 |
|
super(parentWindow); |
36 |
} |
} |
37 |
|
|
38 |
public CancellableDialogAdapter(final Component parentWindow) { |
public CancellableDialogAdapter(Component parentWindow, String title) { |
39 |
this(parentWindow, null); |
super(parentWindow, title); |
40 |
} |
} |
41 |
|
|
42 |
private void initDialog() { |
@Override |
43 |
|
public boolean isCancelled() { |
44 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
return cancelled; |
|
|
|
|
addWindowListener(new WindowAdapter() { |
|
|
|
|
|
@Override |
|
|
public void windowClosing(WindowEvent e) { |
|
|
close(); |
|
|
} |
|
|
|
|
|
}); |
|
45 |
} |
} |
46 |
|
|
47 |
/** |
/** |
50 |
*/ |
*/ |
51 |
public boolean close() { |
public boolean close() { |
52 |
int showConfirmDialog = JOptionPane.showConfirmDialog( |
int showConfirmDialog = JOptionPane.showConfirmDialog( |
53 |
CancellableDialogAdapter.this, "Speichern?", |
CancellableDialogAdapter.this, |
54 |
"Ă„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); |
55 |
|
|
56 |
if (showConfirmDialog == JOptionPane.YES_OPTION) { |
if (showConfirmDialog == JOptionPane.YES_OPTION) { |
57 |
return okClose(); |
return okClose(); |
73 |
public void forceClose() { |
public void forceClose() { |
74 |
|
|
75 |
int res = JOptionPane.showConfirmDialog( |
int res = JOptionPane.showConfirmDialog( |
76 |
CancellableDialogAdapter.this, "Speichern?", |
CancellableDialogAdapter.this, |
77 |
"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); |
78 |
|
|
79 |
if (res == JOptionPane.YES_OPTION) { |
if (res == JOptionPane.YES_OPTION) { |
80 |
okClose(); |
okClose(); |
110 |
} |
} |
111 |
|
|
112 |
|
|
|
/** |
|
|
* Since the registerKeyboardAction() method is part of the JComponent class |
|
|
* 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: |
|
|
*/ |
|
|
@Override |
|
|
protected JRootPane createRootPane() { |
|
|
final KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
|
|
final JRootPane rootPane = new JRootPane(); |
|
|
rootPane.registerKeyboardAction(new ActionListener() { |
|
|
|
|
|
public void actionPerformed(final ActionEvent e) { |
|
|
close(); |
|
|
} |
|
|
|
|
|
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW); |
|
|
|
|
|
return rootPane; |
|
|
} |
|
|
|
|
113 |
|
|
114 |
} |
} |