Skip to content

Commit fb7224b

Browse files
author
silinmykola
committed
1026-Change overwrite templates feature
1 parent b1dbfbe commit fb7224b

11 files changed

+658
-120
lines changed

resources/META-INF/plugin.xml

Lines changed: 4 additions & 1 deletion
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">

src/com/magento/idea/magento2plugin/actions/generation/OverrideInThemeAction.java renamed to src/com/magento/idea/magento2plugin/actions/generation/OverrideLayoutInThemeAction.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,29 @@
55

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

8+
import com.intellij.openapi.actionSystem.AnAction;
89
import com.intellij.openapi.actionSystem.AnActionEvent;
910
import com.intellij.openapi.actionSystem.PlatformDataKeys;
10-
import com.intellij.openapi.project.DumbAwareAction;
1111
import com.intellij.openapi.project.Project;
1212
import com.intellij.openapi.vfs.VirtualFile;
1313
import com.intellij.psi.PsiFile;
1414
import com.magento.idea.magento2plugin.MagentoIcons;
15-
import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideInThemeDialog;
15+
import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideLayoutInThemeDialog;
16+
import com.magento.idea.magento2plugin.magento.files.LayoutXml;
17+
import com.magento.idea.magento2plugin.magento.files.UiComponentGridXmlFile;
1618
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
1719
import com.magento.idea.magento2plugin.magento.packages.Package;
1820
import com.magento.idea.magento2plugin.project.Settings;
19-
import com.magento.idea.magento2plugin.util.magento.GetComponentNameByDirectoryUtil;
20-
import com.magento.idea.magento2plugin.util.magento.GetComponentTypeByNameUtil;
21+
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
2122
import org.jetbrains.annotations.NotNull;
2223

