Skip to content

Commit 30837cc

Browse files
author
Vitaliy
authored
Merge pull request #67 from magento/overide-class-by-prefference
Added override class by a preference action
2 parents 4944013 + af8f358 commit 30837cc

22 files changed

+992
-81
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
* Create a New Magento 2 Module action
1212
* Code Inspection: Duplicated Observer Usage in events XML
1313
* Create a Plugin class for a class public method action
14-
* Code Inspection: Warning regarding Cacheable false attribute in default XML
14+
* Code Inspection: Warning regarding Cacheable false attribute in default XML
15+
* Create a Preference for a class action
1516

1617
0.3.0
1718
=============

resources/META-INF/plugin.xml

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
<action id="MagentoCreateAPlugin.Menu" class="com.magento.idea.magento2plugin.actions.generation.CreateAPluginAction">
6161
<add-to-group group-id="EditorPopupMenu"/>
6262
</action>
63+
<action id="OverrideClassByAPreference.Menu" class="com.magento.idea.magento2plugin.actions.generation.OverrideClassByAPreferenceAction">
64+
<add-to-group group-id="EditorPopupMenu"/>
65+
</action>
6366
</actions>
6467

6568
<extensions defaultExtensionNs="com.intellij">
@@ -130,6 +133,7 @@
130133
<internalFileTemplate name="Magento Module Registration Php"/>
131134
<internalFileTemplate name="Magento Module Xml"/>
132135
<internalFileTemplate name="Magento Module DI Xml"/>
136+
<internalFileTemplate name="Magento Php Preference Class"/>
133137
</extensions>
134138

