update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / wrapreturnvalue / WrapReturnValueHandler.java
blob8624ac7cf6fc8fa8fdfcae9232732b65c811a05a
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.wrapreturnvalue;
18 import com.intellij.ide.util.SuperMethodWarningUtil;
19 import com.intellij.openapi.actionSystem.DataConstants;
20 import com.intellij.openapi.actionSystem.DataContext;
21 import com.intellij.openapi.actionSystem.PlatformDataKeys;
22 import com.intellij.openapi.editor.CaretModel;
23 import com.intellij.openapi.editor.Editor;
24 import com.intellij.openapi.editor.ScrollType;
25 import com.intellij.openapi.editor.ScrollingModel;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.psi.*;
28 import com.intellij.psi.util.PsiTreeUtil;
29 import com.intellij.refactoring.HelpID;
30 import com.intellij.refactoring.RefactorJBundle;
31 import com.intellij.refactoring.RefactoringActionHandler;
32 import com.intellij.refactoring.RefactoringBundle;
33 import com.intellij.refactoring.util.CommonRefactoringUtil;
34 import org.jetbrains.annotations.NotNull;
36 class WrapReturnValueHandler implements RefactoringActionHandler {
37 public static final String REFACTORING_NAME = RefactorJBundle.message("wrap.return.value");
39 public void invoke(@NotNull Project project,
40 Editor editor,
41 PsiFile file,
42 DataContext dataContext){
43 final ScrollingModel scrollingModel = editor.getScrollingModel();
44 scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE);
45 final PsiElement element = (PsiElement) dataContext.getData(DataConstants.PSI_ELEMENT);
46 PsiMethod selectedMethod = null;
47 if(element instanceof PsiMethod){
48 selectedMethod = (PsiMethod) element;
49 } else{
50 final CaretModel caretModel = editor.getCaretModel();
51 final int position = caretModel.getOffset();
52 PsiElement selectedElement = file.findElementAt(position);
53 while(selectedElement != null){
54 if(selectedElement instanceof PsiMethod){
55 selectedMethod = (PsiMethod) selectedElement;
56 break;
58 selectedElement = selectedElement.getParent();
61 if(selectedMethod == null){
62 CommonRefactoringUtil.showErrorHint(project, editor, RefactorJBundle.message("cannot.perform.the.refactoring") + RefactorJBundle.message(
63 "the.caret.should.be.positioned.at.the.name.of.the.method.to.be.refactored"), null, this.getHelpID());
64 return;
66 invoke(project, selectedMethod, editor);
69 protected String getRefactoringName(){
70 return REFACTORING_NAME;
73 protected String getHelpID(){
74 return HelpID.WrapReturnValue;
77 public void invoke(@NotNull Project project,
78 @NotNull PsiElement[] elements,
79 DataContext dataContext){
80 if(elements.length != 1){
81 return;
83 PsiMethod method =
84 PsiTreeUtil.getParentOfType(elements[0], PsiMethod.class, false);
85 if(method == null){
86 return;
88 Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
89 invoke(project, method, editor);
92 private void invoke(final Project project, PsiMethod method, Editor editor) {
93 if(method.isConstructor()){
94 CommonRefactoringUtil.showErrorHint(project, editor, RefactorJBundle.message("cannot.perform.the.refactoring") + RefactorJBundle.message("constructor.returns.can.not.be.wrapped"), null,
95 this.getHelpID());
96 return;
98 final PsiType returnType = method.getReturnType();
99 if(PsiType.VOID.equals(returnType)){
100 CommonRefactoringUtil.showErrorHint(project, editor, RefactorJBundle.message("cannot.perform.the.refactoring") + RefactorJBundle.message("method.selected.returns.void"), null, this.getHelpID());
101 return;
103 method = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringBundle.message("to.refactor"));
104 if (method == null) return;
106 if(method instanceof PsiCompiledElement){
107 CommonRefactoringUtil.showErrorHint(project, editor, RefactorJBundle.message("cannot.perform.the.refactoring") + RefactorJBundle.message(
108 "the.selected.method.cannot.be.wrapped.because.it.is.defined.in.a.non.project.class"), null, this.getHelpID());
109 return;
112 new WrapReturnValueDialog(method).show();