update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / changeSignature / ChangeSignatureHandler.java
blobad997fca7ce4bbfc895ed05a646d4596cc8d103d
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.
16 package com.intellij.refactoring.changeSignature;
18 import com.intellij.codeInsight.TargetElementUtil;
19 import com.intellij.ide.util.SuperMethodWarningUtil;
20 import com.intellij.openapi.actionSystem.DataContext;
21 import com.intellij.openapi.actionSystem.LangDataKeys;
22 import com.intellij.openapi.actionSystem.PlatformDataKeys;
23 import com.intellij.openapi.editor.Editor;
24 import com.intellij.openapi.editor.ScrollType;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.psi.*;
27 import com.intellij.refactoring.HelpID;
28 import com.intellij.refactoring.RefactoringActionHandler;
29 import com.intellij.refactoring.RefactoringBundle;
30 import com.intellij.refactoring.changeClassSignature.ChangeClassSignatureDialog;
31 import com.intellij.refactoring.util.CommonRefactoringUtil;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
35 public class ChangeSignatureHandler implements RefactoringActionHandler {
36 public static final String REFACTORING_NAME = RefactoringBundle.message("changeSignature.refactoring.name");
38 public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
39 editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
40 PsiElement element = LangDataKeys.PSI_ELEMENT.getData(dataContext);
41 invokeOnElement(project, editor, element);
44 private static void invokeOnElement(Project project, Editor editor, PsiElement element) {
45 if (element instanceof PsiMethod) {
46 invoke((PsiMethod) element, project, editor);
48 else if (element instanceof PsiClass) {
49 invoke((PsiClass) element, editor);
51 else {
52 String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.wrong.caret.position.method.or.class.name"));
53 CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.CHANGE_SIGNATURE);
57 public void invoke(@NotNull final Project project, @NotNull final PsiElement[] elements, final DataContext dataContext) {
58 if (elements.length != 1) return;
59 Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
60 invokeOnElement(project, editor, elements[0]);
63 private static void invoke(final PsiMethod method, final Project project, @Nullable final Editor editor) {
64 if (!CommonRefactoringUtil.checkReadOnlyStatus(project, method)) return;
66 PsiMethod newMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringBundle.message("to.refactor"));
67 if (newMethod == null) return;
69 if (!newMethod.equals(method)) {
70 invoke(newMethod, project, editor);
71 return;
74 if (!CommonRefactoringUtil.checkReadOnlyStatus(project, method)) return;
76 final PsiClass containingClass = method.getContainingClass();
77 final PsiReferenceExpression refExpr = editor != null ? TargetElementUtil.findReferenceExpression(editor) : null;
78 final ChangeSignatureDialog dialog = new ChangeSignatureDialog(project, method, containingClass != null && !containingClass.isInterface(),
79 refExpr);
80 dialog.show();
83 private static void invoke(final PsiClass aClass, Editor editor) {
84 final PsiTypeParameterList typeParameterList = aClass.getTypeParameterList();
85 Project project = aClass.getProject();
86 if (typeParameterList == null) {
87 final String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("changeClassSignature.no.type.parameters"));
88 CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.CHANGE_CLASS_SIGNATURE);
89 return;
91 if (!CommonRefactoringUtil.checkReadOnlyStatus(project, aClass)) return;
93 ChangeClassSignatureDialog dialog = new ChangeClassSignatureDialog(aClass);
94 dialog.show();