/[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 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.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 static ResourceProvider RESOURCE = ResourceProvider.newInstance(
29                            LangUtil.extendPackagePath(SwingUtil.class,
30                                            "resource.locales.SwingResourceBundle"), Locale.ENGLISH);
31    
32            /** Has this dialog been canceled ? **/
33          protected boolean cancelled = false;          protected boolean cancelled = false;
34    
35          @Override          private OkButton okButton;
         public boolean isCancelled() {  
                 return cancelled;  
         }  
36    
37          public CancellableDialogAdapter(final Component parentWindow, String title) {          private CancelButton cancelButton;
                 super(SwingUtil.getParentWindow(parentWindow), title);  
                 initDialog();  
         }  
38    
39          public CancellableDialogAdapter(final Component parentWindow) {          public CancellableDialogAdapter(Component parentWindow) {
40                  this(parentWindow, null);                  super(parentWindow);
41          }          }
42    
43          private void initDialog() {          public CancellableDialogAdapter(Component parentWindow, String title) {
44                    super(parentWindow, title);
45                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);          }
   
                 addWindowListener(new WindowAdapter() {  
   
                         @Override  
                         public void windowClosing(WindowEvent e) {  
                                 close();  
                         }  
46    
47                  });          @Override
48            public boolean isCancelled() {
49                    return cancelled;
50          }          }
51    
52          /**          /**
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, "Speichern?",                                  .showConfirmDialog(
60                                  "Ă„nderungen speichern?", JOptionPane.YES_NO_CANCEL_OPTION); // i8n                                                  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 77  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, "Speichern?",                                  .showConfirmDialog(
86                                  "Der Dialog muss nu gesclossen werden. Ă„nderungen speichern?", JOptionPane.YES_NO_OPTION); // i8n                                                  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 96  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 106  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 114  public abstract class CancellableDialogA Line 124  public abstract class CancellableDialogA
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.422  
changed lines
  Added in v.1267

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26