2727import com .magento .idea .magento2plugin .magento .packages .File ;
2828import com .magento .idea .magento2plugin .magento .packages .Package ;
2929import com .magento .idea .magento2plugin .ui .FilteredComboBox ;
30+ import com .magento .idea .magento2plugin .util .php .PhpTypeMetadataParserUtil ;
3031import java .awt .event .ActionEvent ;
3132import java .awt .event .KeyEvent ;
3233import java .awt .event .WindowAdapter ;
3334import java .awt .event .WindowEvent ;
35+ import java .util .ArrayList ;
3436import java .util .List ;
3537import javax .swing .JButton ;
3638import javax .swing .JComboBox ;
5052public class CreateAPluginDialog extends AbstractDialog {
5153 @ NotNull
5254 private final Project project ;
53- private final Method targetMethod ;
55+ private Method targetMethod ;
5456 private final PhpClass targetClass ;
5557 private JPanel contentPane ;
5658 private JButton buttonOK ;
@@ -63,13 +65,20 @@ public class CreateAPluginDialog extends AbstractDialog {
6365 private static final String SORT_ORDER = "sort order" ;
6466 private static final String PLUGIN_NAME = "plugin name" ;
6567 private static final String TARGET_MODULE = "target module" ;
68+ private static final String TARGET_METHOD = "target method" ;
6669
6770 @ FieldValidation (rule = RuleRegistry .NOT_EMPTY ,
6871 message = {NotEmptyRule .MESSAGE , TARGET_MODULE })
6972 @ FieldValidation (rule = RuleRegistry .BOX_NOT_EMPTY ,
7073 message = {BoxNotEmptyRule .MESSAGE , TARGET_MODULE })
7174 private FilteredComboBox pluginModule ;
7275
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+
7382 @ FieldValidation (rule = RuleRegistry .NOT_EMPTY ,
7483 message = {NotEmptyRule .MESSAGE , CLASS_NAME })
7584 @ FieldValidation (rule = RuleRegistry .PHP_CLASS ,
@@ -99,6 +108,7 @@ public class CreateAPluginDialog extends AbstractDialog {
99108 private JLabel pluginNameLabel ;//NOPMD
100109 private JLabel pluginClassNameLabel ;//NOPMD
101110 private JLabel pluginSortOrderLabel ;//NOPMD
111+ private JLabel targetMethodLabel ;
102112
103113 /**
104114 * Constructor.
@@ -124,6 +134,10 @@ public CreateAPluginDialog(
124134 fillPluginTypeOptions ();
125135 fillTargetAreaOptions ();
126136
137+ if (targetMethod != null ) {
138+ this .targetMethodLabel .setVisible (false );
139+ }
140+
127141 buttonOK .addActionListener ((final ActionEvent event ) -> onOK ());
128142 buttonCancel .addActionListener ((final ActionEvent event ) -> onCancel ());
129143
@@ -157,6 +171,9 @@ private void fillTargetAreaOptions() {
157171 }
158172
159173 protected void onOK () {
174+ if (targetMethod == null ) {
175+ targetMethod = getSelectedTargetMethod ();
176+ }
160177 if (validateFormFields ()) {
161178 new PluginClassGenerator (new PluginFileData (
162179 getPluginDirectory (),
@@ -209,6 +226,25 @@ public String getPluginModule() {
209226 return this .pluginModule .getSelectedItem ().toString ();
210227 }
211228
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+
212248 /**
213249 * Open an action dialog.
214250 *
@@ -236,6 +272,18 @@ private void createUIComponents() {
236272 .getEditableModuleNames ();
237273
238274 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+ }
239287 }
240288
241289 private String getNamespace () {
0 commit comments