forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigurationDialog.java
275 lines (240 loc) · 9.85 KB
/
ConfigurationDialog.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
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2uct.ui;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.LabeledComponent;
import com.intellij.openapi.ui.TextBrowseFolderListener;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.ui.JBColor;
import com.intellij.util.ui.UIUtil;
import com.magento.idea.magento2plugin.actions.generation.data.ui.ComboBoxItemData;
import com.magento.idea.magento2plugin.actions.generation.dialog.AbstractDialog;
import com.magento.idea.magento2plugin.project.Settings;
import com.magento.idea.magento2plugin.util.magento.MagentoVersionUtil;
import com.magento.idea.magento2uct.actions.ConfigureUctAction;
import com.magento.idea.magento2uct.packages.IssueSeverityLevel;
import com.magento.idea.magento2uct.packages.SupportedVersion;
import com.magento.idea.magento2uct.settings.UctSettingsService;
import com.magento.idea.magento2uct.util.module.UctModulePathValidatorUtil;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Objects;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings({"PMD.TooManyFields", "PMD.ExcessiveImports"})
public class ConfigurationDialog extends AbstractDialog {
private final Project project;
private final UctSettingsService settingsService;
private JCheckBox enable;
private LabeledComponent<TextFieldWithBrowseButton> modulePath;
private JCheckBox ignoreCurrentVersion;
private JComboBox<ComboBoxItemData> currentVersion;
private JComboBox<ComboBoxItemData> targetVersion;
private JComboBox<ComboBoxItemData> issueSeverityLevel;
private JPanel contentPanel;
private JButton buttonCancel;
private JButton buttonOk;
private JLabel currentVersionLabel;//NOPMD
private JLabel modulePathLabel;//NOPMD
private JLabel targetVersionLabel;//NOPMD
private JLabel issueSeverityLevelLabel;//NOPMD
private JLabel modulePathError;//NOPMD
private JLabel enableComment;//NOPMD
private JLabel enableCommentPath;//NOPMD
/**
* Configuration dialog.
*
* @param project Project
*/
public ConfigurationDialog(final @NotNull Project project) {
super();
this.project = project;
settingsService = UctSettingsService.getInstance(project);
setContentPane(contentPanel);
setModal(true);
setTitle(ConfigureUctAction.ACTION_NAME);
getRootPane().setDefaultButton(buttonOk);
buttonOk.addActionListener(event -> onOK());
buttonCancel.addActionListener(event -> onCancel());
// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(final WindowEvent event) {
onCancel();
}
});
// call onCancel() on ESCAPE
contentPanel.registerKeyboardAction(
event -> onCancel(),
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
);
modulePathError.setText("");
modulePathError.setFont(UIUtil.getLabelFont(UIUtil.FontSize.SMALL));
modulePathError.setForeground(new Color(252, 119, 83));
enableComment.setForeground(JBColor.blue);
enableCommentPath.setForeground(JBColor.blue);
setDefaultValues();
}
/**
* Open configuration dialog window.
*
* @param project Project
*/
public static void open(final @NotNull Project project) {
final ConfigurationDialog dialog = new ConfigurationDialog(project);
dialog.pack();
dialog.centerDialog(dialog);
dialog.setVisible(true);
}
/**
* Save configuration.
*/
private void onOK() {
modulePathError.setText("");
if (modulePath.getComponent().getText().isEmpty()
|| !UctModulePathValidatorUtil.validate(modulePath.getComponent().getText())) {
modulePathError.setText("The `Path To Analyse` field is empty or invalid");
return;
}
settingsService.setEnabled(enable.isSelected());
final ComboBoxItemData currentVersionItemData =
(ComboBoxItemData) currentVersion.getSelectedItem();
String currentVersionValue = "";
if (currentVersionItemData != null) {
currentVersionValue = currentVersionItemData.getKey();
}
settingsService.setCurrentVersion(
currentVersionValue.isEmpty()
? null//NOPMD
: SupportedVersion.getVersion(currentVersionValue)
);
final SupportedVersion targetVersionValue = SupportedVersion.getVersion(
targetVersion.getSelectedItem().toString()
);
if (targetVersionValue != null) {
settingsService.setTargetVersion(targetVersionValue);
}
settingsService.setModulePath(modulePath.getComponent().getText());
settingsService.setMinIssueSeverityLevel(
Integer.parseInt(
((ComboBoxItemData) issueSeverityLevel.getSelectedItem()).getKey()
)
);
settingsService.setIgnoreCurrentVersion(ignoreCurrentVersion.isSelected());
exit();
}
/**
* Set default values for inputs.
*/
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.CognitiveComplexity"})
private void setDefaultValues() {
enable.setSelected(settingsService.isEnabled());
if (settingsService.getModulePath() == null) {
final String basePath = Settings.getMagentoPath(project);
if (basePath != null) {
modulePath.getComponent().setText(basePath);
}
} else {
modulePath.getComponent().setText(settingsService.getModulePath());
}
if (settingsService.getCurrentVersion() == null) {
setSelectedValueByItsKey(currentVersion, null);
final String basePath = Settings.getMagentoPath(project);
if (basePath != null) {
final String resolvedVersion = MagentoVersionUtil.get(project, basePath);
if (resolvedVersion != null
&& !MagentoVersionUtil.DEFAULT_VERSION.equals(resolvedVersion)) {
final SupportedVersion versionCandidate = SupportedVersion.getVersion(
resolvedVersion
);
setSelectedValueByItsKey(
currentVersion,
versionCandidate == null ? null : versionCandidate.getVersion()
);
}
}
} else {
setSelectedValueByItsKey(
currentVersion,
settingsService.getCurrentVersion().getVersion()
);
}
if (settingsService.getTargetVersion() != null) {
setSelectedValueByItsKey(
targetVersion,
settingsService.getTargetVersion().getVersion()
);
}
if (settingsService.getMinIssueLevel() == null) {
setSelectedValueByItsKey(
issueSeverityLevel,
String.valueOf(IssueSeverityLevel.WARNING.getLevel())
);
} else {
setSelectedValueByItsKey(
issueSeverityLevel,
String.valueOf(settingsService.getMinIssueLevel().getLevel())
);
}
final Boolean shouldIgnore = settingsService.shouldIgnoreCurrentVersion();
ignoreCurrentVersion.setSelected(Objects.requireNonNullElse(shouldIgnore, false));
}
/**
* Set selected combobox item by key.
*
* @param comboBox JComboBox[ComboBoxItemData]
* @param key String
*/
private void setSelectedValueByItsKey(
final JComboBox<ComboBoxItemData> comboBox,
final String key
) {
for (int index = 0; index < comboBox.getItemCount(); index++) {
if (comboBox.getItemAt(index).getKey().equals(key)) {
comboBox.setSelectedIndex(index);
}
}
}
/**
* Create custom components and fill their entries.
*/
@SuppressWarnings({"PMD.UnusedPrivateMethod", "PMD.AvoidInstantiatingObjectsInLoops"})
private void createUIComponents() {
targetVersion = new ComboBox<>();
for (final String version : SupportedVersion.getSupportedVersions()) {
targetVersion.addItem(new ComboBoxItemData(version, version));
}
currentVersion = new ComboBox<>();
currentVersion.addItem(new ComboBoxItemData("", "Less than 2.3.0"));
for (final String version : SupportedVersion.getSupportedVersions()) {
currentVersion.addItem(new ComboBoxItemData(version, version));
}
issueSeverityLevel = new ComboBox<>();
for (final IssueSeverityLevel level : IssueSeverityLevel.getSeverityLabels()) {
issueSeverityLevel.addItem(
new ComboBoxItemData(String.valueOf(level.getLevel()), level.getLabel())
);
}
modulePath = new LabeledComponent<>();
modulePath.setComponent(new TextFieldWithBrowseButton());
modulePath.getComponent().addBrowseFolderListener(
new TextBrowseFolderListener(
new FileChooserDescriptor(false, true, false, false, false, false)
)
);
}
}