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
;
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");
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
)) {
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
)) {
81 Collection
<?
extends PsiElement
> addElements
= delegate
.getElementsToSearch(element
, elementsSet
);
82 if (addElements
== null) return;
83 fullElementsSet
.addAll(addElements
);
88 fullElementsSet
.add(element
);
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();
105 final SafeDeleteDialog
.Callback callback
= new SafeDeleteDialog
.Callback() {
106 public void run(final SafeDeleteDialog dialog
) {
107 SafeDeleteProcessor
.createInstance(project
, new Runnable() {
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
);