Skip to content

864: enhanced a new block dialog UX #865

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jetbrains.annotations.NotNull;

public class NewBlockAction extends AnAction {

public static final String ACTION_NAME = "Magento 2 Block";
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Block";

Expand All @@ -26,23 +27,24 @@ public NewBlockAction() {
}

@Override
public void actionPerformed(@NotNull final AnActionEvent event) {
public void actionPerformed(final @NotNull AnActionEvent event) {
final DataContext dataContext = event.getDataContext();
final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);

if (view == null) {
return;
}

final Project project = CommonDataKeys.PROJECT.getData(dataContext);

if (project == null) {
return;
}

final PsiDirectory directory = view.getOrChooseDirectory();

if (directory == null) {
return;
}

NewBlockDialog.open(project, directory);
}

Expand All @@ -51,4 +53,3 @@ public boolean isDumbAware() {
return false;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
</grid>
</children>
</grid>
<grid id="e3588" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="e3588" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<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"/>
Expand Down Expand Up @@ -85,7 +85,7 @@
</component>
<component id="fdc52" class="javax.swing.JTextField" binding="blockParentDir">
<constraints>
<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">
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
Expand All @@ -96,7 +96,7 @@
</component>
<component id="be25f" class="javax.swing.JLabel">
<constraints>
<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"/>
<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"/>
</constraints>
<properties>
<labelFor value="fdc52"/>
Expand All @@ -106,6 +106,22 @@
<html.disable class="java.lang.Boolean" value="true"/>
</clientProperties>
</component>
<component id="48e8" class="javax.swing.JLabel" binding="blockNameErrorMessage">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="340c3" class="javax.swing.JLabel" binding="blockParentDirErrorMessage">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
<grid id="ddd4f" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.magento.idea.magento2plugin.actions.generation.NewBlockAction;
import com.magento.idea.magento2plugin.actions.generation.data.BlockFileData;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
Expand All @@ -26,13 +25,15 @@
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;

public class NewBlockDialog extends AbstractDialog {

private final PsiDirectory baseDir;
private final String moduleName;
private JPanel contentPanel;
Expand All @@ -44,18 +45,18 @@ public class NewBlockDialog extends AbstractDialog {
private static final String NAME = "name";
private static final String DIRECTORY = "directory";

@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, NAME})
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
message = {PhpClassRule.MESSAGE, NAME})
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, NAME})
@FieldValidation(rule = RuleRegistry.PHP_CLASS, message = {PhpClassRule.MESSAGE, NAME})
private JTextField blockName;

@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, DIRECTORY})
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, DIRECTORY})
@FieldValidation(rule = RuleRegistry.PHP_DIRECTORY,
message = {PhpDirectoryRule.MESSAGE, DIRECTORY})
private JTextField blockParentDir;

private JLabel blockNameErrorMessage;//NOPMD
private JLabel blockParentDirErrorMessage;//NOPMD

/**
* Constructor.
*
Expand Down Expand Up @@ -113,12 +114,12 @@ public static void open(final Project project, final PsiDirectory directory) {
protected void onOK() {
if (validateFormFields()) {
generateFile();
exit();
}
exit();
}

private PsiFile generateFile() {
return new ModuleBlockClassGenerator(new BlockFileData(
private void generateFile() {
new ModuleBlockClassGenerator(new BlockFileData(
getBlockDirectory(),
getBlockName(),
getModuleName(),
Expand All @@ -140,13 +141,15 @@ public String getBlockDirectory() {

private void suggestBlockDirectory() {
final String moduleIdentifierPath = getModuleIdentifierPath();

if (moduleIdentifierPath == null) {
blockParentDir.setText(BlockPhp.DEFAULT_DIR);
return;
}
final String path = baseDir.getVirtualFile().getPath();
final String[] pathParts = path.split(moduleIdentifierPath);
final int minimumPathParts = 2;

if (pathParts.length != minimumPathParts) {
blockParentDir.setText(BlockPhp.DEFAULT_DIR);
return;
Expand All @@ -161,27 +164,25 @@ private void suggestBlockDirectory() {

private String getModuleIdentifierPath() {
final String[]parts = moduleName.split(Package.vendorModuleNameSeparator);

if (parts[0] == null || parts[1] == null || parts.length > 2) {
return null;
}

return parts[0] + File.separator + parts[1];
}

private String getNamespace() {
final String[]parts = moduleName.split(Package.vendorModuleNameSeparator);

if (parts[0] == null || parts[1] == null || parts.length > 2) {
return null;
}
final String directoryPart = getBlockDirectory().replace(
File.separator,
Package.fqnSeparator
);
return parts[0] + Package.fqnSeparator + parts[1] + Package.fqnSeparator + directoryPart;
}

@Override
public void onCancel() {
// add your code here if necessary
dispose();
return parts[0] + Package.fqnSeparator + parts[1] + Package.fqnSeparator + directoryPart;
}
}