Skip to content

Commit 9518c86

Browse files
author
Vitaliy Boyko
committed
Added code generation
1 parent a4d27e4 commit 9518c86

File tree

7 files changed

+157
-92
lines changed

7 files changed

+157
-92
lines changed

src/com/magento/idea/magento2plugin/actions/generation/data/BlockFileData.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,22 @@
44
*/
55
package com.magento.idea.magento2plugin.actions.generation.data;
66

7-
import com.jetbrains.php.lang.psi.elements.PhpClass;
8-
97
public class BlockFileData {
108
private String blockDirectory;
119
private String blockClassName;
1210
private String blockModule;
13-
private PhpClass targetClass;
14-
private String blockFqn;
1511
private String namespace;
16-
private boolean inheritClass;
1712

1813
public BlockFileData(
1914
String blockDirectory,
2015
String blockClassName,
2116
String blockModule,
22-
PhpClass targetClass,
23-
String blockFqn,
24-
String namespace,
25-
boolean inheritClass
17+
String namespace
2618
) {
2719
this.blockDirectory = blockDirectory;
2820
this.blockClassName = blockClassName;
2921
this.blockModule = blockModule;
30-
this.targetClass = targetClass;
31-
this.blockFqn = blockFqn;
3222
this.namespace = namespace;
33-
this.inheritClass = inheritClass;
3423
}
3524

3625
public String getBlockClassName() {
@@ -45,14 +34,6 @@ public String getBlockModule() {
4534
return blockModule;
4635
}
4736

48-
public PhpClass getTargetClass() {
49-
return targetClass;
50-
}
51-
52-
public String getBlockFqn() {
53-
return blockFqn;
54-
}
55-
5637
public String getNamespace() {
5738
return namespace;
5839
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import javax.swing.*;
88
import java.awt.*;
99

10+
/**
11+
* All code generate dialog should extend this class
12+
*/
1013
public abstract class AbstractDialog extends JDialog {
1114
protected void pushToMiddle() {
1215
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

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

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.NewBlockDialog">
3-
<grid id="cbd77" binding="contentPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<grid id="cbd77" binding="contentPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="10" left="10" bottom="10" right="10"/>
55
<constraints>
66
<xy x="48" y="54" width="419" height="297"/>
77
</constraints>
88
<properties>
99
<name value="New Magento2 Block.."/>
10+
<preferredSize width="420" height="195"/>
1011
<toolTipText value="New Magento2 Block.."/>
1112
</properties>
1213
<border type="none"/>
1314
<children>
1415
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
1516
<margin top="0" left="0" bottom="0" right="0"/>
1617
<constraints>
17-
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
18+
<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"/>
1819
</constraints>
1920
<properties/>
2021
<border type="none"/>
@@ -52,7 +53,7 @@
5253
</grid>
5354
</children>
5455
</grid>
55-
<grid id="e3588" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
56+
<grid id="e3588" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
5657
<margin top="0" left="0" bottom="0" right="0"/>
5758
<constraints>
5859
<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"/>
@@ -73,56 +74,70 @@
7374
</component>
7475
<component id="2d9cc" class="javax.swing.JTextField" binding="blockName">
7576
<constraints>
76-
<grid row="0" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
77+
<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">
7778
<preferred-size width="150" height="-1"/>
7879
</grid>
7980
</constraints>
8081
<properties/>
81-
</component>
82-
<component id="e6153" class="javax.swing.JRadioButton" binding="frontendRadioButton" default-binding="true">
83-
<constraints>
84-
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
85-
</constraints>
86-
<properties>
87-
<selected value="true"/>
88-
<text value="Frontend"/>
89-
</properties>
90-
</component>
91-
<component id="3fb02" class="javax.swing.JRadioButton" binding="adminhtmlRadioButton" default-binding="true">
92-
<constraints>
93-
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
94-
</constraints>
95-
<properties>
96-
<text value="Adminhtml"/>
97-
</properties>
82+
<clientProperties>
83+
<html.disable class="java.lang.Boolean" value="true"/>
84+
</clientProperties>
9885
</component>
9986
<component id="fdc52" class="javax.swing.JTextField" binding="blockParentDir">
10087
<constraints>
101-
<grid row="2" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
88+
<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">
10289
<preferred-size width="150" height="-1"/>
10390
</grid>
10491
</constraints>
10592
<properties/>
93+
<clientProperties>
94+
<html.disable class="java.lang.Boolean" value="true"/>
95+
</clientProperties>
10696
</component>
10797
<component id="be25f" class="javax.swing.JLabel">
10898
<constraints>
109-
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
99+
<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"/>
110100
</constraints>
111101
<properties>
112102
<labelFor value="fdc52"/>
113103
<text value="Block Directory"/>
114104
</properties>
105+
<clientProperties>
106+
<html.disable class="java.lang.Boolean" value="true"/>
107+
</clientProperties>
115108
</component>
116-
<component id="e81ed" class="javax.swing.JLabel">
109+
</children>
110+
</grid>
111+
<grid id="ddd4f" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
112+
<margin top="0" left="0" bottom="0" right="0"/>
113+
<constraints>
114+
<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"/>
115+
</constraints>
116+
<properties/>
117+
<border type="none"/>
118+
<children>
119+
<component id="1c5cd" class="javax.swing.JTextPane" binding="warning">
117120
<constraints>
118-
<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">
119-
<preferred-size width="113" height="16"/>
121+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
122+
<preferred-size width="150" height="50"/>
120123
</grid>
121124
</constraints>
122125
<properties>
123-
<text value="Block Area"/>
126+
<editable value="false"/>
127+
<enabled value="false"/>
128+
<margin top="0" left="0" bottom="0" right="0"/>
129+
<opaque value="false"/>
130+
<text value="Please consider creating a view model instead of a block if possible for your use case."/>
124131
</properties>
132+
<clientProperties>
133+
<html.disable class="java.lang.Boolean" value="false"/>
134+
</clientProperties>
125135
</component>
136+
<vspacer id="4220f">
137+
<constraints>
138+
<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"/>
139+
</constraints>
140+
</vspacer>
126141
</children>
127142
</grid>
128143
</children>

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

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,45 @@
66

77
import com.intellij.openapi.project.Project;
88
import com.intellij.psi.PsiDirectory;
9-
import com.jetbrains.php.lang.psi.PhpFile;
10-
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
11-
import com.magento.idea.magento2plugin.ui.FilteredComboBox;
12-
9+
import com.intellij.psi.PsiFile;
10+
import com.magento.idea.magento2plugin.actions.generation.NewBlockAction;
11+
import com.magento.idea.magento2plugin.actions.generation.data.BlockFileData;
12+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.NewBlockValidator;
13+
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleBlockClassGenerator;
14+
import com.magento.idea.magento2plugin.magento.files.BlockPhp;
15+
import com.magento.idea.magento2plugin.magento.packages.Package;
16+
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectory;
1317
import javax.swing.*;
14-
import java.awt.*;
1518
import java.awt.event.*;
19+
import java.io.File;
1620

1721
public class NewBlockDialog extends AbstractDialog {
22+
private final NewBlockValidator validator;
23+
private final PsiDirectory baseDir;
24+
private final GetModuleNameByDirectory getModuleNameByDir;
25+
private final String moduleName;
1826
private JPanel contentPanel;
1927
private JButton buttonOK;
2028
private JButton buttonCancel;
2129
private JTextField blockName;
22-
private JRadioButton frontendRadioButton;
2330
private JRadioButton adminhtmlRadioButton;
2431
private JTextField blockParentDir;
32+
private JTextPane warning;
2533
private Project project;
2634

27-
public NewBlockDialog(Project project) {
35+
public NewBlockDialog(Project project, PsiDirectory directory) {
2836
this.project = project;
37+
this.baseDir = directory;
38+
this.moduleName = GetModuleNameByDirectory.getInstance(project).execute(directory);
39+
this.validator = NewBlockValidator.getInstance(this);
40+
this.getModuleNameByDir = GetModuleNameByDirectory.getInstance(project);
2941

3042
setContentPane(contentPanel);
3143
setModal(true);
3244
setTitle("Create a new Magento 2 block..");
3345
getRootPane().setDefaultButton(buttonOK);
3446
pushToMiddle();
47+
suggestBlockDirectory();
3548

3649
buttonOK.addActionListener(new ActionListener() {
3750
public void actionPerformed(ActionEvent e) {
@@ -62,7 +75,7 @@ public void actionPerformed(ActionEvent e) {
6275
}
6376

6477
public static void open(Project project, PsiDirectory directory) {
65-
NewBlockDialog dialog = new NewBlockDialog(project);
78+
NewBlockDialog dialog = new NewBlockDialog(project, directory);
6679
dialog.pack();
6780
dialog.setVisible(true);
6881
}
@@ -71,10 +84,68 @@ private void onOK() {
7184
if (!validator.validate()) {
7285
return;
7386
}
74-
generateFiles();
87+
generateFile();
7588
this.setVisible(false);
7689
}
7790

91+
private PsiFile generateFile() {
92+
return new ModuleBlockClassGenerator(new BlockFileData(
93+
getBlockDirectory(),
94+
getBlockName(),
95+
getModuleName(),
96+
getNamespace()
97+
), project).generate(NewBlockAction.ACTION_NAME, true);
98+
}
99+
100+
private String getModuleName() {
101+
return moduleName;
102+
}
103+
104+
public String getBlockName() {
105+
return blockName.getText().trim();
106+
}
107+
108+
public String getBlockDirectory() {
109+
return blockParentDir.getText().trim();
110+
}
111+
112+
private void suggestBlockDirectory() {
113+
String path = baseDir.getVirtualFile().getPath();
114+
String moduleIdentifierPath = getModuleIdentifierPath();
115+
if (moduleIdentifierPath == null) {
116+
blockParentDir.setText(BlockPhp.DEFAULT_DIR);
117+
return;
118+
}
119+
String[] pathParts = path.split(moduleIdentifierPath);
120+
if (pathParts.length != 2) {
121+
blockParentDir.setText(BlockPhp.DEFAULT_DIR);
122+
return;
123+
}
124+
125+
if (pathParts[1] != null) {
126+
blockParentDir.setText(pathParts[1].substring(1));
127+
return;
128+
}
129+
blockParentDir.setText(BlockPhp.DEFAULT_DIR);
130+
}
131+
132+
private String getModuleIdentifierPath() {
133+
String[]parts = moduleName.split(Package.VENDOR_MODULE_NAME_SEPARATOR);
134+
if (parts[0] == null || parts[1] == null || parts.length > 2) {
135+
return null;
136+
}
137+
return parts[0] + File.separator + parts[1];
138+
}
139+
140+
private String getNamespace() {
141+
String[]parts = moduleName.split(Package.VENDOR_MODULE_NAME_SEPARATOR);
142+
if (parts[0] == null || parts[1] == null || parts.length > 2) {
143+
return null;
144+
}
145+
String directoryPart = getBlockDirectory().replace(File.separator, Package.FQN_SEPARATOR);
146+
return parts[0] + Package.FQN_SEPARATOR + parts[1] + directoryPart;
147+
}
148+
78149
public void onCancel() {
79150
// add your code here if necessary
80151
dispose();

src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/NewBlockValidator.java

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
*/
55
package com.magento.idea.magento2plugin.actions.generation.dialog.validator;
66

7-
import com.magento.idea.magento2plugin.actions.generation.dialog.NewModuleDialog;
7+
import com.magento.idea.magento2plugin.actions.generation.dialog.NewBlockDialog;
88
import com.magento.idea.magento2plugin.util.Regex;
9-
109
import javax.swing.*;
1110

1211
public class NewBlockValidator {
1312
private static NewBlockValidator INSTANCE = null;
14-
private NewModuleDialog dialog;
13+
private NewBlockDialog dialog;
1514

16-
public static NewBlockValidator getInstance(NewModuleDialog dialog) {
15+
public static NewBlockValidator getInstance(NewBlockDialog dialog) {
1716
if (null == INSTANCE) {
1817
INSTANCE = new NewBlockValidator();
1918
}
@@ -24,45 +23,31 @@ public static NewBlockValidator getInstance(NewModuleDialog dialog) {
2423
public boolean validate()
2524
{
2625
String errorTitle = "Error";
27-
String packageName = dialog.getPackageName();
28-
if (packageName.length() == 0) {
29-
JOptionPane.showMessageDialog(null, "Package Name must not be empty.", errorTitle, JOptionPane.ERROR_MESSAGE);
30-
return false;
31-
}
32-
33-
if (!packageName.matches(Regex.ALPHANUMERIC)) {
34-
JOptionPane.showMessageDialog(null, "Package Name must contain letters and numbers only.", errorTitle, JOptionPane.ERROR_MESSAGE);
35-
return false;
36-
}
37-
38-
if (!Character.isUpperCase(packageName.charAt(0)) && !Character.isDigit(packageName.charAt(0))) {
39-
JOptionPane.showMessageDialog(null, "Package Name must start from a number or a capital letter", errorTitle, JOptionPane.ERROR_MESSAGE);
40-
return false;
41-
}
4226

43-
String moduleName = dialog.getModuleName();
27+
String moduleName = dialog.getBlockName();
4428
if (moduleName.length() == 0) {
45-
JOptionPane.showMessageDialog(null, "Module Name must not be empty.", errorTitle, JOptionPane.ERROR_MESSAGE);
29+
JOptionPane.showMessageDialog(null, "Block Name must not be empty.", errorTitle, JOptionPane.ERROR_MESSAGE);
4630
return false;
4731
}
4832

4933
if (!moduleName.matches(Regex.ALPHANUMERIC)) {
50-
JOptionPane.showMessageDialog(null, "Module Name must contain letters and numbers only.", errorTitle, JOptionPane.ERROR_MESSAGE);
34+
JOptionPane.showMessageDialog(null, "Block Name must contain letters and numbers only.", errorTitle, JOptionPane.ERROR_MESSAGE);
5135
return false;
5236
}
5337

5438
if (!Character.isUpperCase(moduleName.charAt(0)) && !Character.isDigit(moduleName.charAt(0))) {
55-
JOptionPane.showMessageDialog(null, "Module Name must start from a number or a capital letter", errorTitle, JOptionPane.ERROR_MESSAGE);
39+
JOptionPane.showMessageDialog(null, "Block Name must start from a number or a capital letter", errorTitle, JOptionPane.ERROR_MESSAGE);
5640
return false;
5741
}
5842

59-
if (dialog.getModuleVersion().length() == 0) {
60-
JOptionPane.showMessageDialog(null, "Module Version must not be empty.", errorTitle, JOptionPane.ERROR_MESSAGE);
43+
String pluginDirectory = dialog.getBlockDirectory();
44+
if (pluginDirectory.length() == 0) {
45+
JOptionPane.showMessageDialog(null, "Block Directory must not be empty.", errorTitle, JOptionPane.ERROR_MESSAGE);
6146
return false;
6247
}
6348

64-
if (dialog.getModuleDescription().length() == 0) {
65-
JOptionPane.showMessageDialog(null, "Module Version must not be empty.", errorTitle, JOptionPane.ERROR_MESSAGE);
49+
if (!pluginDirectory.matches(Regex.DIRECTORY)) {
50+
JOptionPane.showMessageDialog(null, "Block Directory is not valid.", errorTitle, JOptionPane.ERROR_MESSAGE);
6651
return false;
6752
}
6853

0 commit comments

Comments
 (0)