forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewObserverDialog.java
364 lines (317 loc) · 13.5 KB
/
NewObserverDialog.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 © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.actions.generation.dialog;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.ui.DocumentAdapter;
import com.intellij.util.indexing.FileBasedIndex;
import com.magento.idea.magento2plugin.actions.context.php.NewObserverAction;
import com.magento.idea.magento2plugin.actions.generation.ModuleObserverData;
import com.magento.idea.magento2plugin.actions.generation.data.ObserverEventsXmlData;
import com.magento.idea.magento2plugin.actions.generation.data.ui.ComboBoxItemData;
import com.magento.idea.magento2plugin.actions.generation.dialog.reflection.GetReflectionFieldUtil;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.DirectoryRule;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.IdentifierWithColonRule;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpClassRule;
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleObserverGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.ObserverEventsXmlGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator;
import com.magento.idea.magento2plugin.lang.roots.MagentoTestSourceFilter;
import com.magento.idea.magento2plugin.magento.files.ModuleObserverFile;
import com.magento.idea.magento2plugin.magento.packages.Areas;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.stubs.indexes.EventNameIndex;
import com.magento.idea.magento2plugin.ui.FilteredComboBox;
import com.magento.idea.magento2plugin.util.CamelCaseToSnakeCase;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.DocumentEvent;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings({
"PMD.TooManyFields",
"PMD.ExcessiveImports",
"PMD.AvoidInstantiatingObjectsInLoops",
"PMD.ReturnEmptyCollectionRatherThanNull"
})
public class NewObserverDialog extends AbstractDialog {
private static final String OBSERVER_NAME = "Observer Name";
private static final String CLASS_NAME = "Class Name";
private final Project project;
private final PsiDirectory baseDir;
private final String moduleName;
private final String modulePackage;
private JPanel contentPanel;
private JButton buttonOK;
private JButton buttonCancel;
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, OBSERVER_NAME})
@FieldValidation(rule = RuleRegistry.IDENTIFIER_WITH_COLON,
message = {IdentifierWithColonRule.MESSAGE, OBSERVER_NAME})
private JTextField observerName;
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, CLASS_NAME})
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
message = {PhpClassRule.MESSAGE, CLASS_NAME})
private JTextField className;
private JTextField directoryStructure;
private FilteredComboBox eventName;
private JComboBox<ComboBoxItemData> observerArea;
private JLabel observerNameLabel;// NOPMD
private JLabel observerNameErrorMessage;// NOPMD
private JLabel classNameLabel;// NOPMD
private JLabel classNameErrorMessage;// NOPMD
private JLabel directoryStructureLabel;// NOPMD
private JLabel directoryStructureErrorMessage;// NOPMD
private JLabel eventNamesLabel;// NOPMD
private JLabel targetAreaLabel;// NOPMD
/**
* Constructor.
*
* @param project Project
* @param directory PsiDirectory
* @param modulePackage String
* @param moduleName String
*/
public NewObserverDialog(
final Project project,
final PsiDirectory directory,
final String modulePackage,
final String moduleName
) {
super();
this.project = project;
this.baseDir = directory;
this.modulePackage = modulePackage;
this.moduleName = moduleName;
setContentPane(contentPanel);
setModal(false);
setTitle(NewObserverAction.ACTION_DESCRIPTION);
getRootPane().setDefaultButton(buttonOK);
buttonOK.addActionListener((final ActionEvent event) -> onOK());
buttonCancel.addActionListener((final ActionEvent 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(
(final ActionEvent event) -> onCancel(),
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
);
className.getDocument().addDocumentListener(new DocumentAdapter() {
@SuppressWarnings("PMD.AccessorMethodGeneration")
@Override
public void textChanged(final @NotNull DocumentEvent event) {
autoCompleteObserverName();
}
});
addComponentListener(
new FocusOnAFieldListener(() -> className.requestFocusInWindow())
);
}
/**
* Open dialog.
*
* @param project Project
* @param directory PsiDirectory
*/
public static void open(
final Project project,
final PsiDirectory directory,
final String modulePackage,
final String moduleName
) {
final NewObserverDialog dialog = new NewObserverDialog(
project,
directory,
modulePackage,
moduleName
);
dialog.pack();
dialog.centerDialog(dialog);
dialog.setVisible(true);
}
private String getModuleName() {
return modulePackage.concat(Package.fqnSeparator).concat(moduleName);
}
public String getObserverName() {
return observerName.getText().trim();
}
public String getClassName() {
return className.getText().trim();
}
public String getEventName() {
return eventName.getSelectedItem().toString();
}
public String getObserverArea() {
return this.observerArea.getSelectedItem().toString();
}
public String getDirectoryStructure() {
return directoryStructure.getText().trim();
}
protected void onOK() {
if (validateFields()) {
PsiDirectory observerDirectory = baseDir;
if (!getDirectoryStructure().isEmpty()) {
observerDirectory = DirectoryGenerator.getInstance().findOrCreateSubdirectories(
baseDir,
getDirectoryStructure()
);
}
new ModuleObserverGenerator(
new ModuleObserverData(
modulePackage,
moduleName,
getObserverClassFqn(),
getEventName(),
observerDirectory,
ModuleObserverFile.resolveClassNameFromInput(getClassName())
),
project
).generate(NewObserverAction.ACTION_NAME, true);
new ObserverEventsXmlGenerator(
new ObserverEventsXmlData(
getObserverArea(),
getModuleName().replace(
Package.fqnSeparator,
Package.vendorModuleNameSeparator
),
getEventName(),
getObserverName(),
getObserverClassFqn().concat(Package.fqnSeparator).concat(
ModuleObserverFile.resolveClassNameFromInput(getClassName())
)
),
project
).generate(NewObserverAction.ACTION_NAME);
exit();
}
}
private boolean validateFields() {
final PsiFile[] directoryFiles = getDirectoryFiles(baseDir);
final Field classNameField = GetReflectionFieldUtil.getByName("className", this.getClass());
if (directoryFiles != null && classNameField != null) {
for (final PsiFile file : directoryFiles) {
final String className = ModuleObserverFile.resolveClassNameFromInput(
getClassName()
);
if (file.getName().equals(className + ModuleObserverFile.EXTENSION)) {
showErrorMessage(
classNameField,
"Class name " + className + " already exist."
);
return false;
}
}
}
final Field directoryStructureField = GetReflectionFieldUtil.getByName(
"directoryStructure",
this.getClass()
);
if (!getDirectoryStructure().isEmpty() && directoryStructureField != null
&& !DirectoryRule.getInstance().check(getDirectoryStructure())
) {
showErrorMessage(
directoryStructureField,
"The Directory Path field does not contain a valid directory."
);
return false;
}
return validateFormFields();
}
private PsiFile[] getDirectoryFiles(final PsiDirectory targetDirectory) {
PsiDirectory directory = targetDirectory;
if (!getDirectoryStructure().isEmpty()) {
final String[] directories = getDirectoryStructure().split(Package.V_FILE_SEPARATOR);
for (final String dir : directories) {
final PsiDirectory subdirectory = directory.findSubdirectory(dir);
if (subdirectory == null) {
return null;
}
directory = subdirectory;
}
}
return directory.getFiles();
}
private String getObserverClassFqn() {
final String folderStructureFqn = getDirectoryStructure().replace(
Package.V_FILE_SEPARATOR, Package.fqnSeparator
);
String folderFqn = NewObserverAction.ROOT_DIRECTORY;
if (!folderStructureFqn.isEmpty()) {
folderFqn = folderFqn.concat(Package.fqnSeparator).concat(folderStructureFqn);
}
return getModuleName().concat(Package.fqnSeparator).concat(folderFqn);
}
private void autoCompleteObserverName() {
final String className = getClassName();
if (className.isEmpty()) {
return;
}
final String modifiedClassName = ModuleObserverFile.resolveClassNameFromInput(className);
final String classNameInSnakeCase = CamelCaseToSnakeCase.getInstance()
.convert(modifiedClassName);
final String modulePackageModified = modulePackage.substring(0, 1)
.toLowerCase(Locale.getDefault()) + modulePackage.substring(1);
final String moduleNameModified = moduleName.substring(0, 1)
.toLowerCase(Locale.getDefault()) + moduleName.substring(1);
observerName.setText(
modulePackageModified + "_" + moduleNameModified + "_" + classNameInSnakeCase
);
}
@SuppressWarnings({"PMD.UnusedPrivateMethod"})
private void createUIComponents() {
observerArea = new ComboBox<>();
for (final Areas areaEntry : Areas.values()) {
observerArea.addItem(new ComboBoxItemData(areaEntry.toString(), areaEntry.toString()));
}
final Collection<String> events = FileBasedIndex.getInstance().getAllKeys(
EventNameIndex.KEY, project
);
// Filter all events declared only for tests.
events.removeIf(event -> {
final Collection<VirtualFile> files = FileBasedIndex.getInstance()
.getContainingFiles(
EventNameIndex.KEY,
event,
GlobalSearchScope.allScope(project)
);
final List<VirtualFile> realObservers = new ArrayList<>();
for (final VirtualFile file : files) {
if (!MagentoTestSourceFilter.isTestSources(file, project)) {
realObservers.add(file);
}
}
return realObservers.isEmpty();
});
this.eventName = new FilteredComboBox(new ArrayList<>(events));
}
}