Skip to content

Commit 2f3991d

Browse files
Merge branch '4.4.0-develop' into 1102-bug-fix-create-observer-not-work-through-the-context-menu
2 parents b93936f + 19a7791 commit 2f3991d

File tree

13 files changed

+288
-65
lines changed

13 files changed

+288
-65
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!---
2+
You can describe your module here.
3+
We recommend that you add the following information:
4+
- implementation details: why and how to use the module, preferably with some example scenarios
5+
- any dependencies (usually other modules but could be any important dependencies, libraries, etc)
6+
- extension points, APIs, plug-ins, etc
7+
- any introduced events
8+
-->
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html>
2+
<body>
3+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse; border-color:#111111">
4+
<tr>
5+
<td colspan="3"><font face="verdana" size="-1">This is a template is used to describe the MD file. It contains a code fragment that can be included into file templates
6+
(<b>Templates</b> tab) with the help of the <b>#parse</b> directive.<br>
7+
The template is editable. Along with the static text, code and comments.</font><br>
8+
</td>
9+
</tr>
10+
</table>
11+
</body>
12+
</html>
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
# ${PACKAGE}_${MODULE_NAME} module
22

3-
<!---
4-
You can describe your module here.
5-
We recommend that you add the following information:
6-
- implementation details: why and how to use the module, preferably with some example scenarios
7-
- any dependencies (usually other modules but could be any important dependencies, libraries, etc)
8-
- extension points, APIs, plug-ins, etc
9-
- any introduced events
10-
-->
3+
#parse("MD File Description.md")
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.context;
7+
8+
import com.intellij.openapi.actionSystem.AnAction;
9+
import com.intellij.openapi.actionSystem.AnActionEvent;
10+
import com.intellij.openapi.project.Project;
11+
import com.intellij.openapi.util.NlsActions;
12+
import com.intellij.psi.PsiDirectory;
13+
import com.intellij.psi.PsiFile;
14+
import com.magento.idea.magento2plugin.MagentoIcons;
15+
import com.magento.idea.magento2plugin.actions.context.util.GetTargetElementUtil;
16+
import com.magento.idea.magento2plugin.project.Settings;
17+
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
18+
import org.jetbrains.annotations.NotNull;
19+
import org.jetbrains.annotations.Nullable;
20+
21+
public abstract class CustomGeneratorContextAction extends AnAction {
22+
23+
private @Nullable GetMagentoModuleUtil.MagentoModuleData moduleData;
24+
private @Nullable PsiDirectory directory;
25+
private @Nullable PsiFile file;
26+
27+
/**
28+
* Abstract context action with custom generation constructor.
29+
*
30+
* @param text String
31+
* @param description String
32+
*/
33+
public CustomGeneratorContextAction(
34+
final @Nullable @NlsActions.ActionText String text,
35+
final @Nullable @NlsActions.ActionDescription String description
36+
) {
37+
super(text, description, MagentoIcons.MODULE);
38+
}
39+
40+
@Override
41+
public void update(final @NotNull AnActionEvent event) {
42+
setIsAvailableForEvent(event, false);
43+
final Project project = event.getProject();
44+
45+
if (project == null || !Settings.isEnabled(project)) {
46+
return;
47+
}
48+
final PsiDirectory targetDirectory = GetTargetElementUtil.getDirFromEvent(event);
49+
50+
if (targetDirectory == null) {
51+
return;
52+
}
53+
final GetMagentoModuleUtil.MagentoModuleData magentoModuleData = GetMagentoModuleUtil
54+
.getByContext(targetDirectory, project);
55+
final PsiFile targetFile = GetTargetElementUtil.getFileFromEvent(event);
56+
57+
if (magentoModuleData == null
58+
|| magentoModuleData.getName() == null
59+
|| !isVisible(magentoModuleData, targetDirectory, targetFile)) {
60+
return;
61+
}
62+
directory = targetDirectory;
63+
file = targetFile;
64+
moduleData = magentoModuleData;
65+
setIsAvailableForEvent(event, true);
66+
}
67+
68+
/**
69+
* Get clicked on module data object.
70+
*
71+
* @return GetMagentoModuleUtil.MagentoModuleData
72+
*/
73+
public @Nullable GetMagentoModuleUtil.MagentoModuleData getModuleData() {
74+
return moduleData;
75+
}
76+
77+
public @Nullable PsiDirectory getDirectory() {
78+
return directory;
79+
}
80+
81+
public @Nullable PsiFile getFile() {
82+
return file;
83+
}
84+
85+
/**
86+
* Implement check if an action should be shown in the context defined by the module,
87+
* target directory or target file.
88+
*
89+
* @param moduleData GetMagentoModuleUtil.MagentoModuleData
90+
* @param targetDirectory PsiDirectory
91+
* @param targetFile PsiFile
92+
*
93+
* @return boolean
94+
*/
95+
protected abstract boolean isVisible(
96+
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData,
97+
final PsiDirectory targetDirectory,
98+
final PsiFile targetFile
99+
);
100+
101+
/**
102+
* Set is action available for event.
103+
*
104+
* @param event AnActionEvent
105+
* @param isAvailable boolean
106+
*/
107+
private void setIsAvailableForEvent(
108+
final @NotNull AnActionEvent event,
109+
final boolean isAvailable
110+
) {
111+
event.getPresentation().setVisible(isAvailable);
112+
event.getPresentation().setEnabled(isAvailable);
113+
}
114+
}

