/[schmitzm]/trunk/src/skrueger/swing/CancellableDialogAdapter.java
ViewVC logotype

Annotation of /trunk/src/skrueger/swing/CancellableDialogAdapter.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 707 - (hide annotations)
Mon Feb 15 11:07:30 2010 UTC (15 years ago) by alfonx
File MIME type: text/plain
File size: 3786 byte(s)
* Atlas charts do no longer show units if normalization is enabled
* Improvements in AtlasStyler  towards labeling classes, NODATA values and MigLayout
1 alfonx 420 package skrueger.swing;
2    
3 alfonx 422 import java.awt.Component;
4     import java.awt.Dialog;
5 alfonx 661 import java.awt.event.ActionEvent;
6 alfonx 497 import java.util.Locale;
7 alfonx 420
8 alfonx 661 import javax.swing.AbstractAction;
9 alfonx 420 import javax.swing.JDialog;
10     import javax.swing.JOptionPane;
11    
12 alfonx 497 import schmitzm.lang.LangUtil;
13     import schmitzm.lang.ResourceProvider;
14 alfonx 520 import schmitzm.swing.SwingUtil;
15 alfonx 497
16 alfonx 422 /**
17     * An abstract {@link JDialog} that implements a basic structure of how
18     * 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
20     * {@link JDialog}.
21 alfonx 433 *
22     * Pressing ESC or clicking the "Close X" results in asking the user whether to Save/Cancel/Abort .
23 alfonx 422 */
24 alfonx 461 public abstract class CancellableDialogAdapter extends AtlasDialog implements
25 alfonx 420 CancellableDialog {
26 alfonx 497
27     protected static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
28 alfonx 520 .extendPackagePath(SwingUtil.class,
29     "resource.locales.SwingResourceBundle"), Locale.ENGLISH);
30 alfonx 497
31 alfonx 520
32 alfonx 461 /** Has this dialog been canceled ?**/
33 alfonx 420 protected boolean cancelled = false;
34 alfonx 661
35     private OkButton okButton;
36 alfonx 707
37 alfonx 661 private CancelButton cancelButton;
38 alfonx 461
39     public CancellableDialogAdapter(Component parentWindow) {
40     super(parentWindow);
41     }
42 alfonx 420
43 alfonx 461 public CancellableDialogAdapter(Component parentWindow, String title) {
44     super(parentWindow, title);
45     }
46    
47 alfonx 420 @Override
48     public boolean isCancelled() {
49     return cancelled;
50     }
51    
52 alfonx 422 /**
53     * Allows to close the {@link JDialog} from "outside". The user will be
54     * asked and she may cancel the close process.
55     */
56     public boolean close() {
57     int showConfirmDialog = JOptionPane.showConfirmDialog(
58 alfonx 497 CancellableDialogAdapter.this,
59     RESOURCE.getString("CancellableDialogAdapter.close.save.msg",getTitle()), RESOURCE.getString("CancellableDialogAdapter.close.save.title"), JOptionPane.YES_NO_CANCEL_OPTION);
60 alfonx 422
61     if (showConfirmDialog == JOptionPane.YES_OPTION) {
62     return okClose();
63     }
64     else if (showConfirmDialog == JOptionPane.NO_OPTION) {
65     cancelClose();
66     return true;
67     }
68    
69     return false;
70 alfonx 420 }
71 alfonx 422
72 alfonx 420
73 alfonx 422 /**
74     * This method can be called from "outside" to force a close of this
75     * {@link Dialog}. The user can't cancel the process. He may only decide to
76     * save possible changes.
77     */
78     public void forceClose() {
79    
80     int res = JOptionPane.showConfirmDialog(
81 alfonx 497 CancellableDialogAdapter.this,
82     RESOURCE.getString("CancellableDialogAdapter.forceClose.save.msg",getTitle()), RESOURCE.getString("CancellableDialogAdapter.close.save.title"), JOptionPane.YES_NO_OPTION);
83 alfonx 422
84     if (res == JOptionPane.YES_OPTION) {
85     okClose();
86     } else {
87     cancelClose();
88     }
89     }
90    
91    
92     /**
93     * Default implementation that calls {@link #cancel()} and
94     * {@link #dispose()}.
95     */
96 alfonx 420 @Override
97     public void cancelClose() {
98     cancel();
99     dispose();
100     }
101    
102     @Override
103     public abstract void cancel();
104    
105     /**
106     * This method is called when the dialog is closed and not canceled. Can be
107     * overwritten to do anything when the dialog has been accepted. For example
108 alfonx 422 * checking for any {@link Checkable} components. <br/>
109     *
110     * @Return <code>false</code>, if the ok has been vetoed.
111 alfonx 420 */
112 alfonx 422 public boolean okClose() {
113     dispose();
114     return true;
115     }
116 alfonx 661
117     /**
118     * @return a default OkButton that will call {@link #okClose()}
119     */
120     protected OkButton getOkButton() {
121     if (okButton == null) {
122     okButton = new OkButton(new AbstractAction() {
123    
124     @Override
125     public void actionPerformed(ActionEvent e) {
126     okClose();
127     }
128     });
129     }
130     return okButton;
131     }
132 alfonx 433
133 alfonx 420
134 alfonx 661 /**
135     * @return a default CancelButton that will call {@link #cancelClose()}
136     */
137     protected CancelButton getCancelButton() {
138     if (cancelButton == null) {
139     cancelButton = new CancelButton(new AbstractAction() {
140    
141     @Override
142     public void actionPerformed(ActionEvent e) {
143     cancelClose();
144     }
145     });
146     }
147     return cancelButton;
148     }
149 alfonx 433
150 alfonx 420 }

Properties

Name Value
svn:eol-style native
svn:keywords Id URL
svn:mime-type text/plain

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26