135139
<application-components>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<preference for="${FOR}" type="${TYPE}" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!--
2+
/*
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html>
8+
<body>
9+
</body>
10+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
#if (${NAMESPACE})
4+
5+
namespace ${NAMESPACE};
6+
#end
7+
#if (${USE})
8+
9+
use ${USE};
10+
#end
11+
12+
class ${NAME} #if (${EXTENDS})extends ${EXTENDS}#end {
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.actions.generation;
6+
7+
import com.intellij.openapi.actionSystem.AnActionEvent;
8+
import com.intellij.openapi.actionSystem.PlatformDataKeys;
9+
import com.intellij.openapi.project.DumbAwareAction;
10+
import com.intellij.openapi.project.Project;
11+
import com.intellij.openapi.util.Pair;
12+
import com.intellij.psi.PsiFile;
13+
import com.jetbrains.php.lang.psi.PhpFile;
14+
import com.jetbrains.php.lang.psi.elements.PhpClass;
15+
import com.magento.idea.magento2plugin.MagentoIcons;
16+
import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideClassByAPreferenceDialog;
17+
import com.magento.idea.magento2plugin.project.Settings;
18+
import com.magento.idea.magento2plugin.util.GetFirstClassOfFile;
19+
import org.jetbrains.annotations.NotNull;
20+
21+
public class OverrideClassByAPreferenceAction extends DumbAwareAction {
22+
public static String ACTION_NAME = "Override Class By A Preference...";
23+
public static String ACTION_DESCRIPTION = "Create a new Magento 2 preference for the class";
24+
private final GetFirstClassOfFile getFirstClassOfFile;
25+
private PhpClass targetClass;
26+
27+
public OverrideClassByAPreferenceAction() {
28+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
29+
this.getFirstClassOfFile = GetFirstClassOfFile.getInstance();
30+
}
31+
32+
public void update(AnActionEvent event) {
33+
targetClass = null;
34+
Project project = event.getData(PlatformDataKeys.PROJECT);
35+
if (Settings.isEnabled(project)) {
36+
Pair<PsiFile, PhpClass> pair = this.findPhpClass(event);
37+
PsiFile psiFile = pair.getFirst();
38+
PhpClass phpClass = pair.getSecond();
39+
targetClass = phpClass;
40+
if (!(psiFile instanceof PhpFile) && phpClass != null) {
41+
this.setStatus(event, false);
42+
return;
43+
}
44+
} else {
45+
this.setStatus(event, false);
46+
return;
47+
}
48+
this.setStatus(event, true);
49+
}
50+
51+
private void setStatus(AnActionEvent event, boolean status) {
52+
event.getPresentation().setVisible(status);
53+
event.getPresentation().setEnabled(status);
54+
}
55+
56+
@Override
57+
public void actionPerformed(@NotNull AnActionEvent e) {
58+
OverrideClassByAPreferenceDialog.open(e.getProject(), this.targetClass);
59+
}
60+
61+
@Override
62+
public boolean isDumbAware() {
63+
return false;
64+
}
65+
66+
private Pair<PsiFile, PhpClass> findPhpClass(@NotNull AnActionEvent event) {
67+
PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE);
68+
69+
PhpClass phpClass = null;
70+
if (psiFile instanceof PhpFile) {
71+
phpClass = getFirstClassOfFile.execute((PhpFile) psiFile);
72+
}
73+
74+
return Pair.create(psiFile, phpClass);
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.actions.generation.data;
6+
7+
import com.jetbrains.php.lang.psi.elements.PhpClass;
8+
9+
public class PreferenceDiXmFileData {
10+
private String preferenceModule;
11+
private PhpClass targetClass;
12+
private String preferenceFqn;
13+
private String namespace;
14+
private String area;
15+
16+
public PreferenceDiXmFileData(
17+
String preferenceModule,
18+
PhpClass targetClass,
19+
String preferenceFqn,
20+
String namespace,
21+
String area
22+
) {
23+
this.preferenceModule = preferenceModule;
24+
this.targetClass = targetClass;
25+
this.preferenceFqn = preferenceFqn;
26+
this.namespace = namespace;
27+
this.area = area;
28+
}
29+
30+
public String getPreferenceModule() {
31+
return preferenceModule;
32+
}
33+
34+
public PhpClass getTargetClass() {
35+
return targetClass;
36+
}
37+
38+
public String getPreferenceFqn() {
39+
return preferenceFqn;
40+
}
41+
42+
public String getNamespace() {
43+
return namespace;
44+
}
45+
46+
public String getArea() {
47+
return area;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.actions.generation.data;
6+
7+
import com.jetbrains.php.lang.psi.elements.PhpClass;
8+
9+
public class PreferenceFileData {
10+
private String preferenceDirectory;
11+
private String preferenceClassName;
12+
private String preferenceModule;
13+
private PhpClass targetClass;
14+
private String preferenceFqn;
15+
private String namespace;
16+
private boolean inheritClass;
17+
18+
public PreferenceFileData(
19+
String preferenceDirectory,
20+
String preferenceClassName,
21+
String preferenceModule,
22+
PhpClass targetClass,
23+
String preferenceFqn,
24+
String namespace,
25+
boolean inheritClass
26+
) {
27+
this.preferenceDirectory = preferenceDirectory;
28+
this.preferenceClassName = preferenceClassName;
29+
this.preferenceModule = preferenceModule;
30+
this.targetClass = targetClass;
31+
this.preferenceFqn = preferenceFqn;
32+
this.namespace = namespace;
33+
this.inheritClass = inheritClass;
34+
}
35+
36+
public String getPreferenceClassName() {
37+
return preferenceClassName;
38+
}
39+
40+
public String getPreferenceDirectory() {
41+
return preferenceDirectory;
42+
}
43+
44+
public String getPreferenceModule() {
45+
return preferenceModule;
46+
}
47+
48+
public PhpClass getTargetClass() {
49+
return targetClass;
50+
}
51+
52+
public String getPreferenceFqn() {
53+
return preferenceFqn;
54+
}
55+
56+
public String getNamespace() {
57+
return namespace;
58+
}
59+
60+
public boolean isInheritClass() {
61+
return inheritClass;
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.actions.generation.dialog;
6+
7+
import javax.swing.*;
8+
import java.awt.*;
9+
10+
public abstract class AbstractDialog extends JDialog {
11+
protected void pushToMiddle() {
12+
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
13+
this.setLocation(dim.width / 2 -this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2);
14+
}
15+
16+
protected void onCancel() {
17+
this.setVisible(false);
18+
}
19+
}

src/com/magento/idea/magento2plugin/actions/generation/dialog/CreateAPluginDialog.java

+1-10
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import java.io.File;
2525
import java.util.List;
2626

27-
public class CreateAPluginDialog extends JDialog {
27+
public class CreateAPluginDialog extends AbstractDialog {
2828
@NotNull
2929
private final Project project;
3030
private Method targetMethod;
@@ -100,11 +100,6 @@ private void fillTargetAreaOptions() {
100100
}
101101
}
102102

103-
private void pushToMiddle() {
104-
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
105-
this.setLocation(dim.width / 2 -this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2);
106-
}
107-
108103
private void onOK() {
109104
if (!validator.validate(project)) {
110105
return;
@@ -160,10 +155,6 @@ public String getPluginModule() {
160155
return this.pluginModule.getSelectedItem().toString();
161156
}
162157

163-
private void onCancel() {
164-
this.setVisible(false);
165-
}
166-
167158
public static void open(@NotNull Project project, Method targetMethod, PhpClass targetClass) {
168159
CreateAPluginDialog dialog = new CreateAPluginDialog(project, targetMethod, targetClass);
169160
dialog.pack();

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewMagentoModuleDialog.java

+1-10
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.awt.*;
2929
import java.awt.event.*;
3030

31-
public class NewMagentoModuleDialog extends JDialog {
31+
public class NewMagentoModuleDialog extends AbstractDialog {
3232
@NotNull
3333
private final Project project;
3434
@NotNull
@@ -109,11 +109,6 @@ private void detectPackageName(@NotNull PsiDirectory initialBaseDir) {
109109
}
110110
}
111111

112-
private void pushToMiddle() {
113-
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
114-
this.setLocation(dim.width / 2 -this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2);
115-
}
116-
117112
private void onOK() {
118113
if (!validator.validate()) {
119114
return;
@@ -185,10 +180,6 @@ public String getModuleVersion() {
185180
return this.moduleVersion.getText().trim();
186181
}
187182

188-
private void onCancel() {
189-
this.setVisible(false);
190-
}
191-
192183
public static void open(@NotNull Project project, @NotNull PsiDirectory initialBaseDir, @Nullable PsiFile file, @Nullable IdeView view, @Nullable Editor editor) {
193184
NewMagentoModuleDialog dialog = new NewMagentoModuleDialog(project, initialBaseDir, file, view, editor);
194185
dialog.pack();

0 commit comments

Comments
 (0)