Skip to content

Commit 22ed1c2

Browse files
1026: Code refactoring
1 parent 58abf2c commit 22ed1c2

File tree

8 files changed

+219
-178
lines changed

8 files changed

+219
-178
lines changed
Lines changed: 105 additions & 0 deletions
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/OverrideLayoutInThemeAction.java

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,91 +5,55 @@
55

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

8-
import com.intellij.openapi.actionSystem.AnAction;
98
import com.intellij.openapi.actionSystem.AnActionEvent;
10-
import com.intellij.openapi.actionSystem.PlatformDataKeys;
119
import com.intellij.openapi.project.Project;
1210
import com.intellij.openapi.vfs.VirtualFile;
1311
import com.intellij.psi.PsiFile;
12+
import com.intellij.psi.xml.XmlFile;
1413
import com.magento.idea.magento2plugin.MagentoIcons;
1514
import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideLayoutInThemeDialog;
1615
import com.magento.idea.magento2plugin.magento.files.LayoutXml;
17-
import com.magento.idea.magento2plugin.magento.files.UiComponentGridXmlFile;
18-
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
19-
import com.magento.idea.magento2plugin.magento.packages.Package;
20-
import com.magento.idea.magento2plugin.project.Settings;
21-
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
2216
import org.jetbrains.annotations.NotNull;
2317

