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.
20 package com
.intellij
.codeInsight
.generation
.actions
;
22 import com
.intellij
.codeInsight
.CodeInsightActionHandler
;
23 import com
.intellij
.codeInsight
.CodeInsightUtilBase
;
24 import com
.intellij
.codeInsight
.generation
.OverrideImplementUtil
;
25 import com
.intellij
.openapi
.project
.Project
;
26 import com
.intellij
.openapi
.editor
.Editor
;
27 import com
.intellij
.openapi
.editor
.ScrollType
;
28 import com
.intellij
.openapi
.diagnostic
.Logger
;
29 import com
.intellij
.psi
.*;
30 import com
.intellij
.psi
.util
.PsiTreeUtil
;
31 import com
.intellij
.util
.IncorrectOperationException
;
32 import org
.jetbrains
.annotations
.NotNull
;
34 import java
.util
.List
;
36 public class GenerateSuperMethodCallHandler
implements CodeInsightActionHandler
{
37 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInsight.generation.actions.GenerateSuperMethodCallHandler");
39 public void invoke(@NotNull Project project
, @NotNull Editor editor
, @NotNull PsiFile file
) {
40 PsiMethod method
= canInsertSuper(project
, editor
, file
);
42 PsiMethod template
= (PsiMethod
)method
.copy();
44 OverrideImplementUtil
.setupMethodBody(template
, method
, method
.getContainingClass());
45 PsiStatement superCall
= template
.getBody().getStatements()[0];
46 PsiCodeBlock body
= method
.getBody();
48 if (body
.getLBrace() == null) {
49 toGo
= body
.addBefore(superCall
, null);
52 toGo
= body
.addAfter(superCall
, body
.getLBrace());
54 toGo
= CodeInsightUtilBase
.forcePsiPostprocessAndRestoreElement(toGo
);
55 editor
.getCaretModel().moveToOffset(toGo
.getTextOffset());
56 editor
.getScrollingModel().scrollToCaret(ScrollType
.RELATIVE
);
58 catch (IncorrectOperationException e
) {
63 public boolean startInWriteAction() {
67 public static PsiMethod
canInsertSuper(Project project
, Editor editor
, PsiFile file
) {
68 PsiDocumentManager
.getInstance(project
).commitAllDocuments();
70 int offset
= editor
.getCaretModel().getOffset();
71 PsiElement element
= file
.findElementAt(offset
);
72 if (element
== null) return null;
73 PsiCodeBlock codeBlock
= PsiTreeUtil
.getParentOfType(element
, PsiCodeBlock
.class);
74 if (codeBlock
== null) return null;
75 if (!(codeBlock
.getParent() instanceof PsiMethod
)) return null;
76 PsiMethod method
= (PsiMethod
)codeBlock
.getParent();
77 List
<?
extends HierarchicalMethodSignature
> superSignatures
= method
.getHierarchicalMethodSignature().getSuperSignatures();
78 for (HierarchicalMethodSignature superSignature
: superSignatures
) {
79 if (!superSignature
.getMethod().hasModifierProperty(PsiModifier
.ABSTRACT
)) return method
;