Skip to content

Commit 66394a6

Browse files
967: Code refactoring after code review
1 parent 2a0ec5f commit 66394a6

File tree

5 files changed

+58
-22
lines changed

5 files changed

+58
-22
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?xml version="1.0"?>
22
#parse("XML File Header.xml")
3-
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #if (${IS_ADMIN})layout="admin-1column"#end
3+
#if (${IS_ADMIN} == "true")
4+
#set ($isAdmin = true)
5+
#else
6+
#set ($isAdmin = false)
7+
#end
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" #if ($isAdmin)layout="admin-1column"#end
49
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
510
</page>

src/com/magento/idea/magento2plugin/actions/context/xml/NewLayoutXmlAction.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
import com.intellij.openapi.project.Project;
1313
import com.intellij.psi.PsiDirectory;
1414
import com.intellij.psi.PsiElement;
15+
import com.intellij.psi.PsiFile;
1516
import com.magento.idea.magento2plugin.MagentoIcons;
1617
import com.magento.idea.magento2plugin.actions.generation.dialog.NewLayoutTemplateDialog;
18+
import com.magento.idea.magento2plugin.magento.files.LayoutXml;
1719
import com.magento.idea.magento2plugin.magento.packages.Areas;
1820
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
1921
import com.magento.idea.magento2plugin.magento.packages.Package;
@@ -22,6 +24,7 @@
2224
import java.util.Arrays;
2325
import java.util.List;
2426
import org.jetbrains.annotations.NotNull;
27+
import org.jetbrains.annotations.Nullable;
2528

2629
public class NewLayoutXmlAction extends AnAction {
2730

@@ -47,11 +50,11 @@ public void update(final @NotNull AnActionEvent event) {
4750
}
4851
final DataContext context = event.getDataContext();
4952
final PsiElement targetElement = LangDataKeys.PSI_ELEMENT.getData(context);
53+
final PsiDirectory targetDirectoryCandidate = resolveTargetDirectory(targetElement);
5054

51-
if (!(targetElement instanceof PsiDirectory)) {
55+
if (targetDirectoryCandidate == null) {
5256
return;
5357
}
54-
final PsiDirectory targetDirectoryCandidate = (PsiDirectory) targetElement;
5558
final GetMagentoModuleUtil.MagentoModuleData moduleData = GetMagentoModuleUtil
5659
.getByContext(targetDirectoryCandidate, project);
5760

@@ -104,4 +107,31 @@ private void setIsAvailableForEvent(
104107
event.getPresentation().setVisible(isAvailable);
105108
event.getPresentation().setEnabled(isAvailable);
106109
}
110+
111+
/**
112+
* Resolve target directory.
113+
*
114+
* @param targetElement PsiElement
115+
*
116+
* @return PsiDirectory
117+
*/
118+
private @Nullable PsiDirectory resolveTargetDirectory(final PsiElement targetElement) {
119+
PsiDirectory target = null;
120+
121+
if (targetElement instanceof PsiDirectory) {
122+
target = (PsiDirectory) targetElement;
123+
} else if (targetElement instanceof PsiFile) {
124+
target = ((PsiFile) targetElement).getContainingDirectory();
125+
}
126+
127+
if (target == null) {
128+
return null;
129+
}
130+
131+
if (LayoutXml.PARENT_DIR.equals(target.getName())) {
132+
target = target.getParentDirectory();
133+
}
134+
135+
return target;
136+
}
107137
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private void autoSelectCurrentArea() {
168168
}
169169

170170
/**
171-
* Get parts of inserted layout name
171+
* Get parts of inserted layout name.
172172
*
173173
* @return String[]
174174
*/
@@ -187,6 +187,7 @@ private String[] getLayoutNameParts() {
187187
controllerName = layoutNameParts[1];
188188
actionName = layoutNameParts[2];
189189
}
190+
190191
return new String[]{routeName, controllerName, actionName};
191192
}
192193

src/com/magento/idea/magento2plugin/actions/generation/generator/LayoutXmlTemplateGenerator.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import com.intellij.psi.xml.XmlTag;
1515
import com.magento.idea.magento2plugin.actions.generation.data.LayoutXmlData;
1616
import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateLayoutXml;
17-
import com.magento.idea.magento2plugin.magento.packages.Areas;
18-
import java.util.Objects;
1917
import java.util.Properties;
2018
import org.jetbrains.annotations.NotNull;
2119

@@ -38,8 +36,7 @@ public LayoutXmlTemplateGenerator(
3836
super(project);
3937
this.layoutXmlData = layoutXmlData;
4038
this.project = project;
41-
final Properties attributes = getAttributes();
42-
this.findOrCreateLayoutXml = new FindOrCreateLayoutXml(project, attributes);
39+
this.findOrCreateLayoutXml = new FindOrCreateLayoutXml(project);
4340
}
4441

4542
/**
@@ -83,11 +80,6 @@ public PsiFile generate(final @NotNull String actionName) {
8380
}
8481

8582
@Override
86-
protected void fillAttributes(final Properties attributes) {
87-
if (Objects.equals(layoutXmlData.getArea(), Areas.adminhtml.toString())) {
88-
attributes.setProperty("IS_ADMIN", Boolean.TRUE.toString());
89-
} else {
90-
attributes.setProperty("IS_ADMIN", Boolean.FALSE.toString());
91-
}
92-
}
83+
@SuppressWarnings("PMD.UncommentedEmptyMethodBody")
84+
protected void fillAttributes(final Properties attributes) {}
9385
}

src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateLayoutXml.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.magento.idea.magento2plugin.util.magento.FileBasedIndexUtil;
1616
import java.util.ArrayList;
1717
import java.util.Properties;
18+
import org.jetbrains.annotations.NotNull;
1819

1920
public final class FindOrCreateLayoutXml {
2021

@@ -23,12 +24,7 @@ public final class FindOrCreateLayoutXml {
2324

2425
public FindOrCreateLayoutXml(final Project project) {
2526
this.project = project;
26-
this.properties = new Properties();
27-
}
28-
29-
public FindOrCreateLayoutXml(final Project project, Properties properties) {
30-
this.project = project;
31-
this.properties = properties;
27+
properties = new Properties();
3228
}
3329

3430
/**
@@ -82,9 +78,10 @@ public PsiFile execute(
8278
LayoutXml.PARENT_DIR
8379
);
8480
if (layoutXmlFile == null) {
81+
fillDefaultAttributes(area, properties);
8582
layoutXmlFile = fileFromTemplateGenerator.generate(
8683
layoutXml,
87-
this.properties,
84+
properties,
8885
parentDirectory,
8986
actionName
9087
);
@@ -95,4 +92,15 @@ public PsiFile execute(
9592
private Areas getArea(final String area) {
9693
return Areas.getAreaByString(area);
9794
}
95+
96+
private void fillDefaultAttributes(
97+
final @NotNull String area,
98+
final @NotNull Properties properties
99+
) {
100+
if (Areas.adminhtml.toString().equals(area)) {
101+
properties.setProperty("IS_ADMIN", Boolean.TRUE.toString());
102+
} else {
103+
properties.setProperty("IS_ADMIN", Boolean.FALSE.toString());
104+
}
105+
}
98106
}

0 commit comments

Comments
 (0)