Skip to content

Commit 0ea99c2

Browse files
committed
Add Override in Theme Menu Action
1 parent 150b972 commit 0ea99c2

File tree

8 files changed

+460
-84
lines changed

8 files changed

+460
-84
lines changed

resources/META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
7777
<action id="OverrideClassByAPreference.Menu" class="com.magento.idea.magento2plugin.actions.generation.OverrideClassByAPreferenceAction">
7878
<add-to-group group-id="EditorPopupMenu"/>
7979
</action>
80+
<action id="OverrideInTheme.Menu" class="com.magento.idea.magento2plugin.actions.generation.OverrideInThemeAction">
81+
<add-to-group group-id="ProjectViewPopupMenu"/>
82+
</action>
8083

8184
</actions>
8285

resources/magento2/common.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ common.ok=OK
88
common.cancel=Cancel
99
common.error=Error
1010
common.module.target=Target Module
11+
common.theme.target=Target Theme
1112
common.area.target=Target Area
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.actions.generation;
6+
7+
import com.intellij.openapi.actionSystem.AnActionEvent;
8+
import com.intellij.openapi.actionSystem.PlatformDataKeys;
9+
import com.intellij.openapi.project.DumbAwareAction;
10+
import com.intellij.openapi.project.Project;
11+
import com.intellij.openapi.vfs.VirtualFile;
12+
import com.intellij.psi.PsiFile;
13+
import com.magento.idea.magento2plugin.MagentoIcons;
14+
import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideInThemeDialog;
15+
import com.magento.idea.magento2plugin.project.Settings;
16+
import com.magento.idea.magento2plugin.util.RegExUtil;
17+
import com.magento.idea.magento2plugin.util.magento.GetComponentNameByDirectory;
18+
import org.jetbrains.annotations.NotNull;
19+
20+
import java.util.regex.Matcher;
21+
import java.util.regex.Pattern;
22+
23+
public class OverrideInThemeAction extends DumbAwareAction {
24+
public static String ACTION_NAME = "Override in theme...";
25+
public static String ACTION_DESCRIPTION = "Override template in project theme.";
26+
private PsiFile psiFile;
27+
28+
public OverrideInThemeAction() {
29+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
30+
}
31+
32+
public void update(@NotNull AnActionEvent event) {
33+
boolean status = false;
34+
Project project = event.getData(PlatformDataKeys.PROJECT);
35+
psiFile = event.getData(PlatformDataKeys.PSI_FILE);
36+
37+
if (Settings.isEnabled(project)) {
38+
try {
39+
status = isOverrideAllowed(
40+
psiFile.getVirtualFile(),
41+
project
42+
);
43+
} catch (NullPointerException e) {
44+
// Ignore
45+
}
46+
}
47+
48+
this.setStatus(event, status);
49+
}
50+
51+
private boolean isOverrideAllowed(VirtualFile file, Project project) throws NullPointerException {
52+
if (file.isDirectory()) {
53+
return false;
54+
}
55+
56+
boolean isAllowed = false;
57+
58+
GetComponentNameByDirectory getComponentNameByDirectory = GetComponentNameByDirectory.getInstance(project);
59+
String componentName = getComponentNameByDirectory.execute(psiFile.getContainingDirectory());
60+
61+
if (isModule(componentName)) {
62+
isAllowed = file.getPath().contains("view");
63+
} else if (isTheme(componentName)) {
64+
isAllowed = true;
65+
}
66+
67+
return isAllowed;
68+
}
69+
70+
private boolean isModule(String componentName) {
71+
Pattern modulePattern = Pattern.compile(RegExUtil.Magento.MODULE_NAME);
72+
Matcher moduleMatcher = modulePattern.matcher(componentName);
73+
74+
return moduleMatcher.find();
75+
}
76+
77+
private boolean isTheme(String componentName) {
78+
Pattern themePattern = Pattern.compile(RegExUtil.Magento.THEME_NAME);
79+
Matcher themeMatcher = themePattern.matcher(componentName);
80+
81+
return themeMatcher.find();
82+
}
83+
84+
private void setStatus(AnActionEvent event, boolean status) {
85+
event.getPresentation().setVisible(status);
86+
event.getPresentation().setEnabled(status);
87+
}
88+
89+
@Override
90+
public void actionPerformed(@NotNull AnActionEvent e) {
91+
OverrideInThemeDialog.open(e.getProject(), this.psiFile);
92+
}
93+
94+
@Override
95+
public boolean isDumbAware() {
96+
return false;
97+
}
98+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.OverrideInThemeDialog">
3+
<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">
4+
<margin top="10" left="10" bottom="10" right="10"/>
5+
<constraints>
6+
<xy x="48" y="392" width="584" height="162"/>
7+
</constraints>
8+
<properties>
9+
<minimumSize width="350" height="150"/>
10+
<preferredSize width="350" height="150"/>
11+
<requestFocusEnabled value="true"/>
12+
</properties>
13+
<border type="none"/>
14+
<children>
15+
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
16+
<margin top="0" left="0" bottom="0" right="0"/>
17+
<constraints>
18+
<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"/>
19+
</constraints>
20+
<properties/>
21+
<border type="none"/>
22+
<children>
23+
<hspacer id="98af6">
24+
<constraints>
25+
<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"/>
26+
</constraints>
27+
</hspacer>
28+
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
29+
<margin top="0" left="0" bottom="0" right="0"/>
30+
<constraints>
31+
<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"/>
32+
</constraints>
33+
<properties/>
34+
<border type="none"/>
35+
<children>
36+
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
37+
<constraints>
38+
<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"/>
39+
</constraints>
40+
<properties>
41+
<text resource-bundle="magento2/common" key="common.ok"/>
42+
</properties>
43+
</component>
44+
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
45+
<constraints>
46+
<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"/>
47+
</constraints>
48+
<properties>
49+
<text resource-bundle="magento2/common" key="common.cancel"/>
50+
</properties>
51+
</component>
52+
</children>
53+
</grid>
54+
</children>
55+
</grid>
56+
<grid id="e3588" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
57+
<margin top="0" left="0" bottom="0" right="0"/>
58+
<constraints>
59+
<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"/>
60+
</constraints>
61+
<properties/>
62+
<border type="none"/>
63+
<children>
64+
<component id="5196a" class="javax.swing.JLabel" binding="selectTheme">
65+
<constraints>
66+
<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">
67+
<preferred-size width="319" height="16"/>
68+
</grid>
69+
</constraints>
70+
<properties>
71+
<labelFor value=""/>
72+
<text resource-bundle="magento2/common" key="common.theme.target"/>
73+
</properties>
74+
</component>
75+
<component id="bd654" class="com.magento.idea.magento2plugin.ui.FilteredComboBox" binding="theme" custom-create="true">
76+
<constraints>
77+
<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">
78+
<preferred-size width="319" height="30"/>
79+
</grid>
80+
</constraints>
81+
<properties/>
82+
</component>
83+
</children>
84+
</grid>
85+
</children>
86+
</grid>
87+
</form>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.actions.generation.dialog;
6+
7+
import com.intellij.openapi.project.Project;
8+
import com.intellij.openapi.vfs.VirtualFile;
9+
import com.intellij.psi.PsiFile;
10+
import com.jetbrains.php.lang.psi.elements.Method;
11+
import com.jetbrains.php.lang.psi.elements.PhpClass;
12+
import com.magento.idea.magento2plugin.actions.generation.CreateAPluginAction;
13+
import com.magento.idea.magento2plugin.actions.generation.data.PluginDiXmlData;
14+
import com.magento.idea.magento2plugin.actions.generation.data.PluginFileData;
15+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.CreateAPluginDialogValidator;
16+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.OverrideInThemeDialogValidator;
17+
import com.magento.idea.magento2plugin.actions.generation.generator.PluginClassGenerator;
18+
import com.magento.idea.magento2plugin.actions.generation.generator.PluginDiXmlGenerator;
19+
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
20+
import com.magento.idea.magento2plugin.magento.files.Plugin;
21+
import com.magento.idea.magento2plugin.magento.packages.File;
22+
import com.magento.idea.magento2plugin.magento.packages.Package;
23+
import com.magento.idea.magento2plugin.ui.FilteredComboBox;
24+
import org.jetbrains.annotations.NotNull;
25+
26+
import javax.swing.*;
27+
import java.awt.event.*;
28+
import java.util.List;
29+
30+
public class OverrideInThemeDialog extends AbstractDialog {
31+
@NotNull
32+
private final Project project;
33+
private Method targetMethod;
34+
private PhpClass targetClass;
35+
@NotNull
36+
private final OverrideInThemeDialogValidator validator;
37+
private JPanel contentPane;
38+
private JButton buttonOK;
39+
private JButton buttonCancel;
40+
private JLabel selectTheme;
41+
private FilteredComboBox theme;
42+
43+
public OverrideInThemeDialog(@NotNull Project project, PsiFile psiFile) {
44+
this.project = project;
45+
this.validator = OverrideInThemeDialogValidator.getInstance(this);
46+
47+
setContentPane(contentPane);
48+
setModal(true);
49+
getRootPane().setDefaultButton(buttonOK);
50+
pushToMiddle();
51+
52+
buttonOK.addActionListener(new ActionListener() {
53+
public void actionPerformed(ActionEvent e) {
54+
onOK();
55+
}
56+
});
57+
58+
buttonCancel.addActionListener(new ActionListener() {
59+
public void actionPerformed(ActionEvent e) {
60+
onCancel();
61+
}
62+
});
63+
64+
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
65+
addWindowListener(new WindowAdapter() {
66+
public void windowClosing(WindowEvent e) {
67+
onCancel();
68+
}
69+
});
70+
71+
contentPane.registerKeyboardAction(new ActionListener() {
72+
public void actionPerformed(ActionEvent e) {
73+
onCancel();
74+
}
75+
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
76+
}
77+
78+
private void onOK() {
79+
if (!validator.validate(project)) {
80+
return;
81+
}
82+
83+
// TODO: implement generator
84+
85+
this.setVisible(false);
86+
}
87+
88+
public String getTheme() {
89+
return this.theme.getSelectedItem().toString();
90+
}
91+
92+
public static void open(@NotNull Project project, PsiFile psiFile) {
93+
OverrideInThemeDialog dialog = new OverrideInThemeDialog(project, psiFile);
94+
dialog.pack();
95+
dialog.setVisible(true);
96+
}
97+
98+
private void createUIComponents() {
99+
List<String> allThemesList = ModuleIndex.getInstance(project).getEditableThemeNames();
100+
101+
this.theme = new FilteredComboBox(allThemesList);
102+
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.actions.generation.dialog.validator;
6+
7+
import com.intellij.openapi.project.Project;
8+
import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideInThemeDialog;
9+
import com.magento.idea.magento2plugin.bundles.CommonBundle;
10+
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
11+
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
12+
13+
import javax.swing.*;
14+
import java.util.List;
15+
16+
public class OverrideInThemeDialogValidator {
17+
private static OverrideInThemeDialogValidator INSTANCE = null;
18+
private final ValidatorBundle validatorBundle;
19+
private final CommonBundle commonBundle;
20+
private OverrideInThemeDialog dialog;
21+
22+
public static OverrideInThemeDialogValidator getInstance(OverrideInThemeDialog dialog) {
23+
if (null == INSTANCE) {
24+
INSTANCE = new OverrideInThemeDialogValidator();
25+
}
26+
27+
INSTANCE.dialog = dialog;
28+
return INSTANCE;
29+
}
30+
31+
public OverrideInThemeDialogValidator() {
32+
this.validatorBundle = new ValidatorBundle();
33+
this.commonBundle = new CommonBundle();
34+
}
35+
36+
public boolean validate(Project project)
37+
{
38+
String errorTitle = commonBundle.message("common.error");
39+
40+
String theme = dialog.getTheme();
41+
if (theme.length() == 0) {
42+
String errorMessage = validatorBundle.message("validator.notEmpty", "Target Theme");
43+
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
44+
45+
return false;
46+
}
47+
48+
List<String> allThemesList = ModuleIndex.getInstance(project).getEditableThemeNames();
49+
if (!allThemesList.contains(theme)) {
50+
String errorMessage = validatorBundle.message("validator.module.noSuchModule", theme);
51+
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
52+
53+
return false;
54+
}
55+
56+
return true;
57+
}
58+
}

0 commit comments

Comments
 (0)