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.
18 * created at Oct 25, 2001
21 package com
.intellij
.refactoring
.extractSuperclass
;
23 import com
.intellij
.history
.LocalHistory
;
24 import com
.intellij
.history
.LocalHistoryAction
;
25 import com
.intellij
.openapi
.actionSystem
.DataContext
;
26 import com
.intellij
.openapi
.actionSystem
.PlatformDataKeys
;
27 import com
.intellij
.openapi
.application
.ApplicationManager
;
28 import com
.intellij
.openapi
.command
.CommandProcessor
;
29 import com
.intellij
.openapi
.diagnostic
.Logger
;
30 import com
.intellij
.openapi
.editor
.Editor
;
31 import com
.intellij
.openapi
.editor
.ScrollType
;
32 import com
.intellij
.openapi
.project
.Project
;
33 import com
.intellij
.openapi
.ui
.DialogWrapper
;
34 import com
.intellij
.psi
.*;
35 import com
.intellij
.refactoring
.HelpID
;
36 import com
.intellij
.refactoring
.RefactoringActionHandler
;
37 import com
.intellij
.refactoring
.RefactoringBundle
;
38 import com
.intellij
.refactoring
.extractInterface
.ExtractClassUtil
;
39 import com
.intellij
.refactoring
.lang
.ElementsHandler
;
40 import com
.intellij
.refactoring
.memberPullUp
.PullUpConflictsUtil
;
41 import com
.intellij
.refactoring
.ui
.ConflictsDialog
;
42 import com
.intellij
.refactoring
.util
.CommonRefactoringUtil
;
43 import com
.intellij
.refactoring
.util
.DocCommentPolicy
;
44 import com
.intellij
.refactoring
.util
.classMembers
.MemberInfo
;
45 import com
.intellij
.usageView
.UsageViewUtil
;
46 import com
.intellij
.util
.IncorrectOperationException
;
47 import com
.intellij
.util
.containers
.MultiMap
;
48 import org
.jetbrains
.annotations
.NotNull
;
50 import java
.util
.List
;
52 public class ExtractSuperclassHandler
implements RefactoringActionHandler
, ExtractSuperclassDialog
.Callback
, ElementsHandler
{
53 private static final Logger LOG
= Logger
.getInstance("#com.intellij.refactoring.extractSuperclass.ExtractSuperclassHandler");
55 public static final String REFACTORING_NAME
= RefactoringBundle
.message("extract.superclass.title");
57 private PsiClass mySubclass
;
58 private Project myProject
;
61 public void invoke(@NotNull Project project
, Editor editor
, PsiFile file
, DataContext dataContext
) {
62 editor
.getScrollingModel().scrollToCaret(ScrollType
.MAKE_VISIBLE
);
63 int offset
= editor
.getCaretModel().getOffset();
64 PsiElement element
= file
.findElementAt(offset
);
66 if (element
== null || element
instanceof PsiFile
) {
67 String message
= RefactoringBundle
.getCannotRefactorMessage(RefactoringBundle
.message("error.wrong.caret.position.class"));
68 CommonRefactoringUtil
.showErrorHint(project
, editor
, message
, REFACTORING_NAME
, HelpID
.EXTRACT_SUPERCLASS
);
71 if (element
instanceof PsiClass
&& !(element
instanceof PsiAnonymousClass
)) {
72 invoke(project
, new PsiElement
[]{element
}, dataContext
);
75 element
= element
.getParent();
79 public void invoke(@NotNull final Project project
, @NotNull PsiElement
[] elements
, DataContext dataContext
) {
80 if (elements
.length
!= 1) return;
83 mySubclass
= (PsiClass
)elements
[0];
85 if (!CommonRefactoringUtil
.checkReadOnlyStatus(project
, mySubclass
)) return;
87 Editor editor
= PlatformDataKeys
.EDITOR
.getData(dataContext
);
88 if (mySubclass
.isInterface()) {
90 RefactoringBundle
.getCannotRefactorMessage(RefactoringBundle
.message("superclass.cannot.be.extracted.from.an.interface"));
91 CommonRefactoringUtil
.showErrorHint(project
, editor
, message
, REFACTORING_NAME
, HelpID
.EXTRACT_SUPERCLASS
);
95 if (mySubclass
.isEnum()) {
96 String message
= RefactoringBundle
.getCannotRefactorMessage(RefactoringBundle
.message("superclass.cannot.be.extracted.from.an.enum"));
97 CommonRefactoringUtil
.showErrorHint(project
, editor
, message
, REFACTORING_NAME
, HelpID
.EXTRACT_SUPERCLASS
);
102 final List
<MemberInfo
> memberInfos
= MemberInfo
.extractClassMembers(mySubclass
, new MemberInfo
.Filter
<PsiMember
>() {
103 public boolean includeMember(PsiMember element
) {
108 final ExtractSuperclassDialog dialog
=
109 new ExtractSuperclassDialog(project
, mySubclass
, memberInfos
, ExtractSuperclassHandler
.this);
111 if (!dialog
.isOK() || !dialog
.isExtractSuperclass()) return;
113 CommandProcessor
.getInstance().executeCommand(myProject
, new Runnable() {
115 final Runnable action
= new Runnable() {
117 doRefactoring(project
, mySubclass
, dialog
);
120 ApplicationManager
.getApplication().runWriteAction(action
);
122 }, REFACTORING_NAME
, null);
126 public boolean checkConflicts(ExtractSuperclassDialog dialog
) {
127 final MemberInfo
[] infos
= dialog
.getSelectedMemberInfos();
128 final PsiDirectory targetDirectory
= dialog
.getTargetDirectory();
129 final PsiPackage targetPackage
;
130 if (targetDirectory
!= null) {
131 targetPackage
= JavaDirectoryService
.getInstance().getPackage(targetDirectory
);
134 targetPackage
= null;
136 MultiMap
<PsiElement
,String
> conflicts
=
137 PullUpConflictsUtil
.checkConflicts(infos
, mySubclass
, null, targetPackage
, targetDirectory
, dialog
.getContainmentVerifier());
138 if (!conflicts
.isEmpty()) {
139 ConflictsDialog conflictsDialog
= new ConflictsDialog(myProject
, conflicts
);
140 conflictsDialog
.show();
141 final boolean ok
= conflictsDialog
.isOK();
142 if (!ok
&& conflictsDialog
.isShowConflicts()) dialog
.close(DialogWrapper
.CANCEL_EXIT_CODE
);
148 // invoked inside Command and Atomic action
149 private void doRefactoring(final Project project
, final PsiClass subclass
, final ExtractSuperclassDialog dialog
) {
150 final String superclassName
= dialog
.getExtractedSuperName();
151 final PsiDirectory targetDirectory
= dialog
.getTargetDirectory();
152 final MemberInfo
[] selectedMemberInfos
= dialog
.getSelectedMemberInfos();
153 final DocCommentPolicy javaDocPolicy
= new DocCommentPolicy(dialog
.getJavaDocPolicy());
154 LocalHistoryAction a
= LocalHistory
.startAction(myProject
, getCommandName(subclass
, superclassName
));
156 PsiClass superclass
= null;
160 ExtractSuperClassUtil
.extractSuperClass(project
, targetDirectory
, superclassName
, subclass
, selectedMemberInfos
, javaDocPolicy
);
166 // ask whether to search references to subclass and turn them into refs to superclass if possible
167 if (superclass
!= null) {
168 ExtractClassUtil
.askAndTurnRefsToSuper(project
, subclass
, superclass
);
171 catch (IncorrectOperationException e
) {
177 private String
getCommandName(final PsiClass subclass
, String newName
) {
178 return RefactoringBundle
.message("extract.superclass.command.name", newName
, UsageViewUtil
.getDescriptiveName(subclass
));
181 public boolean isEnabledOnElements(PsiElement
[] elements
) {
182 return elements
.length
== 1 && elements
[0] instanceof PsiClass
&& !((PsiClass
) elements
[0]).isInterface()
183 &&!((PsiClass
)elements
[0]).isEnum();