9
9
import com .intellij .openapi .ui .ComboBox ;
10
10
import com .intellij .psi .PsiDirectory ;
11
11
import com .intellij .psi .PsiFile ;
12
+ import com .intellij .ui .DocumentAdapter ;
12
13
import com .intellij .util .indexing .FileBasedIndex ;
13
14
import com .magento .idea .magento2plugin .actions .context .php .NewObserverAction ;
14
15
import com .magento .idea .magento2plugin .actions .generation .ModuleObserverData ;
28
29
import com .magento .idea .magento2plugin .magento .packages .Package ;
29
30
import com .magento .idea .magento2plugin .stubs .indexes .EventNameIndex ;
30
31
import com .magento .idea .magento2plugin .ui .FilteredComboBox ;
32
+ import com .magento .idea .magento2plugin .util .CamelCaseToSnakeCase ;
31
33
import java .awt .event .ActionEvent ;
32
34
import java .awt .event .KeyEvent ;
33
35
import java .awt .event .WindowAdapter ;
34
36
import java .awt .event .WindowEvent ;
35
37
import java .util .ArrayList ;
36
38
import java .util .Collection ;
39
+ import java .util .Locale ;
37
40
import javax .swing .JButton ;
38
41
import javax .swing .JComboBox ;
39
42
import javax .swing .JComponent ;
40
43
import javax .swing .JLabel ;
41
44
import javax .swing .JPanel ;
42
45
import javax .swing .JTextField ;
43
46
import javax .swing .KeyStroke ;
47
+ import javax .swing .event .DocumentEvent ;
48
+ import org .jetbrains .annotations .NotNull ;
44
49
45
50
@ SuppressWarnings ({
46
51
"PMD.TooManyFields" ,
47
52
"PMD.ExcessiveImports" ,
48
- "PMD.UnusedPrivateMethod" ,
49
53
"PMD.AvoidInstantiatingObjectsInLoops" ,
50
54
"PMD.ReturnEmptyCollectionRatherThanNull"
51
55
})
52
56
public class NewObserverDialog extends AbstractDialog {
57
+
53
58
private static final String OBSERVER_NAME = "Observer Name" ;
54
59
private static final String CLASS_NAME = "Class Name" ;
55
60
private final Project project ;
@@ -127,6 +132,14 @@ public void windowClosing(final WindowEvent event) {
127
132
JComponent .WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
128
133
);
129
134
135
+ className .getDocument ().addDocumentListener (new DocumentAdapter () {
136
+ @ SuppressWarnings ("PMD.AccessorMethodGeneration" )
137
+ @ Override
138
+ public void textChanged (final @ NotNull DocumentEvent event ) {
139
+ autoCompleteObserverName ();
140
+ }
141
+ });
142
+
130
143
addComponentListener (
131
144
new FocusOnAFieldListener (() -> className .requestFocusInWindow ())
132
145
);
@@ -155,6 +168,10 @@ public static void open(
155
168
dialog .setVisible (true );
156
169
}
157
170
171
+ private String getModuleName () {
172
+ return modulePackage .concat (Package .fqnSeparator ).concat (moduleName );
173
+ }
174
+
158
175
public String getObserverName () {
159
176
return observerName .getText ().trim ();
160
177
}
@@ -181,7 +198,8 @@ protected void onOK() {
181
198
182
199
if (!getDirectoryStructure ().isEmpty ()) {
183
200
observerDirectory = DirectoryGenerator .getInstance ().findOrCreateSubdirectories (
184
- baseDir , getDirectoryStructure ()
201
+ baseDir ,
202
+ getDirectoryStructure ()
185
203
);
186
204
}
187
205
new ModuleObserverGenerator (
@@ -192,38 +210,29 @@ baseDir, getDirectoryStructure()
192
210
getEvenName (),
193
211
observerDirectory ,
194
212
ModuleObserverFile .resolveClassNameFromInput (getClassName ())
195
- ), project ).generate (NewObserverAction .ACTION_NAME , true );
196
-
197
- new ObserverEventsXmlGenerator (new ObserverEventsXmlData (
198
- getObserverArea (),
199
- getModuleName ().replace (
200
- Package .fqnSeparator ,
201
- Package .vendorModuleNameSeparator
202
213
),
203
- getEvenName (),
204
- getObserverName (),
205
- getObserverClassFqn ().concat (Package .fqnSeparator ).concat (
206
- ModuleObserverFile .resolveClassNameFromInput (getClassName ())
207
- )
208
- ), project ).generate (NewObserverAction .ACTION_NAME );
214
+ project
215
+ ).generate (NewObserverAction .ACTION_NAME , true );
216
+
217
+ new ObserverEventsXmlGenerator (
218
+ new ObserverEventsXmlData (
219
+ getObserverArea (),
220
+ getModuleName ().replace (
221
+ Package .fqnSeparator ,
222
+ Package .vendorModuleNameSeparator
223
+ ),
224
+ getEvenName (),
225
+ getObserverName (),
226
+ getObserverClassFqn ().concat (Package .fqnSeparator ).concat (
227
+ ModuleObserverFile .resolveClassNameFromInput (getClassName ())
228
+ )
229
+ ),
230
+ project
231
+ ).generate (NewObserverAction .ACTION_NAME );
209
232
exit ();
210
233
}
211
234
}
212
235
213
- private void createUIComponents () {
214
- observerArea = new ComboBox <>();
215
-
216
- for (final Areas areaEntry : Areas .values ()) {
217
- observerArea .addItem (new ComboBoxItemData (areaEntry .toString (), areaEntry .toString ()));
218
- }
219
-
220
- final Collection <String > events = FileBasedIndex .getInstance ().getAllKeys (
221
- EventNameIndex .KEY , project
222
- );
223
-
224
- this .eventName = new FilteredComboBox (new ArrayList <>(events ));
225
- }
226
-
227
236
private boolean validateFields () {
228
237
final PsiFile [] directoryFiles = getDirectoryFiles (baseDir );
229
238
@@ -276,10 +285,6 @@ private PsiFile[] getDirectoryFiles(final PsiDirectory targetDirectory) {
276
285
return directory .getFiles ();
277
286
}
278
287
279
- private String getModuleName () {
280
- return modulePackage .concat (Package .fqnSeparator ).concat (moduleName );
281
- }
282
-
283
288
private String getObserverClassFqn () {
284
289
final String folderStructureFqn = getDirectoryStructure ().replace (
285
290
Package .V_FILE_SEPARATOR , Package .fqnSeparator
@@ -292,4 +297,37 @@ private String getObserverClassFqn() {
292
297
293
298
return getModuleName ().concat (Package .fqnSeparator ).concat (folderFqn );
294
299
}
300
+
301
+ private void autoCompleteObserverName () {
302
+ final String className = getClassName ();
303
+
304
+ if (className .isEmpty ()) {
305
+ return ;
306
+ }
307
+ final String modifiedClassName = ModuleObserverFile .resolveClassNameFromInput (className );
308
+ final String classNameInSnakeCase = CamelCaseToSnakeCase .getInstance ()
309
+ .convert (modifiedClassName );
310
+
311
+ final String modulePackageModified = modulePackage .substring (0 , 1 )
312
+ .toLowerCase (Locale .getDefault ()) + modulePackage .substring (1 );
313
+ final String moduleNameModified = moduleName .substring (0 , 1 )
314
+ .toLowerCase (Locale .getDefault ()) + moduleName .substring (1 );
315
+
316
+ observerName .setText (
317
+ modulePackageModified + "_" + moduleNameModified + "_" + classNameInSnakeCase
318
+ );
319
+ }
320
+
321
+ @ SuppressWarnings ({"PMD.UnusedPrivateMethod" })
322
+ private void createUIComponents () {
323
+ observerArea = new ComboBox <>();
324
+
325
+ for (final Areas areaEntry : Areas .values ()) {
326
+ observerArea .addItem (new ComboBoxItemData (areaEntry .toString (), areaEntry .toString ()));
327
+ }
328
+ final Collection <String > events = FileBasedIndex .getInstance ().getAllKeys (
329
+ EventNameIndex .KEY , project
330
+ );
331
+ this .eventName = new FilteredComboBox (new ArrayList <>(events ));
332
+ }
295
333
}
0 commit comments