24-
public class OverrideLayoutInThemeAction extends AnAction {
18+
public class OverrideLayoutInThemeAction extends OverrideFileInThemeAction {
2519

2620
public static final String ACTION_NAME = "Override this layout in a project theme";
2721
public static final String ACTION_DESCRIPTION = "Override layout in project theme";
28-
private PsiFile psiFile;
2922

3023
public OverrideLayoutInThemeAction() {
3124
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
3225
}
3326

34-
/**
35-
* Action entry point.
36-
*
37-
* @param event AnActionEvent
38-
*/
3927
@Override
40-
public void update(final @NotNull AnActionEvent event) {
41-
boolean status = false;
42-
final Project project = event.getData(PlatformDataKeys.PROJECT);
43-
psiFile = event.getData(PlatformDataKeys.PSI_FILE);
28+
public void actionPerformed(final @NotNull AnActionEvent event) {
29+
final Project project = event.getProject();
4430

45-
if (Settings.isEnabled(project)) {
46-
try {
47-
status = isOverrideAllowed(
48-
psiFile.getVirtualFile(),
49-
project
50-
);
51-
} catch (NullPointerException e) { //NOPMD
52-
// Ignore
53-
}
31+
if (project == null || psiFile == null) {
32+
return;
5433
}
55-
56-
this.setStatus(event, status);
34+
OverrideLayoutInThemeDialog.open(project, psiFile);
5735
}
5836

59-
private boolean isOverrideAllowed(final VirtualFile file, final Project project) {
60-
if (file.isDirectory()) {
61-
return false;
62-
}
37+
@Override
38+
protected boolean isOverrideAllowed(
39+
final @NotNull PsiFile file,
40+
final @NotNull Project project
41+
) {
42+
final VirtualFile virtualFile = file.getVirtualFile();
6343

64-
if (!UiComponentGridXmlFile.FILE_EXTENSION
65-
.equals(psiFile.getVirtualFile().getExtension())) {
44+
if (virtualFile == null || virtualFile.isDirectory()) {
6645
return false;
6746
}
6847

69-
if (!LayoutXml.PARENT_DIR.equals(psiFile.getContainingDirectory().getName())
70-
&& !LayoutXml.PAGE_LAYOUT_DIR.equals(psiFile.getContainingDirectory().getName())) {
48+
if (!(file instanceof XmlFile)) {
7149
return false;
7250
}
73-
boolean isAllowed = false;
74-
final GetMagentoModuleUtil.MagentoModuleData moduleData =
75-
GetMagentoModuleUtil.getByContext(psiFile.getContainingDirectory(), project);
7651

77-
if (moduleData.getType().equals(ComponentType.module)) {
78-
isAllowed = file.getPath().contains(Package.moduleViewDir);
79-
} else if (moduleData.getType().equals(ComponentType.theme)) {
80-
isAllowed = true;
52+
if (!LayoutXml.PARENT_DIR.equals(file.getContainingDirectory().getName())
53+
&& !LayoutXml.PAGE_LAYOUT_DIR.equals(file.getContainingDirectory().getName())) {
54+
return false;
8155
}
8256

83-
return isAllowed;
84-
}
85-
86-
private void setStatus(final AnActionEvent event, final boolean status) {
87-
event.getPresentation().setVisible(status);
88-
event.getPresentation().setEnabled(status);
89-
}
90-
91-
@Override
92-
public void actionPerformed(final @NotNull AnActionEvent event) {
93-
OverrideLayoutInThemeDialog.open(event.getProject(), this.psiFile);
57+
return isFileInModuleOrTheme(file, project);
9458
}
9559
}

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

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,93 +5,53 @@
55

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

8-
import com.intellij.openapi.actionSystem.AnAction;
98
import com.intellij.openapi.actionSystem.AnActionEvent;
10-
import com.intellij.openapi.actionSystem.PlatformDataKeys;
119
import com.intellij.openapi.project.Project;
1210
import com.intellij.openapi.vfs.VirtualFile;
1311
import com.intellij.psi.PsiFile;
1412
import com.magento.idea.magento2plugin.MagentoIcons;
1513
import com.magento.idea.magento2plugin.actions.CopyMagentoPath;
1614
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;
2115
import org.jetbrains.annotations.NotNull;
2216

23-
public class OverrideTemplateInThemeAction extends AnAction {
17+
public class OverrideTemplateInThemeAction extends OverrideFileInThemeAction {
2418

25-
public static final String ACTION_NAME = "Override this in a project theme";
19+
public static final String ACTION_NAME = "Override this file in a project theme";
2620
public static final String ACTION_TEMPLATE_DESCRIPTION = "Override template in project theme";
2721
public static final String ACTION_STYLES_DESCRIPTION = "Override styles in project theme";
2822
public static final String LESS_FILE_EXTENSION = "less";
29-
private PsiFile psiFile;
3023

3124
public OverrideTemplateInThemeAction() {
3225
super(ACTION_NAME, ACTION_TEMPLATE_DESCRIPTION, MagentoIcons.MODULE);
3326
}
3427

35-
/**
36-
* Action entry point.
37-
*
38-
* @param event AnActionEvent
39-
*/
4028
@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);
29+
public void actionPerformed(final @NotNull AnActionEvent event) {
30+
final Project project = event.getProject();
4531

46-
if (Settings.isEnabled(project)) {
47-
try {
48-
status = isOverrideAllowed(
49-
psiFile.getVirtualFile(),
50-
project
51-
);
52-
} catch (NullPointerException e) { //NOPMD
53-
// Ignore
54-
}
32+
if (project == null || psiFile == null) {
33+
return;
5534
}
56-
57-
this.setStatus(event, status);
35+
OverrideTemplateInThemeDialog.open(project, psiFile);
5836
}
5937

60-
private boolean isOverrideAllowed(final VirtualFile file, final Project project) {
61-
if (file.isDirectory()) {
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()) {
6246
return false;
6347
}
64-
final String fileExtension = psiFile.getVirtualFile().getExtension();
48+
final String fileExtension = virtualFile.getExtension();
6549

6650
if (!CopyMagentoPath.PHTML_EXTENSION.equals(fileExtension)
6751
&& !LESS_FILE_EXTENSION.equals(fileExtension)) {
6852
return false;
6953
}
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-
}
8254

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;
55+
return isFileInModuleOrTheme(file, project);
9656
}
9757
}

0 commit comments

Comments
 (0)