forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewModuleAction.java
120 lines (105 loc) · 4.32 KB
/
NewModuleAction.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
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.actions.generation;
import com.intellij.ide.IdeView;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.actions.generation.dialog.NewModuleDialog;
import com.magento.idea.magento2plugin.actions.generation.util.IsClickedDirectoryInsideProject;
import com.magento.idea.magento2plugin.project.Settings;
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
import com.magento.idea.magento2plugin.util.magento.MagentoBasePathUtil;
import org.jetbrains.annotations.NotNull;
public class NewModuleAction extends com.intellij.openapi.actionSystem.AnAction {
public static final String ACTION_NAME = "Magento 2 Module";
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Module";
/**
* Constructor.
*/
public NewModuleAction() {
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
}
@Override
public void actionPerformed(final @NotNull AnActionEvent event) {
final DataContext dataContext = event.getDataContext();
final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
if (view == null) {
return;
}
final Project project = CommonDataKeys.PROJECT.getData(dataContext);
if (project != null) {
final PsiDirectory initialBaseDir = view.getOrChooseDirectory();
if (initialBaseDir != null) {
this.invoke(project, initialBaseDir);
}
}
}
public void invoke(final @NotNull Project project, final @NotNull PsiDirectory initialBaseDir) {
NewModuleDialog.open(project, initialBaseDir);
}
@Override
public boolean isDumbAware() {
return false;
}
/**
* Inherit doc.
*
* @param event AnActionEvent
*/
@Override
public void update(final AnActionEvent event) {
final Project project = event.getData(PlatformDataKeys.PROJECT);
if (project == null) {
return;
}
if (Settings.isEnabled(project)) {
final String magentoPath = Settings.getMagentoPath(project);
if (magentoPath == null) {
event.getPresentation().setVisible(false);
return;
}
final PsiElement psiElement = event.getData(PlatformDataKeys.PSI_ELEMENT);
if (!(psiElement instanceof PsiDirectory)) {
event.getPresentation().setVisible(false);
return;
}
if (!IsClickedDirectoryInsideProject.getInstance().execute(
project,
(PsiDirectory) psiElement)
) {
event.getPresentation().setVisible(false);
return;
}
final String moduleName = GetModuleNameByDirectoryUtil
.execute((PsiDirectory) psiElement, project);
if (moduleName == null) {
final String sourceDirPath = ((PsiDirectory) psiElement).getVirtualFile().getPath();
final boolean isCustomCodeSourceDirValid =
MagentoBasePathUtil.isCustomCodeSourceDirValid(sourceDirPath);
final boolean isCustomVendorDirValid =
MagentoBasePathUtil.isCustomVendorDirValid(sourceDirPath);
if (!isCustomCodeSourceDirValid && !isCustomVendorDirValid) { //NOPMD
event.getPresentation().setVisible(false);
return;
}
event.getPresentation().setVisible(true);
return;
}
}
event.getPresentation().setVisible(false);
}
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
}