update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / generation / actions / GenerateSuperMethodCallHandler.java
blob54832df2356e7bb1ca80a99a0dbaefe0e1dba719
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.
17 /**
18 * @author cdr
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);
41 try {
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();
47 PsiElement toGo;
48 if (body.getLBrace() == null) {
49 toGo = body.addBefore(superCall, null);
51 else {
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) {
59 LOG.error(e);
63 public boolean startInWriteAction() {
64 return true;
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;
82 return null;