update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / rename / BeanPropertyRenameHandler.java
blob0b636674281559e73ee493526b869998c391e093
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 package com.intellij.refactoring.rename;
19 import com.intellij.openapi.actionSystem.DataContext;
20 import com.intellij.openapi.editor.Editor;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.ui.DialogWrapper;
23 import com.intellij.psi.PsiElement;
24 import com.intellij.psi.PsiFile;
25 import com.intellij.psi.PsiMethod;
26 import com.intellij.psi.impl.beanProperties.BeanProperty;
27 import com.intellij.psi.util.PropertyUtil;
28 import com.intellij.refactoring.RenameRefactoring;
29 import com.intellij.refactoring.openapi.impl.JavaRenameRefactoringImpl;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
33 /**
34 * @author Dmitry Avdeev
36 public abstract class BeanPropertyRenameHandler implements RenameHandler {
38 public boolean isAvailableOnDataContext(DataContext dataContext) {
39 return false;
42 public boolean isRenaming(DataContext dataContext) {
43 return getProperty(dataContext) != null;
46 public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
47 final BeanProperty property = getProperty(dataContext);
48 new PropertyRenameDialog(property, editor).show();
51 public void invoke(@NotNull Project project, @NotNull PsiElement[] elements, DataContext dataContext) {
55 public static void doRename(@NotNull final BeanProperty property, final String newName, final boolean searchInComments) {
56 final PsiElement psiElement = property.getPsiElement();
57 final RenameRefactoring rename = new JavaRenameRefactoringImpl(psiElement.getProject(), psiElement, newName, searchInComments, false);
59 final PsiMethod setter = property.getSetter();
60 if (setter != null) {
61 final String setterName = PropertyUtil.suggestSetterName(newName);
62 rename.addElement(setter, setterName);
65 final PsiMethod getter = property.getGetter();
66 if (getter != null) {
67 final String getterName = PropertyUtil.suggestGetterName(newName, getter.getReturnType());
68 rename.addElement(getter, getterName);
71 rename.run();
74 @Nullable
75 protected abstract BeanProperty getProperty(DataContext context);
77 private static class PropertyRenameDialog extends RenameDialog {
79 private final BeanProperty myProperty;
81 protected PropertyRenameDialog(BeanProperty property, final Editor editor) {
82 super(property.getMethod().getProject(), property.getPsiElement(), null, editor);
83 myProperty = property;
86 protected void doAction() {
87 final String newName = getNewName();
88 final boolean searchInComments = isSearchInComments();
89 doRename(myProperty, newName, searchInComments);
90 close(DialogWrapper.OK_EXIT_CODE);