/[schmitzm]/trunk/src/skrueger/swing/AtlasDialog.java
ViewVC logotype

Annotation of /trunk/src/skrueger/swing/AtlasDialog.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1270 - (hide annotations)
Sun Nov 14 00:25:52 2010 UTC (14 years, 3 months ago) by alfonx
File MIME type: text/plain
File size: 3157 byte(s)


1 alfonx 461 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 alfonx 1270 import javax.swing.AbstractAction;
11 alfonx 461 import javax.swing.JComponent;
12     import javax.swing.JDialog;
13     import javax.swing.JRootPane;
14     import javax.swing.KeyStroke;
15    
16 alfonx 863 import net.miginfocom.swing.MigLayout;
17 alfonx 461 import schmitzm.swing.SwingUtil;
18    
19 alfonx 863 /**
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 alfonx 461 public class AtlasDialog extends JDialog {
25    
26 alfonx 1270 protected OkButton okButton;
27     protected CancelButton cancelButton;
28    
29 alfonx 617 public AtlasDialog(final Component owner, String title) {
30     super(SwingUtil.getParentWindow(owner), title);
31 alfonx 461 initDialog();
32     }
33 alfonx 1270
34 alfonx 1060 public AtlasDialog(final Component owner) {
35     super(SwingUtil.getParentWindow(owner));
36     initDialog();
37     }
38 alfonx 863
39 alfonx 618 /** A flag checking that we just get disposed once **/
40     protected boolean isDisposed = false;
41 alfonx 461
42     private void initDialog() {
43    
44 alfonx 978 setLayout(new MigLayout("gap 1, inset 1"));
45 alfonx 863
46 alfonx 461 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 alfonx 863
91 alfonx 618 @Override
92     public void dispose() {
93     super.dispose();
94     isDisposed = true;
95     }
96 alfonx 1270
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 alfonx 461 }

Properties

Name Value
svn:eol-style native
svn:keywords Id URL
svn:mime-type text/plain

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26