src/com/magento/idea/magento2plugin/actions/context/md/NewReadmeMdAction.java

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

66
package com.magento.idea.magento2plugin.actions.context.md;
77

8-
import com.intellij.ide.fileTemplates.actions.AttributesDefaults;
8+
import com.intellij.openapi.actionSystem.AnActionEvent;
99
import com.intellij.psi.PsiDirectory;
1010
import com.intellij.psi.PsiFile;
11-
import com.magento.idea.magento2plugin.actions.context.AbstractContextAction;
11+
import com.intellij.util.IncorrectOperationException;
12+
import com.magento.idea.magento2plugin.actions.context.CustomGeneratorContextAction;
13+
import com.magento.idea.magento2plugin.actions.generation.data.ModuleReadmeMdData;
14+
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleReadmeMdGenerator;
1215
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
1316
import com.magento.idea.magento2plugin.magento.files.ModuleReadmeMdFile;
1417
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
1518
import com.magento.idea.magento2plugin.magento.packages.Package;
1619
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
1720
import org.jetbrains.annotations.NotNull;
1821

19-
public class NewReadmeMdAction extends AbstractContextAction {
22+
public class NewReadmeMdAction extends CustomGeneratorContextAction {
2023

2124
public static final String ACTION_NAME = "Magento 2 README File";
2225
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 README file";
2326

2427
public NewReadmeMdAction() {
25-
super(ACTION_NAME, ACTION_DESCRIPTION, new ModuleReadmeMdFile());
28+
super(ACTION_NAME, ACTION_DESCRIPTION);
29+
}
30+
31+
@Override
32+
public void actionPerformed(final @NotNull AnActionEvent event) {
33+
final GetMagentoModuleUtil.MagentoModuleData moduleData = getModuleData();
34+
35+
if (event.getProject() == null || moduleData == null || getDirectory() == null) {
36+
return;
37+
}
38+
final String[] templateData = moduleData.getName().split(Package.vendorModuleNameSeparator);
39+
40+
if (templateData.length != 2) { //NOPMD
41+
return;
42+
}
43+
final ModuleReadmeMdGenerator generator = new ModuleReadmeMdGenerator(
44+
new ModuleReadmeMdData(
45+
templateData[0],
46+
templateData[1],
47+
getDirectory()
48+
),
49+
event.getProject()
50+
);
51+
generator.generate(ACTION_NAME, true);
2652
}
2753

2854
@Override
@@ -36,25 +62,26 @@ protected boolean isVisible(
3662
}
3763
final PsiDirectory moduleDirectory = new ModuleIndex(targetDirectory.getProject())
3864
.getModuleDirectoryByModuleName(moduleData.getName());
39-
final String magentoModuleName = moduleData
40-
.getName()
41-
.split(Package.vendorModuleNameSeparator)[1];
4265

43-
return targetDirectory.getName().equals(magentoModuleName)
44-
&& targetDirectory.equals(moduleDirectory);
66+
if (moduleDirectory == null) {
67+
return false;
68+
}
69+
final String[] templateData = moduleData.getName().split(Package.vendorModuleNameSeparator);
70+
71+
return templateData.length == 2
72+
&& targetDirectory.equals(moduleDirectory)
73+
&& isFileCanBeCreated(moduleDirectory);
4574
}
4675

47-
@Override
48-
protected AttributesDefaults getProperties(
49-
final @NotNull AttributesDefaults defaults,
50-
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData,
51-
final PsiDirectory targetDirectory,
52-
final PsiFile targetFile
76+
private @NotNull Boolean isFileCanBeCreated(
77+
final @NotNull PsiDirectory moduleDirectory
5378
) {
54-
final String[] templateData = moduleData.getName().split(Package.vendorModuleNameSeparator);
55-
defaults.addPredefined("PACKAGE", templateData[0]);
56-
defaults.addPredefined("MODULE_NAME", templateData[1]);
79+
try {
80+
moduleDirectory.checkCreateFile(ModuleReadmeMdFile.FILE_NAME);
81+
} catch (IncorrectOperationException exception) {
82+
return false;
83+
}
5784

58-
return defaults;
85+
return true;
5986
}
6087
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.context.util;
7+
8+
import com.intellij.openapi.actionSystem.AnActionEvent;
9+
import com.intellij.openapi.actionSystem.LangDataKeys;
10+
import com.intellij.psi.PsiDirectory;
11+
import com.intellij.psi.PsiElement;
12+
import com.intellij.psi.PsiFile;
13+
import org.jetbrains.annotations.NotNull;
14+
import org.jetbrains.annotations.Nullable;
15+
16+
public final class GetTargetElementUtil {
17+
18+
private GetTargetElementUtil() {}
19+
20+
/**
21+
* Get clicked on directory from the action event.
22+
*
23+
* @param event AnActionEvent
24+
*
25+
* @return PsiDirectory
26+
*/
27+
public static @Nullable PsiDirectory getDirFromEvent(final @NotNull AnActionEvent event) {
28+
final PsiElement targetElement = LangDataKeys.PSI_ELEMENT.getData(event.getDataContext());
29+
30+
if (targetElement == null) {
31+
return null;
32+
}
33+
PsiDirectory targetDirectory = null;
34+
PsiFile targetFile;
35+
36+
if (targetElement instanceof PsiDirectory) {
37+
targetDirectory = (PsiDirectory) targetElement;
38+
} else if (targetElement instanceof PsiFile) {
39+
targetFile = (PsiFile) targetElement;
40+
targetDirectory = targetFile.getContainingDirectory();
41+
}
42+
43+
return targetDirectory;
44+
}
45+
46+
/**
47+
* Get clicked on file from the action event.
48+
*
49+
* @param event AnActionEvent
50+
*
51+
* @return PsiDirectory
52+
*/
53+
public static @Nullable PsiFile getFileFromEvent(final @NotNull AnActionEvent event) {
54+
final PsiElement targetElement = LangDataKeys.PSI_ELEMENT.getData(event.getDataContext());
55+
56+
if (targetElement == null) {
57+
return null;
58+
}
59+
60+
return targetElement instanceof PsiFile ? (PsiFile) targetElement : null;
61+
}
62+
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
import org.jetbrains.annotations.NotNull;
2424

2525
public class NewModuleAction extends com.intellij.openapi.actionSystem.AnAction {
26-
public static String actionName = "Magento 2 Module";
27-
public static String actionDescription = "Create a new Magento 2 Module";
26+
public static final String ACTION_NAME = "Magento 2 Module";
27+
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Module";
2828

2929
/**
3030
* Constructor.
3131
*/
3232
public NewModuleAction() {
33-
super(actionName, actionDescription, MagentoIcons.MODULE);
33+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
3434
}
3535

3636
@Override
@@ -67,6 +67,10 @@ public boolean isDumbAware() {
6767
public void update(final AnActionEvent event) {
6868
final Project project = event.getData(PlatformDataKeys.PROJECT);
6969

70+
if (project == null) {
71+
return;
72+
}
73+
7074
if (Settings.isEnabled(project)) {
7175
final String magentoPath = Settings.getMagentoPath(project);
7276
if (magentoPath == null) {

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCategoryEavAttributeDialog.form

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
</constraints>
88
<properties>
99
<enabled value="true"/>
10-
<preferredSize width="800" height="800"/>
10+
<preferredSize width="800" height="700"/>
1111
</properties>
1212
<border type="none"/>
1313
<children>
1414
<grid id="6fc7e" layout-manager="GridLayoutManager" row-count="16" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
1515
<margin top="0" left="0" bottom="0" right="0"/>
1616
<constraints>
17-
<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"/>
17+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="9" fill="0" indent="0" use-parent-layout="false">
18+
<preferred-size width="800" height="-1"/>
19+
</grid>
1820
</constraints>
1921
<properties>
2022
<visible value="true"/>
@@ -334,11 +336,6 @@
334336
</component>
335337
</children>
336338
</grid>
337-
<hspacer id="75d92">
338-
<constraints>
339-
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
340-
</constraints>
341-
</hspacer>
342339
<grid id="181ea" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
343340
<margin top="0" left="0" bottom="0" right="0"/>
344341
<constraints>
@@ -420,6 +417,11 @@
420417
</component>
421418
</children>
422419
</grid>
420+
<vspacer id="596dd">
421+
<constraints>
422+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
423+
</constraints>
424+
</vspacer>
423425
</children>
424426
</grid>
425427
</form>

0 commit comments

Comments
 (0)