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 org
.jetbrains
.plugins
.groovy
.annotator
.intentions
.dynamic
;
18 import com
.intellij
.codeInsight
.intention
.IntentionAction
;
19 import com
.intellij
.openapi
.editor
.Editor
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.psi
.PsiFile
;
22 import com
.intellij
.psi
.PsiType
;
23 import com
.intellij
.util
.IncorrectOperationException
;
24 import org
.jetbrains
.annotations
.NotNull
;
25 import org
.jetbrains
.plugins
.groovy
.GroovyBundle
;
26 import org
.jetbrains
.plugins
.groovy
.annotator
.intentions
.QuickfixUtil
;
27 import org
.jetbrains
.plugins
.groovy
.annotator
.intentions
.dynamic
.ui
.DynamicDialog
;
28 import org
.jetbrains
.plugins
.groovy
.annotator
.intentions
.dynamic
.ui
.DynamicElementSettings
;
29 import org
.jetbrains
.plugins
.groovy
.annotator
.intentions
.dynamic
.ui
.DynamicMethodDialog
;
30 import org
.jetbrains
.plugins
.groovy
.annotator
.intentions
.dynamic
.ui
.DynamicPropertyDialog
;
31 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.api
.statements
.expressions
.GrReferenceExpression
;
32 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.util
.PsiUtil
;
35 * User: Dmitry.Krasilschikov
38 public class DynamicFix
implements IntentionAction
{
39 private final GrReferenceExpression myReferenceExpression
;
40 private final boolean myIsMethod
;
42 public DynamicFix(boolean isMethod
, GrReferenceExpression referenceExpression
) {
43 myIsMethod
= isMethod
;
44 myReferenceExpression
= referenceExpression
;
48 public String
getText() {
50 return GroovyBundle
.message("add.dynamic.property", myReferenceExpression
.getName());
53 final PsiType
[] methodArgumentsTypes
= PsiUtil
.getArgumentTypes(myReferenceExpression
, false, false);
54 StringBuilder builder
= new StringBuilder(" '").append(myReferenceExpression
.getName());
57 assert methodArgumentsTypes
!= null;
58 for (int i
= 0; i
< methodArgumentsTypes
.length
; i
++) {
59 PsiType type
= methodArgumentsTypes
[i
];
64 builder
.append(type
.getPresentableText());
69 return GroovyBundle
.message("add.dynamic.method") + builder
.toString();
73 public String
getFamilyName() {
74 return GroovyBundle
.message("add.dynamic.element");
77 public boolean isAvailable(@NotNull Project project
, Editor editor
, PsiFile psiFile
) {
78 return myReferenceExpression
.isValid();
81 public void invoke(@NotNull Project project
, Editor editor
, PsiFile psiFile
) throws IncorrectOperationException
{
82 DynamicDialog dialog
= myIsMethod ?
83 new DynamicMethodDialog(myReferenceExpression
) :
84 new DynamicPropertyDialog(myReferenceExpression
);
89 public void invoke(Project project
) throws IncorrectOperationException
{
90 final DynamicElementSettings settings
= QuickfixUtil
.createSettings(myReferenceExpression
);
93 DynamicManager
.getInstance(project
).addMethod(settings
);
95 DynamicManager
.getInstance(project
).addProperty(settings
);
99 public boolean startInWriteAction() {
103 public boolean isMethod() {
107 public GrReferenceExpression
getReferenceExpression() {
108 return myReferenceExpression
;