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

Change overwrite templates feature for action menu #1036

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
5 changes: 4 additions & 1 deletion resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@
<action id="InjectAViewModelAction.Menu" class="com.magento.idea.magento2plugin.actions.generation.InjectAViewModelAction">
<add-to-group group-id="EditorPopupMenu"/>
</action>
<action id="OverrideInTheme.Menu" class="com.magento.idea.magento2plugin.actions.generation.OverrideInThemeAction">
<action id="OverrideTemplateInTheme.Menu" class="com.magento.idea.magento2plugin.actions.generation.OverrideTemplateInThemeAction">
<add-to-group group-id="ProjectViewPopupMenu"/>
</action>
<action id="OverrideLayoutInTheme.Menu" class="com.magento.idea.magento2plugin.actions.generation.OverrideLayoutInThemeAction">
<add-to-group group-id="ProjectViewPopupMenu"/>
</action>
<action id="MagentoCreateAWebApiDeclaration.Menu" class="com.magento.idea.magento2plugin.actions.generation.NewWebApiDeclarationAction">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.actions.generation;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
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 javax.swing.Icon;
import org.jetbrains.annotations.NotNull;

public abstract class OverrideFileInThemeAction extends AnAction {

protected PsiFile psiFile;

/**
* Override file in theme action constructor.
*
* @param actionName String
* @param actionDescription String
* @param module Icon
*/
public OverrideFileInThemeAction(
final String actionName,
final String actionDescription,
final Icon module
) {
super(actionName, actionDescription, module);
}

/**
* Action entry point.
*
* @param event AnActionEvent
*/
@Override
public void update(final @NotNull AnActionEvent event) {
setStatus(event, false);
final Project project = event.getData(PlatformDataKeys.PROJECT);
final PsiFile targetFile = event.getData(PlatformDataKeys.PSI_FILE);

if (project == null || targetFile == null) {
return;
}

if (Settings.isEnabled(project) && isOverrideAllowed(targetFile, project)) {
setStatus(event, true);
psiFile = targetFile;
}
}

/**
* Implement this method to specify if override allowed for particular file types.
*
* @param file PsiFile
* @param project Project
*
* @return boolean
*/
protected abstract boolean isOverrideAllowed(
final @NotNull PsiFile file,
final @NotNull Project project
);

/**
* Check if file has a path specific to the Magento 2 module or theme.
*
* @param file PsiFile
* @param project Project
*
* @return boolean
*/
protected boolean isFileInModuleOrTheme(
final @NotNull PsiFile file,
final @NotNull Project project
) {
final GetMagentoModuleUtil.MagentoModuleData moduleData =
GetMagentoModuleUtil.getByContext(file.getContainingDirectory(), project);

if (moduleData == null) {
return false;
}
final VirtualFile virtualFile = file.getVirtualFile();

if (moduleData.getType().equals(ComponentType.module)) {
return virtualFile.getPath().contains("/" + Package.moduleViewDir + "/");
} else {
return moduleData.getType().equals(ComponentType.theme);
}
}

private void setStatus(final AnActionEvent event, final boolean status) {
event.getPresentation().setVisible(status);
event.getPresentation().setEnabled(status);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.actions.generation;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.xml.XmlFile;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideLayoutInThemeDialog;
import com.magento.idea.magento2plugin.magento.files.LayoutXml;
import org.jetbrains.annotations.NotNull;

public class OverrideLayoutInThemeAction extends OverrideFileInThemeAction {

public static final String ACTION_NAME = "Override this layout in a project theme";
public static final String ACTION_DESCRIPTION = "Override layout in project theme";

public OverrideLayoutInThemeAction() {
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
}

@Override
public void actionPerformed(final @NotNull AnActionEvent event) {
final Project project = event.getProject();

if (project == null || psiFile == null) {
return;
}
OverrideLayoutInThemeDialog.open(project, psiFile);
}

@Override
protected boolean isOverrideAllowed(
final @NotNull PsiFile file,
final @NotNull Project project
) {
final VirtualFile virtualFile = file.getVirtualFile();

if (virtualFile == null || virtualFile.isDirectory()) {
return false;
}

if (!(file instanceof XmlFile)) {
return false;
}

if (!LayoutXml.PARENT_DIR.equals(file.getContainingDirectory().getName())
&& !LayoutXml.PAGE_LAYOUT_DIR.equals(file.getContainingDirectory().getName())) {
return false;
}

return isFileInModuleOrTheme(file, project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.actions.generation;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.actions.CopyMagentoPath;
import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideTemplateInThemeDialog;
import org.jetbrains.annotations.NotNull;

public class OverrideTemplateInThemeAction extends OverrideFileInThemeAction {

public static final String ACTION_NAME = "Override this file in a project theme";
public static final String ACTION_TEMPLATE_DESCRIPTION = "Override template in project theme";
public static final String ACTION_STYLES_DESCRIPTION = "Override styles in project theme";
public static final String LESS_FILE_EXTENSION = "less";

public OverrideTemplateInThemeAction() {
super(ACTION_NAME, ACTION_TEMPLATE_DESCRIPTION, MagentoIcons.MODULE);
}

@Override
public void actionPerformed(final @NotNull AnActionEvent event) {
final Project project = event.getProject();

if (project == null || psiFile == null) {
return;
}
OverrideTemplateInThemeDialog.open(project, psiFile);
}

@Override
protected boolean isOverrideAllowed(
final @NotNull PsiFile file,
final @NotNull Project project
) {
final VirtualFile virtualFile = file.getVirtualFile();

if (virtualFile == null || virtualFile.isDirectory()) {
return false;
}
final String fileExtension = virtualFile.getExtension();

if (!CopyMagentoPath.PHTML_EXTENSION.equals(fileExtension)
&& !LESS_FILE_EXTENSION.equals(fileExtension)) {
return false;
}

return isFileInModuleOrTheme(file, project);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?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.OverrideInThemeDialog">
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.OverrideLayoutInThemeDialog">
<grid id="cbd77" 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>
Expand Down
Loading