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
;
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];
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
;
50 if (tempExpr
instanceof PsiExpression
) {
51 invokeImpl(project
, (PsiExpression
)tempExpr
, editor
);
53 else if(tempExpr
instanceof PsiLocalVariable
) {
54 invokeImpl(project
, (PsiLocalVariable
)tempExpr
, editor
);
57 LOG
.assertTrue(false, "elements[0] should be PsiExpression or PsiLocalVariable");
64 * @param editor editor to highlight stuff in. Should accept <code>null</code>
67 protected abstract boolean invokeImpl(Project project
, PsiExpression tempExpr
,
72 * @param localVariable
73 * @param editor editor to highlight stuff in. Should accept <code>null</code>
76 protected abstract boolean invokeImpl(Project project
, PsiLocalVariable localVariable
,