update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / refactoring / safeDelete / SafeDeleteHandler.java
bloba25d7116fae3aae18cbb682acdb16b1bcd4580fd
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.
17 package com.intellij.refactoring.safeDelete;
19 import com.intellij.openapi.actionSystem.DataContext;
20 import com.intellij.openapi.actionSystem.LangDataKeys;
21 import com.intellij.openapi.application.ApplicationManager;
22 import com.intellij.openapi.editor.Editor;
23 import com.intellij.openapi.editor.ScrollType;
24 import com.intellij.openapi.extensions.Extensions;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.ui.DialogWrapper;
27 import com.intellij.psi.PsiElement;
28 import com.intellij.psi.PsiFile;
29 import com.intellij.psi.util.PsiTreeUtil;
30 import com.intellij.refactoring.*;
31 import com.intellij.refactoring.util.CommonRefactoringUtil;
32 import com.intellij.util.containers.HashSet;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.Set;
40 /**
41 * @author dsl
43 public class SafeDeleteHandler implements RefactoringActionHandler {
44 public static final String REFACTORING_NAME = RefactoringBundle.message("safe.delete.title");
46 public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
47 PsiElement element = LangDataKeys.PSI_ELEMENT.getData(dataContext);
48 editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
49 if (element == null || !SafeDeleteProcessor.validElement(element)) {
50 String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("is.not.supported.in.the.current.context", REFACTORING_NAME));
51 CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, "refactoring.safeDelete");
52 return;
54 invoke(project, new PsiElement[]{element}, dataContext);
57 public void invoke(@NotNull final Project project, @NotNull PsiElement[] elements, DataContext dataContext) {
58 invoke(project, elements, true);
61 public static void invoke(final Project project, PsiElement[] elements, boolean checkSuperMethods) {
62 invoke(project, elements, checkSuperMethods, null);
65 public static void invoke(final Project project, PsiElement[] elements, boolean checkSuperMethods, @Nullable final Runnable successRunnable) {
66 for (PsiElement element : elements) {
67 if (!SafeDeleteProcessor.validElement(element)) {
68 return;
71 final PsiElement[] temptoDelete = PsiTreeUtil.filterAncestors(elements);
72 Set<PsiElement> elementsSet = new HashSet<PsiElement>(Arrays.asList(temptoDelete));
73 Set<PsiElement> fullElementsSet = new HashSet<PsiElement>();
75 if (checkSuperMethods) {
76 for (PsiElement element : temptoDelete) {
77 boolean found = false;
78 for(SafeDeleteProcessorDelegate delegate: Extensions.getExtensions(SafeDeleteProcessorDelegate.EP_NAME)) {
79 if (delegate.handlesElement(element)) {
80 found = true;
81 Collection<? extends PsiElement> addElements = delegate.getElementsToSearch(element, elementsSet);
82 if (addElements == null) return;
83 fullElementsSet.addAll(addElements);
84 break;
87 if (!found) {
88 fullElementsSet.add(element);
91 } else {
92 fullElementsSet.addAll(Arrays.asList(temptoDelete));
95 if (!CommonRefactoringUtil.checkReadOnlyStatusRecursively(project, fullElementsSet)) return;
97 final PsiElement[] elementsToDelete = fullElementsSet.toArray(new PsiElement[fullElementsSet.size()]);
99 if (ApplicationManager.getApplication().isUnitTestMode()) {
100 RefactoringSettings settings = RefactoringSettings.getInstance();
101 SafeDeleteProcessor.createInstance(project, null, elementsToDelete, settings.SAFE_DELETE_SEARCH_IN_COMMENTS,
102 settings.SAFE_DELETE_SEARCH_IN_NON_JAVA, true).run();
104 else {
105 final SafeDeleteDialog.Callback callback = new SafeDeleteDialog.Callback() {
106 public void run(final SafeDeleteDialog dialog) {
107 SafeDeleteProcessor.createInstance(project, new Runnable() {
108 public void run() {
109 if (successRunnable != null) {
110 successRunnable.run();
112 dialog.close(DialogWrapper.CANCEL_EXIT_CODE);
114 }, elementsToDelete, dialog.isSearchInComments(), dialog.isSearchForTextOccurences(), true).run();
119 SafeDeleteDialog dialog = new SafeDeleteDialog(project, elementsToDelete, callback);
120 dialog.show();