/[schmitzm]/branches/2.4.x/src/skrueger/swing/CancellableDialogAdapter.java
ViewVC logotype

Annotation of /branches/2.4.x/src/skrueger/swing/CancellableDialogAdapter.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1267 - (hide annotations)
Sat Nov 13 01:11:10 2010 UTC (14 years, 3 months ago) by alfonx
Original Path: trunk/src/skrueger/swing/CancellableDialogAdapter.java
File MIME type: text/plain
File size: 3916 byte(s)
Hardcore refactored and worked on the new AtlasStyler features. It will be so much more powerfull. The code will be so much cleaner. It will be so much mre work ;-)
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 alfonx 1267 * Pressing ESC or clicking the "Close X" results in asking the user whether to
23     * Save/Cancel/Abort .
24 alfonx 422 */
25 alfonx 461 public abstract class CancellableDialogAdapter extends AtlasDialog implements
26 alfonx 420 CancellableDialog {
27 alfonx 1267
28     protected static ResourceProvider RESOURCE = ResourceProvider.newInstance(
29     LangUtil.extendPackagePath(SwingUtil.class,
30 alfonx 520 "resource.locales.SwingResourceBundle"), Locale.ENGLISH);
31 alfonx 497
32 alfonx 1267 /** Has this dialog been canceled ? **/
33 alfonx 420 protected boolean cancelled = false;
34 alfonx 661
35     private OkButton okButton;
36 alfonx 1267
37 alfonx 661 private CancelButton cancelButton;
38 alfonx 1267
39 alfonx 461 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 alfonx 1267 @Override
57 alfonx 422 public boolean close() {
58 alfonx 1267 int showConfirmDialog = JOptionPane
59     .showConfirmDialog(
60     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 alfonx 422
67     if (showConfirmDialog == JOptionPane.YES_OPTION) {
68     return okClose();
69 alfonx 1267 } else if (showConfirmDialog == JOptionPane.NO_OPTION) {
70 alfonx 422 cancelClose();
71     return true;
72     }
73 alfonx 1267
74 alfonx 422 return false;
75 alfonx 420 }
76    
77 alfonx 422 /**
78     * This method can be called from "outside" to force a close of this
79     * {@link Dialog}. The user can't cancel the process. He may only decide to
80     * save possible changes.
81     */
82     public void forceClose() {
83    
84 alfonx 1267 int res = JOptionPane
85     .showConfirmDialog(
86     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 alfonx 422 if (res == JOptionPane.YES_OPTION) {
94     okClose();
95     } else {
96     cancelClose();
97     }
98     }
99    
100     /**
101     * Default implementation that calls {@link #cancel()} and
102     * {@link #dispose()}.
103     */
104 alfonx 420 @Override
105     public void cancelClose() {
106     cancel();
107 alfonx 863 cancelled = true;
108 alfonx 420 dispose();
109     }
110    
111     @Override
112     public abstract void cancel();
113    
114     /**
115     * 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
117 alfonx 422 * checking for any {@link Checkable} components. <br/>
118 alfonx 1267 * Call super.okClose() at the end.
119 alfonx 422 *
120     * @Return <code>false</code>, if the ok has been vetoed.
121 alfonx 420 */
122 alfonx 422 public boolean okClose() {
123     dispose();
124     return true;
125     }
126 alfonx 661
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 alfonx 1267
134 alfonx 661 @Override
135     public void actionPerformed(ActionEvent e) {
136     okClose();
137     }
138     });
139     }
140     return okButton;
141     }
142 alfonx 420
143 alfonx 661 /**
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 alfonx 1267
150 alfonx 661 @Override
151     public void actionPerformed(ActionEvent e) {
152     cancelClose();
153     }
154     });
155     }
156     return cancelButton;
157     }
158 alfonx 433
159 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