Skip to content

Commit ae4645b

Browse files
author
Vitaliy
authored
Merge pull request #111 from vasilii-b/code-generation-view-model
Code generation view model
2 parents 46451ef + 78ebc5a commit ae4645b

File tree

9 files changed

+559
-4
lines changed

9 files changed

+559
-4
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<!-- Module file generators -->
5858
<group id="MagentoNewModuleFileGroup" class="com.magento.idea.magento2plugin.actions.groups.NewModuleFileGroup" text="Module File" popup="true">
5959
<action id="MagentoCreateABlock" class="com.magento.idea.magento2plugin.actions.generation.NewBlockAction" />
60+
<action id="MagentoCreateAViewModel" class="com.magento.idea.magento2plugin.actions.generation.NewViewModelAction" />
6061
<add-to-group group-id="NewGroup" anchor="last"/>
6162
</group>
6263

Lines changed: 49 additions & 0 deletions
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;
6+
7+
import com.intellij.ide.IdeView;
8+
import com.intellij.openapi.actionSystem.*;
9+
import com.intellij.openapi.project.Project;
10+
import com.intellij.psi.PsiDirectory;
11+
import com.magento.idea.magento2plugin.MagentoIcons;
12+
import com.magento.idea.magento2plugin.actions.generation.dialog.NewViewModelDialog;
13+
import org.jetbrains.annotations.NotNull;
14+
15+
public class NewViewModelAction extends AnAction {
16+
public static String ACTION_NAME = "Magento 2 View Model";
17+
public static String ACTION_DESCRIPTION = "Create a new Magento 2 View Model";
18+
19+
NewViewModelAction() {
20+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
21+
}
22+
23+
@Override
24+
public void actionPerformed(@NotNull AnActionEvent e) {
25+
DataContext dataContext = e.getDataContext();
26+
IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
27+
if (view == null) {
28+
return;
29+
}
30+
31+
Project project = CommonDataKeys.PROJECT.getData(dataContext);
32+
if (project == null) {
33+
return;
34+
}
35+
36+
PsiDirectory directory = view.getOrChooseDirectory();
37+
if (directory == null) {
38+
return;
39+
}
40+
41+
NewViewModelDialog.open(project, directory);
42+
}
43+
44+
@Override
45+
public boolean isDumbAware() {
46+
return false;
47+
}
48+
}
49+

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ public void update(AnActionEvent event) {
3737
PsiFile psiFile = pair.getFirst();
3838
PhpClass phpClass = pair.getSecond();
3939
targetClass = phpClass;
40-
if (!(psiFile instanceof PhpFile) && phpClass != null) {
40+
if (psiFile instanceof PhpFile && phpClass != null) {
41+
this.setStatus(event, true);
42+
} else {
4143
this.setStatus(event, false);
42-
return;
4344
}
4445
} else {
4546
this.setStatus(event, false);
46-
return;
4747
}
48-
this.setStatus(event, true);
4948
}
5049

