annotations patterns support in DeadCodeInspection (IDEA-24043 Jersey request methods...
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / extractInterface / ExtractInterfaceHandler.java
blob1bb22c32b850cc5322618226c4eb76f65cf1792e
1 package com.intellij.refactoring.extractInterface;
3 import com.intellij.history.LocalHistory;
4 import com.intellij.history.LocalHistoryAction;
5 import com.intellij.openapi.actionSystem.DataContext;
6 import com.intellij.openapi.application.ApplicationManager;
7 import com.intellij.openapi.command.CommandProcessor;
8 import com.intellij.openapi.diagnostic.Logger;
9 import com.intellij.openapi.editor.Editor;
10 import com.intellij.openapi.editor.ScrollType;
11 import com.intellij.openapi.project.Project;
12 import com.intellij.psi.*;
13 import com.intellij.refactoring.HelpID;
14 import com.intellij.refactoring.RefactoringActionHandler;
15 import com.intellij.refactoring.RefactoringBundle;
16 import com.intellij.refactoring.extractSuperclass.ExtractSuperClassUtil;
17 import com.intellij.refactoring.lang.ElementsHandler;
18 import com.intellij.refactoring.memberPullUp.PullUpHelper;
19 import com.intellij.refactoring.util.CommonRefactoringUtil;
20 import com.intellij.refactoring.util.DocCommentPolicy;
21 import com.intellij.refactoring.util.classMembers.MemberInfo;
22 import com.intellij.usageView.UsageViewUtil;
23 import com.intellij.util.IncorrectOperationException;
24 import org.jetbrains.annotations.NotNull;
26 public class ExtractInterfaceHandler implements RefactoringActionHandler, ElementsHandler {
27 private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.extractInterface.ExtractInterfaceHandler");
29 public static final String REFACTORING_NAME = RefactoringBundle.message("extract.interface.title");
32 private Project myProject;
33 private PsiClass myClass;
35 private String myInterfaceName;
36 private MemberInfo[] mySelectedMembers;
37 private PsiDirectory myTargetDir;
38 private DocCommentPolicy myJavaDocPolicy;
40 public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
41 int offset = editor.getCaretModel().getOffset();
42 editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
43 PsiElement element = file.findElementAt(offset);
44 while (true) {
45 if (element == null || element instanceof PsiFile) {
46 String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.wrong.caret.position.class"));
47 CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.EXTRACT_INTERFACE);
48 return;
50 if (element instanceof PsiClass && !(element instanceof PsiAnonymousClass)) {
51 invoke(project, new PsiElement[]{element}, dataContext);
52 return;
54 element = element.getParent();
58 public void invoke(@NotNull final Project project, @NotNull PsiElement[] elements, DataContext dataContext) {
59 if (elements.length != 1) return;
61 myProject = project;
62 myClass = (PsiClass)elements[0];
65 if (!CommonRefactoringUtil.checkReadOnlyStatus(project, myClass)) return;
67 final ExtractInterfaceDialog dialog = new ExtractInterfaceDialog(myProject, myClass);
68 dialog.show();
69 if (!dialog.isOK() || !dialog.isExtractSuperclass()) return;
71 CommandProcessor.getInstance().executeCommand(myProject, new Runnable() {
72 public void run() {
73 ApplicationManager.getApplication().runWriteAction(new Runnable() {
74 public void run() {
75 myInterfaceName = dialog.getExtractedSuperName();
76 mySelectedMembers = dialog.getSelectedMembers();
77 myTargetDir = dialog.getTargetDirectory();
78 myJavaDocPolicy = new DocCommentPolicy(dialog.getJavaDocPolicy());
79 try {
80 doRefactoring();
82 catch (IncorrectOperationException e) {
83 LOG.error(e);
86 });
88 }, REFACTORING_NAME, null);
93 private void doRefactoring() throws IncorrectOperationException {
94 LocalHistoryAction a = LocalHistory.startAction(myProject, getCommandName());
95 PsiClass anInterface = null;
96 try {
97 anInterface = extractInterface(myTargetDir, myClass, myInterfaceName, mySelectedMembers, myJavaDocPolicy);
99 finally {
100 a.finish();
103 if (anInterface != null) {
104 ExtractClassUtil.askAndTurnRefsToSuper(myProject, myClass, anInterface);
108 static PsiClass extractInterface(PsiDirectory targetDir,
109 PsiClass aClass,
110 String interfaceName,
111 MemberInfo[] selectedMembers,
112 DocCommentPolicy javaDocPolicy) throws IncorrectOperationException {
113 PsiClass anInterface = JavaDirectoryService.getInstance().createInterface(targetDir, interfaceName);
114 PsiJavaCodeReferenceElement ref = ExtractSuperClassUtil.createExtendingReference(anInterface, aClass, selectedMembers);
115 final PsiReferenceList referenceList = aClass.isInterface() ? aClass.getExtendsList() : aClass.getImplementsList();
116 assert referenceList != null;
117 referenceList.add(ref);
118 PullUpHelper pullUpHelper = new PullUpHelper(aClass, anInterface, selectedMembers, javaDocPolicy);
119 pullUpHelper.moveMembersToBase();
120 return anInterface;
123 private String getCommandName() {
124 return RefactoringBundle.message("extract.interface.command.name", myInterfaceName, UsageViewUtil.getDescriptiveName(myClass));
127 public boolean isEnabledOnElements(PsiElement[] elements) {
128 return elements.length == 1 && elements[0] instanceof PsiClass;