diff --git a/resources/magento2/common.properties b/resources/magento2/common.properties index 21ab21a87..7270fbea4 100644 --- a/resources/magento2/common.properties +++ b/resources/magento2/common.properties @@ -72,3 +72,4 @@ common.template.type=Email Type common.diagnostic.reportButtonText=Report Me common.diagnostic.reportSubmittedTitle=The report is successfully submitted! common.diagnostic.reportSubmittedMessage=Thank you for submitting your report! We will check it as soon as possible. +common.targetMethod=Target Method diff --git a/src/com/magento/idea/magento2plugin/actions/generation/CreateAPluginAction.java b/src/com/magento/idea/magento2plugin/actions/generation/CreateAPluginAction.java index 96b017534..4aabe0fe9 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/CreateAPluginAction.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/CreateAPluginAction.java @@ -26,7 +26,7 @@ public class CreateAPluginAction extends DumbAwareAction { - public static final String ACTION_NAME = "Create a new Plugin for this method"; + public static final String ACTION_NAME = "Create a new Plugin"; public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Plugin"; private final GetFirstClassOfFile getFirstClassOfFile; private Method targetMethod; @@ -58,7 +58,6 @@ public void update(final AnActionEvent event) { || !(psiFile instanceof PhpFile) || phpClass.isFinal() || PhpClassImplementsNoninterceptableInterfaceUtil.execute(phpClass) - || this.targetMethod == null ) { this.setStatus(event, false); return; @@ -103,6 +102,7 @@ private Pair findPhpClass(final @NotNull AnActionEvent event) return Pair.create(psiFile, phpClass); } + @SuppressWarnings({"PMD.CyclomaticComplexity"}) private void fetchTargetMethod( final @NotNull AnActionEvent event, final PsiFile psiFile, @@ -125,6 +125,11 @@ private void fetchTargetMethod( this.targetMethod = (Method) element; return; } + + if (element instanceof PhpClass) { + return; + } + final PsiElement parent = element.getParent(); if (parent instanceof Method && parent.getParent().equals(phpClass) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.form b/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.form index 405f17927..12308c5b1 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.form +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.form @@ -1,13 +1,11 @@
- + - - @@ -15,7 +13,7 @@ - + @@ -56,7 +54,7 @@ - + @@ -149,7 +147,7 @@ - + @@ -158,7 +156,7 @@ - + @@ -167,7 +165,7 @@ - + @@ -193,6 +191,20 @@ + + + + + + + + + + + + + + diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.java index c2257e1ae..7e6cc0dbe 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.java @@ -27,10 +27,12 @@ import com.magento.idea.magento2plugin.magento.packages.File; import com.magento.idea.magento2plugin.magento.packages.Package; import com.magento.idea.magento2plugin.ui.FilteredComboBox; +import com.magento.idea.magento2plugin.util.php.PhpTypeMetadataParserUtil; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JComboBox; @@ -50,7 +52,7 @@ public class CreateAPluginDialog extends AbstractDialog { @NotNull private final Project project; - private final Method targetMethod; + private Method targetMethod; private final PhpClass targetClass; private JPanel contentPane; private JButton buttonOK; @@ -63,6 +65,7 @@ public class CreateAPluginDialog extends AbstractDialog { private static final String SORT_ORDER = "sort order"; private static final String PLUGIN_NAME = "plugin name"; private static final String TARGET_MODULE = "target module"; + private static final String TARGET_METHOD = "target method"; @FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, TARGET_MODULE}) @@ -70,6 +73,12 @@ public class CreateAPluginDialog extends AbstractDialog { message = {BoxNotEmptyRule.MESSAGE, TARGET_MODULE}) private FilteredComboBox pluginModule; + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, TARGET_METHOD}) + @FieldValidation(rule = RuleRegistry.BOX_NOT_EMPTY, + message = {BoxNotEmptyRule.MESSAGE, TARGET_METHOD}) + private FilteredComboBox targetMethodSelect; + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, CLASS_NAME}) @FieldValidation(rule = RuleRegistry.PHP_CLASS, @@ -99,6 +108,7 @@ public class CreateAPluginDialog extends AbstractDialog { private JLabel pluginNameLabel;//NOPMD private JLabel pluginClassNameLabel;//NOPMD private JLabel pluginSortOrderLabel;//NOPMD + private JLabel targetMethodLabel; /** * Constructor. @@ -124,6 +134,10 @@ public CreateAPluginDialog( fillPluginTypeOptions(); fillTargetAreaOptions(); + if (targetMethod != null) { + this.targetMethodLabel.setVisible(false); + } + buttonOK.addActionListener((final ActionEvent event) -> onOK()); buttonCancel.addActionListener((final ActionEvent event) -> onCancel()); @@ -157,6 +171,9 @@ private void fillTargetAreaOptions() { } protected void onOK() { + if (targetMethod == null) { + targetMethod = getSelectedTargetMethod(); + } if (validateFormFields()) { new PluginClassGenerator(new PluginFileData( getPluginDirectory(), @@ -209,6 +226,25 @@ public String getPluginModule() { return this.pluginModule.getSelectedItem().toString(); } + /** + * Searches and returns a selected target method. + * + * @return Method target method + */ + public Method getSelectedTargetMethod() { + final String selectedMethodString = this.targetMethodSelect.getSelectedItem().toString(); + final List publicMethods = PhpTypeMetadataParserUtil.getPublicMethods( + this.targetClass + ); + for (final Method method: publicMethods) { + if (method.getName().equals(selectedMethodString)) { + return method; + } + } + + return null; + } + /** * Open an action dialog. * @@ -236,6 +272,18 @@ private void createUIComponents() { .getEditableModuleNames(); this.pluginModule = new FilteredComboBox(allModulesList); + + final List publicMethods + = PhpTypeMetadataParserUtil.getPublicMethods(this.targetClass); + final List methodList = new ArrayList<>(); + for (final Method method: publicMethods) { + methodList.add(method.getName()); + } + + this.targetMethodSelect = new FilteredComboBox(methodList); + if (targetMethod != null) { + this.targetMethodSelect.setVisible(false); + } } private String getNamespace() {