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
.memberPushDown
;
18 import com
.intellij
.openapi
.actionSystem
.DataContext
;
19 import com
.intellij
.openapi
.editor
.Editor
;
20 import com
.intellij
.openapi
.editor
.ScrollType
;
21 import com
.intellij
.openapi
.project
.Project
;
22 import com
.intellij
.psi
.*;
23 import com
.intellij
.psi
.impl
.source
.jsp
.jspJava
.JspClass
;
24 import com
.intellij
.refactoring
.HelpID
;
25 import com
.intellij
.refactoring
.RefactoringActionHandler
;
26 import com
.intellij
.refactoring
.RefactoringBundle
;
27 import com
.intellij
.refactoring
.classMembers
.MemberInfoBase
;
28 import com
.intellij
.refactoring
.lang
.ElementsHandler
;
29 import com
.intellij
.refactoring
.util
.CommonRefactoringUtil
;
30 import com
.intellij
.refactoring
.util
.RefactoringMessageUtil
;
31 import com
.intellij
.refactoring
.util
.classMembers
.MemberInfo
;
32 import com
.intellij
.refactoring
.util
.classMembers
.MemberInfoStorage
;
33 import org
.jetbrains
.annotations
.NotNull
;
35 import java
.util
.List
;
40 public class JavaPushDownHandler
implements RefactoringActionHandler
, ElementsHandler
{
41 public static final String REFACTORING_NAME
= RefactoringBundle
.message("push.members.down.title");
43 public void invoke(@NotNull Project project
, Editor editor
, PsiFile file
, DataContext dataContext
) {
44 int offset
= editor
.getCaretModel().getOffset();
45 editor
.getScrollingModel().scrollToCaret(ScrollType
.MAKE_VISIBLE
);
46 PsiElement element
= file
.findElementAt(offset
);
49 if (element
== null || element
instanceof PsiFile
) {
50 String message
= RefactoringBundle
.getCannotRefactorMessage(
51 RefactoringBundle
.message("the.caret.should.be.positioned.inside.a.class.to.push.members.from"));
52 CommonRefactoringUtil
.showErrorHint(project
, editor
, message
, REFACTORING_NAME
, HelpID
.MEMBERS_PUSH_DOWN
);
56 if (element
instanceof PsiClass
|| element
instanceof PsiField
|| element
instanceof PsiMethod
) {
57 if (element
instanceof JspClass
) {
58 RefactoringMessageUtil
.showNotSupportedForJspClassesError(project
, editor
, REFACTORING_NAME
, HelpID
.MEMBERS_PUSH_DOWN
);
61 invoke(project
, new PsiElement
[]{element
}, dataContext
);
64 element
= element
.getParent();
68 public void invoke(@NotNull final Project project
, @NotNull PsiElement
[] elements
, DataContext dataContext
) {
69 if (elements
.length
!= 1) return;
71 PsiElement element
= elements
[0];
73 PsiElement aMember
= null;
75 if (element
instanceof PsiClass
) {
76 aClass
= (PsiClass
) element
;
77 } else if (element
instanceof PsiMethod
) {
78 aClass
= ((PsiMethod
) element
).getContainingClass();
80 } else if (element
instanceof PsiField
) {
81 aClass
= ((PsiField
) element
).getContainingClass();
86 if (!CommonRefactoringUtil
.checkReadOnlyStatus(project
, aClass
)) return;
87 MemberInfoStorage memberInfoStorage
= new MemberInfoStorage(aClass
, new MemberInfo
.Filter
<PsiMember
>() {
88 public boolean includeMember(PsiMember element
) {
92 List
<MemberInfo
> members
= memberInfoStorage
.getClassMemberInfos(aClass
);
93 PsiManager manager
= aClass
.getManager();
95 for (MemberInfoBase
<PsiMember
> member
: members
) {
96 if (manager
.areElementsEquivalent(member
.getMember(), aMember
)) {
97 member
.setChecked(true);
101 PushDownDialog dialog
= new PushDownDialog(
103 members
.toArray(new MemberInfo
[members
.size()]),
108 public boolean isEnabledOnElements(PsiElement
[] elements
) {
110 if (elements.length == 1) {
111 return elements[0] instanceof PsiClass || elements[0] instanceof PsiField || elements[0] instanceof PsiMethod;
113 else if (elements.length > 1){
114 for (int idx = 0; idx < elements.length; idx++) {
115 PsiElement element = elements[idx];
116 if (!(element instanceof PsiField || element instanceof PsiMethod)) return false;
122 // todo: multiple selection etc
123 return elements
.length
== 1 && elements
[0] instanceof PsiClass
;