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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26