forked from rapidminer/rapidminer-studio
/
ConfirmDialog.java
364 lines (318 loc) · 12 KB
/
ConfirmDialog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* Copyright (C) 2001-2015 by RapidMiner and the contributors
*
* Complete list of developers available at our web site:
*
* http://rapidminer.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.rapidminer.gui.tools.dialogs;
import java.awt.Dialog;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Collection;
import java.util.LinkedList;
import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import com.rapidminer.gui.ApplicationFrame;
import com.rapidminer.gui.tools.ResourceAction;
import com.rapidminer.gui.tools.ResourceActionAdapter;
import com.rapidminer.gui.tools.SwingTools;
import com.rapidminer.tools.I18N;
import com.rapidminer.tools.ParameterService;
/**
* @author Tobias Malbrecht, Adrian Wilke
*/
public class ConfirmDialog extends ButtonDialog {
private static final long serialVersionUID = -5825873580778775409L;
public static final int OK_OPTION = JOptionPane.OK_OPTION;
public static final int YES_OPTION = JOptionPane.YES_OPTION;
public static final int NO_OPTION = JOptionPane.NO_OPTION;
public static final int CANCEL_OPTION = JOptionPane.CANCEL_OPTION;
public static final int CLOSED_OPTION = JOptionPane.CLOSED_OPTION;
public static final int OK_CANCEL_OPTION = JOptionPane.OK_CANCEL_OPTION;
public static final int YES_NO_OPTION = JOptionPane.YES_NO_OPTION;
public static final int YES_NO_CANCEL_OPTION = JOptionPane.YES_NO_CANCEL_OPTION;
protected int returnOption = CANCEL_OPTION;
private JCheckBox dontAskAgainCheckbox = null;
/**
* Creates a confirm dialog where the user can chose his action.
*
* @param key
* the i18n key
* @param mode
* see the static constants of {@link ConfirmDialog}
* @param showAskAgainCheckbox
* the user can chose to never be asked again
* @param arguments
* additional i18n arguments
* @deprecated use {@link #ConfirmDialog(Window, String, int, boolean, Object...)} instead
*/
@Deprecated
public ConfirmDialog(String key, int mode, boolean showAskAgainCheckbox, Object... arguments) {
this(ApplicationFrame.getApplicationFrame(), key, mode, showAskAgainCheckbox, arguments);
}
/**
* Creates a confirm dialog where the user can chose his action.
*
* @param owner
* the owner of the dialog
* @param key
* the i18n key
* @param mode
* see the static constants of {@link ConfirmDialog}
* @param showAskAgainCheckbox
* the user can chose to never be asked again
* @param arguments
* additional i18n arguments
*/
public ConfirmDialog(Dialog owner, String key, int mode, boolean showAskAgainCheckbox, Object... arguments) {
this((Window) owner, key, mode, showAskAgainCheckbox, arguments);
}
/**
* Creates a confirm dialog where the user can chose his action.
*
* @param owner
* the owner of the dialog
* @param key
* the i18n key
* @param mode
* see the static constants of {@link ConfirmDialog}
* @param showAskAgainCheckbox
* the user can chose to never be asked again
* @param arguments
* additional i18n arguments
* @since 6.5.0
*/
public ConfirmDialog(Window owner, String key, int mode, boolean showAskAgainCheckbox, Object... arguments) {
super(owner, "confirm." + key, ModalityType.APPLICATION_MODAL, arguments);
constructConfirmDialog(mode, showAskAgainCheckbox);
}
private void constructConfirmDialog(int mode, boolean showAskAgainCheckbox) {
Collection<AbstractButton> buttons = new LinkedList<>();
switch (mode) {
case OK_CANCEL_OPTION:
buttons.add(makeOkButton());
buttons.add(makeCancelButton());
break;
case YES_NO_OPTION:
buttons.add(makeYesButton());
buttons.add(makeNoButton());
break;
case YES_NO_CANCEL_OPTION:
buttons.add(makeYesButton());
buttons.add(makeNoButton());
buttons.add(makeCancelButton());
break;
default:
break;
}
if (showAskAgainCheckbox) {
this.dontAskAgainCheckbox = new JCheckBox(new ResourceActionAdapter("dont_ask_again"));
}
layoutDefault(this.dontAskAgainCheckbox, buttons);
}
@Override
protected Icon getInfoIcon() {
String iconKey = I18N.getMessageOrNull(I18N.getGUIBundle(), getKey() + ".icon");
if (iconKey == null) {
return SwingTools.createIcon("48/" + I18N.getMessage(I18N.getGUIBundle(), "gui.dialog.confirm.icon"));
} else {
return SwingTools.createIcon("48/" + iconKey);
}
}
@Override
protected JButton makeOkButton() {
JButton okButton = new JButton(new ResourceAction("ok") {
private static final long serialVersionUID = -8887199234055845095L;
@Override
public void actionPerformed(ActionEvent e) {
returnOption = OK_OPTION;
ok();
}
});
getRootPane().setDefaultButton(okButton);
return okButton;
}
@Override
protected JButton makeCancelButton() {
ResourceAction cancelAction = new ResourceAction("cancel") {
private static final long serialVersionUID = -8887199234055845095L;
@Override
public void actionPerformed(ActionEvent e) {
returnOption = CANCEL_OPTION;
cancel();
}
};
JButton cancelButton = new JButton(cancelAction);
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false), "CANCEL");
getRootPane().getActionMap().put("CANCEL", cancelAction);
return cancelButton;
}
protected JButton makeYesButton() {
JButton yesButton = new JButton(new ResourceAction("confirm.yes") {
private static final long serialVersionUID = -8887199234055845095L;
@Override
public void actionPerformed(ActionEvent e) {
returnOption = YES_OPTION;
yes();
}
});
getRootPane().setDefaultButton(yesButton);
return yesButton;
}
protected JButton makeNoButton() {
ResourceAction noAction = new ResourceAction("confirm.no") {
private static final long serialVersionUID = -8887199234055845095L;
@Override
public void actionPerformed(ActionEvent e) {
returnOption = NO_OPTION;
no();
}
};
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false), "NO");
getRootPane().getActionMap().put("NO", noAction);
return new JButton(noAction);
}
@Override
protected void ok() {
dispose();
}
@Override
protected void cancel() {
dispose();
}
protected void yes() {
dispose();
}
protected void no() {
dispose();
}
public int getReturnOption() {
return returnOption;
}
public boolean isDontAskAgainCheckboxSelected() {
if (dontAskAgainCheckbox == null) {
return false;
} else {
return dontAskAgainCheckbox.isSelected();
}
}
protected void setReturnOption(int option) {
this.returnOption = option;
}
/**
* Shows a dialog and returns the return code of the dialog.
*
* @param key
* The i18n-key. Will be prepended with gui.dialog.confirm and prefixed with .title,
* .message and .icon .
* @param mode
* One of {@link #OK_CANCEL_OPTION}, {@link #YES_NO_OPTION} and
* {@link #YES_NO_CANCEL_OPTION}
* @param propertyConfirmExit
* If null, the dialog is shown in any case. If not null, the parameter specified
* will be from fetched from {@link ParameterService}. If it is "false", the dialog
* will not be shown.
* @param defaultOption
* Will be returned if the dialog is not shown (see parameter propertyConfirmExit.
* @param i18nArgs
* Arguments to the i18n string.
* @return The return code of the dialog, one of the constants specified above.
* @deprecated use {@link #showConfirmDialog(Dialog, String, int, String, int, Object...)}
* instead
*
*/
@Deprecated
public static int showConfirmDialog(String key, int mode, String propertyConfirmExit, int defaultOption,
Object... i18nArgs) {
return showConfirmDialog(ApplicationFrame.getApplicationFrame(), key, mode, propertyConfirmExit, defaultOption,
true, i18nArgs);
}
/**
* Shows a dialog and returns the return code of the dialog.
*
* @param owner
* the owner where the dialog will be shown in
* @param key
* The i18n-key. Will be prepended with gui.dialog.confirm and prefixed with .title,
* .message and .icon .
* @param mode
* One of {@link #OK_CANCEL_OPTION}, {@link #YES_NO_OPTION} and
* {@link #YES_NO_CANCEL_OPTION}
* @param propertyConfirmExit
* If null, the dialog is shown in any case. If not null, the parameter specified
* will be from fetched from {@link ParameterService}. If it is "false", the dialog
* will not be shown.
* @param defaultOption
* Will be returned if the dialog is not shown (see parameter propertyConfirmExit.
* @param i18nArgs
* Arguments to the i18n string.
* @return The return code of the dialog, one of the constants specified above.
* @since 6.5.0
*/
public static int showConfirmDialog(Window owner, String key, int mode, String propertyConfirmExit, int defaultOption,
Object... i18nArgs) {
return showConfirmDialogWithOptionalCheckbox(owner, key, mode, propertyConfirmExit, defaultOption, true, i18nArgs);
}
/**
* Same as {@link #showConfirmDialog(Window, String, int, String, int, Object...)} with the
* additional option showAskAgainCheckbox.
*
* @deprecated use
* {@link #showConfirmDialogWithOptionalCheckbox(Window, String, int, String, int, boolean, Object...)}
* instead
*/
@Deprecated
public static int showConfirmDialogWithOptionalCheckbox(String key, int mode, String propertyConfirmExit,
int defaultOption, boolean showAskAgainCheckbox, Object... i18nArgs) {
return showConfirmDialogWithOptionalCheckbox(ApplicationFrame.getApplicationFrame(), key, mode, propertyConfirmExit,
defaultOption, showAskAgainCheckbox, i18nArgs);
}
/**
* Same as {@link #showConfirmDialog(Window, String, int, String, int, Object...)} with the
* additional option showAskAgainCheckbox.
*
* @since 6.5.0
*/
public static int showConfirmDialogWithOptionalCheckbox(Window owner, String key, int mode, String propertyConfirmExit,
int defaultOption, boolean showAskAgainCheckbox, Object... i18nArgs) {
if (propertyConfirmExit == null) {
ConfirmDialog dialog = new ConfirmDialog(owner, key, mode, showAskAgainCheckbox, i18nArgs);
dialog.setVisible(true);
return dialog.getReturnOption();
} else {
String askProperty = ParameterService.getParameterValue(propertyConfirmExit);
if (!"false".equals(askProperty)) {
ConfirmDialog dialog = new ConfirmDialog(owner, key, mode, showAskAgainCheckbox, i18nArgs);
dialog.setVisible(true);
ParameterService.setParameterValue(propertyConfirmExit,
Boolean.toString(!dialog.dontAskAgainCheckbox.isSelected()));
ParameterService.saveParameters();
return dialog.getReturnOption();
} else {
return defaultOption;
}
}
}
}