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
.ide
.fileTemplates
.FileTemplate
;
20 import com
.intellij
.ide
.fileTemplates
.FileTemplateManager
;
21 import com
.intellij
.openapi
.diagnostic
.Logger
;
22 import com
.intellij
.openapi
.fileTypes
.StdFileTypes
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import com
.intellij
.openapi
.ui
.DialogWrapper
;
25 import com
.intellij
.openapi
.application
.ApplicationManager
;
26 import com
.intellij
.psi
.*;
27 import com
.intellij
.ui
.ColoredListCellRenderer
;
28 import com
.intellij
.ui
.DocumentAdapter
;
29 import com
.intellij
.ui
.SimpleTextAttributes
;
30 import com
.intellij
.uiDesigner
.GuiDesignerConfiguration
;
31 import com
.intellij
.uiDesigner
.UIDesignerBundle
;
32 import com
.intellij
.uiDesigner
.radComponents
.LayoutManagerRegistry
;
33 import com
.intellij
.util
.Icons
;
34 import com
.intellij
.util
.IncorrectOperationException
;
35 import org
.jetbrains
.annotations
.NonNls
;
36 import org
.jetbrains
.annotations
.NotNull
;
39 import javax
.swing
.event
.ChangeEvent
;
40 import javax
.swing
.event
.ChangeListener
;
41 import javax
.swing
.event
.DocumentEvent
;
46 public class CreateFormAction
extends AbstractCreateFormAction
{
47 private static final Logger LOG
= Logger
.getInstance("#com.intellij.uiDesigner.actions.CreateFormAction");
49 private String myLastClassName
= null;
50 private String myLastLayoutManager
= null;
52 public CreateFormAction() {
53 super(UIDesignerBundle
.message("action.gui.form.text"),
54 UIDesignerBundle
.message("action.gui.form.description"), Icons
.UI_FORM_ICON
);
56 // delete obsolete template
57 ApplicationManager
.getApplication().executeOnPooledThread(new Runnable() {
59 // to prevent deadlocks, this code must run while not holding the ActionManager lock
60 FileTemplateManager manager
= FileTemplateManager
.getInstance();
61 final FileTemplate template
= manager
.getTemplate("GUI Form");
62 //noinspection HardCodedStringLiteral
63 if (template
!= null && template
.getExtension().equals("form")) {
64 manager
.removeTemplate(template
, false);
71 protected PsiElement
[] invokeDialog(Project project
, PsiDirectory directory
) {
72 final MyInputValidator validator
= new MyInputValidator(project
, directory
);
74 final DialogWrapper dialog
= new MyDialog(project
, validator
);
77 return validator
.getCreatedElements();
80 protected void checkBeforeCreate(String newName
, PsiDirectory directory
) throws IncorrectOperationException
{
81 if (myLastClassName
!= null) {
82 JavaDirectoryService
.getInstance().checkCreateClass(directory
, myLastClassName
);
87 protected PsiElement
[] create(String newName
, PsiDirectory directory
) throws Exception
{
88 PsiElement createdFile
;
89 PsiClass newClass
= null;
91 final PsiPackage aPackage
= JavaDirectoryService
.getInstance().getPackage(directory
);
92 assert aPackage
!= null;
93 final String packageName
= aPackage
.getQualifiedName();
94 String fqClassName
= null;
95 if (myLastClassName
!= null) {
96 fqClassName
= packageName
.length() == 0 ? newName
: packageName
+ "." + myLastClassName
;
99 final String formBody
= createFormBody(fqClassName
, "/com/intellij/uiDesigner/NewForm.xml",
100 myLastLayoutManager
);
101 @NonNls final String fileName
= newName
+ ".form";
102 final PsiFile formFile
= PsiFileFactory
.getInstance(directory
.getProject())
103 .createFileFromText(fileName
, StdFileTypes
.GUI_DESIGNER_FORM
, formBody
);
104 createdFile
= directory
.add(formFile
);
106 if (myLastClassName
!= null) {
107 newClass
= JavaDirectoryService
.getInstance().createClass(directory
, myLastClassName
);
110 catch(IncorrectOperationException e
) {
113 catch (Exception e
) {
115 return PsiElement
.EMPTY_ARRAY
;
118 if (newClass
!= null) {
119 return new PsiElement
[] { newClass
.getContainingFile(), createdFile
};
121 return new PsiElement
[] { createdFile
};
124 protected String
getErrorTitle() {
125 return UIDesignerBundle
.message("error.cannot.create.form");
128 protected String
getCommandName() {
129 return UIDesignerBundle
.message("command.create.form");
132 private class MyDialog
extends DialogWrapper
{
133 private JPanel myTopPanel
;
134 private JTextField myFormNameTextField
;
135 private JCheckBox myCreateBoundClassCheckbox
;
136 private JTextField myClassNameTextField
;
137 private JComboBox myBaseLayoutManagerCombo
;
138 private boolean myAdjusting
= false;
139 private boolean myNeedAdjust
= true;
141 private final Project myProject
;
142 private final MyInputValidator myValidator
;
144 public MyDialog(final Project project
,
145 final MyInputValidator validator
) {
146 super(project
, true);
148 myValidator
= validator
;
150 setTitle(UIDesignerBundle
.message("title.new.gui.form"));
151 setOKActionEnabled(false);
153 myCreateBoundClassCheckbox
.addChangeListener(new ChangeListener() {
154 public void stateChanged(ChangeEvent e
) {
155 myClassNameTextField
.setEnabled(myCreateBoundClassCheckbox
.isSelected());
159 myFormNameTextField
.getDocument().addDocumentListener(new DocumentAdapter() {
160 protected void textChanged(DocumentEvent e
) {
161 setOKActionEnabled(myFormNameTextField
.getText().length() > 0);
164 myClassNameTextField
.setText(myFormNameTextField
.getText());
170 myClassNameTextField
.getDocument().addDocumentListener(new DocumentAdapter() {
171 protected void textChanged(DocumentEvent e
) {
173 myNeedAdjust
= false;
178 myBaseLayoutManagerCombo
.setModel(new DefaultComboBoxModel(LayoutManagerRegistry
.getNonDeprecatedLayoutManagerNames()));
179 myBaseLayoutManagerCombo
.setRenderer(new ColoredListCellRenderer() {
180 protected void customizeCellRenderer(JList list
, Object value
, int index
, boolean selected
, boolean hasFocus
) {
181 append(LayoutManagerRegistry
.getLayoutManagerDisplayName((String
) value
), SimpleTextAttributes
.REGULAR_ATTRIBUTES
);
184 myBaseLayoutManagerCombo
.setSelectedItem(GuiDesignerConfiguration
.getInstance(project
).DEFAULT_LAYOUT_MANAGER
);
187 protected JComponent
createCenterPanel() {
191 protected void doOKAction() {
192 if (myCreateBoundClassCheckbox
.isSelected()) {
193 myLastClassName
= myClassNameTextField
.getText();
196 myLastClassName
= null;
198 myLastLayoutManager
= (String
)myBaseLayoutManagerCombo
.getSelectedItem();
199 GuiDesignerConfiguration
.getInstance(myProject
).DEFAULT_LAYOUT_MANAGER
= myLastLayoutManager
;
200 final String inputString
= myFormNameTextField
.getText().trim();
201 if (myValidator
.checkInput(inputString
) && myValidator
.canClose(inputString
)) {
207 public JComponent
getPreferredFocusedComponent() {
208 return myFormNameTextField
;