forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewLayoutXmlAction.java
137 lines (118 loc) · 4.63 KB
/
NewLayoutXmlAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.actions.context.xml;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.actions.generation.dialog.NewLayoutTemplateDialog;
import com.magento.idea.magento2plugin.magento.files.LayoutXml;
import com.magento.idea.magento2plugin.magento.packages.Areas;
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 java.util.Arrays;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class NewLayoutXmlAction extends AnAction {
public static final String ACTION_NAME = "Magento 2 Layout File";
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 layout.xml file";
private PsiDirectory targetDirectory;
/**
* New layout.xml file generation action constructor.
*/
public NewLayoutXmlAction() {
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
}
@Override
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
public void update(final @NotNull AnActionEvent event) {
setIsAvailableForEvent(event, false);
final Project project = event.getProject();
if (project == null || !Settings.isEnabled(project)) {
return;
}
final DataContext context = event.getDataContext();
final PsiElement targetElement = LangDataKeys.PSI_ELEMENT.getData(context);
final PsiDirectory targetDirectoryCandidate = resolveTargetDirectory(targetElement);
if (targetDirectoryCandidate == null) {
return;
}
final GetMagentoModuleUtil.MagentoModuleData moduleData = GetMagentoModuleUtil
.getByContext(targetDirectoryCandidate, project);
if (moduleData == null) {
return;
}
final PsiDirectory viewDir = moduleData.getViewDir();
if (viewDir == null) {
return;
}
final List<String> allowedDirectories = Arrays.asList(
Package.moduleViewDir,
Areas.adminhtml.toString(),
Areas.frontend.toString()
);
if (!allowedDirectories.contains(targetDirectoryCandidate.getName())
|| !moduleData.getType().equals(ComponentType.module)) {
return;
}
final PsiDirectory parentDir = targetDirectoryCandidate.getParentDirectory();
if (parentDir == null
|| !targetDirectoryCandidate.equals(viewDir) && !parentDir.equals(viewDir)) {
return;
}
targetDirectory = targetDirectoryCandidate;
setIsAvailableForEvent(event, true);
}
@Override
public void actionPerformed(final @NotNull AnActionEvent event) {
if (event.getProject() == null || targetDirectory == null) {
return;
}
NewLayoutTemplateDialog.open(event.getProject(), targetDirectory);
}
/**
* Set is action available for event.
*
* @param event AnActionEvent
* @param isAvailable boolean
*/
private void setIsAvailableForEvent(
final @NotNull AnActionEvent event,
final boolean isAvailable
) {
event.getPresentation().setVisible(isAvailable);
event.getPresentation().setEnabled(isAvailable);
}
/**
* Resolve target directory.
*
* @param targetElement PsiElement
*
* @return PsiDirectory
*/
private @Nullable PsiDirectory resolveTargetDirectory(final PsiElement targetElement) {
PsiDirectory target = null;
if (targetElement instanceof PsiDirectory) {
target = (PsiDirectory) targetElement;
} else if (targetElement instanceof PsiFile) {
target = ((PsiFile) targetElement).getContainingDirectory();
}
if (target == null) {
return null;
}
if (LayoutXml.PARENT_DIR.equals(target.getName())) {
target = target.getParentDirectory();
}
return target;
}
}