/[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 420 by alfonx, Thu Oct 1 20:22:48 2009 UTC branches/2.0-RC1/src/skrueger/swing/CancellableDialogAdapter.java revision 607 by alfonx, Wed Dec 9 15:13:42 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.event.WindowAdapter;  import java.awt.Dialog;
5  import java.awt.event.WindowEvent;  import java.util.Locale;
6    
7  import javax.swing.JDialog;  import javax.swing.JDialog;
8  import javax.swing.JOptionPane;  import javax.swing.JOptionPane;
9    
10  public abstract class CancellableDialogAdapter extends JDialog implements  import schmitzm.lang.LangUtil;
11    import schmitzm.lang.ResourceProvider;
12    import schmitzm.swing.SwingUtil;
13    
14    /**
15     * An abstract {@link JDialog} that implements a basic structure of how
16     * cancellable {@link JDialog}s work. The {@link JDialog} is designed to work on
17     * the real object and restore it's state when the user cancels the
18     * {@link JDialog}.
19     *
20     * Pressing ESC or clicking the "Close X" results in asking the user whether to Save/Cancel/Abort .  
21     */
22    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    
29    
30            /** Has this dialog been canceled ?**/
31          protected boolean cancelled = false;          protected boolean cancelled = false;
32            
33            public CancellableDialogAdapter(Component parentWindow) {
34                    super(parentWindow);
35            }
36    
37            public CancellableDialogAdapter(Component parentWindow, String title) {
38                    super(parentWindow, title);
39            }
40    
41          @Override          @Override
42          public boolean isCancelled() {          public boolean isCancelled() {
43                  return cancelled;                  return cancelled;
44          }          }
45    
46          public CancellableDialogAdapter(final Window parentWindow) {          /**
47                  super(parentWindow);           * Allows to close the {@link JDialog} from "outside". The user will be
48                  initDialog();           * asked and she may cancel the close process.
49             */
50            public boolean close() {
51                    int showConfirmDialog = JOptionPane.showConfirmDialog(
52                                    CancellableDialogAdapter.this,
53                                    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) {
56                            return okClose();
57                    }
58                    else if (showConfirmDialog == JOptionPane.NO_OPTION) {
59                            cancelClose();
60                            return true;
61                    }
62                    
63                    return false;
64          }          }
65            
66    
67          private void initDialog() {          /**
68             * This method can be called from "outside" to force a close of this
69                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);           * {@link Dialog}. The user can't cancel the process. He may only decide to
70             * save possible changes.
71                  addWindowListener(new WindowAdapter() {           */
72            public void forceClose() {
                         @Override  
                         public void windowClosing(WindowEvent e) {  
                                 setDefaultCloseOperation(JDialog.DO_NOTHING_ON_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();  
                         }  
   
                 });  
         }  
73    
74          public CancellableDialogAdapter(final Window parentWindow, String title) {                  int res = JOptionPane.showConfirmDialog(
75                  super(parentWindow, title);                                  CancellableDialogAdapter.this,
76                  initDialog();                                  RESOURCE.getString("CancellableDialogAdapter.forceClose.save.msg",getTitle()), RESOURCE.getString("CancellableDialogAdapter.close.save.title"), JOptionPane.YES_NO_OPTION);
77                    
78                    if (res == JOptionPane.YES_OPTION) {
79                            okClose();
80                    } else {
81                            cancelClose();
82                    }
83          }          }
84            
85    
86            /**
87             * Default implementation that calls {@link #cancel()} and
88             * {@link #dispose()}.
89             */
90          @Override          @Override
91          public void cancelClose() {          public void cancelClose() {
92                  cancel();                  cancel();
# Line 62  public abstract class CancellableDialogA Line 99  public abstract class CancellableDialogA
99          /**          /**
100           * 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
101           * overwritten to do anything when the dialog has been accepted. For example           * overwritten to do anything when the dialog has been accepted. For example
102           * cheking for any {@link Checkable} components. Returns false, if the ok           * checking for any {@link Checkable} components. <br/>
103           * has been vetoed.           *
104             * @Return <code>false</code>, if the ok has been vetoed.
105           */           */
106          public abstract boolean okClose();          public boolean okClose() {
107                    dispose();
108                    return true;
109            }
110            
111    
112    
113  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26