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

Diff of /trunk/src/skrueger/swing/CancellableDialogAdapter.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 420 by alfonx, Thu Oct 1 20:22:48 2009 UTC revision 433 by alfonx, Sun Oct 4 16:57:29 2009 UTC
# Line 1  Line 1 
1  package skrueger.swing;  package skrueger.swing;
2    
3  import java.awt.Window;  import java.awt.Component;
4    import java.awt.Dialog;
5    import java.awt.event.ActionEvent;
6    import java.awt.event.ActionListener;
7    import java.awt.event.KeyEvent;
8  import java.awt.event.WindowAdapter;  import java.awt.event.WindowAdapter;
9  import java.awt.event.WindowEvent;  import java.awt.event.WindowEvent;
10    
11    import javax.swing.JComponent;
12  import javax.swing.JDialog;  import javax.swing.JDialog;
13  import javax.swing.JOptionPane;  import javax.swing.JOptionPane;
14    import javax.swing.JRootPane;
15    import javax.swing.KeyStroke;
16    
17    import schmitzm.swing.SwingUtil;
18    
19    /**
20     * An abstract {@link JDialog} that implements a basic structure of how
21     * cancellable {@link JDialog}s work. The {@link JDialog} is designed to work on
22     * the real object and restore it's state when the user cancels the
23     * {@link JDialog}.
24     *
25     * Pressing ESC or clicking the "Close X" results in asking the user whether to Save/Cancel/Abort .  
26     */
27  public abstract class CancellableDialogAdapter extends JDialog implements  public abstract class CancellableDialogAdapter extends JDialog implements
28                  CancellableDialog {                  CancellableDialog {
29    
# Line 17  public abstract class CancellableDialogA Line 34  public abstract class CancellableDialogA
34                  return cancelled;                  return cancelled;
35          }          }
36    
37          public CancellableDialogAdapter(final Window parentWindow) {          public CancellableDialogAdapter(final Component parentWindow, String title) {
38                  super(parentWindow);                  super(SwingUtil.getParentWindow(parentWindow), title);
39                  initDialog();                  initDialog();
40          }          }
41    
42            public CancellableDialogAdapter(final Component parentWindow) {
43                    this(parentWindow, null);
44            }
45    
46          private void initDialog() {          private void initDialog() {
47    
48                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
# Line 30  public abstract class CancellableDialogA Line 51  public abstract class CancellableDialogA
51    
52                          @Override                          @Override
53                          public void windowClosing(WindowEvent e) {                          public void windowClosing(WindowEvent e) {
54                                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);                                  close();
                                   
                                 int showConfirmDialog = JOptionPane.showConfirmDialog(  
                                                 CancellableDialogAdapter.this, "Speichern?",  
                                                 "Änderungen speichern?", JOptionPane.YES_NO_CANCEL_OPTION); // i8n  
   
                                 if (showConfirmDialog == JOptionPane.YES_OPTION)  
                                         okClose();  
                                 else if (showConfirmDialog == JOptionPane.NO_OPTION)  
                                         cancelClose();  
55                          }                          }
56    
57                  });                  });
58          }          }
59    
60          public CancellableDialogAdapter(final Window parentWindow, String title) {          /**
61                  super(parentWindow, title);           * Allows to close the {@link JDialog} from "outside". The user will be
62                  initDialog();           * asked and she may cancel the close process.
63             */
64            public boolean close() {
65                    int showConfirmDialog = JOptionPane.showConfirmDialog(
66                                    CancellableDialogAdapter.this, "Speichern?",
67                                    "Änderungen speichern?", JOptionPane.YES_NO_CANCEL_OPTION); // i8n
68    
69                    if (showConfirmDialog == JOptionPane.YES_OPTION) {
70                            return okClose();
71                    }
72                    else if (showConfirmDialog == JOptionPane.NO_OPTION) {
73                            cancelClose();
74                            return true;
75                    }
76                    
77                    return false;
78          }          }
79            
80    
81            /**
82             * This method can be called from "outside" to force a close of this
83             * {@link Dialog}. The user can't cancel the process. He may only decide to
84             * save possible changes.
85             */
86            public void forceClose() {
87    
88                    int res = JOptionPane.showConfirmDialog(
89                                    CancellableDialogAdapter.this, "Speichern?",
90                                    "Der Dialog muss nu gesclossen werden. Änderungen speichern?", JOptionPane.YES_NO_OPTION); // i8n
91                    
92                    if (res == JOptionPane.YES_OPTION) {
93                            okClose();
94                    } else {
95                            cancelClose();
96                    }
97            }
98            
99    
100            /**
101             * Default implementation that calls {@link #cancel()} and
102             * {@link #dispose()}.
103             */
104          @Override          @Override
105          public void cancelClose() {          public void cancelClose() {
106                  cancel();                  cancel();
# Line 62  public abstract class CancellableDialogA Line 113  public abstract class CancellableDialogA
113          /**          /**
114           * This method is called when the dialog is closed and not canceled. Can be           * This method is called when the dialog is closed and not canceled. Can be
115           * overwritten to do anything when the dialog has been accepted. For example           * overwritten to do anything when the dialog has been accepted. For example
116           * cheking for any {@link Checkable} components. Returns false, if the ok           * checking for any {@link Checkable} components. <br/>
117           * has been vetoed.           *
118             * @Return <code>false</code>, if the ok has been vetoed.
119           */           */
120          public abstract boolean okClose();          public boolean okClose() {
121                    dispose();
122                    return true;
123            }
124            
125    
126            /**
127             * Since the registerKeyboardAction() method is part of the JComponent class
128             * definition, you must define the Escape keystroke and register the
129             * keyboard action with a JComponent, not with a JDialog. The JRootPane for
130             * the JDialog serves as an excellent choice to associate the registration,
131             * as this will always be visible. If you override the protected
132             * createRootPane() method of JDialog, you can return your custom JRootPane
133             * with the keystroke enabled:
134             */
135            @Override
136            protected JRootPane createRootPane() {
137                    final KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
138                    final JRootPane rootPane = new JRootPane();
139                    rootPane.registerKeyboardAction(new ActionListener() {
140    
141                            public void actionPerformed(final ActionEvent e) {
142                                    close();
143                            }
144    
145                    }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
146    
147                    return rootPane;
148            }
149    
150    
151  }  }

Legend:
Removed from v.420  
changed lines
  Added in v.433

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26