/[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 433 by alfonx, Sun Oct 4 16:57:29 2009 UTC revision 461 by alfonx, Tue Oct 13 09:22:23 2009 UTC
# Line 2  package skrueger.swing; Line 2  package skrueger.swing;
2    
3  import java.awt.Component;  import java.awt.Component;
4  import java.awt.Dialog;  import java.awt.Dialog;
 import java.awt.event.ActionEvent;  
 import java.awt.event.ActionListener;  
 import java.awt.event.KeyEvent;  
 import java.awt.event.WindowAdapter;  
 import java.awt.event.WindowEvent;  
5    
 import javax.swing.JComponent;  
6  import javax.swing.JDialog;  import javax.swing.JDialog;
7  import javax.swing.JOptionPane;  import javax.swing.JOptionPane;
 import javax.swing.JRootPane;  
 import javax.swing.KeyStroke;  
   
 import schmitzm.swing.SwingUtil;  
8    
9  /**  /**
10   * An abstract {@link JDialog} that implements a basic structure of how   * An abstract {@link JDialog} that implements a basic structure of how
# Line 24  import schmitzm.swing.SwingUtil; Line 14  import schmitzm.swing.SwingUtil;
14   *   *
15   * 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 .  
16   */   */
17  public abstract class CancellableDialogAdapter extends JDialog implements  public abstract class CancellableDialogAdapter extends AtlasDialog implements
18                  CancellableDialog {                  CancellableDialog {
19    
20            /** Has this dialog been canceled ?**/
21          protected boolean cancelled = false;          protected boolean cancelled = false;
22            
23          @Override          public CancellableDialogAdapter(Component parentWindow) {
24          public boolean isCancelled() {                  super(parentWindow);
                 return cancelled;  
         }  
   
         public CancellableDialogAdapter(final Component parentWindow, String title) {  
                 super(SwingUtil.getParentWindow(parentWindow), title);  
                 initDialog();  
25          }          }
26    
27          public CancellableDialogAdapter(final Component parentWindow) {          public CancellableDialogAdapter(Component parentWindow, String title) {
28                  this(parentWindow, null);                  super(parentWindow, title);
29          }          }
30    
31          private void initDialog() {          @Override
32            public boolean isCancelled() {
33                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);                  return cancelled;
   
                 addWindowListener(new WindowAdapter() {  
   
                         @Override  
                         public void windowClosing(WindowEvent e) {  
                                 close();  
                         }  
   
                 });  
34          }          }
35    
36          /**          /**
# Line 123  public abstract class CancellableDialogA Line 99  public abstract class CancellableDialogA
99          }          }
100                    
101    
         /**  
          * 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;  
         }  
   
102    
103  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26