Skip to content

Commit 476b4ee

Browse files
Merge pull request #1036 from SilinMykola/1026-change-overwrite-templates-feature
Change overwrite templates feature for action menu
2 parents ed59dc9 + 5e10bcd commit 476b4ee

14 files changed

+802
-222
lines changed

resources/META-INF/plugin.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@
118118
<action id="InjectAViewModelAction.Menu" class="com.magento.idea.magento2plugin.actions.generation.InjectAViewModelAction">
119119
<add-to-group group-id="EditorPopupMenu"/>
120120
</action>
121-
<action id="OverrideInTheme.Menu" class="com.magento.idea.magento2plugin.actions.generation.OverrideInThemeAction">
121+
<action id="OverrideTemplateInTheme.Menu" class="com.magento.idea.magento2plugin.actions.generation.OverrideTemplateInThemeAction">
122+
<add-to-group group-id="ProjectViewPopupMenu"/>
123+
</action>
124+
<action id="OverrideLayoutInTheme.Menu" class="com.magento.idea.magento2plugin.actions.generation.OverrideLayoutInThemeAction">
122125
<add-to-group group-id="ProjectViewPopupMenu"/>
123126
</action>
124127
<action id="MagentoCreateAWebApiDeclaration.Menu" class="com.magento.idea.magento2plugin.actions.generation.NewWebApiDeclarationAction">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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.AnAction;
9+
import com.intellij.openapi.actionSystem.AnActionEvent;
10+
import com.intellij.openapi.actionSystem.PlatformDataKeys;
11+
import com.intellij.openapi.project.Project;
12+
import com.intellij.openapi.vfs.VirtualFile;
13+
import com.intellij.psi.PsiFile;
14+
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
15+
import com.magento.idea.magento2plugin.magento.packages.Package;
16+
import com.magento.idea.magento2plugin.project.Settings;
17+
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
18+
import javax.swing.Icon;
19+
import org.jetbrains.annotations.NotNull;
20+
21+
public abstract class OverrideFileInThemeAction extends AnAction {
22+
23+
protected PsiFile psiFile;
24+
25+
/**
26+
* Override file in theme action constructor.
27+
*
28+
* @param actionName String
29+
* @param actionDescription String
30+
* @param module Icon
31+
*/
32+
public OverrideFileInThemeAction(
33+
final String actionName,
34+
final String actionDescription,
35+
final Icon module
36+
) {
37+
super(actionName, actionDescription, module);
38+
}
39+
40+
/**
41+
* Action entry point.
42+
*
43+
* @param event AnActionEvent
44+
*/
45+
@Override
46+
public void update(final @NotNull AnActionEvent event) {
47+
setStatus(event, false);
48+
final Project project = event.getData(PlatformDataKeys.PROJECT);
49+
final PsiFile targetFile = event.getData(PlatformDataKeys.PSI_FILE);
50+
51+
if (project == null || targetFile == null) {
52+
return;
53+
}
54+
55+
if (Settings.isEnabled(project) && isOverrideAllowed(targetFile, project)) {
56+
setStatus(event, true);
57+
psiFile = targetFile;
58+
}
59+
}
60+
61+
/**
62+
* Implement this method to specify if override allowed for particular file types.
63+
*
64+
* @param file PsiFile
65+
* @param project Project
66+
*
67+
* @return boolean
68+
*/
69+
protected abstract boolean isOverrideAllowed(
70+
final @NotNull PsiFile file,
71+
final @NotNull Project project
72+
);
73+
74+
/**
75+
* Check if file has a path specific to the Magento 2 module or theme.
76+
*
77+
* @param file PsiFile
78+
* @param project Project
79+
*
80+
* @return boolean
81+
*/
82+
protected boolean isFileInModuleOrTheme(
83+
final @NotNull PsiFile file,
84+
final @NotNull Project project
85+
) {
86+
final GetMagentoModuleUtil.MagentoModuleData moduleData =
87+
GetMagentoModuleUtil.getByContext(file.getContainingDirectory(), project);
88+
89+
if (moduleData == null) {
90+
return false;
91+
}
92+
final VirtualFile virtualFile = file.getVirtualFile();
93+
94+
if (moduleData.getType().equals(ComponentType.module)) {
95+
return virtualFile.getPath().contains("/" + Package.moduleViewDir + "/");
96+
} else {
97+
return moduleData.getType().equals(ComponentType.theme);
98+
}
99+
}
100+
101+
private void setStatus(final AnActionEvent event, final boolean status) {
102+
event.getPresentation().setVisible(status);
103+
event.getPresentation().setEnabled(status);
104+
}
105+
}

