Skip to content

Commit 9dbe896

Browse files
committed
2080: Thread context was already set
1 parent 48ae360 commit 9dbe896

36 files changed

+557
-618
lines changed

src/main/java/com/magento/idea/magento2plugin/actions/generation/dialog/AbstractDialog.java

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
package com.magento.idea.magento2plugin.actions.generation.dialog;
77

88
import com.intellij.openapi.application.WriteAction;
9+
import com.intellij.openapi.project.Project;
10+
import com.intellij.openapi.ui.DialogWrapper;
11+
import com.intellij.openapi.ui.Messages;
912
import com.intellij.openapi.util.Pair;
1013
import com.magento.idea.magento2plugin.actions.generation.data.ui.ComboBoxItemData;
1114
import com.magento.idea.magento2plugin.actions.generation.dialog.prompt.PlaceholderInitializerUtil;
@@ -26,20 +29,20 @@
2629
import java.util.List;
2730
import javax.swing.JComboBox;
2831
import javax.swing.JComponent;
29-
import javax.swing.JDialog;
3032
import javax.swing.JOptionPane;
3133
import javax.swing.JTabbedPane;
3234
import javax.swing.JTextArea;
3335
import javax.swing.JTextField;
3436
import org.jetbrains.annotations.NotNull;
37+
import org.jetbrains.annotations.Nullable;
3538

