update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / move / moveClassesOrPackages / MoveJavaFileHandler.java
blob3810b2a29d0753b9f344171ac1c3aa1a9737ad2f
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.
18 * User: anna
19 * Date: 05-Aug-2009
21 package com.intellij.refactoring.move.moveClassesOrPackages;
23 import com.intellij.codeInsight.ChangeContextUtil;
24 import com.intellij.codeInsight.daemon.impl.CollectHighlightsUtil;
25 import com.intellij.openapi.util.text.StringUtil;
26 import com.intellij.psi.*;
27 import com.intellij.refactoring.move.moveFilesOrDirectories.MoveFileHandler;
28 import com.intellij.refactoring.util.MoveRenameUsageInfo;
29 import com.intellij.usageView.UsageInfo;
30 import com.intellij.util.IncorrectOperationException;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Map;
37 public class MoveJavaFileHandler extends MoveFileHandler {
38 @Override
39 public boolean canProcessElement(PsiFile element) {
40 return element instanceof PsiJavaFile && !JspPsiUtil.isInJspFile(element) && !CollectHighlightsUtil.isOutsideSourceRootJavaFile(element);
43 @Override
44 public void prepareMovedFile(PsiFile file, PsiDirectory moveDestination, Map<PsiElement, PsiElement> oldToNewMap) {
45 final PsiJavaFile javaFile = (PsiJavaFile)file;
46 ChangeContextUtil.encodeContextInfo(javaFile, true);
47 for (PsiClass psiClass : javaFile.getClasses()) {
48 oldToNewMap.put(psiClass, MoveClassesOrPackagesUtil.doMoveClass(psiClass, moveDestination));
52 public List<UsageInfo> findUsages(PsiFile psiFile, PsiDirectory newParent, boolean searchInComments, boolean searchInNonJavaFiles) {
53 final List<UsageInfo> result = new ArrayList<UsageInfo>();
54 final PsiPackage newParentPackage = JavaDirectoryService.getInstance().getPackage(newParent);
55 final String qualifiedName = newParentPackage == null ? "" : newParentPackage.getQualifiedName();
56 for (PsiClass aClass : ((PsiJavaFile)psiFile).getClasses()) {
57 Collections.addAll(result, MoveClassesOrPackagesUtil.findUsages(aClass, searchInComments, searchInNonJavaFiles,
58 StringUtil.getQualifiedName(qualifiedName, aClass.getName())));
60 return result.isEmpty() ? null : result;
63 @Override
64 public void retargetUsages(List<UsageInfo> usageInfos, Map<PsiElement, PsiElement> oldToNewMap) {
65 for (UsageInfo usage : usageInfos) {
66 if (usage instanceof MoveRenameUsageInfo) {
67 final MoveRenameUsageInfo moveRenameUsage = (MoveRenameUsageInfo)usage;
68 final PsiElement oldElement = moveRenameUsage.getReferencedElement();
69 final PsiElement newElement = oldToNewMap.get(oldElement);
70 final PsiReference reference = moveRenameUsage.getReference();
71 if (reference != null) {
72 reference.bindToElement(newElement);
78 @Override
79 public void updateMovedFile(PsiFile file) throws IncorrectOperationException {
80 ChangeContextUtil.decodeContextInfo(file, null, null);