|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.magento.idea.magento2plugin.actions.generation; |
| 7 | + |
| 8 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 9 | +import com.intellij.openapi.actionSystem.PlatformDataKeys; |
| 10 | +import com.intellij.openapi.editor.Caret; |
| 11 | +import com.intellij.openapi.project.DumbAwareAction; |
| 12 | +import com.intellij.openapi.project.Project; |
| 13 | +import com.intellij.psi.PsiElement; |
| 14 | +import com.intellij.psi.PsiFile; |
| 15 | +import com.intellij.psi.util.PsiTreeUtil; |
| 16 | +import com.intellij.psi.xml.XmlTag; |
| 17 | +import com.magento.idea.magento2plugin.MagentoIcons; |
| 18 | +import com.magento.idea.magento2plugin.actions.generation.dialog.InjectAViewModelDialog; |
| 19 | +import com.magento.idea.magento2plugin.magento.files.CommonXml; |
| 20 | +import com.magento.idea.magento2plugin.magento.files.LayoutXml; |
| 21 | +import com.magento.idea.magento2plugin.project.Settings; |
| 22 | +import org.jetbrains.annotations.NotNull; |
| 23 | + |
| 24 | +public class InjectAViewModelAction extends DumbAwareAction { |
| 25 | + public static String actionName = "Inject a View Model..."; |
| 26 | + public static String actionDescription = "Inject a View Model as an argument of block"; |
| 27 | + private XmlTag targetXmlTag; |
| 28 | + |
| 29 | + public InjectAViewModelAction() { |
| 30 | + super(actionName, actionDescription, MagentoIcons.MODULE); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void update(final AnActionEvent event) { |
| 35 | + final Project project = event.getData(PlatformDataKeys.PROJECT); |
| 36 | + if (Settings.isEnabled(project)) { |
| 37 | + final XmlTag element = getElement(event); |
| 38 | + if (element == null) { |
| 39 | + this.setStatus(event, false); |
| 40 | + } else { |
| 41 | + targetXmlTag = element; |
| 42 | + this.setStatus(event, true); |
| 43 | + } |
| 44 | + } else { |
| 45 | + this.setStatus(event, false); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private void setStatus(final AnActionEvent event, final boolean status) { |
| 50 | + event.getPresentation().setVisible(status); |
| 51 | + event.getPresentation().setEnabled(status); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void actionPerformed(final @NotNull AnActionEvent event) { |
| 56 | + InjectAViewModelDialog.open(event.getProject(), this.targetXmlTag); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean isDumbAware() { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + private XmlTag getElement(final @NotNull AnActionEvent event) { |
| 65 | + final Caret caret = event.getData(PlatformDataKeys.CARET); |
| 66 | + if (caret == null) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + final int offset = caret.getOffset(); |
| 71 | + final PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE); |
| 72 | + final PsiElement element = psiFile.findElementAt(offset); |
| 73 | + if (element == null) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + final XmlTag xmlTag = PsiTreeUtil.getParentOfType(element, XmlTag.class); |
| 78 | + |
| 79 | + if (xmlTag == null) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + XmlTag resultTag; |
| 83 | + if (xmlTag.getName().equals(CommonXml.ATTRIBUTE_ARGUMENTS)) { |
| 84 | + resultTag = PsiTreeUtil.getParentOfType(xmlTag, XmlTag.class); |
| 85 | + } else { |
| 86 | + resultTag = xmlTag; |
| 87 | + } |
| 88 | + if (resultTag == null) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + if (!resultTag.getName().equals(LayoutXml.BLOCK_ATTRIBUTE_TAG_NAME) |
| 93 | + && !resultTag.getName().equals(LayoutXml.REFERENCE_BLOCK_ATTRIBUTE_TAG_NAME)) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + return resultTag; |
| 98 | + } |
| 99 | +} |
0 commit comments