update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / extractMethodObject / ExtractMethodObjectHandler.java
blob208ae2c9c4f4ef21cb180b0cf5f6bbc43f6dad05
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.
18 * User: anna
19 * Date: 06-May-2008
21 package com.intellij.refactoring.extractMethodObject;
23 import com.intellij.openapi.actionSystem.DataContext;
24 import com.intellij.openapi.application.Result;
25 import com.intellij.openapi.command.WriteCommandAction;
26 import com.intellij.openapi.diagnostic.Logger;
27 import com.intellij.openapi.editor.Editor;
28 import com.intellij.openapi.project.Project;
29 import com.intellij.openapi.util.Pass;
30 import com.intellij.psi.PsiElement;
31 import com.intellij.psi.PsiFile;
32 import com.intellij.refactoring.HelpID;
33 import com.intellij.refactoring.RefactoringActionHandler;
34 import com.intellij.refactoring.RefactoringBundle;
35 import com.intellij.refactoring.extractMethod.ExtractMethodHandler;
36 import com.intellij.refactoring.extractMethod.PrepareFailedException;
37 import com.intellij.refactoring.util.CommonRefactoringUtil;
38 import com.intellij.refactoring.util.duplicates.DuplicatesImpl;
39 import org.jetbrains.annotations.NotNull;
41 public class ExtractMethodObjectHandler implements RefactoringActionHandler {
42 private static final Logger LOG = Logger.getInstance("#" + ExtractMethodObjectHandler.class.getName());
44 public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, final DataContext dataContext) {
45 ExtractMethodHandler.selectAndPass(project, editor, file, new Pass<PsiElement[]>() {
46 public void pass(final PsiElement[] selectedValue) {
47 invokeOnElements(project, editor, file, selectedValue);
49 });
52 private void invokeOnElements(final Project project, final Editor editor, PsiFile file, PsiElement[] elements) {
53 if (elements.length == 0) {
54 String message = RefactoringBundle
55 .getCannotRefactorMessage(RefactoringBundle.message("selected.block.should.represent.a.set.of.statements.or.an.expression"));
56 CommonRefactoringUtil.showErrorHint(project, editor, message, ExtractMethodObjectProcessor.REFACTORING_NAME, HelpID.EXTRACT_METHOD_OBJECT);
57 return;
60 final ExtractMethodObjectProcessor processor = new ExtractMethodObjectProcessor(project, editor, elements, "");
61 final ExtractMethodObjectProcessor.MyExtractMethodProcessor extractProcessor = processor.getExtractProcessor();
62 try {
63 if (!extractProcessor.prepare()) return;
65 catch (PrepareFailedException e) {
66 CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), ExtractMethodObjectProcessor.REFACTORING_NAME, HelpID.EXTRACT_METHOD_OBJECT);
67 ExtractMethodHandler.highlightPrepareError(e, file, editor, project);
68 return;
71 if (!CommonRefactoringUtil.checkReadOnlyStatus(project, extractProcessor.getTargetClass().getContainingFile())) return;
73 if (extractProcessor.showDialog()) {
74 new WriteCommandAction(project, ExtractMethodObjectProcessor.REFACTORING_NAME, ExtractMethodObjectProcessor.REFACTORING_NAME) {
75 protected void run(final Result result) throws Throwable {
76 extractProcessor.doRefactoring();
77 processor.run();
78 if (processor.isCreateInnerClass()) {
80 processor.moveUsedMethodsToInner();
82 DuplicatesImpl.processDuplicates(extractProcessor, project, editor);
83 processor.changeInstanceAccess(project);
85 final PsiElement method = processor.getMethod();
86 LOG.assertTrue(method != null);
87 method.delete();
89 }.execute();
93 public void invoke(@NotNull final Project project, @NotNull final PsiElement[] elements, final DataContext dataContext) {
94 throw new UnsupportedOperationException();