Ok Button is Empty

Answered

I created a DialogWrapper for a plugin I'm working on, but for some reason the "Ok" Buton is blank. Any reason why?

package io.github.seggan.slimefunaddonplugin;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassType;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementFactory;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiModifierList;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class NewItemDialog extends DialogWrapper {

private final JTextField idField = new JTextField();
private final JTextField materialField = new JTextField();
private final JTextField nameField = new JTextField();
private final JTextArea loreField = new JTextArea();
private final AnActionEvent e;

public NewItemDialog(AnActionEvent e) {
super(true);

this.setTitle("Create a Slimefun Item");
this.init();

this.e = e;
}

@Nullable
@Override
protected JComponent createCenterPanel() {
JPanel dialogPanel = new JPanel();

JLabel label = new JLabel("Id");
dialogPanel.add(label);
dialogPanel.add(idField);

label = new JLabel("Material");
dialogPanel.add(label);
dialogPanel.add(materialField);

label = new JLabel("Name");
dialogPanel.add(label);
dialogPanel.add(nameField);

label = new JLabel("Lore");
dialogPanel.add(label);
loreField.setPreferredSize(new Dimension(100, 100));
dialogPanel.add(loreField);

return dialogPanel;
}

@Override
protected void doOKAction() {
WriteCommandAction.runWriteCommandAction(e.getProject(), () -> {
PsiFile file = e.getDataContext().getData(CommonDataKeys.PSI_FILE);
assert file instanceof PsiJavaFile;
PsiJavaFile javaFile = (PsiJavaFile) file;

PsiClass mainClass = javaFile.getClasses()[0];
if (mainClass == null) {
System.out.println("NO CLASS");
return;
}

PsiElementFactory factory = JavaPsiFacade.getInstance(e.getProject()).getElementFactory();

PsiClassType sfisClass = factory.createTypeByFQClassName("me.mrCookieSlime.Slimefun.api.SlimefunItemStack");

String id = idField.getText();

PsiField field = factory.createField(id, sfisClass);
PsiModifierList modifierList = field.getModifierList();
modifierList.setModifierProperty(PsiModifier.PUBLIC, true);
modifierList.setModifierProperty(PsiModifier.STATIC, true);
modifierList.setModifierProperty(PsiModifier.FINAL, true);

field.setInitializer(factory.createExpressionFromText(
"new SlimefunItemStack(\n" + id + ")", sfisClass.getPsiContext()));

PsiMethod[] ctors = mainClass.getConstructors();
if (ctors.length == 0) {
mainClass.add(field);
} else {
mainClass.addAfter(field, mainClass.getConstructors()[0]);
}

close(OK_EXIT_CODE);
});
}
}
1 comment
Comment actions Permalink

You can not store and reference AnActionEvent later. Extract all needed information before showing dialog.

0

Please sign in to leave a comment.