27
27
import com .magento .idea .magento2plugin .magento .packages .File ;
28
28
import com .magento .idea .magento2plugin .magento .packages .Package ;
29
29
import com .magento .idea .magento2plugin .ui .FilteredComboBox ;
30
+ import com .magento .idea .magento2plugin .util .php .PhpTypeMetadataParserUtil ;
30
31
import java .awt .event .ActionEvent ;
31
32
import java .awt .event .KeyEvent ;
32
33
import java .awt .event .WindowAdapter ;
33
34
import java .awt .event .WindowEvent ;
35
+ import java .util .ArrayList ;
34
36
import java .util .List ;
35
37
import javax .swing .JButton ;
36
38
import javax .swing .JComboBox ;
50
52
public class CreateAPluginDialog extends AbstractDialog {
51
53
@ NotNull
52
54
private final Project project ;
53
- private final Method targetMethod ;
55
+ private Method targetMethod ;
54
56
private final PhpClass targetClass ;
55
57
private JPanel contentPane ;
56
58
private JButton buttonOK ;
@@ -63,13 +65,20 @@ public class CreateAPluginDialog extends AbstractDialog {
63
65
private static final String SORT_ORDER = "sort order" ;
64
66
private static final String PLUGIN_NAME = "plugin name" ;
65
67
private static final String TARGET_MODULE = "target module" ;
68
+ private static final String TARGET_METHOD = "target method" ;
66
69
67
70
@ FieldValidation (rule = RuleRegistry .NOT_EMPTY ,
68
71
message = {NotEmptyRule .MESSAGE , TARGET_MODULE })
69
72
@ FieldValidation (rule = RuleRegistry .BOX_NOT_EMPTY ,
70
73
message = {BoxNotEmptyRule .MESSAGE , TARGET_MODULE })
71
74
private FilteredComboBox pluginModule ;
72
75
76
+ @ FieldValidation (rule = RuleRegistry .NOT_EMPTY ,
77
+ message = {NotEmptyRule .MESSAGE , TARGET_METHOD })
78
+ @ FieldValidation (rule = RuleRegistry .BOX_NOT_EMPTY ,
79
+ message = {BoxNotEmptyRule .MESSAGE , TARGET_METHOD })
80
+ private FilteredComboBox targetMethodSelect ;
81
+
73
82
@ FieldValidation (rule = RuleRegistry .NOT_EMPTY ,
74
83
message = {NotEmptyRule .MESSAGE , CLASS_NAME })
75
84
@ FieldValidation (rule = RuleRegistry .PHP_CLASS ,
@@ -99,6 +108,7 @@ public class CreateAPluginDialog extends AbstractDialog {
99
108
private JLabel pluginNameLabel ;//NOPMD
100
109
private JLabel pluginClassNameLabel ;//NOPMD
101
110
private JLabel pluginSortOrderLabel ;//NOPMD
111
+ private JLabel targetMethodLabel ;
102
112
103
113
/**
104
114
* Constructor.
@@ -124,6 +134,10 @@ public CreateAPluginDialog(
124
134
fillPluginTypeOptions ();
125
135
fillTargetAreaOptions ();
126
136
137
+ if (targetMethod != null ) {
138
+ this .targetMethodLabel .setVisible (false );
139
+ }
140
+
127
141
buttonOK .addActionListener ((final ActionEvent event ) -> onOK ());
128
142
buttonCancel .addActionListener ((final ActionEvent event ) -> onCancel ());
129
143
@@ -157,6 +171,9 @@ private void fillTargetAreaOptions() {
157
171
}
158
172
159
173
protected void onOK () {
174
+ if (targetMethod == null ) {
175
+ targetMethod = getSelectedTargetMethod ();
176
+ }
160
177
if (validateFormFields ()) {
161
178
new PluginClassGenerator (new PluginFileData (
162
179
getPluginDirectory (),
@@ -209,6 +226,25 @@ public String getPluginModule() {
209
226
return this .pluginModule .getSelectedItem ().toString ();
210
227
}
211
228
229
+ /**
230
+ * Searches and returns a selected target method.
231
+ *
232
+ * @return Method target method
233
+ */
234
+ public Method getSelectedTargetMethod () {
235
+ final String selectedMethodString = this .targetMethodSelect .getSelectedItem ().toString ();
236
+ final List <Method > publicMethods = PhpTypeMetadataParserUtil .getPublicMethods (
237
+ this .targetClass
238
+ );
239
+ for (final Method method : publicMethods ) {
240
+ if (method .getName ().equals (selectedMethodString )) {
241
+ return method ;
242
+ }
243
+ }
244
+
245
+ return null ;
246
+ }
247
+
212
248
/**
213
249
* Open an action dialog.
214
250
*
@@ -236,6 +272,18 @@ private void createUIComponents() {
236
272
.getEditableModuleNames ();
237
273
238
274
this .pluginModule = new FilteredComboBox (allModulesList );
275
+
276
+ final List <Method > publicMethods
277
+ = PhpTypeMetadataParserUtil .getPublicMethods (this .targetClass );
278
+ final List <String > methodList = new ArrayList <>();
279
+ for (final Method method : publicMethods ) {
280
+ methodList .add (method .getName ());
281
+ }
282
+
283
+ this .targetMethodSelect = new FilteredComboBox (methodList );
284
+ if (targetMethod != null ) {
285
+ this .targetMethodSelect .setVisible (false );
286
+ }
239
287
}
240
288
241
289
private String getNamespace () {
0 commit comments