/[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

branches/1.0-gt2-2.6/src/skrueger/swing/CancellableDialogAdapter.java revision 433 by alfonx, Sun Oct 4 16:57:29 2009 UTC branches/2.0-RC1/src/skrueger/swing/CancellableDialogAdapter.java revision 607 by alfonx, Wed Dec 9 15:13:42 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;
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.lang.LangUtil;
11    import schmitzm.lang.ResourceProvider;
12  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
13    
14  /**  /**
# Line 24  import schmitzm.swing.SwingUtil; Line 19  import schmitzm.swing.SwingUtil;
19   *   *
20   * 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 .  
21   */   */
22  public abstract class CancellableDialogAdapter extends JDialog implements  public abstract class CancellableDialogAdapter extends AtlasDialog implements
23                  CancellableDialog {                  CancellableDialog {
24            
25            protected static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
26                            .extendPackagePath(SwingUtil.class,
27                                            "resource.locales.SwingResourceBundle"), Locale.ENGLISH);
28    
         protected boolean cancelled = false;  
   
         @Override  
         public boolean isCancelled() {  
                 return cancelled;  
         }  
29    
30          public CancellableDialogAdapter(final Component parentWindow, String title) {          /** Has this dialog been canceled ?**/
31                  super(SwingUtil.getParentWindow(parentWindow), title);          protected boolean cancelled = false;
32                  initDialog();          
33            public CancellableDialogAdapter(Component parentWindow) {
34                    super(parentWindow);
35          }          }
36    
37          public CancellableDialogAdapter(final Component parentWindow) {          public CancellableDialogAdapter(Component parentWindow, String title) {
38                  this(parentWindow, null);                  super(parentWindow, title);
39          }          }
40    
41          private void initDialog() {          @Override
42            public boolean isCancelled() {
43                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);                  return cancelled;
   
                 addWindowListener(new WindowAdapter() {  
   
                         @Override  
                         public void windowClosing(WindowEvent e) {  
                                 close();  
                         }  
   
                 });  
44          }          }
45    
46          /**          /**
# Line 63  public abstract class CancellableDialogA Line 49  public abstract class CancellableDialogA
49           */           */
50          public boolean close() {          public boolean close() {
51                  int showConfirmDialog = JOptionPane.showConfirmDialog(                  int showConfirmDialog = JOptionPane.showConfirmDialog(
52                                  CancellableDialogAdapter.this, "Speichern?",                                  CancellableDialogAdapter.this,
53                                  "Ă„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);
54    
55                  if (showConfirmDialog == JOptionPane.YES_OPTION) {                  if (showConfirmDialog == JOptionPane.YES_OPTION) {
56                          return okClose();                          return okClose();
# Line 86  public abstract class CancellableDialogA Line 72  public abstract class CancellableDialogA
72          public void forceClose() {          public void forceClose() {
73    
74                  int res = JOptionPane.showConfirmDialog(                  int res = JOptionPane.showConfirmDialog(
75                                  CancellableDialogAdapter.this, "Speichern?",                                  CancellableDialogAdapter.this,
76                                  "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);
77                                    
78                  if (res == JOptionPane.YES_OPTION) {                  if (res == JOptionPane.YES_OPTION) {
79                          okClose();                          okClose();
# Line 123  public abstract class CancellableDialogA Line 109  public abstract class CancellableDialogA
109          }          }
110                    
111    
         /**  
          * 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;  
         }  
   
112    
113  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26