/[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/2.0-RC1/src/skrueger/swing/CancellableDialogAdapter.java revision 604 by alfonx, Wed Dec 9 14:15:53 2009 UTC trunk/src/skrueger/swing/CancellableDialogAdapter.java revision 1267 by alfonx, Sat Nov 13 01:11:10 2010 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;
6  import java.util.Locale;  import java.util.Locale;
7    
8    import javax.swing.AbstractAction;
9  import javax.swing.JDialog;  import javax.swing.JDialog;
10  import javax.swing.JOptionPane;  import javax.swing.JOptionPane;
11    
 import schmitzm.geotools.gui.GeotoolsGUIUtil;  
12  import schmitzm.lang.LangUtil;  import schmitzm.lang.LangUtil;
13  import schmitzm.lang.ResourceProvider;  import schmitzm.lang.ResourceProvider;
14  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
# Line 18  import schmitzm.swing.SwingUtil; Line 19  import schmitzm.swing.SwingUtil;
19   * the real object and restore it's state when the user cancels the   * the real object and restore it's state when the user cancels the
20   * {@link JDialog}.   * {@link JDialog}.
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
23     * Save/Cancel/Abort .
24   */   */
25  public abstract class CancellableDialogAdapter extends AtlasDialog implements  public abstract class CancellableDialogAdapter extends AtlasDialog implements
26                  CancellableDialog {                  CancellableDialog {
           
         protected static ResourceProvider RESOURCE = new ResourceProvider(LangUtil  
                         .extendPackagePath(SwingUtil.class,  
                                         "resource.locales.SwingResourceBundle"), Locale.ENGLISH);  
27    
28            protected static ResourceProvider RESOURCE = ResourceProvider.newInstance(
29                            LangUtil.extendPackagePath(SwingUtil.class,
30                                            "resource.locales.SwingResourceBundle"), Locale.ENGLISH);
31    
32          /** Has this dialog been canceled ?**/          /** Has this dialog been canceled ? **/
33          protected boolean cancelled = false;          protected boolean cancelled = false;
34            
35            private OkButton okButton;
36    
37            private CancelButton cancelButton;
38    
39          public CancellableDialogAdapter(Component parentWindow) {          public CancellableDialogAdapter(Component parentWindow) {
40                  super(parentWindow);                  super(parentWindow);
41          }          }
# Line 48  public abstract class CancellableDialogA Line 53  public abstract class CancellableDialogA
53           * Allows to close the {@link JDialog} from "outside". The user will be           * Allows to close the {@link JDialog} from "outside". The user will be
54           * asked and she may cancel the close process.           * asked and she may cancel the close process.
55           */           */
56            @Override
57          public boolean close() {          public boolean close() {
58                  int showConfirmDialog = JOptionPane.showConfirmDialog(                  int showConfirmDialog = JOptionPane
59                                  CancellableDialogAdapter.this,                                  .showConfirmDialog(
60                                  RESOURCE.getString("CancellableDialogAdapter.close.save.msg",getTitle()), RESOURCE.getString("CancellableDialogAdapter.close.save.title"), JOptionPane.YES_NO_CANCEL_OPTION);                                                  CancellableDialogAdapter.this,
61                                                    RESOURCE.getString(
62                                                                    "CancellableDialogAdapter.close.save.msg",
63                                                                    getTitle()),
64                                                    RESOURCE.getString("CancellableDialogAdapter.close.save.title"),
65                                                    JOptionPane.YES_NO_CANCEL_OPTION);
66    
67                  if (showConfirmDialog == JOptionPane.YES_OPTION) {                  if (showConfirmDialog == JOptionPane.YES_OPTION) {
68                          return okClose();                          return okClose();
69                  }                  } else if (showConfirmDialog == JOptionPane.NO_OPTION) {
                 else if (showConfirmDialog == JOptionPane.NO_OPTION) {  
70                          cancelClose();                          cancelClose();
71                          return true;                          return true;
72                  }                  }
73                    
74                  return false;                  return false;
75          }          }
           
76    
77          /**          /**
78           * This method can be called from "outside" to force a close of this           * This method can be called from "outside" to force a close of this
# Line 72  public abstract class CancellableDialogA Line 81  public abstract class CancellableDialogA
81           */           */
82          public void forceClose() {          public void forceClose() {
83    
84                  int res = JOptionPane.showConfirmDialog(                  int res = JOptionPane
85                                  CancellableDialogAdapter.this,                                  .showConfirmDialog(
86                                  RESOURCE.getString("CancellableDialogAdapter.forceClose.save.msg",getTitle()), RESOURCE.getString("CancellableDialogAdapter.close.save.title"), JOptionPane.YES_NO_OPTION);                                                  CancellableDialogAdapter.this,
87                                                                    RESOURCE.getString(
88                                                                    "CancellableDialogAdapter.forceClose.save.msg",
89                                                                    getTitle()),
90                                                    RESOURCE.getString("CancellableDialogAdapter.close.save.title"),
91                                                    JOptionPane.YES_NO_OPTION);
92    
93                  if (res == JOptionPane.YES_OPTION) {                  if (res == JOptionPane.YES_OPTION) {
94                          okClose();                          okClose();
95                  } else {                  } else {
96                          cancelClose();                          cancelClose();
97                  }                  }
98          }          }
           
99    
100          /**          /**
101           * Default implementation that calls {@link #cancel()} and           * Default implementation that calls {@link #cancel()} and
# Line 91  public abstract class CancellableDialogA Line 104  public abstract class CancellableDialogA
104          @Override          @Override
105          public void cancelClose() {          public void cancelClose() {
106                  cancel();                  cancel();
107                    cancelled = true;
108                  dispose();                  dispose();
109          }          }
110    
# Line 101  public abstract class CancellableDialogA Line 115  public abstract class CancellableDialogA
115           * 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
116           * overwritten to do anything when the dialog has been accepted. For example           * overwritten to do anything when the dialog has been accepted. For example
117           * checking for any {@link Checkable} components. <br/>           * checking for any {@link Checkable} components. <br/>
118             * Call super.okClose() at the end.
119           *           *
120           * @Return <code>false</code>, if the ok has been vetoed.           * @Return <code>false</code>, if the ok has been vetoed.
121           */           */
# Line 108  public abstract class CancellableDialogA Line 123  public abstract class CancellableDialogA
123                  dispose();                  dispose();
124                  return true;                  return true;
125          }          }
           
126    
127            /**
128             * @return a default OkButton that will call {@link #okClose()}
129             */
130            protected OkButton getOkButton() {
131                    if (okButton == null) {
132                            okButton = new OkButton(new AbstractAction() {
133    
134                                    @Override
135                                    public void actionPerformed(ActionEvent e) {
136                                            okClose();
137                                    }
138                            });
139                    }
140                    return okButton;
141            }
142    
143            /**
144             * @return a default CancelButton that will call {@link #cancelClose()}
145             */
146            protected CancelButton getCancelButton() {
147                    if (cancelButton == null) {
148                            cancelButton = new CancelButton(new AbstractAction() {
149    
150                                    @Override
151                                    public void actionPerformed(ActionEvent e) {
152                                            cancelClose();
153                                    }
154                            });
155                    }
156                    return cancelButton;
157            }
158    
159  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26