ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / migration / MigrationProcessor.java
blob593cc62eebc28299c76a8d45073802226c6da65f
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.
16 package com.intellij.refactoring.migration;
18 import com.intellij.history.LocalHistory;
19 import com.intellij.history.LocalHistoryAction;
20 import com.intellij.openapi.diagnostic.Logger;
21 import com.intellij.openapi.extensions.Extensions;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.openapi.ui.Messages;
24 import com.intellij.openapi.util.Ref;
25 import com.intellij.psi.JavaPsiFacade;
26 import com.intellij.psi.PsiElement;
27 import com.intellij.psi.PsiManager;
28 import com.intellij.psi.PsiMigration;
29 import com.intellij.refactoring.BaseRefactoringProcessor;
30 import com.intellij.refactoring.RefactoringBundle;
31 import com.intellij.refactoring.RefactoringHelper;
32 import com.intellij.usageView.UsageInfo;
33 import com.intellij.usageView.UsageViewDescriptor;
34 import org.jetbrains.annotations.NotNull;
36 import java.util.ArrayList;
38 /**
39 * @author ven
41 class MigrationProcessor extends BaseRefactoringProcessor {
42 private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.migration.MigrationProcessor");
43 private final MigrationMap myMigrationMap;
44 private static final String REFACTORING_NAME = RefactoringBundle.message("migration.title");
45 private PsiMigration myPsiMigration;
47 public MigrationProcessor(Project project, MigrationMap migrationMap) {
48 super(project);
49 myMigrationMap = migrationMap;
50 myPsiMigration = startMigration(PsiManager.getInstance(project));
53 protected UsageViewDescriptor createUsageViewDescriptor(UsageInfo[] usages) {
54 return new MigrationUsagesViewDescriptor(myMigrationMap, false);
57 private PsiMigration startMigration(final PsiManager psiManager) {
58 final PsiMigration migration = JavaPsiFacade.getInstance(psiManager.getProject()).startMigration();
59 findOrCreateEntries(psiManager, migration);
60 return migration;
63 private void findOrCreateEntries(final PsiManager psiManager, final PsiMigration migration) {
64 for (int i = 0; i < myMigrationMap.getEntryCount(); i++) {
65 MigrationMapEntry entry = myMigrationMap.getEntryAt(i);
66 if (entry.getType() == MigrationMapEntry.PACKAGE) {
67 MigrationUtil.findOrCreatePackage(psiManager, migration, entry.getOldName());
69 else {
70 MigrationUtil.findOrCreateClass(psiManager, migration, entry.getOldName());
75 @NotNull
76 protected UsageInfo[] findUsages() {
77 ArrayList<UsageInfo> usagesVector = new ArrayList<UsageInfo>();
78 PsiManager psiManager = PsiManager.getInstance(myProject);
79 try {
80 if (myMigrationMap == null) {
81 return UsageInfo.EMPTY_ARRAY;
83 for (int i = 0; i < myMigrationMap.getEntryCount(); i++) {
84 MigrationMapEntry entry = myMigrationMap.getEntryAt(i);
85 UsageInfo[] usages;
86 if (entry.getType() == MigrationMapEntry.PACKAGE) {
87 usages = MigrationUtil.findPackageUsages(psiManager, myPsiMigration, entry.getOldName());
89 else {
90 usages = MigrationUtil.findClassUsages(psiManager, myPsiMigration, entry.getOldName());
93 for (UsageInfo usage : usages) {
94 usagesVector.add(new MigrationUsageInfo(usage, entry));
98 finally {
99 myPsiMigration.finish();
100 myPsiMigration = null;
102 return usagesVector.toArray(new MigrationUsageInfo[usagesVector.size()]);
105 protected void refreshElements(PsiElement[] elements) {
108 protected boolean preprocessUsages(Ref<UsageInfo[]> refUsages) {
109 if (refUsages.get().length == 0) {
110 Messages.showInfoMessage(myProject, RefactoringBundle.message("migration.no.usages.found.in.the.project"), REFACTORING_NAME);
111 return false;
113 setPreviewUsages(true);
114 return true;
117 protected void performRefactoring(UsageInfo[] usages) {
118 PsiManager psiManager = PsiManager.getInstance(myProject);
119 final PsiMigration psiMigration = JavaPsiFacade.getInstance(psiManager.getProject()).startMigration();
120 LocalHistoryAction a = LocalHistory.startAction(myProject, getCommandName());
122 try {
123 for (int i = 0; i < myMigrationMap.getEntryCount(); i++) {
124 MigrationMapEntry entry = myMigrationMap.getEntryAt(i);
125 if (entry.getType() == MigrationMapEntry.PACKAGE) {
126 MigrationUtil.doPackageMigration(psiManager, psiMigration, entry.getNewName(), usages);
128 if (entry.getType() == MigrationMapEntry.CLASS) {
129 MigrationUtil.doClassMigration(psiManager, psiMigration, entry.getNewName(), usages);
133 for(RefactoringHelper helper: Extensions.getExtensions(RefactoringHelper.EP_NAME)) {
134 Object preparedData = helper.prepareOperation(usages);
135 //noinspection unchecked
136 helper.performOperation(myProject, preparedData);
139 finally {
140 a.finish();
141 psiMigration.finish();
146 protected String getCommandName() {
147 return REFACTORING_NAME;
150 public static class MigrationUsageInfo extends UsageInfo {
151 public MigrationMapEntry mapEntry;
153 public MigrationUsageInfo(UsageInfo info, MigrationMapEntry mapEntry) {
154 super(info.getElement(), info.startOffset, info.endOffset);
155 this.mapEntry = mapEntry;