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.
16 package com
.intellij
.uiDesigner
.quickFixes
;
18 import com
.intellij
.CommonBundle
;
19 import com
.intellij
.openapi
.application
.ApplicationManager
;
20 import com
.intellij
.openapi
.command
.CommandProcessor
;
21 import com
.intellij
.openapi
.diagnostic
.Logger
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.ui
.Messages
;
24 import com
.intellij
.psi
.*;
25 import com
.intellij
.psi
.search
.GlobalSearchScope
;
26 import com
.intellij
.refactoring
.util
.CommonRefactoringUtil
;
27 import com
.intellij
.uiDesigner
.FormEditingUtil
;
28 import com
.intellij
.uiDesigner
.GuiDesignerConfiguration
;
29 import com
.intellij
.uiDesigner
.UIDesignerBundle
;
30 import com
.intellij
.uiDesigner
.designSurface
.GuiEditor
;
31 import com
.intellij
.uiDesigner
.lw
.IContainer
;
32 import com
.intellij
.uiDesigner
.radComponents
.RadContainer
;
33 import com
.intellij
.util
.IncorrectOperationException
;
34 import org
.jetbrains
.annotations
.NotNull
;
35 import org
.jetbrains
.annotations
.Nullable
;
38 * @author Anton Katilin
39 * @author Vladimir Kondratyev
41 public final class CreateFieldFix
extends QuickFix
{
42 private static final Logger LOG
= Logger
.getInstance("#com.intellij.uiDesigner.quickFixes.CreateFieldFix");
44 private final PsiClass myClass
;
45 private final String myFieldClassName
;
46 private final String myFieldName
;
48 public CreateFieldFix(
49 final GuiEditor editor
,
50 @NotNull final PsiClass aClass
,
51 @NotNull final String fieldClass
,
52 @NotNull final String fieldName
54 super(editor
, UIDesignerBundle
.message("action.create.field", fieldName
), null);
56 myFieldClassName
= fieldClass
;
57 myFieldName
= fieldName
;
61 * @param showErrors if <code>true</code> the error messages will be shown to the
62 * @param undoGroupId the group used to undo the action together with some other action.
64 public static void runImpl(@NotNull final Project project
,
65 @NotNull final RadContainer rootContainer
,
66 @NotNull final PsiClass boundClass
,
67 @NotNull final String fieldClassName
,
68 @NotNull final String fieldName
,
69 final boolean showErrors
,
70 @Nullable final Object undoGroupId
) {
71 ApplicationManager
.getApplication().assertReadAccessAllowed();
73 PsiDocumentManager
.getInstance(project
).commitAllDocuments();
75 // Do nothing if file becomes invalid
76 if(!boundClass
.isValid()){
80 if(!boundClass
.isWritable()){
82 if (!CommonRefactoringUtil
.checkReadOnlyStatus(boundClass
, project
,
83 UIDesignerBundle
.message("error.cannot.create.field", fieldClassName
))) {
89 final PsiClass fieldClass
= JavaPsiFacade
.getInstance(project
)
90 .findClass(fieldClassName
.replace('$', '.'), GlobalSearchScope
.moduleWithDependenciesAndLibrariesScope(rootContainer
.getModule()));
91 if(fieldClass
== null){
93 Messages
.showErrorDialog(
95 UIDesignerBundle
.message("error.cannot.create.field.no.class", fieldName
, fieldClassName
),
96 CommonBundle
.getErrorTitle()
102 CommandProcessor
.getInstance().executeCommand(
106 ApplicationManager
.getApplication().runWriteAction(
109 createField(project
, fieldClass
, fieldName
, boundClass
, showErrors
, rootContainer
);
115 UIDesignerBundle
.message("command.create.field"),
120 private static void createField(final Project project
,
121 final PsiClass fieldClass
,
122 final String fieldName
,
123 final PsiClass boundClass
,
124 final boolean showErrors
,
125 final IContainer rootContainer
) {
127 final PsiElementFactory factory
= JavaPsiFacade
.getInstance(project
).getElementFactory();
128 final PsiType type
= factory
.createType(fieldClass
);
130 final PsiField field
= factory
.createField(fieldName
, type
);
131 final String accessibility
= GuiDesignerConfiguration
.getInstance(project
).DEFAULT_FIELD_ACCESSIBILITY
;
132 final PsiModifierList modifierList
= field
.getModifierList();
133 assert modifierList
!= null;
134 String
[] modifiers
= new String
[]{PsiModifier
.PRIVATE
, PsiModifier
.PROTECTED
, PsiModifier
.PUBLIC
};
135 for(@Modifier String modifier
: modifiers
) {
136 modifierList
.setModifierProperty(modifier
, accessibility
.equals(modifier
));
138 PsiField lastUiField
= null;
139 for(PsiField uiField
: boundClass
.getFields()) {
140 if (FormEditingUtil
.findComponentWithBinding(rootContainer
, uiField
.getName()) != null) {
141 lastUiField
= uiField
;
144 if (lastUiField
!= null) {
145 boundClass
.addAfter(field
, lastUiField
);
148 boundClass
.add(field
);
151 catch (final IncorrectOperationException exc
) {
153 ApplicationManager
.getApplication().invokeLater(
156 Messages
.showErrorDialog(
158 UIDesignerBundle
.message("error.cannot.create.field.reason", fieldName
, exc
.getMessage()),
159 CommonBundle
.getErrorTitle()
169 runImpl(myEditor
.getProject(), myEditor
.getRootContainer(), myClass
, myFieldClassName
, myFieldName
, true, null);