836f0b6abe23466536b162dc59a1ae76d8f25c48
[idea/community.git] / python / src / com / jetbrains / python / refactoring / PyBaseRefactoringAction.java
1 /*
2  * Copyright 2000-2015 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.jetbrains.python.refactoring;
17
18 import com.intellij.lang.Language;
19 import com.intellij.openapi.actionSystem.DataContext;
20 import com.intellij.openapi.editor.Editor;
21 import com.intellij.psi.PsiElement;
22 import com.intellij.psi.PsiFile;
23 import com.intellij.refactoring.actions.BaseRefactoringAction;
24 import com.jetbrains.python.PythonLanguage;
25 import org.jetbrains.annotations.NotNull;
26
27 /**
28  * @author Mikhail Golubev
29  */
30 public abstract class PyBaseRefactoringAction extends BaseRefactoringAction {
31
32   protected abstract boolean isEnabledOnElementInsideEditor(@NotNull PsiElement element,
33                                                             @NotNull Editor editor,
34                                                             @NotNull PsiFile file,
35                                                             @NotNull DataContext context);
36
37   @Override
38   protected final boolean isAvailableOnElementInEditorAndFile(@NotNull PsiElement element,
39                                                               @NotNull Editor editor,
40                                                               @NotNull PsiFile file,
41                                                               @NotNull DataContext context) {
42     return isEnabledOnElementInsideEditor(element, editor, file, context);
43   }
44
45   protected abstract boolean isEnabledOnElementsOutsideEditor(@NotNull PsiElement[] elements);
46
47   @Override
48   protected final boolean isEnabledOnElements(@NotNull PsiElement[] elements) {
49     return isEnabledOnElementsOutsideEditor(elements);
50   }
51
52   @Override
53   protected final boolean isAvailableForLanguage(Language language) {
54     return language.isKindOf(PythonLanguage.getInstance());
55   }
56
57   @Override
58   protected boolean isAvailableForFile(PsiFile file) {
59     return isAvailableForLanguage(file.getLanguage());
60   }
61 }