Search in sources :

Example 1 with PsiGenerationInfo

use of com.intellij.codeInsight.generation.PsiGenerationInfo in project intellij-community by JetBrains.

the class GenerateMainAction method actionPerformed.

public void actionPerformed(AnActionEvent e) {
    final Project project = e.getData(CommonDataKeys.PROJECT);
    assert project != null;
    final Editor editor = e.getData(CommonDataKeys.EDITOR);
    assert editor != null;
    final int offset = editor.getCaretModel().getOffset();
    final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    PsiClass psiClass = PsiTreeUtil.getParentOfType(file.findElementAt(offset), PsiClass.class);
    assert psiClass != null;
    if (!PsiUtil.hasDefaultConstructor(psiClass)) {
        Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.no.default.constructor"), UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
        return;
    }
    final List<PsiFile> boundForms = FormClassIndex.findFormsBoundToClass(project, psiClass.getQualifiedName());
    final LwRootContainer rootContainer;
    try {
        rootContainer = Utils.getRootContainer(boundForms.get(0).getText(), null);
    } catch (AlienFormFileException ex) {
        Messages.showMessageDialog(project, "The form bound to the class is not a valid IntelliJ IDEA form", UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
        return;
    } catch (Exception ex) {
        LOG.error(ex);
        return;
    }
    if (rootContainer.getComponentCount() == 0) {
        Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.empty.form"), UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
        return;
    }
    String rootBinding = rootContainer.getComponent(0).getBinding();
    if (rootBinding == null || psiClass.findFieldByName(rootBinding, true) == null) {
        Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.no.root.binding"), UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
        return;
    }
    @NonNls final StringBuilder mainBuilder = new StringBuilder("public static void main(String[] args) { ");
    final JavaCodeStyleManager csm = JavaCodeStyleManager.getInstance(project);
    SuggestedNameInfo nameInfo = csm.suggestVariableName(VariableKind.LOCAL_VARIABLE, "frame", null, null);
    String varName = nameInfo.names[0];
    mainBuilder.append(JFrame.class.getName()).append(" ").append(varName).append("= new ").append(JFrame.class.getName());
    mainBuilder.append("(\"").append(psiClass.getName()).append("\");");
    mainBuilder.append(varName).append(".setContentPane(new ").append(psiClass.getQualifiedName()).append("().").append(rootBinding).append(");");
    mainBuilder.append(varName).append(".setDefaultCloseOperation(").append(JFrame.class.getName()).append(".EXIT_ON_CLOSE);");
    mainBuilder.append(varName).append(".pack();");
    mainBuilder.append(varName).append(".setVisible(true);");
    mainBuilder.append("}\n");
    CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
        try {
            PsiMethod method = JavaPsiFacade.getInstance(file.getProject()).getElementFactory().createMethodFromText(mainBuilder.toString(), file);
            List<PsiGenerationInfo<PsiMethod>> infos = Collections.singletonList(new PsiGenerationInfo<>(method));
            List<PsiGenerationInfo<PsiMethod>> resultMembers = GenerateMembersUtil.insertMembersAtOffset(file, offset, infos);
            resultMembers.get(0).positionCaret(editor, false);
        } catch (IncorrectOperationException e1) {
            LOG.error(e1);
        }
    }), null, null);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) AlienFormFileException(com.intellij.uiDesigner.compiler.AlienFormFileException) LwRootContainer(com.intellij.uiDesigner.lw.LwRootContainer) IncorrectOperationException(com.intellij.util.IncorrectOperationException) AlienFormFileException(com.intellij.uiDesigner.compiler.AlienFormFileException) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) List(java.util.List) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) PsiGenerationInfo(com.intellij.codeInsight.generation.PsiGenerationInfo)

Aggregations

PsiGenerationInfo (com.intellij.codeInsight.generation.PsiGenerationInfo)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)1 SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)1 AlienFormFileException (com.intellij.uiDesigner.compiler.AlienFormFileException)1 LwRootContainer (com.intellij.uiDesigner.lw.LwRootContainer)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 List (java.util.List)1 NonNls (org.jetbrains.annotations.NonNls)1