Skip to content

Commit 875b63d

Browse files
Merge pull request #1021 from SilinMykola/967-add-context-action-to-create-layout-xml-file
967 add context action to create layout xml file
2 parents 2dd6eac + 83fc536 commit 875b63d

File tree

16 files changed

+740
-20
lines changed

16 files changed

+740
-20
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<action id="MagentoCreateViewFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewViewXmlAction"/>
7676
<action id="MagentoCreateWebapiFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewWebapiXmlAction"/>
7777
<action id="MagentoCreateWidgetFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewWidgetXmlAction"/>
78+
<action id="MagentoCreateLayoutFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewLayoutXmlAction"/>
7879
<!-- Context dependent actions -->
7980
<separator/>
8081
<add-to-group group-id="NewGroup" anchor="before" relative-to-action="NewXml"/>
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
<?xml version="1.0"?>
22
#parse("XML File Header.xml")
3-
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
3+
#if (${IS_ADMIN} == "true")
4+
#set ($isAdmin = true)
5+
#else
6+
#set ($isAdmin = false)
7+
#end
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #if ($isAdmin)layout="admin-1column"#end
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
410
</page>

resources/magento2/common.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,5 @@ common.template.type=Email Type
7272
common.diagnostic.reportButtonText=Report Me
7373
common.diagnostic.reportSubmittedTitle=The report is successfully submitted!
7474
common.diagnostic.reportSubmittedMessage=Thank you for submitting your report! We will check it as soon as possible.
75+
common.layout.filename=Layout File Name
7576
common.targetMethod=Target Method

resources/magento2/validation.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ validator.dbSchema.invalidColumnType=Invalid ''{0}'' column type specified
4545
validator.arrayValuesDialog.invalidValueForRowWithName=Invalid value ''{0}'' specified for the row with name ''{1}''
4646
validator.arrayValuesDialog.namesMustBeUnique=Duplicated items names
4747
validator.arrayValuesDialog.nameMustNotBeEmpty=The array name cannot be empty
48+
validator.layoutNameRuleInvalid=The layout name is invalid
49+
validator.layoutNameUnderscoreQtyInvalid=Wrong layout name, please check
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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.xml;
7+
8+
import com.intellij.openapi.actionSystem.AnAction;
9+
import com.intellij.openapi.actionSystem.AnActionEvent;
10+
import com.intellij.openapi.actionSystem.DataContext;
11+
import com.intellij.openapi.actionSystem.LangDataKeys;
12+
import com.intellij.openapi.project.Project;
13+
import com.intellij.psi.PsiDirectory;
14+
import com.intellij.psi.PsiElement;
15+
import com.intellij.psi.PsiFile;
16+
import com.magento.idea.magento2plugin.MagentoIcons;
17+
import com.magento.idea.magento2plugin.actions.generation.dialog.NewLayoutTemplateDialog;
18+
import com.magento.idea.magento2plugin.magento.files.LayoutXml;
19+
import com.magento.idea.magento2plugin.magento.packages.Areas;
20+
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
21+
import com.magento.idea.magento2plugin.magento.packages.Package;
22+
import com.magento.idea.magento2plugin.project.Settings;
23+
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
24+
import java.util.Arrays;
25+
import java.util.List;
26+
import org.jetbrains.annotations.NotNull;
27+
import org.jetbrains.annotations.Nullable;
28+
29+
public class NewLayoutXmlAction extends AnAction {
30+
31+
public static final String ACTION_NAME = "Magento 2 Layout File";
32+
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 layout.xml file";
33+
private PsiDirectory targetDirectory;
34+
35+
/**
36+
* New layout.xml file generation action constructor.
37+
*/
38+
public NewLayoutXmlAction() {
39+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
40+
}
41+
42+
@Override
43+
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
44+
public void update(final @NotNull AnActionEvent event) {
45+
setIsAvailableForEvent(event, false);
46+
final Project project = event.getProject();
47+
48+
if (project == null || !Settings.isEnabled(project)) {
49+
return;
50+
}
51+
final DataContext context = event.getDataContext();
52+
final PsiElement targetElement = LangDataKeys.PSI_ELEMENT.getData(context);
53+
final PsiDirectory targetDirectoryCandidate = resolveTargetDirectory(targetElement);
54+
55+
if (targetDirectoryCandidate == null) {
56+
return;
57+
}
58+
final GetMagentoModuleUtil.MagentoModuleData moduleData = GetMagentoModuleUtil
59+
.getByContext(targetDirectoryCandidate, project);
60+
61+
if (moduleData == null) {
62+
return;
63+
}
64+
final PsiDirectory viewDir = moduleData.getViewDir();
65+
66+
if (viewDir == null) {
67+
return;
68+
}
69+
final List<String> allowedDirectories = Arrays.asList(
70+
Package.moduleViewDir,
71+
Areas.adminhtml.toString(),
72+
Areas.frontend.toString()
73+
);
74+
if (!allowedDirectories.contains(targetDirectoryCandidate.getName())
75+
|| !moduleData.getType().equals(ComponentType.module)) {
76+
return;
77+
}
78+
final PsiDirectory parentDir = targetDirectoryCandidate.getParentDirectory();
79+
80+
if (parentDir == null
81+
|| !targetDirectoryCandidate.equals(viewDir) && !parentDir.equals(viewDir)) {
82+
return;
83+
}
84+
targetDirectory = targetDirectoryCandidate;
85+
setIsAvailableForEvent(event, true);
86+
}
87+
88+
@Override
89+
public void actionPerformed(final @NotNull AnActionEvent event) {
90+
if (event.getProject() == null || targetDirectory == null) {
91+
return;
92+
}
93+
94+
NewLayoutTemplateDialog.open(event.getProject(), targetDirectory);
95+
}
96+
97+
/**
98+
* Set is action available for event.
99+
*
100+
* @param event AnActionEvent
101+
* @param isAvailable boolean
102+
*/
103+
private void setIsAvailableForEvent(
104+
final @NotNull AnActionEvent event,
105+
final boolean isAvailable
106+
) {
107+
event.getPresentation().setVisible(isAvailable);
108+
event.getPresentation().setEnabled(isAvailable);
109+
}
110+
111+
/**
112+
* Resolve target directory.
113+
*
114+
* @param targetElement PsiElement
115+
*
116+
* @return PsiDirectory
117+
*/
118+
private @Nullable PsiDirectory resolveTargetDirectory(final PsiElement targetElement) {
119+
PsiDirectory target = null;
120+
121+
if (targetElement instanceof PsiDirectory) {
122+
target = (PsiDirectory) targetElement;
123+
} else if (targetElement instanceof PsiFile) {
124+
target = ((PsiFile) targetElement).getContainingDirectory();
125+
}
126+
127+
if (target == null) {
128+
return null;
129+
}
130+
131+
if (LayoutXml.PARENT_DIR.equals(target.getName())) {
132+
target = target.getParentDirectory();
133+
}
134+
135+
return target;
136+
}
137+
}

