/[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 422 by alfonx, Fri Oct 2 00:47:48 2009 UTC trunk/src/skrueger/swing/CancellableDialogAdapter.java revision 1270 by alfonx, Sun Nov 14 00:25:52 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.WindowAdapter;  import java.awt.event.ActionEvent;
6  import java.awt.event.WindowEvent;  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    
12    import schmitzm.lang.LangUtil;
13    import schmitzm.lang.ResourceProvider;
14  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
15    
16  /**  /**
# Line 15  import schmitzm.swing.SwingUtil; Line 18  import schmitzm.swing.SwingUtil;
18   * cancellable {@link JDialog}s work. The {@link JDialog} is designed to work on   * cancellable {@link JDialog}s work. The {@link JDialog} is designed to work on
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
23     * Save/Cancel/Abort .
24   */   */
25  public abstract class CancellableDialogAdapter extends JDialog implements  public abstract class CancellableDialogAdapter extends AtlasDialog implements
26                  CancellableDialog {                  CancellableDialog {
27    
28          protected boolean cancelled = false;          protected static ResourceProvider RESOURCE = ResourceProvider.newInstance(
29                            LangUtil.extendPackagePath(SwingUtil.class,
30                                            "resource.locales.SwingResourceBundle"), Locale.ENGLISH);
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) {          public CancellableDialogAdapter(Component parentWindow) {
36                  super(SwingUtil.getParentWindow(parentWindow), title);                  super(parentWindow);
                 initDialog();  
37          }          }
38    
39          public CancellableDialogAdapter(final Component parentWindow) {          public CancellableDialogAdapter(Component parentWindow, String title) {
40                  this(parentWindow, null);                  super(parentWindow, title);
41          }          }
42    
43          private void initDialog() {          @Override
44            public boolean isCancelled() {
45                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);                  return cancelled;
   
                 addWindowListener(new WindowAdapter() {  
   
                         @Override  
                         public void windowClosing(WindowEvent e) {  
                                 close();  
                         }  
   
                 });  
46          }          }
47    
48          /**          /**
49           * Allows to close the {@link JDialog} from "outside". The user will be           * Allows to close the {@link JDialog} from "outside". The user will be
50           * asked and she may cancel the close process.           * asked and she may cancel the close process.
51           */           */
52            @Override
53          public boolean close() {          public boolean close() {
54                  int showConfirmDialog = JOptionPane.showConfirmDialog(                  int showConfirmDialog = JOptionPane
55                                  CancellableDialogAdapter.this, "Speichern?",                                  .showConfirmDialog(
56                                  "Ă„nderungen speichern?", JOptionPane.YES_NO_CANCEL_OPTION); // i8n                                                  CancellableDialogAdapter.this,
57                                                    RESOURCE.getString(
58                                                                    "CancellableDialogAdapter.close.save.msg",
59                                                                    getTitle()),
60                                                    RESOURCE.getString("CancellableDialogAdapter.close.save.title"),
61                                                    JOptionPane.YES_NO_CANCEL_OPTION);
62    
63                  if (showConfirmDialog == JOptionPane.YES_OPTION) {                  if (showConfirmDialog == JOptionPane.YES_OPTION) {
64                          return okClose();                          return okClose();
65                  }                  } else if (showConfirmDialog == JOptionPane.NO_OPTION) {
                 else if (showConfirmDialog == JOptionPane.NO_OPTION) {  
66                          cancelClose();                          cancelClose();
67                          return true;                          return true;
68                  }                  }
69                    
70                  return false;                  return false;
71          }          }
           
72    
73          /**          /**
74           * 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 77  public abstract class CancellableDialogA Line 77  public abstract class CancellableDialogA
77           */           */
78          public void forceClose() {          public void forceClose() {
79    
80                  int res = JOptionPane.showConfirmDialog(                  int res = JOptionPane
81                                  CancellableDialogAdapter.this, "Speichern?",                                  .showConfirmDialog(
82                                  "Der Dialog muss nu gesclossen werden. Ă„nderungen speichern?", JOptionPane.YES_NO_OPTION); // i8n                                                  CancellableDialogAdapter.this,
83                                                                    RESOURCE.getString(
84                                                                    "CancellableDialogAdapter.forceClose.save.msg",
85                                                                    getTitle()),
86                                                    RESOURCE.getString("CancellableDialogAdapter.close.save.title"),
87                                                    JOptionPane.YES_NO_OPTION);
88    
89                  if (res == JOptionPane.YES_OPTION) {                  if (res == JOptionPane.YES_OPTION) {
90                          okClose();                          okClose();
91                  } else {                  } else {
92                          cancelClose();                          cancelClose();
93                  }                  }
94          }          }
           
95    
96          /**          /**
97           * Default implementation that calls {@link #cancel()} and           * Default implementation that calls {@link #cancel()} and
# Line 96  public abstract class CancellableDialogA Line 100  public abstract class CancellableDialogA
100          @Override          @Override
101          public void cancelClose() {          public void cancelClose() {
102                  cancel();                  cancel();
103                    cancelled = true;
104                  dispose();                  dispose();
105          }          }
106    
# Line 106  public abstract class CancellableDialogA Line 111  public abstract class CancellableDialogA
111           * 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
112           * overwritten to do anything when the dialog has been accepted. For example           * overwritten to do anything when the dialog has been accepted. For example
113           * checking for any {@link Checkable} components. <br/>           * checking for any {@link Checkable} components. <br/>
114             * Call super.okClose() at the end.
115           *           *
116           * @Return <code>false</code>, if the ok has been vetoed.           * @Return <code>false</code>, if the ok has been vetoed.
117           */           */
# Line 114  public abstract class CancellableDialogA Line 120  public abstract class CancellableDialogA
120                  return true;                  return true;
121          }          }
122    
123            /**
124             * @return a default OkButton that will call {@link #okClose()}
125             */
126            @Override
127            protected OkButton getOkButton() {
128                    if (okButton == null) {
129                            okButton = new OkButton(new AbstractAction() {
130    
131                                    @Override
132                                    public void actionPerformed(ActionEvent e) {
133                                            okClose();
134                                    }
135                            });
136                    }
137                    return okButton;
138            }
139    
140            /**
141             * @return a default CancelButton that will call {@link #cancelClose()}
142             */
143            @Override
144            protected CancelButton getCancelButton() {
145                    if (cancelButton == null) {
146                            cancelButton = new CancelButton(new AbstractAction() {
147    
148                                    @Override
149                                    public void actionPerformed(ActionEvent e) {
150                                            cancelClose();
151                                    }
152                            });
153                    }
154                    return cancelButton;
155            }
156    
157  }  }

Legend:
Removed from v.422  
changed lines
  Added in v.1270

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26