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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1270 - (show annotations)
Sun Nov 14 00:25:52 2010 UTC (14 years, 3 months ago) by alfonx
File MIME type: text/plain
File size: 3872 byte(s)


1 package skrueger.swing;
2
3 import java.awt.Component;
4 import java.awt.Dialog;
5 import java.awt.event.ActionEvent;
6 import java.util.Locale;
7
8 import javax.swing.AbstractAction;
9 import javax.swing.JDialog;
10 import javax.swing.JOptionPane;
11
12 import schmitzm.lang.LangUtil;
13 import schmitzm.lang.ResourceProvider;
14 import schmitzm.swing.SwingUtil;
15
16 /**
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 *
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 AtlasDialog implements
26 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;
34
35 public CancellableDialogAdapter(Component parentWindow) {
36 super(parentWindow);
37 }
38
39 public CancellableDialogAdapter(Component parentWindow, String title) {
40 super(parentWindow, title);
41 }
42
43 @Override
44 public boolean isCancelled() {
45 return cancelled;
46 }
47
48 /**
49 * Allows to close the {@link JDialog} from "outside". The user will be
50 * asked and she may cancel the close process.
51 */
52 @Override
53 public boolean close() {
54 int showConfirmDialog = JOptionPane
55 .showConfirmDialog(
56 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) {
64 return okClose();
65 } else if (showConfirmDialog == JOptionPane.NO_OPTION) {
66 cancelClose();
67 return true;
68 }
69
70 return false;
71 }
72
73 /**
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
81 .showConfirmDialog(
82 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) {
90 okClose();
91 } else {
92 cancelClose();
93 }
94 }
95
96 /**
97 * Default implementation that calls {@link #cancel()} and
98 * {@link #dispose()}.
99 */
100 @Override
101 public void cancelClose() {
102 cancel();
103 cancelled = true;
104 dispose();
105 }
106
107 @Override
108 public abstract void cancel();
109
110 /**
111 * 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
113 * 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.
117 */
118 public boolean okClose() {
119 dispose();
120 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 }

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