23-
public class OverrideInThemeAction extends DumbAwareAction {
24+
public class OverrideLayoutInThemeAction extends AnAction {
2425

25-
public static final String ACTION_NAME = "Override this template in a project theme";
26-
public static final String ACTION_DESCRIPTION = "Override in project theme";
26+
public static final String ACTION_NAME = "Override this layout in a project theme";
27+
public static final String ACTION_DESCRIPTION = "Override layout in project theme";
2728
private PsiFile psiFile;
2829

29-
public OverrideInThemeAction() {
30+
public OverrideLayoutInThemeAction() {
3031
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
3132
}
3233

@@ -60,15 +61,22 @@ private boolean isOverrideAllowed(final VirtualFile file, final Project project)
6061
return false;
6162
}
6263

63-
boolean isAllowed = false;
64+
if (!UiComponentGridXmlFile.FILE_EXTENSION
65+
.equals(psiFile.getVirtualFile().getExtension())) {
66+
return false;
67+
}
6468

65-
final String componentType = GetComponentTypeByNameUtil.execute(
66-
GetComponentNameByDirectoryUtil.execute(psiFile.getContainingDirectory(), project)
67-
);
69+
if (!LayoutXml.PARENT_DIR.equals(psiFile.getContainingDirectory().getName())
70+
&& !LayoutXml.PAGE_LAYOUT_DIR.equals(psiFile.getContainingDirectory().getName())) {
71+
return false;
72+
}
73+
boolean isAllowed = false;
74+
final GetMagentoModuleUtil.MagentoModuleData moduleData =
75+
GetMagentoModuleUtil.getByContext(psiFile.getContainingDirectory(), project);
6876

69-
if (componentType.equals(ComponentType.module.toString())) {
77+
if (moduleData.getType().equals(ComponentType.module)) {
7078
isAllowed = file.getPath().contains(Package.moduleViewDir);
71-
} else if (componentType.equals(ComponentType.theme.toString())) {
79+
} else if (moduleData.getType().equals(ComponentType.theme)) {
7280
isAllowed = true;
7381
}
7482

@@ -82,11 +90,6 @@ private void setStatus(final AnActionEvent event, final boolean status) {
8290

8391
@Override
8492
public void actionPerformed(final @NotNull AnActionEvent event) {
85-
OverrideInThemeDialog.open(event.getProject(), this.psiFile);
86-
}
87-
88-
@Override
89-
public boolean isDumbAware() {
90-
return false;
93+
OverrideLayoutInThemeDialog.open(event.getProject(), this.psiFile);
9194
}
9295
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.MagentoIcons;
15+
import com.magento.idea.magento2plugin.actions.CopyMagentoPath;
16+
import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideTemplateInThemeDialog;
17+
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
18+
import com.magento.idea.magento2plugin.magento.packages.Package;
19+
import com.magento.idea.magento2plugin.project.Settings;
20+
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
21+
import org.jetbrains.annotations.NotNull;
22+
23+
public class OverrideTemplateInThemeAction extends AnAction {
24+
25+
public static final String ACTION_NAME = "Override this in a project theme";
26+
public static final String ACTION_TEMPLATE_DESCRIPTION = "Override template in project theme";
27+
public static final String ACTION_STYLES_DESCRIPTION = "Override styles in project theme";
28+
public static final String LESS_FILE_EXTENSION = "less";
29+
private PsiFile psiFile;
30+
31+
public OverrideTemplateInThemeAction() {
32+
super(ACTION_NAME, ACTION_TEMPLATE_DESCRIPTION, MagentoIcons.MODULE);
33+
}
34+
35+
/**
36+
* Action entry point.
37+
*
38+
* @param event AnActionEvent
39+
*/
40+
@Override
41+
public void update(final @NotNull AnActionEvent event) {
42+
boolean status = false;
43+
final Project project = event.getData(PlatformDataKeys.PROJECT);
44+
psiFile = event.getData(PlatformDataKeys.PSI_FILE);
45+
46+
if (Settings.isEnabled(project)) {
47+
try {
48+
status = isOverrideAllowed(
49+
psiFile.getVirtualFile(),
50+
project
51+
);
52+
} catch (NullPointerException e) { //NOPMD
53+
// Ignore
54+
}
55+
}
56+
57+
this.setStatus(event, status);
58+
}
59+
60+
private boolean isOverrideAllowed(final VirtualFile file, final Project project) {
61+
if (file.isDirectory()) {
62+
return false;
63+
}
64+
final String fileExtension = psiFile.getVirtualFile().getExtension();
65+
66+
if (!CopyMagentoPath.PHTML_EXTENSION.equals(fileExtension)
67+
&& !LESS_FILE_EXTENSION.equals(fileExtension)) {
68+
return false;
69+
}
70+
boolean isAllowed = false;
71+
final GetMagentoModuleUtil.MagentoModuleData moduleData =
72+
GetMagentoModuleUtil.getByContext(psiFile.getContainingDirectory(), project);
73+
74+
if (moduleData.getType().equals(ComponentType.module)) {
75+
isAllowed = file.getPath().contains(Package.moduleViewDir);
76+
} else if (moduleData.getType().equals(ComponentType.theme)) {
77+
isAllowed = true;
78+
}
79+
80+
return isAllowed;
81+
}
82+
83+
private void setStatus(final AnActionEvent event, final boolean status) {
84+
event.getPresentation().setVisible(status);
85+
event.getPresentation().setEnabled(status);
86+
}
87+
88+
@Override
89+
public void actionPerformed(final @NotNull AnActionEvent event) {
90+
OverrideTemplateInThemeDialog.open(event.getProject(), this.psiFile);
91+
}
92+
93+
@Override
94+
public boolean isDumbAware() {
95+
return false;
96+
}
97+
}

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

Lines changed: 1 addition & 1 deletion
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>

src/com/magento/idea/magento2plugin/actions/generation/dialog/OverrideInThemeDialog.java renamed to src/com/magento/idea/magento2plugin/actions/generation/dialog/OverrideLayoutInThemeDialog.java

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
package com.magento.idea.magento2plugin.actions.generation.dialog;
77

88
import com.intellij.openapi.project.Project;
9+
import com.intellij.psi.PsiDirectory;
910
import com.intellij.psi.PsiFile;
10-
import com.magento.idea.magento2plugin.actions.generation.OverrideInThemeAction;
11+
import com.magento.idea.magento2plugin.actions.generation.OverrideLayoutInThemeAction;
1112
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
1213
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry;
1314
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
14-
import com.magento.idea.magento2plugin.actions.generation.generator.OverrideInThemeGenerator;
15+
import com.magento.idea.magento2plugin.actions.generation.generator.OverrideLayoutInThemeGenerator;
1516
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
1617
import com.magento.idea.magento2plugin.magento.packages.Areas;
18+
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
19+
import com.magento.idea.magento2plugin.magento.packages.Package;
20+
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
1721
import java.awt.event.ActionEvent;
1822
import java.awt.event.KeyEvent;
1923
import java.awt.event.WindowAdapter;
@@ -28,7 +32,7 @@
2832
import javax.swing.KeyStroke;
2933
import org.jetbrains.annotations.NotNull;
3034

31-
public class OverrideInThemeDialog extends AbstractDialog {
35+
public class OverrideLayoutInThemeDialog extends AbstractDialog {
3236
@NotNull
3337
private final Project project;
3438
private final PsiFile psiFile;
@@ -50,15 +54,15 @@ public class OverrideInThemeDialog extends AbstractDialog {
5054
* @param project Project
5155
* @param psiFile PsiFile
5256
*/
53-
public OverrideInThemeDialog(final @NotNull Project project, final PsiFile psiFile) {
57+
public OverrideLayoutInThemeDialog(final @NotNull Project project, final PsiFile psiFile) {
5458
super();
5559

5660
this.project = project;
5761
this.psiFile = psiFile;
5862

5963
setContentPane(contentPane);
6064
setModal(true);
61-
setTitle(OverrideInThemeAction.ACTION_DESCRIPTION);
65+
setTitle(OverrideLayoutInThemeAction.ACTION_DESCRIPTION);
6266
getRootPane().setDefaultButton(buttonOK);
6367
fillThemeOptions();
6468

@@ -97,10 +101,10 @@ private void onExtend() {
97101

98102
private void onOK() {
99103
if (validateFormFields()) {
100-
final OverrideInThemeGenerator overrideInThemeGenerator =
101-
new OverrideInThemeGenerator(project);
104+
final OverrideLayoutInThemeGenerator overrideLayoutInThemeGenerator =
105+
new OverrideLayoutInThemeGenerator(project);
102106

103-
overrideInThemeGenerator.execute(psiFile, this.getTheme(), this.isOverride());
107+
overrideLayoutInThemeGenerator.execute(psiFile, this.getTheme(), this.isOverride());
104108
}
105109
exit();
106110
}
@@ -125,18 +129,38 @@ public boolean isOverride() {
125129
* @param psiFile PsiFile
126130
*/
127131
public static void open(final @NotNull Project project, final PsiFile psiFile) {
128-
final OverrideInThemeDialog dialog = new OverrideInThemeDialog(project, psiFile);
132+
final OverrideLayoutInThemeDialog dialog =
133+
new OverrideLayoutInThemeDialog(project, psiFile);
129134
dialog.pack();
130135
dialog.centerDialog(dialog);
131136
dialog.setVisible(true);
132137
}
133138

134139
private void fillThemeOptions() {
135-
final String area = psiFile.getVirtualFile().getPath().split("view/")[1].split("/")[0];
136-
final List<String> themeNames = new ModuleIndex(project).getEditableThemeNames();
140+
final GetMagentoModuleUtil.MagentoModuleData moduleData =
141+
GetMagentoModuleUtil.getByContext(psiFile.getContainingDirectory(), project);
142+
143+
if (moduleData == null) {
144+
return;
145+
}
146+
String area = ""; // NOPMD;
147+
148+
if (moduleData.getType().equals(ComponentType.module)) {
149+
final PsiDirectory viewDir = moduleData.getViewDir();
137150

151+
if (viewDir == null) {
152+
return;
153+
}
154+
final String filePath = psiFile.getVirtualFile().getPath();
155+
final String relativePath = filePath.replace(viewDir.getVirtualFile().getPath(), "");
156+
area = relativePath.split(Package.V_FILE_SEPARATOR)[1];
157+
} else {
158+
area = moduleData.getName().split(Package.V_FILE_SEPARATOR)[0];
159+
}
160+
final List<String> themeNames = new ModuleIndex(project).getEditableThemeNames();
138161
for (final String themeName : themeNames) {
139-
if (Areas.base.toString().equals(area) || themeName.split("/")[0].equals(area)) {
162+
if (Areas.base.toString().equals(area)
163+
|| themeName.split(Package.V_FILE_SEPARATOR)[0].equals(area)) {
140164
theme.addItem(themeName);
141165
}
142166
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.OverrideTemplateInThemeDialog">
3+
<grid id="27dc6" 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="20" y="20" width="497" height="150"/>
7+
</constraints>
8+
<properties>
9+
<minimumSize width="350" height="150"/>
10+
<preferredSize width="350" height="150"/>
11+
</properties>
12+
<border type="none"/>
13+
<children>
14+
<grid id="99368" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
15+
<margin top="0" left="0" bottom="0" right="0"/>
16+
<constraints>
17+
<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"/>
18+
</constraints>
19+
<properties/>
20+
<border type="none"/>
21+
<children>
22+
<grid id="7170c" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
23+
<margin top="0" left="0" bottom="0" right="0"/>
24+
<constraints>
25+
<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"/>
26+
</constraints>
27+
<properties/>
28+
<border type="none"/>
29+
<children>
30+
<component id="939da" class="javax.swing.JButton" binding="buttonOK">
31+
<constraints>
32+
<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"/>
33+
</constraints>
34+
<properties>
35+
<text resource-bundle="magento2/common" key="common.ok"/>
36+
</properties>
37+
</component>
38+
<component id="894c3" class="javax.swing.JButton" binding="buttonCancel">
39+
<constraints>
40+
<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"/>
41+
</constraints>
42+
<properties>
43+
<text resource-bundle="magento2/common" key="common.cancel"/>
44+
</properties>
45+
</component>
46+
</children>
47+
</grid>
48+
<grid id="88bed" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
49+
<margin top="0" left="0" bottom="0" right="0"/>
50+
<constraints>
51+
<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"/>
52+
</constraints>
53+
<properties/>
54+
<border type="none"/>
55+
<children/>
56+
</grid>
57+
</children>
58+
</grid>
59+
<grid id="b8a26" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
60+
<margin top="0" left="0" bottom="0" right="0"/>
61+
<constraints>
62+
<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"/>
63+
</constraints>
64+
<properties/>
65+
<border type="none"/>
66+
<children>
67+
<component id="922a6" class="javax.swing.JLabel" binding="selectTheme">
68+
<constraints>
69+
<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">
70+
<preferred-size width="319" height="16"/>
71+
</grid>
72+
</constraints>
73+
<properties>
74+
<labelFor value=""/>
75+
<text resource-bundle="magento2/common" key="common.theme.target"/>
76+
</properties>
77+
</component>
78+
<component id="7b530" class="javax.swing.JComboBox" binding="theme">
79+
<constraints>
80+
<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">
81+
<preferred-size width="319" height="30"/>
82+
</grid>
83+
</constraints>
84+
<properties/>
85+
</component>
86+
</children>
87+
</grid>
88+
</children>
89+
</grid>
90+
</form>

0 commit comments

Comments
 (0)