Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

967 add context action to create layout xml file #1021

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 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
8 changes: 7 additions & 1 deletion resources/fileTemplates/internal/Magento Layout XML.xml.ft
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?xml version="1.0"?>
#parse("XML File Header.xml")
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
#if (${IS_ADMIN} == "true")
#set ($isAdmin = true)
#else
#set ($isAdmin = false)
#end
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #if ($isAdmin)layout="admin-1column"#end
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
</page>
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 @@ -45,3 +45,5 @@ validator.dbSchema.invalidColumnType=Invalid ''{0}'' column type specified
validator.arrayValuesDialog.invalidValueForRowWithName=Invalid value ''{0}'' specified for the row with name ''{1}''
validator.arrayValuesDialog.namesMustBeUnique=Duplicated items names
validator.arrayValuesDialog.nameMustNotBeEmpty=The array name cannot be empty
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,137 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

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

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.files.LayoutXml;
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;
import org.jetbrains.annotations.Nullable;

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
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
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);
final PsiDirectory targetDirectoryCandidate = resolveTargetDirectory(targetElement);

if (targetDirectoryCandidate == null) {
return;
}
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);
}

/**
* Resolve target directory.
*
* @param targetElement PsiElement
*
* @return PsiDirectory
*/
private @Nullable PsiDirectory resolveTargetDirectory(final PsiElement targetElement) {
PsiDirectory target = null;

if (targetElement instanceof PsiDirectory) {
target = (PsiDirectory) targetElement;
} else if (targetElement instanceof PsiFile) {
target = ((PsiFile) targetElement).getContainingDirectory();
}

if (target == null) {
return null;
}

if (LayoutXml.PARENT_DIR.equals(target.getName())) {
target = target.getParentDirectory();
}

return target;
}
}
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