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
.move
.moveClassesOrPackages
;
18 import com
.intellij
.psi
.*;
19 import com
.intellij
.psi
.impl
.source
.jsp
.jspJava
.JspClass
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.openapi
.actionSystem
.DataContext
;
22 import com
.intellij
.openapi
.actionSystem
.ex
.DataConstantsEx
;
23 import com
.intellij
.openapi
.editor
.Editor
;
24 import com
.intellij
.codeInsight
.daemon
.impl
.CollectHighlightsUtil
;
25 import org
.jetbrains
.annotations
.Nullable
;
27 public class MoveClassesHandler
extends MoveClassesOrPackagesHandlerBase
{
28 public boolean canMove(final PsiElement
[] elements
, @Nullable final PsiElement targetContainer
) {
29 for(PsiElement element
: elements
) {
30 if (element
instanceof JspClass
) return false;
31 if (!(element
instanceof PsiClass
)) return false;
32 if (!(element
.getParent() instanceof PsiFile
)) return false;
33 if (CollectHighlightsUtil
.isOutsideSourceRootJavaFile((PsiFile
)element
.getParent())) return false;
35 return super.canMove(elements
, targetContainer
);
38 public boolean isValidTarget(final PsiElement psiElement
) {
39 return psiElement
instanceof PsiClass
||
40 MovePackagesHandler
.isPackageOrDirectory(psiElement
);
43 public boolean tryToMove(final PsiElement element
, final Project project
, final DataContext dataContext
, final PsiReference reference
,
44 final Editor editor
) {
45 if (CollectHighlightsUtil
.isOutsideSourceRootJavaFile(element
.getContainingFile())) return false;
46 if (isReferenceInAnonymousClass(reference
)) return false;
48 if (element
instanceof PsiClass
&& !(element
instanceof PsiAnonymousClass
) && element
.getParent() instanceof PsiFile
) {
49 MoveClassesOrPackagesImpl
.doMove(project
, new PsiElement
[]{element
},
50 (PsiElement
)dataContext
.getData(DataConstantsEx
.TARGET_PSI_ELEMENT
), null);
56 public static boolean isReferenceInAnonymousClass(@Nullable final PsiReference reference
) {
57 if (reference
instanceof PsiJavaCodeReferenceElement
&&
58 ((PsiJavaCodeReferenceElement
)reference
).getParent() instanceof PsiAnonymousClass
) {