src/com/magento/idea/magento2plugin/actions/generation/OverrideInThemeAction.java

-92
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.project.Project;
10+
import com.intellij.openapi.vfs.VirtualFile;
11+
import com.intellij.psi.PsiFile;
12+
import com.intellij.psi.xml.XmlFile;
13+
import com.magento.idea.magento2plugin.MagentoIcons;
14+
import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideLayoutInThemeDialog;
15+
import com.magento.idea.magento2plugin.magento.files.LayoutXml;
16+
import org.jetbrains.annotations.NotNull;
17+
18+
public class OverrideLayoutInThemeAction extends OverrideFileInThemeAction {
19+
20+
public static final String ACTION_NAME = "Override this layout in a project theme";
21+
public static final String ACTION_DESCRIPTION = "Override layout in project theme";
22+
23+
public OverrideLayoutInThemeAction() {
24+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
25+
}
26+
27+
@Override
28+
public void actionPerformed(final @NotNull AnActionEvent event) {
29+
final Project project = event.getProject();
30+
31+
if (project == null || psiFile == null) {
32+
return;
33+
}
34+
OverrideLayoutInThemeDialog.open(project, psiFile);
35+
}
36+
37+
@Override
38+
protected boolean isOverrideAllowed(
39+
final @NotNull PsiFile file,
40+
final @NotNull Project project
41+
) {
42+
final VirtualFile virtualFile = file.getVirtualFile();
43+
44+
if (virtualFile == null || virtualFile.isDirectory()) {
45+
return false;
46+
}
47+
48+
if (!(file instanceof XmlFile)) {
49+
return false;
50+
}
51+
52+
if (!LayoutXml.PARENT_DIR.equals(file.getContainingDirectory().getName())
53+
&& !LayoutXml.PAGE_LAYOUT_DIR.equals(file.getContainingDirectory().getName())) {
54+
return false;
55+
}
56+
57+
return isFileInModuleOrTheme(file, project);
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.project.Project;
10+
import com.intellij.openapi.vfs.VirtualFile;
11+
import com.intellij.psi.PsiFile;
12+
import com.magento.idea.magento2plugin.MagentoIcons;
13+
import com.magento.idea.magento2plugin.actions.CopyMagentoPath;
14+
import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideTemplateInThemeDialog;
15+
import org.jetbrains.annotations.NotNull;
16+
17+
public class OverrideTemplateInThemeAction extends OverrideFileInThemeAction {
18+
19+
public static final String ACTION_NAME = "Override this file in a project theme";
20+
public static final String ACTION_TEMPLATE_DESCRIPTION = "Override template in project theme";
21+
public static final String ACTION_STYLES_DESCRIPTION = "Override styles in project theme";
22+
public static final String LESS_FILE_EXTENSION = "less";
23+
24+
public OverrideTemplateInThemeAction() {
25+
super(ACTION_NAME, ACTION_TEMPLATE_DESCRIPTION, MagentoIcons.MODULE);
26+
}
27+
28+
@Override
29+
public void actionPerformed(final @NotNull AnActionEvent event) {
30+
final Project project = event.getProject();
31+
32+
if (project == null || psiFile == null) {
33+
return;
34+
}
35+
OverrideTemplateInThemeDialog.open(project, psiFile);
36+
}
37+
38+
@Override
39+
protected boolean isOverrideAllowed(
40+
final @NotNull PsiFile file,
41+
final @NotNull Project project
42+
) {
43+
final VirtualFile virtualFile = file.getVirtualFile();
44+
45+
if (virtualFile == null || virtualFile.isDirectory()) {
46+
return false;
47+
}
48+
final String fileExtension = virtualFile.getExtension();
49+
50+
if (!CopyMagentoPath.PHTML_EXTENSION.equals(fileExtension)
51+
&& !LESS_FILE_EXTENSION.equals(fileExtension)) {
52+
return false;
53+
}
54+
55+
return isFileInModuleOrTheme(file, project);
56+
}
57+
}

src/com/magento/idea/magento2plugin/actions/generation/dialog/OverrideInTheme.form renamed to src/com/magento/idea/magento2plugin/actions/generation/dialog/OverrideLayoutInTheme.form

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?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.OverrideInThemeDialog">
2+
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.OverrideLayoutInThemeDialog">
33
<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">
44
<margin top="10" left="10" bottom="10" right="10"/>
55
<constraints>

0 commit comments

Comments
 (0)