Skip to content

Commit

Permalink
966 add context action to create layout xml file
Browse files Browse the repository at this point in the history
  • Loading branch information
silinmykola committed Mar 7, 2022
1 parent e4be0b9 commit c4f5568
Show file tree
Hide file tree
Showing 15 changed files with 708 additions and 28 deletions.
11 changes: 1 addition & 10 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<action id="MagentoCreateViewFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewViewXmlAction"/>
<action id="MagentoCreateWebapiFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewWebapiXmlAction"/>
<action id="MagentoCreateWidgetFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewWidgetXmlAction"/>
<action id="MagentoCreateLayoutFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewLayoutXmlAction"/>
<!-- Context dependent actions -->
<separator/>
<add-to-group group-id="NewGroup" anchor="before" relative-to-action="NewXml"/>
Expand Down Expand Up @@ -578,16 +579,6 @@
<internalFileTemplate name="Magento Entity Delete Controller Class"/>
<internalFileTemplate name="Magento Web API XML"/>
<internalFileTemplate name="Web API Interface"/>
<internalFileTemplate name="Magento Config XML"/>
<internalFileTemplate name="Magento Extension Attributes XML"/>
<internalFileTemplate name="Magento Widget XML"/>
<internalFileTemplate name="Magento Mview XML"/>
<internalFileTemplate name="Magento Indexer XML"/>
<internalFileTemplate name="Magento View XML"/>
<internalFileTemplate name="Magento Fieldset XML"/>
<internalFileTemplate name="Magento Sections XML"/>
<internalFileTemplate name="Magento Module Email Templates Xml"/>
<internalFileTemplate name="Magento Page Types XML"/>

<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>

Expand Down
1 change: 1 addition & 0 deletions resources/magento2/common.properties
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ common.template.type=Email Type
common.diagnostic.reportButtonText=Report Me
common.diagnostic.reportSubmittedTitle=The report is successfully submitted!
common.diagnostic.reportSubmittedMessage=Thank you for submitting your report! We will check it as soon as possible.
common.layout.filename=Layout File Name
common.targetMethod=Target Method
2 changes: 2 additions & 0 deletions resources/magento2/validation.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ validator.lowerSnakeCase=The {0} field must be of the lower snake case format
validator.menuIdentifierInvalid=The menu identifier is invalid
validator.someFieldsHaveErrors=Please, check the dialog. Some fields have errors
validator.dbSchema.invalidColumnType=Invalid ''{0}'' column type specified
validator.layoutNameRuleInvalid=The layout name is invalid
validator.layoutNameUnderscoreQtyInvalid=Wrong layout name, please check
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.actions.context.xml;

import com.intellij.ide.fileTemplates.actions.AttributesDefaults;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.actions.generation.dialog.NewLayoutTemplateDialog;
import com.magento.idea.magento2plugin.magento.packages.Areas;
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.project.Settings;
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
import java.util.Arrays;
import java.util.List;
import org.jetbrains.annotations.NotNull;

public class NewLayoutXmlAction extends AnAction {

public static final String ACTION_NAME = "Magento 2 Layout File";
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 layout.xml file";
private PsiDirectory targetDirectory;

/**
* New layout.xml file generation action constructor.
*/
public NewLayoutXmlAction() {
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
}

@Override
public void update(final @NotNull AnActionEvent event) {
setIsAvailableForEvent(event, false);
final Project project = event.getProject();

if (project == null || !Settings.isEnabled(project)) {
return;
}
final DataContext context = event.getDataContext();
final PsiElement targetElement = LangDataKeys.PSI_ELEMENT.getData(context);

if (!(targetElement instanceof PsiDirectory)) {
return;
}
final PsiDirectory targetDirectoryCandidate = (PsiDirectory) targetElement;
final GetMagentoModuleUtil.MagentoModuleData moduleData = GetMagentoModuleUtil
.getByContext(targetDirectoryCandidate, project);

if (moduleData == null) {
return;
}
final PsiDirectory viewDir = moduleData.getViewDir();

if (viewDir == null) {
return;
}
final List<String> allowedDirectories = Arrays.asList(
Package.moduleViewDir,
Areas.adminhtml.toString(),
Areas.frontend.toString()
);
if (!allowedDirectories.contains(targetDirectoryCandidate.getName())
|| !moduleData.getType().equals(ComponentType.module)) {
return;
}
final PsiDirectory parentDir = targetDirectoryCandidate.getParentDirectory();

if (parentDir == null
|| !targetDirectoryCandidate.equals(viewDir) && !parentDir.equals(viewDir)) {
return;
}
targetDirectory = targetDirectoryCandidate;
setIsAvailableForEvent(event, true);
}

@Override
public void actionPerformed(final @NotNull AnActionEvent event) {
if (event.getProject() == null || targetDirectory == null) {
return;
}

NewLayoutTemplateDialog.open(event.getProject(), targetDirectory);
}

/**
* Set is action available for event.
*
* @param event AnActionEvent
* @param isAvailable boolean
*/
private void setIsAvailableForEvent(
final @NotNull AnActionEvent event,
final boolean isAvailable
) {
event.getPresentation().setVisible(isAvailable);
event.getPresentation().setEnabled(isAvailable);
}

protected AttributesDefaults getProperties(
final @NotNull AttributesDefaults defaults,
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData,
final PsiDirectory targetDirectory,
final PsiFile targetFile
) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ public LayoutXmlData(
this.uiComponentName = uiComponentName;
}

/**
* Layout XML data.
*
* @param area String
* @param route String
* @param moduleName String
* @param controllerName String
* @param actionName String
*/
public LayoutXmlData(
final String area,
final String route,
final String moduleName,
final String controllerName,
final String actionName
) {
this.area = area;
this.route = route;
this.moduleName = moduleName;
this.controllerName = controllerName;
this.actionName = actionName;
this.uiComponentName = "";
}

public String getArea() {
return area;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.NewLayoutTemplateDialog">
<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">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="136" y="92" width="545" height="340"/>
</constraints>
<properties>
<preferredSize width="360" height="170"/>
</properties>
<border type="none"/>
<children>
<grid id="72800" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<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"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="9d205">
<constraints>
<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"/>
</constraints>
</hspacer>
<grid id="f08e3" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<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"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="4f53d" class="javax.swing.JButton" binding="buttonOK">
<constraints>
<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"/>
</constraints>
<properties>
<text value="OK"/>
</properties>
</component>
<component id="80459" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<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"/>
</constraints>
<properties>
<text value="Cancel"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="c5548" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<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"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<vspacer id="668b9">
<constraints>
<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"/>
</constraints>
</vspacer>
<component id="89b74" class="javax.swing.JComboBox" binding="area" custom-create="true">
<constraints>
<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"/>
</constraints>
<properties/>
</component>
<component id="765fc" class="javax.swing.JLabel" binding="areaLabel">
<constraints>
<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"/>
</constraints>
<properties>
<text value="Layout Area"/>
</properties>
</component>
<component id="ccfdf" class="javax.swing.JTextField" binding="layoutName">
<constraints>
<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">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
<clientProperties>
<promptText class="java.lang.String" value="routeId_controller_action"/>
</clientProperties>
</component>
<component id="4cd93" class="javax.swing.JLabel" binding="layoutNameLabel">
<constraints>
<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"/>
</constraints>
<properties>
<labelFor value="ccfdf"/>
<text value="Layout Name"/>
</properties>
</component>
<component id="6cf67" class="javax.swing.JLabel" binding="layoutNameErrorMessage">
<constraints>
<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"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>
Loading

0 comments on commit c4f5568

Please sign in to comment.