1 |
package skrueger.swing; |
2 |
|
3 |
import java.awt.Component; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.awt.event.KeyEvent; |
7 |
import java.awt.event.WindowAdapter; |
8 |
import java.awt.event.WindowEvent; |
9 |
|
10 |
import javax.swing.AbstractAction; |
11 |
import javax.swing.JComponent; |
12 |
import javax.swing.JDialog; |
13 |
import javax.swing.JRootPane; |
14 |
import javax.swing.KeyStroke; |
15 |
|
16 |
import net.miginfocom.swing.MigLayout; |
17 |
import schmitzm.swing.SwingUtil; |
18 |
|
19 |
/** |
20 |
* A basic super class for atlas dialogs. It listens to the ESC key and calls |
21 |
* the {@link #close()} method. The layout manager is initialized with |
22 |
* {@link MigLayout}. |
23 |
*/ |
24 |
public class AtlasDialog extends JDialog { |
25 |
|
26 |
protected OkButton okButton; |
27 |
protected CancelButton cancelButton; |
28 |
|
29 |
public AtlasDialog(final Component owner, String title) { |
30 |
super(SwingUtil.getParentWindow(owner), title); |
31 |
initDialog(); |
32 |
} |
33 |
|
34 |
public AtlasDialog(final Component owner) { |
35 |
super(SwingUtil.getParentWindow(owner)); |
36 |
initDialog(); |
37 |
} |
38 |
|
39 |
/** A flag checking that we just get disposed once **/ |
40 |
protected boolean isDisposed = false; |
41 |
|
42 |
private void initDialog() { |
43 |
|
44 |
setLayout(new MigLayout("gap 1, inset 1")); |
45 |
|
46 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
47 |
|
48 |
addWindowListener(new WindowAdapter() { |
49 |
|
50 |
@Override |
51 |
public void windowClosing(WindowEvent e) { |
52 |
close(); |
53 |
} |
54 |
|
55 |
}); |
56 |
} |
57 |
|
58 |
/** |
59 |
* Allows to close the {@link JDialog} from "outside". {@link AtlasDialog} |
60 |
* is not implementing {@link Cancellable}, so the dialog is just disposed. |
61 |
*/ |
62 |
public boolean close() { |
63 |
dispose(); |
64 |
return true; |
65 |
} |
66 |
|
67 |
/** |
68 |
* Since the registerKeyboardAction() method is part of the JComponent class |
69 |
* definition, you must define the Escape keystroke and register the |
70 |
* keyboard action with a JComponent, not with a JDialog. The JRootPane for |
71 |
* the JDialog serves as an excellent choice to associate the registration, |
72 |
* as this will always be visible. If you override the protected |
73 |
* createRootPane() method of JDialog, you can return your custom JRootPane |
74 |
* with the keystroke enabled: |
75 |
*/ |
76 |
@Override |
77 |
protected JRootPane createRootPane() { |
78 |
final KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
79 |
final JRootPane rootPane = new JRootPane(); |
80 |
rootPane.registerKeyboardAction(new ActionListener() { |
81 |
|
82 |
public void actionPerformed(final ActionEvent e) { |
83 |
close(); |
84 |
} |
85 |
|
86 |
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW); |
87 |
|
88 |
return rootPane; |
89 |
} |
90 |
|
91 |
@Override |
92 |
public void dispose() { |
93 |
super.dispose(); |
94 |
isDisposed = true; |
95 |
} |
96 |
|
97 |
/** |
98 |
* @return a default OkButton that will call {@link #okClose()} |
99 |
*/ |
100 |
protected OkButton getOkButton() { |
101 |
if (okButton == null) { |
102 |
okButton = new OkButton(new AbstractAction() { |
103 |
|
104 |
@Override |
105 |
public void actionPerformed(ActionEvent e) { |
106 |
close(); |
107 |
} |
108 |
}); |
109 |
} |
110 |
return okButton; |
111 |
} |
112 |
|
113 |
/** |
114 |
* @return a default CancelButton that will call {@link #cancelClose()} |
115 |
*/ |
116 |
protected CancelButton getCancelButton() { |
117 |
if (cancelButton == null) { |
118 |
cancelButton = new CancelButton(new AbstractAction() { |
119 |
|
120 |
@Override |
121 |
public void actionPerformed(ActionEvent e) { |
122 |
dispose(); |
123 |
} |
124 |
}); |
125 |
} |
126 |
return cancelButton; |
127 |
} |
128 |
} |