Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added override class by a preference action #67

Merged
merged 1 commit into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* Create a New Magento 2 Module action
* Code Inspection: Duplicated Observer Usage in events XML
* Create a Plugin class for a class public method action
* Code Inspection: Warning regarding Cacheable false attribute in default XML
* Code Inspection: Warning regarding Cacheable false attribute in default XML
* Create a Preference for a class action

0.3.0
=============
Expand Down
4 changes: 4 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<action id="MagentoCreateAPlugin.Menu" class="com.magento.idea.magento2plugin.actions.generation.CreateAPluginAction">
<add-to-group group-id="EditorPopupMenu"/>
</action>
<action id="OverrideClassByAPreference.Menu" class="com.magento.idea.magento2plugin.actions.generation.OverrideClassByAPreferenceAction">
<add-to-group group-id="EditorPopupMenu"/>
</action>
</actions>

<extensions defaultExtensionNs="com.intellij">
Expand Down Expand Up @@ -130,6 +133,7 @@
<internalFileTemplate name="Magento Module Registration Php"/>
<internalFileTemplate name="Magento Module Xml"/>
<internalFileTemplate name="Magento Module DI Xml"/>
<internalFileTemplate name="Magento Php Preference Class"/>
</extensions>

<application-components>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<preference for="${FOR}" type="${TYPE}" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!--
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<html>
<body>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
#parse("PHP File Header.php")
#if (${NAMESPACE})

namespace ${NAMESPACE};
#end
#if (${USE})

use ${USE};
#end

class ${NAME} #if (${EXTENDS})extends ${EXTENDS}#end {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.actions.generation;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiFile;
import com.jetbrains.php.lang.psi.PhpFile;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideClassByAPreferenceDialog;
import com.magento.idea.magento2plugin.project.Settings;
import com.magento.idea.magento2plugin.util.GetFirstClassOfFile;
import org.jetbrains.annotations.NotNull;

public class OverrideClassByAPreferenceAction extends DumbAwareAction {
public static String ACTION_NAME = "Override Class By A Preference...";
public static String ACTION_DESCRIPTION = "Create a new Magento 2 preference for the class";
private final GetFirstClassOfFile getFirstClassOfFile;
private PhpClass targetClass;

public OverrideClassByAPreferenceAction() {
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
this.getFirstClassOfFile = GetFirstClassOfFile.getInstance();
}

public void update(AnActionEvent event) {
targetClass = null;
Project project = event.getData(PlatformDataKeys.PROJECT);
if (Settings.isEnabled(project)) {
Pair<PsiFile, PhpClass> pair = this.findPhpClass(event);
PsiFile psiFile = pair.getFirst();
PhpClass phpClass = pair.getSecond();
targetClass = phpClass;
if (!(psiFile instanceof PhpFile) && phpClass != null) {
this.setStatus(event, false);
return;
}
} else {
this.setStatus(event, false);
return;
}
this.setStatus(event, true);
}

private void setStatus(AnActionEvent event, boolean status) {
event.getPresentation().setVisible(status);
event.getPresentation().setEnabled(status);
}

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
OverrideClassByAPreferenceDialog.open(e.getProject(), this.targetClass);
}

@Override
public boolean isDumbAware() {
return false;
}

private Pair<PsiFile, PhpClass> findPhpClass(@NotNull AnActionEvent event) {
PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE);

PhpClass phpClass = null;
if (psiFile instanceof PhpFile) {
phpClass = getFirstClassOfFile.execute((PhpFile) psiFile);
}

return Pair.create(psiFile, phpClass);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.actions.generation.data;

import com.jetbrains.php.lang.psi.elements.PhpClass;

public class PreferenceDiXmFileData {
private String preferenceModule;
private PhpClass targetClass;
private String preferenceFqn;
private String namespace;
private String area;

public PreferenceDiXmFileData(
String preferenceModule,
PhpClass targetClass,
String preferenceFqn,
String namespace,
String area
) {
this.preferenceModule = preferenceModule;
this.targetClass = targetClass;
this.preferenceFqn = preferenceFqn;
this.namespace = namespace;
this.area = area;
}

public String getPreferenceModule() {
return preferenceModule;
}

public PhpClass getTargetClass() {
return targetClass;
}

public String getPreferenceFqn() {
return preferenceFqn;
}

public String getNamespace() {
return namespace;
}

public String getArea() {
return area;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.actions.generation.data;

import com.jetbrains.php.lang.psi.elements.PhpClass;

public class PreferenceFileData {
private String preferenceDirectory;
private String preferenceClassName;
private String preferenceModule;
private PhpClass targetClass;
private String preferenceFqn;
private String namespace;
private boolean inheritClass;

public PreferenceFileData(
String preferenceDirectory,
String preferenceClassName,
String preferenceModule,
PhpClass targetClass,
String preferenceFqn,
String namespace,
boolean inheritClass
) {
this.preferenceDirectory = preferenceDirectory;
this.preferenceClassName = preferenceClassName;
this.preferenceModule = preferenceModule;
this.targetClass = targetClass;
this.preferenceFqn = preferenceFqn;
this.namespace = namespace;
this.inheritClass = inheritClass;
}

public String getPreferenceClassName() {
return preferenceClassName;
}

public String getPreferenceDirectory() {
return preferenceDirectory;
}

public String getPreferenceModule() {
return preferenceModule;
}

public PhpClass getTargetClass() {
return targetClass;
}

public String getPreferenceFqn() {
return preferenceFqn;
}

public String getNamespace() {
return namespace;
}

public boolean isInheritClass() {
return inheritClass;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.actions.generation.dialog;

import javax.swing.*;
import java.awt.*;

public abstract class AbstractDialog extends JDialog {
protected void pushToMiddle() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width / 2 -this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2);
}

protected void onCancel() {
this.setVisible(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.io.File;
import java.util.List;

public class CreateAPluginDialog extends JDialog {
public class CreateAPluginDialog extends AbstractDialog {
@NotNull
private final Project project;
private Method targetMethod;
Expand Down Expand Up @@ -100,11 +100,6 @@ private void fillTargetAreaOptions() {
}
}

private void pushToMiddle() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width / 2 -this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2);
}

private void onOK() {
if (!validator.validate(project)) {
return;
Expand Down Expand Up @@ -160,10 +155,6 @@ public String getPluginModule() {
return this.pluginModule.getSelectedItem().toString();
}

private void onCancel() {
this.setVisible(false);
}

public static void open(@NotNull Project project, Method targetMethod, PhpClass targetClass) {
CreateAPluginDialog dialog = new CreateAPluginDialog(project, targetMethod, targetClass);
dialog.pack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.awt.*;
import java.awt.event.*;

public class NewMagentoModuleDialog extends JDialog {
public class NewMagentoModuleDialog extends AbstractDialog {
@NotNull
private final Project project;
@NotNull
Expand Down Expand Up @@ -109,11 +109,6 @@ private void detectPackageName(@NotNull PsiDirectory initialBaseDir) {
}
}

private void pushToMiddle() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width / 2 -this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2);
}

private void onOK() {
if (!validator.validate()) {
return;
Expand Down Expand Up @@ -185,10 +180,6 @@ public String getModuleVersion() {
return this.moduleVersion.getText().trim();
}

private void onCancel() {
this.setVisible(false);
}

public static void open(@NotNull Project project, @NotNull PsiDirectory initialBaseDir, @Nullable PsiFile file, @Nullable IdeView view, @Nullable Editor editor) {
NewMagentoModuleDialog dialog = new NewMagentoModuleDialog(project, initialBaseDir, file, view, editor);
dialog.pack();
Expand Down
Loading