update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / turnRefsToSuper / TurnRefsToSuperHandler.java
blob72c5d3b59f7a29ad30dd8f7493b55a0d5e06add4
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.
17 /**
18 * created at Oct 25, 2001
19 * @author Jeka
21 package com.intellij.refactoring.turnRefsToSuper;
23 import com.intellij.openapi.actionSystem.DataContext;
24 import com.intellij.openapi.actionSystem.PlatformDataKeys;
25 import com.intellij.openapi.editor.Editor;
26 import com.intellij.openapi.editor.ScrollType;
27 import com.intellij.openapi.project.Project;
28 import com.intellij.psi.PsiAnonymousClass;
29 import com.intellij.psi.PsiClass;
30 import com.intellij.psi.PsiElement;
31 import com.intellij.psi.PsiFile;
32 import com.intellij.refactoring.HelpID;
33 import com.intellij.refactoring.RefactoringActionHandler;
34 import com.intellij.refactoring.RefactoringBundle;
35 import com.intellij.refactoring.util.CommonRefactoringUtil;
36 import com.intellij.refactoring.util.RefactoringHierarchyUtil;
37 import org.jetbrains.annotations.NotNull;
39 import java.util.ArrayList;
41 public class TurnRefsToSuperHandler implements RefactoringActionHandler {
42 public static final String REFACTORING_NAME = RefactoringBundle.message("use.interface.where.possible.title");
45 public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
46 int offset = editor.getCaretModel().getOffset();
47 editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
48 PsiElement element = file.findElementAt(offset);
49 while (true) {
50 if (element == null || element instanceof PsiFile) {
51 String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.wrong.caret.position.class"));
52 CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.TURN_REFS_TO_SUPER);
53 return;
55 if (element instanceof PsiClass && !(element instanceof PsiAnonymousClass)) {
56 invoke(project, new PsiElement[]{element}, dataContext);
57 return;
59 element = element.getParent();
63 public void invoke(@NotNull final Project project, @NotNull PsiElement[] elements, DataContext dataContext) {
64 if (elements.length != 1) return;
66 PsiClass subClass = (PsiClass) elements[0];
68 ArrayList basesList = RefactoringHierarchyUtil.createBasesList(subClass, true, true);
70 if (basesList.isEmpty()) {
71 String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("interface.does.not.have.base.interfaces", subClass.getQualifiedName()));
72 Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
73 CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.TURN_REFS_TO_SUPER);
74 return;
77 new TurnRefsToSuperDialog(project, subClass, basesList).show();