5150
private void setStatus(AnActionEvent event, boolean status) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
public class ViewModelFileData {
8+
private String viewModelDirectory;
9+
private String viewModelClassName;
10+
private String viewModelModule;
11+
private String namespace;
12+
13+
public ViewModelFileData(
14+
String viewModelDirectory,
15+
String viewModelClassName,
16+
String viewModelModule,
17+
String namespace
18+
) {
19+
this.viewModelDirectory = viewModelDirectory;
20+
this.viewModelClassName = viewModelClassName;
21+
this.viewModelModule = viewModelModule;
22+
this.namespace = namespace;
23+
}
24+
25+
public String getViewModelClassName() {
26+
return viewModelClassName;
27+
}
28+
29+
public String getViewModelDirectory() {
30+
return viewModelDirectory;
31+
}
32+
33+
public String getViewModelModule() {
34+
return viewModelModule;
35+
}
36+
37+
public String getNamespace() {
38+
return namespace;
39+
}
40+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.NewViewModelDialog">
3+
<grid id="1871d" binding="contentPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
4+
<margin top="10" left="10" bottom="10" right="10"/>
5+
<constraints>
6+
<xy x="70" y="43" width="419" height="356"/>
7+
</constraints>
8+
<properties>
9+
<opaque value="true"/>
10+
<preferredSize width="420" height="120"/>
11+
<requestFocusEnabled value="true"/>
12+
</properties>
13+
<border type="none"/>
14+
<children>
15+
<grid id="90a70" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
16+
<margin top="0" left="0" bottom="0" right="0"/>
17+
<constraints>
18+
<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"/>
19+
</constraints>
20+
<properties/>
21+
<border type="none"/>
22+
<children>
23+
<component id="b0da4" class="javax.swing.JLabel">
24+
<constraints>
25+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
26+
<preferred-size width="113" height="16"/>
27+
</grid>
28+
</constraints>
29+
<properties>
30+
<labelFor value="2d9cc"/>
31+
<text value="View Model Name"/>
32+
</properties>
33+
</component>
34+
<component id="1f03b" class="javax.swing.JTextField" binding="viewModelName">
35+
<constraints>
36+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
37+
<preferred-size width="150" height="-1"/>
38+
</grid>
39+
</constraints>
40+
<properties/>
41+
<clientProperties>
42+
<html.disable class="java.lang.Boolean" value="true"/>
43+
</clientProperties>
44+
</component>
45+
<component id="29a9d" class="javax.swing.JTextField" binding="viewModelParentDir">
46+
<constraints>
47+
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
48+
<preferred-size width="150" height="-1"/>
49+
</grid>
50+
</constraints>
51+
<properties/>
52+
<clientProperties>
53+
<html.disable class="java.lang.Boolean" value="true"/>
54+
</clientProperties>
55+
</component>
56+
<component id="b5004" class="javax.swing.JLabel">
57+
<constraints>
58+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
59+
</constraints>
60+
<properties>
61+
<labelFor value="fdc52"/>
62+
<text value="View Model Directory"/>
63+
</properties>
64+
<clientProperties>
65+
<html.disable class="java.lang.Boolean" value="true"/>
66+
</clientProperties>
67+
</component>
68+
</children>
69+
</grid>
70+
<grid id="b0154" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
71+
<margin top="0" left="0" bottom="0" right="0"/>
72+
<constraints>
73+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
74+
</constraints>
75+
<properties/>
76+
<border type="none"/>
77+
<children>
78+
<vspacer id="28111">
79+
<constraints>
80+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
81+
</constraints>
82+
</vspacer>
83+
</children>
84+
</grid>
85+
<grid id="9ad5" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
86+
<margin top="0" left="0" bottom="0" right="0"/>
87+
<constraints>
88+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
89+
</constraints>
90+
<properties/>
91+
<border type="none"/>
92+
<children>
93+
<hspacer id="4aa6d">
94+
<constraints>
95+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
96+
</constraints>
97+
</hspacer>
98+
<grid id="f892c" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
99+
<margin top="0" left="0" bottom="0" right="0"/>
100+
<constraints>
101+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
102+
</constraints>
103+
<properties/>
104+
<border type="none"/>
105+
<children>
106+
<component id="20801" class="javax.swing.JButton" binding="buttonOK">
107+
<constraints>
108+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
109+
</constraints>
110+
<properties>
111+
<text value="OK"/>
112+
</properties>
113+
</component>
114+
<component id="1b860" class="javax.swing.JButton" binding="buttonCancel">
115+
<constraints>
116+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
117+
</constraints>
118+
<properties>
119+
<text value="Cancel"/>
120+
</properties>
121+
</component>
122+
</children>
123+
</grid>
124+
</children>
125+
</grid>
126+
</children>
127+
</grid>
128+
</form>
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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 com.intellij.openapi.project.Project;
8+
import com.intellij.psi.PsiDirectory;
9+
import com.intellij.psi.PsiFile;
10+
import com.magento.idea.magento2plugin.actions.generation.NewViewModelAction;
11+
import com.magento.idea.magento2plugin.actions.generation.data.ViewModelFileData;
12+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.NewViewModelValidator;
13+
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleViewModelClassGenerator;
14+
import com.magento.idea.magento2plugin.magento.files.ViewModelPhp;
15+
import com.magento.idea.magento2plugin.magento.packages.Package;
16+
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectory;
17+
import javax.swing.*;
18+
import java.awt.event.*;
19+
import java.io.File;
20+
21+
public class NewViewModelDialog extends AbstractDialog {
22+
private final NewViewModelValidator validator;
23+
private final PsiDirectory baseDir;
24+
private final GetModuleNameByDirectory getModuleNameByDir;
25+
private final String moduleName;
26+
private JPanel contentPanel;
27+
private JButton buttonOK;
28+
private JButton buttonCancel;
29+
private JTextField viewModelName;
30+
private JTextField viewModelParentDir;
31+
private Project project;
32+
33+
public NewViewModelDialog(Project project, PsiDirectory directory) {
34+
this.project = project;
35+
this.baseDir = directory;
36+
this.moduleName = GetModuleNameByDirectory.getInstance(project).execute(directory);
37+
this.validator = NewViewModelValidator.getInstance(this);
38+
this.getModuleNameByDir = GetModuleNameByDirectory.getInstance(project);
39+
40+
setContentPane(contentPanel);
41+
setModal(true);
42+
setTitle("Create a new Magento 2 View Model.");
43+
getRootPane().setDefaultButton(buttonOK);
44+
pushToMiddle();
45+
suggestViewModelDirectory();
46+
47+
buttonOK.addActionListener(new ActionListener() {
48+
public void actionPerformed(ActionEvent e) {
49+
onOK();
50+
}
51+
});
52+
53+
buttonCancel.addActionListener(new ActionListener() {
54+
public void actionPerformed(ActionEvent e) {
55+
onCancel();
56+
}
57+
});
58+
59+
// call onCancel() when cross is clicked
60+
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
61+
addWindowListener(new WindowAdapter() {
62+
public void windowClosing(WindowEvent e) {
63+
onCancel();
64+
}
65+
});
66+
67+
// call onCancel() on ESCAPE
68+
contentPanel.registerKeyboardAction(new ActionListener() {
69+
public void actionPerformed(ActionEvent e) {
70+
onCancel();
71+
}
72+
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
73+
}
74+
75+
public static void open(Project project, PsiDirectory directory) {
76+
NewViewModelDialog dialog = new NewViewModelDialog(project, directory);
77+
dialog.pack();
78+
dialog.setVisible(true);
79+
}
80+
81+
private void onOK() {
82+
if (!validator.validate()) {
83+
return;
84+
}
85+
generateFile();
86+
this.setVisible(false);
87+
}
88+
89+
private PsiFile generateFile() {
90+
return new ModuleViewModelClassGenerator(new ViewModelFileData(
91+
getViewModelDirectory(),
92+
getViewModelName(),
93+
getModuleName(),
94+
getNamespace()
95+
), project).generate(NewViewModelAction.ACTION_NAME, true);
96+
}
97+
98+
private String getModuleName() {
99+
return moduleName;
100+
}
101+
102+
public String getViewModelName() {
103+
return viewModelName.getText().trim();
104+
}
105+
106+
public String getViewModelDirectory() {
107+
return viewModelParentDir.getText().trim();
108+
}
109+
110+
private void suggestViewModelDirectory() {
111+
String path = baseDir.getVirtualFile().getPath();
112+
String moduleIdentifierPath = getModuleIdentifierPath();
113+
if (moduleIdentifierPath == null) {
114+
viewModelParentDir.setText(ViewModelPhp.DEFAULT_DIR);
115+
return;
116+
}
117+
String[] pathParts = path.split(moduleIdentifierPath);
118+
if (pathParts.length != 2) {
119+
viewModelParentDir.setText(ViewModelPhp.DEFAULT_DIR);
120+
return;
121+
}
122+
123+
if (pathParts[1] != null) {
124+
viewModelParentDir.setText(pathParts[1].substring(1));
125+
return;
126+
}
127+
viewModelParentDir.setText(ViewModelPhp.DEFAULT_DIR);
128+
}
129+
130+
private String getModuleIdentifierPath() {
131+
String[]parts = moduleName.split(Package.VENDOR_MODULE_NAME_SEPARATOR);
132+
if (parts[0] == null || parts[1] == null || parts.length > 2) {
133+
return null;
134+
}
135+
return parts[0] + File.separator + parts[1];
136+
}
137+
138+
private String getNamespace() {
139+
String[]parts = moduleName.split(Package.VENDOR_MODULE_NAME_SEPARATOR);
140+
if (parts[0] == null || parts[1] == null || parts.length > 2) {
141+
return null;
142+
}
143+
String directoryPart = getViewModelDirectory().replace(File.separator, Package.FQN_SEPARATOR);
144+
return parts[0] + Package.FQN_SEPARATOR + parts[1] + Package.FQN_SEPARATOR + directoryPart;
145+
}
146+
147+
public void onCancel() {
148+
// add your code here if necessary
149+
dispose();
150+
}
151+
}

0 commit comments

Comments
 (0)