update copyright
[fedora-idea.git] / plugins / ui-designer / src / com / intellij / uiDesigner / actions / GenerateMainAction.java
blob0b90e10c4cd182bbc77bf6a13869a3c63337b8e4
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.uiDesigner.actions;
19 import com.intellij.codeInsight.generation.GenerateMembersUtil;
20 import com.intellij.codeInsight.generation.PsiGenerationInfo;
21 import com.intellij.openapi.actionSystem.AnAction;
22 import com.intellij.openapi.actionSystem.AnActionEvent;
23 import com.intellij.openapi.actionSystem.PlatformDataKeys;
24 import com.intellij.openapi.application.ApplicationManager;
25 import com.intellij.openapi.command.CommandProcessor;
26 import com.intellij.openapi.diagnostic.Logger;
27 import com.intellij.openapi.editor.Editor;
28 import com.intellij.openapi.project.Project;
29 import com.intellij.openapi.ui.Messages;
30 import com.intellij.psi.*;
31 import com.intellij.psi.codeStyle.JavaCodeStyleManager;
32 import com.intellij.psi.codeStyle.SuggestedNameInfo;
33 import com.intellij.psi.codeStyle.VariableKind;
34 import com.intellij.psi.util.PsiMethodUtil;
35 import com.intellij.psi.util.PsiTreeUtil;
36 import com.intellij.psi.util.PsiUtil;
37 import com.intellij.uiDesigner.UIDesignerBundle;
38 import com.intellij.uiDesigner.binding.FormClassIndex;
39 import com.intellij.uiDesigner.compiler.AlienFormFileException;
40 import com.intellij.uiDesigner.compiler.Utils;
41 import com.intellij.uiDesigner.lw.LwRootContainer;
42 import com.intellij.util.IncorrectOperationException;
43 import org.jetbrains.annotations.NonNls;
45 import javax.swing.*;
46 import java.util.Collections;
47 import java.util.List;
49 /**
50 * @author yole
52 public class GenerateMainAction extends AnAction {
53 private static final Logger LOG = Logger.getInstance("#com.intellij.uiDesigner.actions.GenerateMainAction");
55 public void actionPerformed(AnActionEvent e) {
56 final Project project = e.getData(PlatformDataKeys.PROJECT);
57 assert project != null;
58 final Editor editor = e.getData(PlatformDataKeys.EDITOR);
59 assert editor != null;
60 final int offset = editor.getCaretModel().getOffset();
61 final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
62 PsiClass psiClass = PsiTreeUtil.getParentOfType(file.findElementAt(offset), PsiClass.class);
63 assert psiClass != null;
65 if (!PsiUtil.hasDefaultConstructor(psiClass)) {
66 Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.no.default.constructor"), UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
67 return;
70 final List<PsiFile> boundForms = FormClassIndex.findFormsBoundToClass(project, psiClass.getQualifiedName());
71 final LwRootContainer rootContainer;
72 try {
73 rootContainer = Utils.getRootContainer(boundForms.get(0).getText(), null);
75 catch (AlienFormFileException ex) {
76 Messages.showMessageDialog(project, "The form bound to the class is not a valid IntelliJ IDEA form",
77 UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
78 return;
80 catch (Exception ex) {
81 LOG.error(ex);
82 return;
85 if (rootContainer.getComponentCount() == 0) {
86 Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.empty.form"),
87 UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
88 return;
90 String rootBinding = rootContainer.getComponent(0).getBinding();
91 if (rootBinding == null || psiClass.findFieldByName(rootBinding, true) == null) {
92 Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.no.root.binding"),
93 UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
94 return;
97 @NonNls final StringBuilder mainBuilder = new StringBuilder("public static void main(String[] args) { ");
98 final JavaCodeStyleManager csm = JavaCodeStyleManager.getInstance(project);
99 SuggestedNameInfo nameInfo = csm.suggestVariableName(VariableKind.LOCAL_VARIABLE, "frame", null, null);
100 String varName = nameInfo.names [0];
101 mainBuilder.append(JFrame.class.getName()).append(" ").append(varName).append("= new ").append(JFrame.class.getName());
102 mainBuilder.append("(\"").append(psiClass.getName()).append("\");");
103 mainBuilder.append(varName).append(".setContentPane(new ").append(psiClass.getQualifiedName()).append("().").append(rootBinding).append(");");
104 mainBuilder.append(varName).append(".setDefaultCloseOperation(").append(JFrame.class.getName()).append(".EXIT_ON_CLOSE);");
105 mainBuilder.append(varName).append(".pack();");
106 mainBuilder.append(varName).append(".setVisible(true);");
108 mainBuilder.append("}\n");
110 CommandProcessor.getInstance().executeCommand(project, new Runnable() {
111 public void run() {
112 ApplicationManager.getApplication().runWriteAction(new Runnable() {
113 public void run() {
114 try {
115 PsiMethod method =
116 JavaPsiFacade.getInstance(file.getProject()).getElementFactory().createMethodFromText(mainBuilder.toString(), file);
117 List<PsiGenerationInfo<PsiMethod>> infos = Collections.singletonList(new PsiGenerationInfo<PsiMethod>(method));
118 List<PsiGenerationInfo<PsiMethod>> resultMembers = GenerateMembersUtil.insertMembersAtOffset(file, offset, infos);
119 GenerateMembersUtil.positionCaret(editor, resultMembers.get(0).getPsiMember(), false);
121 catch (IncorrectOperationException e1) {
122 LOG.error(e1);
127 }, null, null);
130 @Override
131 public void update(AnActionEvent e) {
132 e.getPresentation().setVisible(isActionEnabled(e));
135 private static boolean isActionEnabled(final AnActionEvent e) {
136 Project project = e.getData(PlatformDataKeys.PROJECT);
137 if (project == null) return false;
138 Editor editor = e.getData(PlatformDataKeys.EDITOR);
139 if (editor == null) return false;
140 int offset = editor.getCaretModel().getOffset();
141 PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
142 if (file == null) return false;
143 PsiElement element = file.findElementAt(offset);
144 if (element == null) return false;
145 PsiClass psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
146 if (psiClass == null) return false;
147 if (PsiMethodUtil.findMainMethod(psiClass) != null) return false;
148 if (FormClassIndex.findFormsBoundToClass(psiClass).isEmpty()) return false;
149 return true;