update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / IntroduceHandlerBase.java
blob3216086a31ca298e3a1375af4b969c841b5c5f7f
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;
18 import com.intellij.openapi.actionSystem.DataContext;
19 import com.intellij.openapi.actionSystem.PlatformDataKeys;
20 import com.intellij.openapi.diagnostic.Logger;
21 import com.intellij.openapi.editor.Editor;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.psi.*;
24 import com.intellij.psi.util.PsiTreeUtil;
25 import org.jetbrains.annotations.NotNull;
27 /**
28 * @author dsl
30 public abstract class IntroduceHandlerBase {
31 private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.IntroduceHandlerBase");
33 public void invoke(@NotNull Project project, @NotNull PsiElement[] elements, DataContext dataContext) {
34 LOG.assertTrue(elements.length >= 1 && elements[0] instanceof PsiExpression, "incorrect invoke() parameters");
35 final PsiElement tempExpr = elements[0];
36 final Editor editor;
37 if (dataContext != null) {
38 final Editor editorFromDC = PlatformDataKeys.EDITOR.getData(dataContext);
39 final PsiFile cachedPsiFile = PsiDocumentManager.getInstance(project).getCachedPsiFile(editorFromDC.getDocument());
40 if (cachedPsiFile != null && PsiTreeUtil.isAncestor(cachedPsiFile, tempExpr, false)) {
41 editor = editorFromDC;
43 else {
44 editor = null;
47 else {
48 editor = null;
50 if (tempExpr instanceof PsiExpression) {
51 invokeImpl(project, (PsiExpression)tempExpr, editor);
53 else if(tempExpr instanceof PsiLocalVariable) {
54 invokeImpl(project, (PsiLocalVariable)tempExpr, editor);
56 else {
57 LOG.assertTrue(false, "elements[0] should be PsiExpression or PsiLocalVariable");
61 /**
62 * @param project
63 * @param tempExpr
64 * @param editor editor to highlight stuff in. Should accept <code>null</code>
65 * @return
67 protected abstract boolean invokeImpl(Project project, PsiExpression tempExpr,
68 Editor editor);
70 /**
71 * @param project
72 * @param localVariable
73 * @param editor editor to highlight stuff in. Should accept <code>null</code>
74 * @return
76 protected abstract boolean invokeImpl(Project project, PsiLocalVariable localVariable,
77 Editor editor);