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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26