/[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 trunk/src/skrueger/swing/CancellableDialogAdapter.java revision 863 by alfonx, Sun May 23 13:42:13 2010 UTC
# Line 3  package skrueger.swing; Line 3  package skrueger.swing;
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.awt.event.ActionEvent;
6  import java.awt.event.ActionListener;  import java.util.Locale;
 import java.awt.event.KeyEvent;  
 import java.awt.event.WindowAdapter;  
 import java.awt.event.WindowEvent;  
7    
8  import javax.swing.JComponent;  import javax.swing.AbstractAction;
9  import javax.swing.JDialog;  import javax.swing.JDialog;
10  import javax.swing.JOptionPane;  import javax.swing.JOptionPane;
 import javax.swing.JRootPane;  
 import javax.swing.KeyStroke;  
11    
12    import schmitzm.lang.LangUtil;
13    import schmitzm.lang.ResourceProvider;
14  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
15    
16  /**  /**
# Line 24  import schmitzm.swing.SwingUtil; Line 21  import schmitzm.swing.SwingUtil;
21   *   *
22   * 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 .  
23   */   */
24  public abstract class CancellableDialogAdapter extends JDialog implements  public abstract class CancellableDialogAdapter extends AtlasDialog implements
25                  CancellableDialog {                  CancellableDialog {
26            
27            protected static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
28                            .extendPackagePath(SwingUtil.class,
29                                            "resource.locales.SwingResourceBundle"), Locale.ENGLISH);
30    
         protected boolean cancelled = false;  
31    
32          @Override          /** Has this dialog been canceled ?**/
33          public boolean isCancelled() {          protected boolean cancelled = false;
                 return cancelled;  
         }  
34    
35          public CancellableDialogAdapter(final Component parentWindow, String title) {          private OkButton okButton;
36                  super(SwingUtil.getParentWindow(parentWindow), title);          
37                  initDialog();          private CancelButton cancelButton;
38            
39            public CancellableDialogAdapter(Component parentWindow) {
40                    super(parentWindow);
41          }          }
42    
43          public CancellableDialogAdapter(final Component parentWindow) {          public CancellableDialogAdapter(Component parentWindow, String title) {
44                  this(parentWindow, null);                  super(parentWindow, title);
45          }          }
46    
47          private void initDialog() {          @Override
48            public boolean isCancelled() {
49                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);                  return cancelled;
   
                 addWindowListener(new WindowAdapter() {  
   
                         @Override  
                         public void windowClosing(WindowEvent e) {  
                                 close();  
                         }  
   
                 });  
50          }          }
51    
52          /**          /**
# Line 63  public abstract class CancellableDialogA Line 55  public abstract class CancellableDialogA
55           */           */
56          public boolean close() {          public boolean close() {
57                  int showConfirmDialog = JOptionPane.showConfirmDialog(                  int showConfirmDialog = JOptionPane.showConfirmDialog(
58                                  CancellableDialogAdapter.this, "Speichern?",                                  CancellableDialogAdapter.this,
59                                  "Ă„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);
60    
61                  if (showConfirmDialog == JOptionPane.YES_OPTION) {                  if (showConfirmDialog == JOptionPane.YES_OPTION) {
62                          return okClose();                          return okClose();
# Line 86  public abstract class CancellableDialogA Line 78  public abstract class CancellableDialogA
78          public void forceClose() {          public void forceClose() {
79    
80                  int res = JOptionPane.showConfirmDialog(                  int res = JOptionPane.showConfirmDialog(
81                                  CancellableDialogAdapter.this, "Speichern?",                                  CancellableDialogAdapter.this,
82                                  "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);
83                                    
84                  if (res == JOptionPane.YES_OPTION) {                  if (res == JOptionPane.YES_OPTION) {
85                          okClose();                          okClose();
# Line 104  public abstract class CancellableDialogA Line 96  public abstract class CancellableDialogA
96          @Override          @Override
97          public void cancelClose() {          public void cancelClose() {
98                  cancel();                  cancel();
99                    cancelled = true;
100                  dispose();                  dispose();
101          }          }
102    
# Line 121  public abstract class CancellableDialogA Line 114  public abstract class CancellableDialogA
114                  dispose();                  dispose();
115                  return true;                  return true;
116          }          }
           
117    
118          /**          /**
119           * Since the registerKeyboardAction() method is part of the JComponent class           * @return a default OkButton that will call {@link #okClose()}
          * 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:  
120           */           */
121          @Override          protected OkButton getOkButton() {
122          protected JRootPane createRootPane() {                  if (okButton == null) {
123                  final KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);                          okButton = new OkButton(new AbstractAction() {
124                  final JRootPane rootPane = new JRootPane();                                  
125                  rootPane.registerKeyboardAction(new ActionListener() {                                  @Override
126                                    public void actionPerformed(ActionEvent e) {
127                          public void actionPerformed(final ActionEvent e) {                                          okClose();
128                                  close();                                  }
129                          }                          });
130                    }
131                  }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);                  return okButton;
   
                 return rootPane;  
132          }          }
133            
134    
135            /**
136             * @return a default CancelButton that will call {@link #cancelClose()}
137             */
138            protected CancelButton getCancelButton() {
139                    if (cancelButton == null) {
140                            cancelButton = new CancelButton(new AbstractAction() {
141                                    
142                                    @Override
143                                    public void actionPerformed(ActionEvent e) {
144                                            cancelClose();
145                                    }
146                            });
147                    }
148                    return cancelButton;
149            }
150    
151  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26