Skip to content

Commit 6492f45

Browse files
Bug-653: Fixed NullPointerException for com.magento.idea.magento2plugin.actions.generation.InjectAViewModelAction#getElement
1 parent bb71207 commit 6492f45

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

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

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,78 +22,110 @@
2222
import org.jetbrains.annotations.NotNull;
2323

2424
public class InjectAViewModelAction extends DumbAwareAction {
25-
public static String actionName = "Inject a new View Model for this block";
26-
public static String actionDescription = "Inject a new Magento 2 View Model";
25+
26+
public static final String ACTION_NAME = "Inject a new View Model for this block";
27+
public static final String ACTION_DESCRIPTION = "Inject a new Magento 2 View Model";
2728
private XmlTag targetXmlTag;
2829

30+
/**
31+
* An action constructor.
32+
*/
2933
public InjectAViewModelAction() {
30-
super(actionName, actionDescription, MagentoIcons.MODULE);
34+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
3135
}
3236

3337
@Override
34-
public void update(final AnActionEvent event) {
38+
public void update(final @NotNull AnActionEvent event) {
39+
this.setStatus(event, false);
3540
final Project project = event.getData(PlatformDataKeys.PROJECT);
41+
42+
if (project == null) {
43+
return;
44+
}
45+
3646
if (Settings.isEnabled(project)) {
3747
final XmlTag element = getElement(event);
38-
if (element == null) {
39-
this.setStatus(event, false);
40-
} else {
48+
49+
if (element != null) {
4150
targetXmlTag = element;
4251
this.setStatus(event, true);
4352
}
44-
} else {
45-
this.setStatus(event, false);
4653
}
4754
}
4855

49-
private void setStatus(final AnActionEvent event, final boolean status) {
50-
event.getPresentation().setVisible(status);
51-
event.getPresentation().setEnabled(status);
52-
}
53-
5456
@Override
5557
public void actionPerformed(final @NotNull AnActionEvent event) {
56-
InjectAViewModelDialog.open(event.getProject(), this.targetXmlTag);
58+
final Project project = event.getData(PlatformDataKeys.PROJECT);
59+
60+
if (project == null || targetXmlTag == null) {
61+
return;
62+
}
63+
InjectAViewModelDialog.open(project, targetXmlTag);
5764
}
5865

5966
@Override
6067
public boolean isDumbAware() {
6168
return false;
6269
}
6370

71+
/**
72+
* Get focused (target) element for the event.
73+
*
74+
* @param event AnActionEvent
75+
*
76+
* @return XmlTag
77+
*/
6478
private XmlTag getElement(final @NotNull AnActionEvent event) {
6579
final Caret caret = event.getData(PlatformDataKeys.CARET);
80+
6681
if (caret == null) {
6782
return null;
6883
}
6984

70-
final int offset = caret.getOffset();
7185
final PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE);
86+
87+
if (psiFile == null) {
88+
return null;
89+
}
90+
final int offset = caret.getOffset();
7291
final PsiElement element = psiFile.findElementAt(offset);
92+
7393
if (element == null) {
7494
return null;
7595
}
76-
7796
final XmlTag xmlTag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
7897

7998
if (xmlTag == null) {
8099
return null;
81100
}
82101
XmlTag resultTag;
83-
if (xmlTag.getName().equals(CommonXml.ATTRIBUTE_ARGUMENTS)) {
102+
103+
if (CommonXml.ATTRIBUTE_ARGUMENTS.equals(xmlTag.getName())) {
84104
resultTag = PsiTreeUtil.getParentOfType(xmlTag, XmlTag.class);
85105
} else {
86106
resultTag = xmlTag;
87107
}
108+
88109
if (resultTag == null) {
89110
return null;
90111
}
91112

92-
if (!resultTag.getName().equals(LayoutXml.BLOCK_ATTRIBUTE_TAG_NAME)
93-
&& !resultTag.getName().equals(LayoutXml.REFERENCE_BLOCK_ATTRIBUTE_TAG_NAME)) {
113+
if (!LayoutXml.BLOCK_ATTRIBUTE_TAG_NAME.equals(resultTag.getName())
114+
&& !LayoutXml.REFERENCE_BLOCK_ATTRIBUTE_TAG_NAME.equals(resultTag.getName())) {
94115
return null;
95116
}
96117

97118
return resultTag;
98119
}
120+
121+
/**
122+
* Set presentation status.
123+
*
124+
* @param event AnActionEvent
125+
* @param status boolean
126+
*/
127+
private void setStatus(final AnActionEvent event, final boolean status) {
128+
event.getPresentation().setVisible(status);
129+
event.getPresentation().setEnabled(status);
130+
}
99131
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected void textChanged(final @NotNull DocumentEvent event) {
104104

105105
setContentPane(contentPane);
106106
setModal(true);
107-
setTitle(InjectAViewModelAction.actionDescription);
107+
setTitle(InjectAViewModelAction.ACTION_DESCRIPTION);
108108
getRootPane().setDefaultButton(buttonOK);
109109

110110
buttonOK.addActionListener((final ActionEvent event) -> onOK());
@@ -154,7 +154,7 @@ protected void onOK() {
154154
getViewModelClassName(),
155155
moduleName,
156156
namespaceBuilder.getNamespace()
157-
), project).generate(InjectAViewModelAction.actionName, true);
157+
), project).generate(InjectAViewModelAction.ACTION_NAME, true);
158158
if (viewModel == null) {
159159
final String errorMessage = validatorBundle.message(
160160
"validator.class.alreadyDeclared",

0 commit comments

Comments
 (0)