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 Nov 21, 2001
21 package com
.intellij
.refactoring
.move
.moveMembers
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import com
.intellij
.psi
.*;
25 import com
.intellij
.psi
.util
.PsiFormatUtil
;
26 import com
.intellij
.refactoring
.HelpID
;
27 import com
.intellij
.refactoring
.RefactoringBundle
;
28 import com
.intellij
.refactoring
.move
.MoveCallback
;
29 import com
.intellij
.refactoring
.util
.CommonRefactoringUtil
;
31 import java
.util
.HashSet
;
34 public class MoveMembersImpl
{
35 public static final String REFACTORING_NAME
= RefactoringBundle
.message("move.members.title");
38 * element should be either not anonymous PsiClass whose members should be moved
39 * or PsiMethod of a non-anonymous PsiClass
40 * or PsiField of a non-anonymous PsiClass
43 public static void doMove(final Project project
, PsiElement
[] elements
, PsiElement targetContainer
, MoveCallback moveCallback
) {
44 if (elements
.length
== 0) {
48 final PsiClass sourceClass
;
49 final PsiElement first
= elements
[0];
50 if (first
instanceof PsiMember
&& ((PsiMember
)first
).getContainingClass() != null) {
51 sourceClass
= ((PsiMember
)first
).getContainingClass();
56 final Set
<PsiMember
> preselectMembers
= new HashSet
<PsiMember
>();
57 for (PsiElement element
: elements
) {
58 if (element
instanceof PsiMember
&& !sourceClass
.equals(((PsiMember
)element
).getContainingClass())) {
59 String message
= RefactoringBundle
.getCannotRefactorMessage(
60 RefactoringBundle
.message("members.to.be.moved.should.belong.to.the.same.class"));
61 CommonRefactoringUtil
.showErrorMessage(REFACTORING_NAME
, message
, HelpID
.MOVE_MEMBERS
, project
);
64 if (element
instanceof PsiField
) {
65 PsiField field
= (PsiField
)element
;
66 if (!field
.hasModifierProperty(PsiModifier
.STATIC
)) {
67 String fieldName
= PsiFormatUtil
.formatVariable(
69 PsiFormatUtil
.SHOW_NAME
| PsiFormatUtil
.SHOW_TYPE
| PsiFormatUtil
.TYPE_AFTER
,
70 PsiSubstitutor
.EMPTY
);
71 String message
= RefactoringBundle
.message("field.0.is.not.static", fieldName
,
73 CommonRefactoringUtil
.showErrorMessage(REFACTORING_NAME
, message
, HelpID
.MOVE_MEMBERS
, project
);
76 preselectMembers
.add(field
);
78 else if (element
instanceof PsiMethod
) {
79 PsiMethod method
= (PsiMethod
)element
;
80 String methodName
= PsiFormatUtil
.formatMethod(
82 PsiSubstitutor
.EMPTY
, PsiFormatUtil
.SHOW_NAME
| PsiFormatUtil
.SHOW_PARAMETERS
,
83 PsiFormatUtil
.SHOW_TYPE
85 if (method
.isConstructor()) {
86 String message
= RefactoringBundle
.message("0.refactoring.cannot.be.applied.to.constructors", REFACTORING_NAME
);
87 CommonRefactoringUtil
.showErrorMessage(REFACTORING_NAME
, message
, HelpID
.MOVE_MEMBERS
, project
);
90 if (!method
.hasModifierProperty(PsiModifier
.STATIC
)) {
91 String message
= RefactoringBundle
.message("method.0.is.not.static", methodName
,
93 CommonRefactoringUtil
.showErrorMessage(REFACTORING_NAME
, message
, HelpID
.MOVE_MEMBERS
, project
);
96 preselectMembers
.add(method
);
98 else if (element
instanceof PsiClass
) {
99 PsiClass aClass
= (PsiClass
)element
;
100 if (!aClass
.hasModifierProperty(PsiModifier
.STATIC
)) {
101 String message
= RefactoringBundle
.message("inner.class.0.is.not.static", aClass
.getQualifiedName(),
103 CommonRefactoringUtil
.showErrorMessage(REFACTORING_NAME
, message
, HelpID
.MOVE_MEMBERS
, project
);
106 preselectMembers
.add(aClass
);
110 if (!CommonRefactoringUtil
.checkReadOnlyStatus(project
, sourceClass
)) return;
112 final PsiClass initialTargerClass
= targetContainer
instanceof PsiClass?
(PsiClass
) targetContainer
: null;
114 MoveMembersDialog dialog
= new MoveMembersDialog(