forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomGeneratorContextAction.java
120 lines (105 loc) · 3.83 KB
/
CustomGeneratorContextAction.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.context;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsActions;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.actions.context.util.GetTargetElementUtil;
import com.magento.idea.magento2plugin.project.Settings;
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public abstract class CustomGeneratorContextAction extends AnAction {
private @Nullable GetMagentoModuleUtil.MagentoModuleData moduleData;
private @Nullable PsiDirectory directory;
private @Nullable PsiFile file;
/**
* Abstract context action with custom generation constructor.
*
* @param text String
* @param description String
*/
public CustomGeneratorContextAction(
final @Nullable @NlsActions.ActionText String text,
final @Nullable @NlsActions.ActionDescription String description
) {
super(text, description, MagentoIcons.MODULE);
}
@Override
public void update(final @NotNull AnActionEvent event) {
setIsAvailableForEvent(event, false);
final Project project = event.getProject();
if (project == null || !Settings.isEnabled(project)) {
return;
}
final PsiDirectory targetDirectory = GetTargetElementUtil.getDirFromEvent(event);
if (targetDirectory == null) {
return;
}
final GetMagentoModuleUtil.MagentoModuleData magentoModuleData = GetMagentoModuleUtil
.getByContext(targetDirectory, project);
final PsiFile targetFile = GetTargetElementUtil.getFileFromEvent(event);
if (magentoModuleData == null
|| magentoModuleData.getName() == null
|| !isVisible(magentoModuleData, targetDirectory, targetFile)) {
return;
}
directory = targetDirectory;
file = targetFile;
moduleData = magentoModuleData;
setIsAvailableForEvent(event, true);
}
/**
* Get clicked on module data object.
*
* @return GetMagentoModuleUtil.MagentoModuleData
*/
public @Nullable GetMagentoModuleUtil.MagentoModuleData getModuleData() {
return moduleData;
}
public @Nullable PsiDirectory getDirectory() {
return directory;
}
public @Nullable PsiFile getFile() {
return file;
}
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
/**
* Implement check if an action should be shown in the context defined by the module,
* target directory or target file.
*
* @param moduleData GetMagentoModuleUtil.MagentoModuleData
* @param targetDirectory PsiDirectory
* @param targetFile PsiFile
*
* @return boolean
*/
protected abstract boolean isVisible(
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData,
final PsiDirectory targetDirectory,
final PsiFile targetFile
);
/**
* 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);
}
}