3639
/**
3740
* All code generate dialog should extend this class.
3841
*/
3942
@SuppressWarnings({
4043
"PMD.TooManyMethods"
4144
})
42-
public abstract class AbstractDialog extends JDialog {
45+
public abstract class AbstractDialog extends DialogWrapper {
4346

4447
protected transient CommonBundle bundle;
4548
protected final transient ValidatorBundle validatorBundle = new ValidatorBundle();
@@ -50,19 +53,44 @@ public abstract class AbstractDialog extends JDialog {
5053

5154
/**
5255
* Abstract Dialog Constructor.
56+
*
57+
* @param project Project
5358
*/
54-
public AbstractDialog() {
55-
super();
59+
public AbstractDialog(final @Nullable Project project) {
60+
super(project, true);
5661
bundle = new CommonBundle();
5762
errorTitle = bundle.message("common.error");
5863
fieldsValidationsList = new TypeFieldsRulesParser(this).parseValidationRules();
64+
init();
65+
}
66+
67+
/**
68+
* Abstract Dialog Constructor without project.
69+
*/
70+
public AbstractDialog() {
71+
this(null);
5972
}
6073

74+
/**
75+
* Center the dialog on the screen.
76+
* Note: This is handled automatically by DialogWrapper, so this method is kept for compatibility.
77+
*
78+
* @param dialog AbstractDialog
79+
*/
6180
protected void centerDialog(final AbstractDialog dialog) {
62-
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
63-
final int coordinateX = screenSize.width / 2 - dialog.getSize().width / 2;
64-
final int coordinateY = screenSize.height / 2 - dialog.getSize().height / 2;
65-
dialog.setLocation(coordinateX, coordinateY);
81+
// DialogWrapper handles centering automatically
82+
}
83+
84+
/**
85+
* Create the center panel for the dialog.
86+
* This method must be implemented by subclasses to provide the content panel.
87+
*
88+
* @return JComponent
89+
*/
90+
@Nullable
91+
@Override
92+
protected JComponent createCenterPanel() {
93+
return null; // Subclasses must override this method
6694
}
6795

6896
/**
@@ -76,7 +104,7 @@ protected void onCancel() {
76104
* Right way to hide dialog window.
77105
*/
78106
protected void exit() {
79-
dispose();
107+
close(CANCEL_EXIT_CODE);
80108
}
81109

82110
/**
@@ -102,6 +130,24 @@ protected final void onOK() {
102130
}
103131
}
104132

133+
/**
134+
* Called when the OK button is pressed.
135+
* This method is called by DialogWrapper.
136+
*/
137+
@Override
138+
public void doOKAction() {
139+
onOK();
140+
}
141+
142+
/**
143+
* Called when the Cancel button is pressed.
144+
* This method is called by DialogWrapper.
145+
*/
146+
@Override
147+
public void doCancelAction() {
148+
onCancel();
149+
}
150+
105151
/**
106152
* Validate all form fields.
107153
*
@@ -217,11 +263,10 @@ protected void showErrorMessage(final String errorMessage) {
217263
if (isValidationErrorShown) {
218264
return;
219265
}
220-
JOptionPane.showMessageDialog(
221-
this,
266+
Messages.showErrorDialog(
267+
getContentPanel(),
222268
errorMessage,
223-
errorTitle,
224-
JOptionPane.ERROR_MESSAGE
269+
errorTitle
225270
);
226271
isValidationErrorShown = true;
227272
}
@@ -302,10 +347,13 @@ private int getParentTabPaneForComponent(final @NotNull Container component) {
302347
return getParentTabPaneForComponent(parent);
303348
}
304349

305-
@Override
306-
public void setVisible(final boolean status) {
350+
/**
351+
* Show the dialog.
352+
* This method should be used instead of setVisible(true).
353+
*/
354+
public void showDialog() {
307355
new PlaceholderInitializerUtil(this).initialize();
308-
super.setVisible(status);
356+
show();
309357
}
310358

311359
/**
@@ -317,7 +365,7 @@ public void setVisible(final boolean status) {
317365
* <p>1) specify method in which desired field is focused:</p><br/>
318366
* <pre>
319367
* public void focusOnTheSampleField() {
320-
* sampleField.requestFocusInWindow();
368+
* sampleField.requestFocus();
321369
* }
322370
* </pre>
323371
*
@@ -327,8 +375,6 @@ public void setVisible(final boolean status) {
327375
* new FocusOnAFieldListener(this::focusOnTheSampleField)
328376
* )
329377
* </pre>
330-
*
331-
* @see #requestFocusInWindow()
332378
*/
333379
public static final class FocusOnAFieldListener implements ComponentListener {
334380

src/main/java/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import javax.swing.JTextField;
4343
import javax.swing.KeyStroke;
4444
import org.jetbrains.annotations.NotNull;
45+
import org.jetbrains.annotations.Nullable;
4546

4647
@SuppressWarnings({
4748
"PMD.TooManyFields",
@@ -122,40 +123,31 @@ public CreateAPluginDialog(
122123
final Method targetMethod,
123124
final PhpClass targetClass
124125
) {
125-
super();
126+
super(project);
126127
this.project = project;
127128
this.targetMethod = targetMethod;
128129
this.targetClass = targetClass;
129130

130-
setContentPane(contentPane);
131-
setModal(true);
132131
setTitle(CreateAPluginAction.ACTION_DESCRIPTION);
133-
getRootPane().setDefaultButton(buttonOK);
134132
fillPluginTypeOptions();
135133
fillTargetAreaOptions();
136134

137135
if (targetMethod != null) {
138136
this.targetMethodLabel.setVisible(false);
139137
}
140138

141-
buttonOK.addActionListener((final ActionEvent event) -> onOK());
142-
buttonCancel.addActionListener((final ActionEvent event) -> onCancel());
143-
144-
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
145-
addWindowListener(new WindowAdapter() {
146-
@Override
147-
public void windowClosing(final WindowEvent event) {
148-
onCancel();
149-
}
150-
});
151-
152-
contentPane.registerKeyboardAction(
153-
(final ActionEvent event) -> onCancel(),
154-
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
155-
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
156-
);
139+
init();
140+
}
157141

158-
addComponentListener(new FocusOnAFieldListener(() -> pluginModule.requestFocusInWindow()));
142+
/**
143+
* Create center panel.
144+
*
145+
* @return JComponent
146+
*/
147+
@Nullable
148+
@Override
149+
protected JComponent createCenterPanel() {
150+
return contentPane;
159151
}
160152

161153
private void fillPluginTypeOptions() {
@@ -261,9 +253,8 @@ public static void open(
261253
targetMethod,
262254
targetClass
263255
);
264-
dialog.pack();
265256
dialog.centerDialog(dialog);
266-
dialog.setVisible(true);
257+
dialog.showDialog();
267258
}
268259

269260
private void createUIComponents() {

src/main/java/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAnObserverDialog.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import javax.swing.JTextField;
3737
import javax.swing.KeyStroke;
3838
import org.jetbrains.annotations.NotNull;
39+
import org.jetbrains.annotations.Nullable;
3940

4041
@SuppressWarnings({
4142
"PMD.TooManyFields",
@@ -93,35 +94,24 @@ public class CreateAnObserverDialog extends AbstractDialog {
9394
* @param targetEvent Action Event
9495
*/
9596
public CreateAnObserverDialog(@NotNull final Project project, final String targetEvent) {
96-
super();
97+
super(project);
9798

9899
this.project = project;
99100
this.targetEvent = targetEvent;
100101

101-
setContentPane(contentPane);
102-
setModal(true);
103102
setTitle(CreateAnObserverAction.ACTION_DESCRIPTION);
104-
getRootPane().setDefaultButton(buttonOK);
105103
fillTargetAreaOptions();
106104

107105
buttonOK.addActionListener(e -> onOK());
108106
buttonCancel.addActionListener(e -> onCancel());
109107

110-
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
111-
addWindowListener(new WindowAdapter() {
112-
@Override
113-
public void windowClosing(final WindowEvent event) {
114-
onCancel();
115-
}
116-
});
117-
118108
contentPane.registerKeyboardAction(
119109
e -> onCancel(),
120110
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
121111
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
122112
);
123113

124-
addComponentListener(new FocusOnAFieldListener(() -> observerName.requestFocusInWindow()));
114+
init();
125115
}
126116

127117
/**
@@ -132,9 +122,19 @@ public void windowClosing(final WindowEvent event) {
132122
*/
133123
public static void open(@NotNull final Project project, final String targetEvent) {
134124
final CreateAnObserverDialog dialog = new CreateAnObserverDialog(project, targetEvent);
135-
dialog.pack();
136125
dialog.centerDialog(dialog);
137-
dialog.setVisible(true);
126+
dialog.showDialog();
127+
}
128+
129+
/**
130+
* Create center panel.
131+
*
132+
* @return JComponent
133+
*/
134+
@Nullable
135+
@Override
136+
protected JComponent createCenterPanel() {
137+
return contentPane;
138138
}
139139

140140
/**
@@ -215,4 +215,3 @@ private String getObserverClassFqn() {
215215
return getNamespace().concat(Package.fqnSeparator).concat(getObserverClassName());
216216
}
217217
}
218-

src/main/java/com/magento/idea/magento2plugin/actions/generation/dialog/GatherArrayValuesDialog.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
1414
import com.magento.idea.magento2plugin.magento.packages.DiArgumentType;
1515
import com.magento.idea.magento2plugin.ui.table.TableGroupWrapper;
16+
import org.jetbrains.annotations.Nullable;
1617
import java.awt.Color;
1718
import java.awt.event.KeyEvent;
1819
import java.awt.event.WindowAdapter;
@@ -61,28 +62,16 @@ public GatherArrayValuesDialog(
6162
final @NotNull Project project,
6263
final DiArrayValueData arrayValueData
6364
) {
64-
super();
65+
super(project);
6566

6667
this.project = project;
6768
this.arrayValueData = arrayValueData;
6869

69-
setContentPane(contentPane);
70-
setModal(true);
7170
setTitle(InjectConstructorArgumentAction.GATHER_ARRAY_VALUES_ACTION_DESCRIPTION);
72-
getRootPane().setDefaultButton(buttonOK);
7371

7472
buttonOK.addActionListener(event -> onOK());
7573
buttonCancel.addActionListener(event -> onCancel());
7674

77-
// call onCancel() when cross is clicked
78-
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
79-
addWindowListener(new WindowAdapter() {
80-
@Override
81-
public void windowClosing(final WindowEvent event) {
82-
onCancel();
83-
}
84-
});
85-
8675
// call onCancel() on ESCAPE
8776
contentPane.registerKeyboardAction(
8877
event -> onCancel(),
@@ -93,6 +82,8 @@ public void windowClosing(final WindowEvent event) {
9382
initTable();
9483
itemsTableErrorMessage.setVisible(false);
9584
itemsTableErrorMessage.setText("");
85+
86+
init();
9687
}
9788

9889
/**
@@ -106,9 +97,19 @@ public static void open(
10697
final DiArrayValueData arrayValueData
10798
) {
10899
final GatherArrayValuesDialog dialog = new GatherArrayValuesDialog(project, arrayValueData);
109-
dialog.pack();
110100
dialog.centerDialog(dialog);
111-
dialog.setVisible(true);
101+
dialog.showDialog();
102+
}
103+
104+
/**
105+
* Create center panel.
106+
*
107+
* @return JComponent
108+
*/
109+
@Nullable
110+
@Override
111+
protected JComponent createCenterPanel() {
112+
return contentPane;
112113
}
113114

114115
/**

0 commit comments

Comments
 (0)