src/com/magento/idea/magento2plugin/actions/generation/data/LayoutXmlData.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@ public LayoutXmlData(
3939
this.uiComponentName = uiComponentName;
4040
}
4141

42+
/**
43+
* Layout XML data.
44+
*
45+
* @param area String
46+
* @param route String
47+
* @param moduleName String
48+
* @param controllerName String
49+
* @param actionName String
50+
*/
51+
public LayoutXmlData(
52+
final String area,
53+
final String route,
54+
final String moduleName,
55+
final String controllerName,
56+
final String actionName
57+
) {
58+
this.area = area;
59+
this.route = route;
60+
this.moduleName = moduleName;
61+
this.controllerName = controllerName;
62+
this.actionName = actionName;
63+
this.uiComponentName = "";
64+
}
65+
4266
public String getArea() {
4367
return area;
4468
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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.NewLayoutTemplateDialog">
3+
<grid id="7afc" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" 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="136" y="92" width="545" height="340"/>
7+
</constraints>
8+
<properties>
9+
<preferredSize width="360" height="170"/>
10+
</properties>
11+
<border type="none"/>
12+
<children>
13+
<grid id="72800" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
14+
<margin top="0" left="0" bottom="0" right="0"/>
15+
<constraints>
16+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
17+
</constraints>
18+
<properties/>
19+
<border type="none"/>
20+
<children>
21+
<hspacer id="9d205">
22+
<constraints>
23+
<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"/>
24+
</constraints>
25+
</hspacer>
26+
<grid id="f08e3" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
27+
<margin top="0" left="0" bottom="0" right="0"/>
28+
<constraints>
29+
<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"/>
30+
</constraints>
31+
<properties/>
32+
<border type="none"/>
33+
<children>
34+
<component id="4f53d" class="javax.swing.JButton" binding="buttonOK">
35+
<constraints>
36+
<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"/>
37+
</constraints>
38+
<properties>
39+
<text value="OK"/>
40+
</properties>
41+
</component>
42+
<component id="80459" class="javax.swing.JButton" binding="buttonCancel">
43+
<constraints>
44+
<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"/>
45+
</constraints>
46+
<properties>
47+
<text value="Cancel"/>
48+
</properties>
49+
</component>
50+
</children>
51+
</grid>
52+
</children>
53+
</grid>
54+
<grid id="c5548" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
55+
<margin top="0" left="0" bottom="0" right="0"/>
56+
<constraints>
57+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
58+
</constraints>
59+
<properties/>
60+
<border type="none"/>
61+
<children>
62+
<vspacer id="668b9">
63+
<constraints>
64+
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
65+
</constraints>
66+
</vspacer>
67+
<component id="89b74" class="javax.swing.JComboBox" binding="area" custom-create="true">
68+
<constraints>
69+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
70+
</constraints>
71+
<properties/>
72+
</component>
73+
<component id="765fc" class="javax.swing.JLabel" binding="areaLabel">
74+
<constraints>
75+
<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"/>
76+
</constraints>
77+
<properties>
78+
<text value="Layout Area"/>
79+
</properties>
80+
</component>
81+
<component id="ccfdf" class="javax.swing.JTextField" binding="layoutName">
82+
<constraints>
83+
<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">
84+
<preferred-size width="150" height="-1"/>
85+
</grid>
86+
</constraints>
87+
<properties/>
88+
<clientProperties>
89+
<promptText class="java.lang.String" value="routeId_controller_action"/>
90+
</clientProperties>
91+
</component>
92+
<component id="4cd93" class="javax.swing.JLabel" binding="layoutNameLabel">
93+
<constraints>
94+
<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"/>
95+
</constraints>
96+
<properties>
97+
<labelFor value="ccfdf"/>
98+
<text value="Layout Name"/>
99+
</properties>
100+
</component>
101+
<component id="6cf67" class="javax.swing.JLabel" binding="layoutNameErrorMessage">
102+
<constraints>
103+
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
104+
</constraints>
105+
<properties>
106+
<text value=""/>
107+
</properties>
108+
</component>
109+
</children>
110+
</grid>
111+
</children>
112+
</grid>
113+
</form>

0 commit comments

Comments
 (0)