-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
DialogWrapper.java
1804 lines (1564 loc) · 55 KB
/
DialogWrapper.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.ui;
import com.intellij.CommonBundle;
import com.intellij.ide.ui.UISettings;
import com.intellij.idea.ActionsBundle;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.MnemonicHelper;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CustomShortcutSet;
import com.intellij.openapi.actionSystem.MacOtherAction;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.help.HelpManager;
import com.intellij.openapi.keymap.KeymapUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.StackingPopupDispatcher;
import com.intellij.openapi.util.*;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.openapi.wm.IdeGlassPaneUtil;
import com.intellij.openapi.wm.impl.content.GraphicsConfig;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.UIBundle;
import com.intellij.ui.components.JBOptionButton;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.util.Alarm;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ui.AwtVisitor;
import com.intellij.util.ui.DialogUtil;
import com.intellij.util.ui.UIUtil;
import org.intellij.lang.annotations.MagicConstant;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.UIResource;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
/**
* The dialog wrapper. The dialog wrapper could be used only on event dispatch thread.
* In case when the dialog must be created from other threads use
* {@link EventQueue#invokeLater(Runnable)} or {@link EventQueue#invokeAndWait(Runnable)}.
*/
@SuppressWarnings({"SSBasedInspection", "MethodMayBeStatic", "UnusedDeclaration"})
public abstract class DialogWrapper {
/**
* The default exit code for "OK" action.
*/
public static final int OK_EXIT_CODE = 0;
/**
* The default exit code for "Cancel" action.
*/
public static final int CANCEL_EXIT_CODE = 1;
/**
* If you use your custom exit codes you have have to start them with
* this constant.
*/
public static final int NEXT_USER_EXIT_CODE = 2;
/**
* If your action returned by <code>createActions</code> method has non
* <code>null</code> value for this key, then the button that corresponds to the action will be the
* default button for the dialog. It's true if you don't change this behaviour
* of <code>createJButtonForAction(Action)</code> method.
*/
@NonNls public static final String DEFAULT_ACTION = "DefaultAction";
@NonNls public static final String FOCUSED_ACTION = "FocusedAction";
private static final KeyStroke SHOW_OPTION_KEYSTROKE = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
InputEvent.ALT_MASK | InputEvent.SHIFT_MASK);
private final DialogWrapperPeer myPeer;
private int myExitCode = CANCEL_EXIT_CODE;
/**
* The shared instance of default border for dialog's content pane.
*/
public static final Border ourDefaultBorder = new EmptyBorder(UIUtil.PANEL_REGULAR_INSETS);
private float myHorizontalStretch = 1.0f;
private float myVerticalStretch = 1.0f;
/**
* Defines horizontal alignment of buttons.
*/
private int myButtonAlignment = SwingConstants.RIGHT;
private boolean myCrossClosesWindow = true;
private Insets myButtonMargins = new Insets(2, 16, 2, 16);
protected Action myOKAction;
protected Action myCancelAction;
protected Action myHelpAction;
private JButton[] myButtons;
private boolean myClosed = false;
protected boolean myPerformAction = false;
private Action myYesAction = null;
private Action myNoAction = null;
protected JCheckBox myCheckBoxDoNotShowDialog;
@Nullable
private DoNotAskOption myDoNotAsk;
private JComponent myPreferredFocusedComponent;
private Computable<Point> myInitialLocationCallback;
protected final Disposable myDisposable = new Disposable() {
public String toString() {
return DialogWrapper.this.toString();
}
public void dispose() {
DialogWrapper.this.dispose();
}
};
private List<JBOptionButton> myOptionsButtons = new ArrayList<JBOptionButton>();
private int myCurrentOptionsButtonIndex = -1;
private boolean myResizeInProgress = false;
protected String getDoNotShowMessage() {
return CommonBundle.message("dialog.options.do.not.show");
}
public void setDoNotAskOption(@Nullable DoNotAskOption doNotAsk) {
myDoNotAsk = doNotAsk;
}
private ErrorText myErrorText;
private int myMaxErrorTextLength;
private final Alarm myErrorTextAlarm = new Alarm();
/**
* Creates modal <code>DialogWrapper</code>. The currently active window will be the dialog's parent.
*
* @param project parent window for the dialog will be calculated based on focused window for the
* specified <code>project</code>. This parameter can be <code>null</code>. In this case parent window
* will be suggested based on current focused window.
* @param canBeParent specifies whether the dialog can be parent for other windows. This parameter is used
* by <code>WindowManager</code>.
* @throws IllegalStateException if the dialog is invoked not on the event dispatch thread
*/
protected DialogWrapper(Project project, boolean canBeParent) {
myPeer = createPeer(project, canBeParent);
final Window window = myPeer.getWindow();
if (window != null) {
window.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if (!myResizeInProgress) {
myActualSize = myPeer.getSize();
if (myErrorText.isVisible()) {
myActualSize.height -= myErrorText.getHeight() + 10;
}
}
}
});
}
createDefaultActions();
}
/**
* Creates modal <code>DialogWrapper</code> that can be parent for other windows.
* The currently active window will be the dialog's parent.
*
* @param project parent window for the dialog will be calculated based on focused window for the
* specified <code>project</code>. This parameter can be <code>null</code>. In this case parent window
* will be suggested based on current focused window.
* @throws IllegalStateException if the dialog is invoked not on the event dispatch thread
* @see com.intellij.openapi.ui.DialogWrapper#DialogWrapper(com.intellij.openapi.project.Project, boolean)
*/
protected DialogWrapper(Project project) {
this(project, true);
}
/**
* Creates modal <code>DialogWrapper</code>. The currently active window will be the dialog's parent.
*
* @param canBeParent specifies whether the dialog can be parent for other windows. This parameter is used
* by <code>WindowManager</code>.
* @throws IllegalStateException if the dialog is invoked not on the event dispatch thread
*/
protected DialogWrapper(boolean canBeParent) {
this((Project)null, canBeParent);
}
protected DialogWrapper(boolean canBeParent, boolean toolkitModalIfPossible) {
ensureEventDispatchThread();
myPeer = createPeer(canBeParent, toolkitModalIfPossible);
createDefaultActions();
}
/**
* @param parent parent component which is used to calculate heavy weight window ancestor.
* <code>parent</code> cannot be <code>null</code> and must be showing.
* @param canBeParent can be parent
* @throws IllegalStateException if the dialog is invoked not on the event dispatch thread
*/
protected DialogWrapper(Component parent, boolean canBeParent) {
ensureEventDispatchThread();
myPeer = createPeer(parent, canBeParent);
createDefaultActions();
}
//validation
private final Alarm myValidationAlarm = new Alarm(myDisposable);
private int myValidationDelay = 300;
private boolean myDisposed = false;
private boolean myValidationStarted = false;
private static final Icon ERROR_POINT = IconLoader.getIcon("/ide/errorPoint.png");
private static final Icon ERROR_SIGN = IconLoader.getIcon("/ide/errorSign.png");
private final ErrorPainter myErrorPainter = new ErrorPainter();
private JComponent myErrorPane;
private boolean myErrorPainterInstalled = false;
/**
* Allows to postpone first start of validation
*
* @return <code>false</code> if start validation in <code>init()</code> method
*/
protected boolean postponeValidation() {
return true;
}
/**
* Validates a user input and returns <code>null</code> if everything is fine
* or returns a problem description with component where is the problem has been found.
*
* @return <code>null</code> if everything is OK or a problem descriptor
*/
@Nullable
protected ValidationInfo doValidate() {
return null;
}
public void setValidationDelay(int delay) {
myValidationDelay = delay;
}
private void reportProblem(final ValidationInfo info) {
installErrorPainter();
myErrorPainter.setValidationInfo(info);
if (! myErrorText.isTextSet(info.message)) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (myDisposed) return;
setErrorText(info.message);
myPeer.getRootPane().getGlassPane().repaint();
getOKAction().setEnabled(false);
}
});
}
}
private void installErrorPainter() {
if (myErrorPainterInstalled) return;
myErrorPainterInstalled = true;
IdeGlassPaneUtil.installPainter(myErrorPane, myErrorPainter, myDisposable);
}
private void clearProblems() {
myErrorPainter.setValidationInfo(null);
if (! myErrorText.isTextSet(null)) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (myDisposed) return;
setErrorText(null);
myPeer.getRootPane().getGlassPane().repaint();
getOKAction().setEnabled(true);
}
});
}
}
protected void createDefaultActions() {
myOKAction = new OkAction();
myCancelAction = new CancelAction();
myHelpAction = new HelpAction();
}
public void setUndecorated(boolean undecorated) {
myPeer.setUndecorated(undecorated);
}
public final void addMouseListener(MouseListener listener) {
myPeer.addMouseListener(listener);
}
public final void addMouseListener(MouseMotionListener listener) {
myPeer.addMouseListener(listener);
}
public final void addKeyListener(KeyListener listener) {
myPeer.addKeyListener(listener);
}
/**
* Closes and disposes the dialog and sets the specified exit code.
*
* @param exitCode exit code
* @param isOk is OK
* @throws IllegalStateException if the dialog is invoked not on the event dispatch thread
*/
public final void close(int exitCode, boolean isOk) {
ensureEventDispatchThread();
if (myClosed) return;
myClosed = true;
myExitCode = exitCode;
if (isOk) {
processDoNotAskOnOk(exitCode);
} else {
processDoNotAskOnCancel();
}
Disposer.dispose(myDisposable);
}
public final void close(int exitCode) {
close(exitCode, exitCode != CANCEL_EXIT_CODE);
}
/**
* Factory method. It creates border for dialog's content pane. By default content
* pane has has empty border with <code>(8,12,8,12)</code> insets. The subclasses can
* return <code>null</code> in overridden methods. In this case there will be no
* any border in the content pane.
*
* @return content pane border
*/
@Nullable
protected Border createContentPaneBorder() {
return ourDefaultBorder;
}
/**
* This is factory method. It creates the panel located at the south of the content pane. By default that
* panel contains dialog's buttons. This default implementation uses <code>createActions()</code>
* and <code>createJButtonForAction(Action)</code> methods to construct the panel.
*
* @return south panel
*/
@Nullable
protected JComponent createSouthPanel() {
Action[] actions = filter(createActions());
Action[] leftSideActions = createLeftSideActions();
List<JButton> buttons = new ArrayList<JButton>();
boolean hasHelpToMoveToLeftSide = false;
if (UIUtil.isUnderAquaLookAndFeel() && Arrays.asList(actions).contains(getHelpAction())) {
hasHelpToMoveToLeftSide = true;
actions = ArrayUtil.remove(actions, getHelpAction());
}
if (SystemInfo.isMac) {
for (Action action : actions) {
if (action instanceof MacOtherAction) {
leftSideActions = ArrayUtil.append(leftSideActions, action);
actions = ArrayUtil.remove(actions, action);
break;
}
}
}
else if (UIUtil.isUnderGTKLookAndFeel() && Arrays.asList(actions).contains(getHelpAction())) {
leftSideActions = ArrayUtil.append(leftSideActions, getHelpAction());
actions = ArrayUtil.remove(actions, getHelpAction());
}
JPanel panel = new JPanel(new BorderLayout());
final JPanel lrButtonsPanel = new JPanel(new GridBagLayout());
final Insets insets = SystemInfo.isMacOSLeopard ? new Insets(0, 0, 0, 0) : new Insets(8, 0, 0, 0);
if (actions.length > 0 || leftSideActions.length > 0) {
int gridX = 0;
if (leftSideActions.length > 0) {
JPanel buttonsPanel = createButtons(leftSideActions, buttons);
if (actions.length > 0) {
buttonsPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20)); // leave some space between button groups
}
lrButtonsPanel.add(buttonsPanel,
new GridBagConstraints(gridX++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, insets, 0, 0));
}
lrButtonsPanel.add(Box.createHorizontalGlue(), // left strut
new GridBagConstraints(gridX++, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
if (actions.length > 0) {
if (SystemInfo.isMac) {
// move ok action to the right
int okNdx = ArrayUtil.indexOf(actions, getOKAction());
if (okNdx >= 0 && okNdx != actions.length - 1) {
actions = ArrayUtil.append(ArrayUtil.remove(actions, getOKAction()), getOKAction());
}
// move cancel action to the left
int cancelNdx = ArrayUtil.indexOf(actions, getCancelAction());
if (cancelNdx > 0) {
actions = ArrayUtil.mergeArrays(new Action[] {getCancelAction()}, ArrayUtil.remove(actions, getCancelAction()));
}
/*if (!hasFocusedAction(actions)) {
int ndx = ArrayUtil.find(actions, getCancelAction());
if (ndx >= 0) {
actions[ndx].putValue(FOCUSED_ACTION, Boolean.TRUE);
}
}*/
}
JPanel buttonsPanel = createButtons(actions, buttons);
lrButtonsPanel.add(buttonsPanel,
new GridBagConstraints(gridX++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, insets, 0, 0));
}
if (SwingConstants.CENTER == myButtonAlignment) {
lrButtonsPanel.add(Box.createHorizontalGlue(), // right strut
new GridBagConstraints(gridX, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 0));
}
myButtons = buttons.toArray(new JButton[buttons.size()]);
}
if (hasHelpToMoveToLeftSide) {
JButton helpButton = new JButton(getHelpAction());
helpButton.putClientProperty("JButton.buttonType", "help");
helpButton.setText("");
helpButton.setMargin(insets);
helpButton.setToolTipText(ActionsBundle.actionDescription("HelpTopics"));
panel.add(helpButton, BorderLayout.WEST);
}
panel.add(lrButtonsPanel, BorderLayout.CENTER);
final DoNotAskOption askOption = myDoNotAsk;
if (askOption != null) {
myCheckBoxDoNotShowDialog = new JCheckBox(askOption.getDoNotShowMessage());
JComponent southPanel = panel;
if (!askOption.canBeHidden()) {
return southPanel;
}
final JPanel withCB = addDoNotShowCheckBox(southPanel, myCheckBoxDoNotShowDialog);
myCheckBoxDoNotShowDialog.setSelected(!askOption.isToBeShown());
DialogUtil.registerMnemonic(myCheckBoxDoNotShowDialog, '&');
panel = withCB;
}
panel.setBorder(IdeBorderFactory.createEmptyBorder(new Insets(8, 0, 0, 0)));
return panel;
}
private Action[] filter(Action[] actions) {
ArrayList<Action> answer = new ArrayList<Action>();
for (Action action : actions) {
if (action != null && (ApplicationInfo.contextHelpAvailable() || action != getHelpAction())) {
answer.add(action);
}
}
return answer.toArray(new Action[answer.size()]);
}
protected boolean toBeShown() {
return !myCheckBoxDoNotShowDialog.isSelected();
}
public boolean isTypeAheadEnabled() {
return false;
}
public static JPanel addDoNotShowCheckBox(JComponent southPanel, JCheckBox checkBox) {
final JPanel panel = new JPanel(new BorderLayout());
JPanel wrapper = new JPanel(new GridBagLayout());
wrapper.add(checkBox);
panel.add(wrapper, BorderLayout.WEST);
panel.add(southPanel, BorderLayout.EAST);
checkBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));
return panel;
}
private boolean hasFocusedAction(Action[] actions) {
for (Action action : actions) {
if (action.getValue(FOCUSED_ACTION) != null && (Boolean)action.getValue(FOCUSED_ACTION)) {
return true;
}
}
return false;
}
private JPanel createButtons(Action[] actions, List<JButton> buttons) {
if (!UISettings.getShadowInstance().ALLOW_MERGE_BUTTONS) {
final List<Action> actionList = new ArrayList<Action>();
for (Action action : actions) {
actionList.add(action);
if (action instanceof OptionAction) {
final Action[] options = ((OptionAction)action).getOptions();
actionList.addAll(Arrays.asList(options));
}
}
if (actionList.size() != actions.length) {
actions = actionList.toArray(actionList.toArray(new Action[actionList.size()]));
}
}
JPanel buttonsPanel = new JPanel(new GridLayout(1, actions.length, SystemInfo.isMacOSLeopard ? 0 : 5, 0));
for (final Action action : actions) {
JButton button = createJButtonForAction(action);
final Object value = action.getValue(Action.MNEMONIC_KEY);
if (value instanceof Integer) {
final int mnemonic = ((Integer)value).intValue();
final Object name = action.getValue(Action.NAME);
if (mnemonic == 'Y' && "Yes".equals(name)) {
myYesAction = action;
}
else if (mnemonic == 'N' && "No".equals(name)) {
myNoAction = action;
}
button.setMnemonic(mnemonic);
}
if (action.getValue(FOCUSED_ACTION) != null) {
myPreferredFocusedComponent = button;
}
buttons.add(button);
buttonsPanel.add(button);
}
return buttonsPanel;
}
/**
* Creates <code>JButton</code> for the specified action. If the button has not <code>null</code>
* value for <code>DialogWrapper.DEFAULT_ACTION</code> key then the created button will be the
* default one for the dialog.
*
* @param action action for the button
* @return button with action specified
*
* @see com.intellij.openapi.ui.DialogWrapper#DEFAULT_ACTION
*/
protected JButton createJButtonForAction(Action action) {
JButton button;
if (action instanceof OptionAction && UISettings.getShadowInstance().ALLOW_MERGE_BUTTONS) {
final Action[] options = ((OptionAction)action).getOptions();
button = new JBOptionButton(action, options);
final JBOptionButton eachOptionsButton = (JBOptionButton)button;
eachOptionsButton.setOkToProcessDefaultMnemonics(false);
eachOptionsButton.setOptionTooltipText(
"Press " + KeymapUtil.getKeystrokeText(SHOW_OPTION_KEYSTROKE) + " to expand or use a mnemonic of a contained action");
myOptionsButtons.add(eachOptionsButton);
final Set<JBOptionButton.OptionInfo> infos = eachOptionsButton.getOptionInfos();
for (final JBOptionButton.OptionInfo eachInfo : infos) {
if (eachInfo.getMnemonic() >=0) {
final CustomShortcutSet sc =
new CustomShortcutSet(KeyStroke.getKeyStroke("alt pressed " + Character.valueOf((char)eachInfo.getMnemonic())));
new AnAction() {
@Override
public void actionPerformed(AnActionEvent e) {
final JBOptionButton buttonToActivate = eachInfo.getButton();
buttonToActivate.showPopup(eachInfo.getAction(), true);
}
}.registerCustomShortcutSet(sc, getPeer().getRootPane());
}
}
} else {
button = new JButton(action);
}
String text = button.getText();
if (SystemInfo.isMac) {
button.putClientProperty("JButton.buttonType", "text");
}
if (text != null) {
int mnemonic = 0;
StringBuilder plainText = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (ch == '_' || ch == '&') {
i++;
if (i >= text.length()) {
break;
}
ch = text.charAt(i);
if (ch != '_' && ch != '&') {
// Mnemonic is case insensitive.
int vk = ch;
if (vk >= 'a' && vk <= 'z') {
vk -= 'a' - 'A';
}
mnemonic = vk;
}
}
plainText.append(ch);
}
button.setText(plainText.toString());
final Object name = action.getValue(Action.NAME);
if (mnemonic == KeyEvent.VK_Y && "Yes".equals(name)) {
myYesAction = action;
}
else if (mnemonic == KeyEvent.VK_N && "No".equals(name)) {
myNoAction = action;
}
button.setMnemonic(mnemonic);
}
setMargin(button);
if (action.getValue(DEFAULT_ACTION) != null) {
if (myPeer != null && !myPeer.isHeadless()) {
getRootPane().setDefaultButton(button);
}
}
return button;
}
private void setMargin(JButton button) {
// Aqua LnF does a good job of setting proper margin between buttons. Setting them specifically causes them be 'square' style instead of
// 'rounded', which is expected by apple users.
if (!SystemInfo.isMac) {
if (myButtonMargins == null) {
return;
}
button.setMargin(myButtonMargins);
}
}
protected DialogWrapperPeer createPeer(final Component parent, final boolean canBeParent) {
return DialogWrapperPeerFactory.getInstance().createPeer(this, parent, canBeParent);
}
protected DialogWrapperPeer createPeer(boolean canBeParent, boolean toolkitModalIfPossible) {
return DialogWrapperPeerFactory.getInstance().createPeer(this, canBeParent, toolkitModalIfPossible);
}
protected DialogWrapperPeer createPeer(final Project project, final boolean canBeParent) {
return DialogWrapperPeerFactory.getInstance().createPeer(this, project, canBeParent);
}
@Nullable
protected JComponent createTitlePane() {
return null;
}
/**
* Factory method. It creates the panel located at the
* north of the dialog's content pane. The implementation can return <code>null</code>
* value. In this case there will be no input panel.
*
* @return north panel
*/
@Nullable
protected JComponent createNorthPanel() {
return null;
}
/**
* Factory method. It creates panel with dialog options. Options panel is located at the
* center of the dialog's content pane. The implementation can return <code>null</code>
* value. In this case there will be no options panel.
*
* @return center panel
*/
@Nullable
protected abstract JComponent createCenterPanel();
/**
* @see java.awt.Window#toFront()
*/
public void toFront() {
myPeer.toFront();
}
/**
* @see java.awt.Window#toBack()
*/
public void toBack() {
myPeer.toBack();
}
/**
* Dispose the wrapped and releases all resources allocated be the wrapper to help
* more effecient garbage collection. You should never invoke this method twice or
* invoke any method of the wrapper after invocation of <code>dispose</code>.
*
* @throws IllegalStateException if the dialog is disposed not on the event dispatch thread
*/
protected void dispose() {
ensureEventDispatchThread();
myErrorTextAlarm.cancelAllRequests();
myValidationAlarm.cancelAllRequests();
myDisposed = true;
if (myButtons != null) {
for (JButton button : myButtons) {
button.setAction(null); // avoid memory leak via KeyboardManager
}
}
final JRootPane rootPane = getRootPane();
// if rootPane = null, dialog has already been disposed
if (rootPane != null) {
new AwtVisitor(rootPane) {
public boolean visit(final Component component) {
if (component instanceof JComponent) {
final JComponent eachComp = (JComponent)component;
final ActionMap actionMap = eachComp.getActionMap();
final KeyStroke[] strokes = eachComp.getRegisteredKeyStrokes();
for (KeyStroke eachStroke : strokes) {
boolean remove = true;
if (actionMap != null) {
for (int i = 0; i < 3; i++) {
final InputMap inputMap = eachComp.getInputMap(i);
final Object key = inputMap.get(eachStroke);
if (key != null) {
final Action action = actionMap.get(key);
if (action instanceof UIResource) remove = false;
}
}
}
if (remove) eachComp.unregisterKeyboardAction(eachStroke);
}
}
return false;
}
};
myPeer.dispose();
}
}
/**
* This method is invoked by default implementation of "Cancel" action. It just closes dialog
* with <code>CANCEL_EXIT_CODE</code>. This is convenient place to override functionality of "Cancel" action.
* Note that the method does nothing if "Cancel" action isn't enabled.
*/
public void doCancelAction() {
processDoNotAskOnCancel();
if (getCancelAction().isEnabled()) {
close(CANCEL_EXIT_CODE);
}
}
private void processDoNotAskOnCancel() {
if (myDoNotAsk != null) {
if (myDoNotAsk.shouldSaveOptionsOnCancel() && myDoNotAsk.canBeHidden()) {
myDoNotAsk.setToBeShown(toBeShown(), CANCEL_EXIT_CODE);
}
}
}
/**
* You can use this method if you want to know by which event this actions got triggered. It is called only if
* the cancel action was triggered by some input event, <code>doCancelAction</code> is called otherwise.
*
* @param source AWT event
* @see #doCancelAction
*/
public void doCancelAction(AWTEvent source) {
doCancelAction();
}
/**
* Programmatically perform a "click" of default dialog's button. The method does
* nothing if the dialog has no default button.
*/
public void clickDefaultButton() {
JButton button = getRootPane().getDefaultButton();
if (button != null) {
button.doClick();
}
}
/**
* This method is invoked by default implementation of "OK" action. It just closes dialog
* with <code>OK_EXIT_CODE</code>. This is convenient place to override functionality of "OK" action.
* Note that the method does nothing if "OK" action isn't enabled.
*/
protected void doOKAction() {
processDoNotAskOnOk(OK_EXIT_CODE);
if (getOKAction().isEnabled()) {
close(OK_EXIT_CODE);
}
}
protected void processDoNotAskOnOk(int exitCode) {
if (myDoNotAsk != null) {
if (myDoNotAsk.canBeHidden()) {
myDoNotAsk.setToBeShown(toBeShown(), exitCode);
}
}
}
/**
* @return whether the native window cross butoon closes the window or not.
* <code>true</code> means that cross performs hide or dispose of the dialog.
*/
public boolean shouldCloseOnCross() {
return myCrossClosesWindow;
}
/**
* This is factory method which creates action of dialog. Each action is represented
* by <code>JButton</code> which is created by <code>createJButtonForAction(Action)</code>
* method. These buttons are places into panel which is created by <code>createButtonsPanel</code>
* method. Therefore you have anough ways to customise the dialog by ovverriding of
* <code>createActions()</code>, <code>createButtonsPanel()</code> and
* </code>createJButtonForAction(Action)</code> methods. By default the <code>createActions()</code>
* method returns "OK" and "Cancel" action. The help action is automatically added is if
* {@link #getHelpId()} returns non null value.
*
* @return dialog actions
*
* @see #createSouthPanel
* @see #createJButtonForAction
*/
protected Action[] createActions() {
if (getHelpId() == null) {
if (SystemInfo.isMac) {
return new Action[]{getCancelAction(), getOKAction()};
}
return new Action[]{getOKAction(), getCancelAction()};
}
else {
if (SystemInfo.isMac) {
return new Action[]{getHelpAction(), getCancelAction(), getOKAction()};
}
return new Action[]{getOKAction(), getCancelAction(), getHelpAction()};
}
}
protected Action[] createLeftSideActions() {
return new Action[0];
}
/**
* @return default implementation of "OK" action. This action just invokes
* <code>doOKAction()</code> method.
* @see #doOKAction
*/
protected Action getOKAction() {
return myOKAction;
}
/**
* @return default implementation of "Cancel" action. This action just invokes
* <code>doCancelAction()</code> method.
* @see #doCancelAction
*/
protected Action getCancelAction() {
return myCancelAction;
}
/**
* @return default implementation of "Help" action. This action just invokes
* <code>doHelpAction()</code> method.
* @see #doHelpAction
*/
protected Action getHelpAction() {
return myHelpAction;
}
protected boolean isProgressDialog() {
return false;
}
public final boolean isModalProgress() {
return isProgressDialog();
}
/**
* Returns content pane
*
* @return content pane
* @see javax.swing.JDialog#getContentPane
*/
public Container getContentPane() {
assert myPeer != null;
return myPeer.getContentPane();
}
/**
* @see javax.swing.JDialog#validate
*/
public void validate() {
myPeer.validate();
}
/**
* @see javax.swing.JDialog#repaint
*/
public void repaint() {
myPeer.repaint();
}
/**
* This is factory method. It returns key for installation into the dimension service.
* If this method returns <code>null</code> then the component does not require installation
* into dimension service. This default implementation returns <code>null</code>.
*
* @return dimension service key
*/
@Nullable
@NonNls
protected String getDimensionServiceKey() {
return null;
}
@Nullable
public final String getDimensionKey() {
return getDimensionServiceKey();
}
public int getExitCode() {
return myExitCode;
}
/**
* @return component which should be focused when the dialog appears
* on the screen.
*/
@Nullable
public JComponent getPreferredFocusedComponent() {
return SystemInfo.isMac ? myPreferredFocusedComponent : null;
}
/**
* @return horizontal stretch of the dialog. It means that the dialog's horizontal size is
* the product of horizontal stretch by horizontal size of packed dialog. The default value
* is <code>1.0f</code>
*/
public final float getHorizontalStretch() {
return myHorizontalStretch;
}
/**
* @return vertical stretch of the dialog. It means that the dialog's vertical size is
* the product of vertical stretch by vertical size of packed dialog. The default value
* is <code>1.0f</code>
*/
public final float getVerticalStretch() {
return myVerticalStretch;
}
protected final void setHorizontalStretch(float hStretch) {
myHorizontalStretch = hStretch;
}
protected final void setVerticalStretch(float vStretch) {
myVerticalStretch = vStretch;
}
/**
* @see java.awt.Window#getOwner
* @return window owner
*/
public Window getOwner() {
return myPeer.getOwner();
}
public Window getWindow() {
return myPeer.getWindow();