|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | +package com.magento.idea.magento2plugin.actions.generation; |
| 6 | + |
| 7 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 8 | +import com.intellij.openapi.actionSystem.PlatformDataKeys; |
| 9 | +import com.intellij.openapi.project.DumbAwareAction; |
| 10 | +import com.intellij.openapi.project.Project; |
| 11 | +import com.intellij.openapi.util.Pair; |
| 12 | +import com.intellij.psi.PsiFile; |
| 13 | +import com.jetbrains.php.lang.psi.PhpFile; |
| 14 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 15 | +import com.magento.idea.magento2plugin.MagentoIcons; |
| 16 | +import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideClassByAPreferenceDialog; |
| 17 | +import com.magento.idea.magento2plugin.project.Settings; |
| 18 | +import com.magento.idea.magento2plugin.util.GetFirstClassOfFile; |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | + |
| 21 | +public class OverrideClassByAPreferenceAction extends DumbAwareAction { |
| 22 | + public static String ACTION_NAME = "Override Class By A Preference..."; |
| 23 | + public static String ACTION_DESCRIPTION = "Create a new Magento 2 preference for the class"; |
| 24 | + private final GetFirstClassOfFile getFirstClassOfFile; |
| 25 | + private PhpClass targetClass; |
| 26 | + |
| 27 | + public OverrideClassByAPreferenceAction() { |
| 28 | + super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE); |
| 29 | + this.getFirstClassOfFile = GetFirstClassOfFile.getInstance(); |
| 30 | + } |
| 31 | + |
| 32 | + public void update(AnActionEvent event) { |
| 33 | + targetClass = null; |
| 34 | + Project project = event.getData(PlatformDataKeys.PROJECT); |
| 35 | + if (Settings.isEnabled(project)) { |
| 36 | + Pair<PsiFile, PhpClass> pair = this.findPhpClass(event); |
| 37 | + PsiFile psiFile = pair.getFirst(); |
| 38 | + PhpClass phpClass = pair.getSecond(); |
| 39 | + targetClass = phpClass; |
| 40 | + if (!(psiFile instanceof PhpFile) && phpClass != null) { |
| 41 | + this.setStatus(event, false); |
| 42 | + return; |
| 43 | + } |
| 44 | + } else { |
| 45 | + this.setStatus(event, false); |
| 46 | + return; |
| 47 | + } |
| 48 | + this.setStatus(event, true); |
| 49 | + } |
| 50 | + |
| 51 | + private void setStatus(AnActionEvent event, boolean status) { |
| 52 | + event.getPresentation().setVisible(status); |
| 53 | + event.getPresentation().setEnabled(status); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void actionPerformed(@NotNull AnActionEvent e) { |
| 58 | + OverrideClassByAPreferenceDialog.open(e.getProject(), this.targetClass); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean isDumbAware() { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + private Pair<PsiFile, PhpClass> findPhpClass(@NotNull AnActionEvent event) { |
| 67 | + PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE); |
| 68 | + |
| 69 | + PhpClass phpClass = null; |
| 70 | + if (psiFile instanceof PhpFile) { |
| 71 | + phpClass = getFirstClassOfFile.execute((PhpFile) psiFile); |
| 72 | + } |
| 73 | + |
| 74 | + return Pair.create(psiFile, phpClass); |
| 75 | + } |
| 76 | +} |
0 commit comments