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
.refactoring
.introduceparameterobject
;
18 import com
.intellij
.ide
.util
.SuperMethodWarningUtil
;
19 import com
.intellij
.openapi
.actionSystem
.DataContext
;
20 import com
.intellij
.openapi
.actionSystem
.LangDataKeys
;
21 import com
.intellij
.openapi
.actionSystem
.PlatformDataKeys
;
22 import com
.intellij
.openapi
.editor
.CaretModel
;
23 import com
.intellij
.openapi
.editor
.Editor
;
24 import com
.intellij
.openapi
.editor
.ScrollType
;
25 import com
.intellij
.openapi
.editor
.ScrollingModel
;
26 import com
.intellij
.openapi
.project
.Project
;
27 import com
.intellij
.psi
.*;
28 import com
.intellij
.psi
.util
.PsiTreeUtil
;
29 import com
.intellij
.refactoring
.HelpID
;
30 import com
.intellij
.refactoring
.RefactorJBundle
;
31 import com
.intellij
.refactoring
.RefactoringActionHandler
;
32 import com
.intellij
.refactoring
.RefactoringBundle
;
33 import com
.intellij
.refactoring
.util
.CommonRefactoringUtil
;
34 import org
.jetbrains
.annotations
.NotNull
;
36 public class IntroduceParameterObjectHandler
implements RefactoringActionHandler
{
37 private static final String REFACTORING_NAME
= RefactorJBundle
.message("introduce.parameter.object");
39 public void invoke(@NotNull Project project
, Editor editor
, PsiFile file
, DataContext dataContext
) {
40 final ScrollingModel scrollingModel
= editor
.getScrollingModel();
41 scrollingModel
.scrollToCaret(ScrollType
.MAKE_VISIBLE
);
42 final PsiElement element
= LangDataKeys
.PSI_ELEMENT
.getData(dataContext
);
43 PsiMethod selectedMethod
= null;
44 if (element
instanceof PsiMethod
) {
45 selectedMethod
= (PsiMethod
)element
;
48 final CaretModel caretModel
= editor
.getCaretModel();
49 final int position
= caretModel
.getOffset();
50 final PsiMethodCallExpression methodCallExpression
=
51 PsiTreeUtil
.getParentOfType(file
.findElementAt(position
), PsiMethodCallExpression
.class);
52 if (methodCallExpression
!= null) {
53 selectedMethod
= methodCallExpression
.resolveMethod();
56 if (selectedMethod
== null) {
57 final String message
= RefactorJBundle
.message("cannot.perform.the.refactoring") +
58 RefactorJBundle
.message("the.caret.should.be.positioned.at.the.name.of.the.method.to.be.refactored");
59 CommonRefactoringUtil
.showErrorHint(project
, editor
, message
, REFACTORING_NAME
, HelpID
.IntroduceParameterObject
);
62 invoke(project
, selectedMethod
, editor
);
65 public void invoke(@NotNull Project project
, @NotNull PsiElement
[] elements
, DataContext dataContext
) {
66 if (elements
.length
!= 1) {
69 final PsiMethod method
= PsiTreeUtil
.getParentOfType(elements
[0], PsiMethod
.class, false);
73 Editor editor
= PlatformDataKeys
.EDITOR
.getData(dataContext
);
74 invoke(project
, method
, editor
);
77 private static void invoke(final Project project
, final PsiMethod selectedMethod
, Editor editor
) {
78 PsiMethod newMethod
= SuperMethodWarningUtil
.checkSuperMethod(selectedMethod
, RefactoringBundle
.message("to.refactor"));
79 if (newMethod
== null) return;
80 if (!CommonRefactoringUtil
.checkReadOnlyStatus(project
, newMethod
)) return;
82 final PsiParameter
[] parameters
= newMethod
.getParameterList().getParameters();
83 if (parameters
.length
== 0) {
84 final String message
=
85 RefactorJBundle
.message("cannot.perform.the.refactoring") + RefactorJBundle
.message("method.selected.has.no.parameters");
86 CommonRefactoringUtil
.showErrorHint(project
, editor
, message
, REFACTORING_NAME
, HelpID
.IntroduceParameterObject
);
89 if (newMethod
instanceof PsiCompiledElement
) {
90 CommonRefactoringUtil
.showErrorHint(project
, editor
, RefactorJBundle
.message("cannot.perform.the.refactoring") + RefactorJBundle
.message(
91 "the.selected.method.cannot.be.wrapped.because.it.is.defined.in.a.non.project.class"), REFACTORING_NAME
, HelpID
.IntroduceParameterObject
);
94 new IntroduceParameterObjectDialog(newMethod
).show();