Skip to content

Commit 62332ba

Browse files
author
Vitaliy Boyko
committed
Inject a view model to a block and a reference block from the context menu
1 parent 87df276 commit 62332ba

12 files changed

+775
-0
lines changed

gradle-tasks/pmd/ruleset.xml

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<rule ref="category/java/errorprone.xml">
3333
<exclude name="BeanMembersShouldSerialize"/>
3434
<exclude name="DataflowAnomalyAnalysis"/>
35+
<exclude name="MissingSerialVersionUID"/>
3536
</rule>
3637
<exclude-pattern>.*/resources/.*</exclude-pattern>
3738
<exclude-pattern>.*/testData/.*</exclude-pattern>

resources/META-INF/plugin.xml

+3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080
<action id="OverrideClassByAPreference.Menu" class="com.magento.idea.magento2plugin.actions.generation.OverrideClassByAPreferenceAction">
8181
<add-to-group group-id="EditorPopupMenu"/>
8282
</action>
83+
<action id="InjectAViewModelAction.Menu" class="com.magento.idea.magento2plugin.actions.generation.InjectAViewModelAction">
84+
<add-to-group group-id="EditorPopupMenu"/>
85+
</action>
8386

8487
</actions>
8588

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!--
2+
/*
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html>
8+
<body>
9+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
10+
<tr>
11+
<td>
12+
<font face="verdana" size="-1">Constructor arguments. The object manager injects these arguments into the class during creation. The name of the argument configured in the XML file must correspond to the name of the parameter in the constructor in the configured class.</font>
13+
</td>
14+
</tr>
15+
</table>
16+
</body>
17+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<argument name="${ARGUMENT_NAME}" xsi:type="${ARGUMENT_TYPE}">${ARGUMENT_VALUE}</argument>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.InjectAViewModelDialog">
3+
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
4+
<margin top="10" left="10" bottom="10" right="10"/>
5+
<constraints>
6+
<xy x="48" y="54" width="600" height="500"/>
7+
</constraints>
8+
<properties>
9+
<preferredSize width="450" height="180"/>
10+
<requestFocusEnabled value="true"/>
11+
</properties>
12+
<border type="none"/>
13+
<children>
14+
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
15+
<margin top="0" left="0" bottom="0" right="0"/>
16+
<constraints>
17+
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
18+
</constraints>
19+
<properties/>
20+
<border type="none"/>
21+
<children>
22+
<hspacer id="98af6">
23+
<constraints>
24+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
25+
</constraints>
26+
</hspacer>
27+
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
28+
<margin top="0" left="0" bottom="0" right="0"/>
29+
<constraints>
30+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
31+
</constraints>
32+
<properties/>
33+
<border type="none"/>
34+
<children>
35+
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
36+
<constraints>
37+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
38+
</constraints>
39+
<properties>
40+
<text resource-bundle="magento2/common" key="common.ok"/>
41+
</properties>
42+
</component>
43+
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
44+
<constraints>
45+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
46+
</constraints>
47+
<properties>
48+
<text resource-bundle="magento2/common" key="common.cancel"/>
49+
</properties>
50+
</component>
51+
</children>
52+
</grid>
53+
</children>
54+
</grid>
55+
<grid id="e3588" layout-manager="GridLayoutManager" row-count="3" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
56+
<margin top="0" left="0" bottom="0" right="0"/>
57+
<constraints>
58+
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
59+
</constraints>
60+
<properties/>
61+
<border type="none"/>
62+
<children>
63+
<component id="ad3ba" class="javax.swing.JTextField" binding="viewModelClassName">
64+
<constraints>
65+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
66+
<preferred-size width="150" height="-1"/>
67+
</grid>
68+
</constraints>
69+
<properties/>
70+
<clientProperties>
71+
<html.disable class="java.lang.Boolean" value="true"/>
72+
</clientProperties>
73+
</component>
74+
<component id="3fe2b" class="javax.swing.JLabel" binding="viewModelClassNameLabel">
75+
<constraints>
76+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
77+
</constraints>
78+
<properties>
79+
<text resource-bundle="magento2/common" key="common.className"/>
80+
</properties>
81+
</component>
82+
<component id="e4858" class="javax.swing.JTextField" binding="viewModelDirectory">
83+
<constraints>
84+
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
85+
<preferred-size width="150" height="-1"/>
86+
</grid>
87+
</constraints>
88+
<properties>
89+
<enabled value="true"/>
90+
<text value=""/>
91+
<toolTipText value="Preference directory path separated by slashes"/>
92+
</properties>
93+
<clientProperties>
94+
<html.disable class="java.lang.Boolean" value="true"/>
95+
</clientProperties>
96+
</component>
97+
<component id="6831e" class="javax.swing.JLabel" binding="viewModelDirectoryLabel">
98+
<constraints>
99+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
100+
</constraints>
101+
<properties>
102+
<text resource-bundle="magento2/common" key="common.directory"/>
103+
</properties>
104+
</component>
105+
<component id="2bc2c" class="javax.swing.JLabel" binding="viewModelArgumentNameLabel">
106+
<constraints>
107+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
108+
</constraints>
109+
<properties>
110+
<text value="Argument Name"/>
111+
</properties>
112+
</component>
113+
<component id="c3a92" class="javax.swing.JTextField" binding="viewModelArgumentName">
114+
<constraints>
115+
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
116+
<preferred-size width="150" height="-1"/>
117+
</grid>
118+
</constraints>
119+
<properties/>
120+
</component>
121+
</children>
122+
</grid>
123+
</children>
124+
</grid>
125+
</form>

0 commit comments

Comments
 (0)