|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.magento.idea.magento2plugin.actions.context.php; |
| 7 | + |
| 8 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 9 | +import com.intellij.psi.PsiDirectory; |
| 10 | +import com.intellij.psi.PsiFile; |
| 11 | +import com.magento.idea.magento2plugin.actions.context.CustomGeneratorContextAction; |
| 12 | +import com.magento.idea.magento2plugin.actions.generation.dialog.NewSetupDataPatch.NewSetupDataPatchDialog; |
| 13 | +import com.magento.idea.magento2plugin.magento.packages.ComponentType; |
| 14 | +import com.magento.idea.magento2plugin.magento.packages.Package; |
| 15 | +import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; |
| 16 | +import org.jetbrains.annotations.NotNull; |
| 17 | + |
| 18 | +public class NewSetupDataPatchAction extends CustomGeneratorContextAction { |
| 19 | + |
| 20 | + public static final String ACTION_NAME = "Magento 2 Setup Data Patch"; |
| 21 | + public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Setup Data Patch"; |
| 22 | + public static final String ROOT_DIRECTORY = "Setup"; |
| 23 | + public static final String PATCH_DIRECTORY = "Patch"; |
| 24 | + public static final String DATA_DIRECTORY = "Data"; |
| 25 | + |
| 26 | + public NewSetupDataPatchAction() { |
| 27 | + super(ACTION_NAME, ACTION_DESCRIPTION); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void actionPerformed(final @NotNull AnActionEvent event) { |
| 32 | + final GetMagentoModuleUtil.MagentoModuleData moduleData = getModuleData(); |
| 33 | + |
| 34 | + if (event.getProject() == null || moduleData == null || getDirectory() == null) { |
| 35 | + return; |
| 36 | + } |
| 37 | + final String[] templateData = moduleData.getName().split(Package.vendorModuleNameSeparator); |
| 38 | + |
| 39 | + if (templateData.length != 2) { //NOPMD |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + NewSetupDataPatchDialog.open( |
| 44 | + event.getProject(), |
| 45 | + getDirectory(), |
| 46 | + templateData[0], |
| 47 | + templateData[1] |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + protected boolean isVisible( |
| 53 | + final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData, |
| 54 | + final PsiDirectory targetDirectory, |
| 55 | + final PsiFile targetFile |
| 56 | + ) { |
| 57 | + if (!moduleData.getType().equals(ComponentType.module)) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + final PsiDirectory parentDirFirst = targetDirectory.getParentDirectory(); |
| 61 | + final PsiDirectory parentDirSecond = parentDirFirst != null ? |
| 62 | + parentDirFirst.getParentDirectory() : null; |
| 63 | + |
| 64 | + return targetDirectory.getName().equals(ROOT_DIRECTORY) || |
| 65 | + (parentDirFirst != null && parentDirFirst.getName().equals(ROOT_DIRECTORY)) || |
| 66 | + (parentDirSecond != null && parentDirSecond.getName().equals(ROOT_DIRECTORY)); |
| 67 | + } |
| 68 | +} |
0 commit comments