diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index c5bf5b0a4..e7c65a8c5 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -65,6 +65,7 @@ + diff --git a/src/com/magento/idea/magento2plugin/actions/context/xml/NewEventsXmlAction.java b/src/com/magento/idea/magento2plugin/actions/context/xml/NewEventsXmlAction.java new file mode 100644 index 000000000..88ccf6359 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/context/xml/NewEventsXmlAction.java @@ -0,0 +1,70 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.context.xml; + +import com.intellij.ide.fileTemplates.actions.AttributesDefaults; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.actions.context.AbstractContextAction; +import com.magento.idea.magento2plugin.magento.files.ModuleEventsXml; +import com.magento.idea.magento2plugin.magento.packages.Areas; +import com.magento.idea.magento2plugin.magento.packages.ComponentType; +import com.magento.idea.magento2plugin.magento.packages.Package; +import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; +import java.util.Arrays; +import java.util.List; +import org.jetbrains.annotations.NotNull; + +public class NewEventsXmlAction extends AbstractContextAction { + + public static final String ACTION_NAME = "Magento 2 Events File"; + public static final String ACTION_DESCRIPTION = "Create a new Magento 2 events.xml file"; + + /** + * New events.xml file generation action constructor. + */ + public NewEventsXmlAction() { + super(ACTION_NAME, ACTION_DESCRIPTION, new ModuleEventsXml()); + } + + @Override + protected boolean isVisible( + final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData, + final PsiDirectory targetDirectory, + final PsiFile targetFile + ) { + final PsiDirectory configDir = moduleData.getConfigDir(); + final PsiDirectory globalScopeDir = getGlobalScopeDir(targetDirectory); + + if (configDir == null || globalScopeDir == null) { + return false; + } + + final List allowedDirectories = Arrays.asList( + Package.moduleBaseAreaDir, + Areas.adminhtml.toString(), + Areas.crontab.toString(), + Areas.frontend.toString(), + Areas.graphql.toString(), + Areas.webapi_rest.toString(), + Areas.webapi_soap.toString() + ); + + return allowedDirectories.contains(targetDirectory.getName()) + && globalScopeDir.equals(configDir) + && moduleData.getType().equals(ComponentType.module); + } + + @Override + protected AttributesDefaults getProperties( + final @NotNull AttributesDefaults defaults, + final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData, + final PsiDirectory targetDirectory, + final PsiFile targetFile + ) { + return defaults; + } +}