IDEADEV-39979: RE from PropertyRenameHandler.invoke() on Refactor / Rename a controll...
[fedora-idea.git] / plugins / groovy / src / org / jetbrains / plugins / groovy / refactoring / rename / PropertyRenameHandler.java
blob1b2e03381633bfe44639b12065d084b9ff986dd8
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 org.jetbrains.plugins.groovy.refactoring.rename;
18 import com.intellij.openapi.actionSystem.DataConstants;
19 import com.intellij.openapi.actionSystem.DataContext;
20 import com.intellij.openapi.actionSystem.PlatformDataKeys;
21 import com.intellij.openapi.editor.Editor;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.psi.PsiElement;
24 import com.intellij.psi.PsiFile;
25 import com.intellij.psi.PsiMethod;
26 import com.intellij.psi.util.PropertyUtil;
27 import com.intellij.refactoring.RenameRefactoring;
28 import com.intellij.refactoring.openapi.impl.JavaRenameRefactoringImpl;
29 import com.intellij.refactoring.rename.RenameDialog;
30 import com.intellij.refactoring.rename.RenameHandler;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField;
34 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod;
35 import org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringUtil;
37 /**
38 * @author ven
40 public class PropertyRenameHandler implements RenameHandler {
41 public boolean isAvailableOnDataContext(DataContext dataContext) {
42 return getProperty(dataContext) != null;
45 @Nullable
46 private static GrField getProperty(DataContext dataContext) {
47 final PsiElement element = (PsiElement)dataContext.getData(DataConstants.PSI_ELEMENT);
48 if (element instanceof GrField && ((GrField)element).isProperty()) return (GrField)element;
49 if (element instanceof GrAccessorMethod) return ((GrAccessorMethod)element).getProperty();
50 return null;
53 public boolean isRenaming(DataContext dataContext) {
54 return getProperty(dataContext) != null;
57 public void invoke(@NotNull Project project, Editor editor, PsiFile file, @Nullable DataContext dataContext) {
58 final GrField property = getProperty(dataContext);
59 assert property != null;
60 new PropertyRenameDialog(property, editor).show();
63 private static class PropertyRenameDialog extends RenameDialog {
65 private final GrField myProperty;
67 protected PropertyRenameDialog(GrField property, final Editor editor) {
68 super(property.getProject(), property, null, editor);
69 myProperty = property;
72 protected void doAction() {
73 final String newName = getNewName();
74 final boolean searchInComments = isSearchInComments();
75 doRename(newName, searchInComments);
76 close(OK_EXIT_CODE);
79 private void doRename(String newName, boolean searchInComments) {
80 final RenameRefactoring rename = new JavaRenameRefactoringImpl(myProperty.getProject(), myProperty, newName, searchInComments, false);
82 final PsiMethod setter = myProperty.getSetter();
83 if (setter != null && !(setter instanceof GrAccessorMethod)) {
84 final String setterName = PropertyUtil.suggestSetterName(newName);
85 rename.addElement(setter, setterName);
88 rename.run();
91 protected boolean areButtonsValid() {
92 final String newName = getNewName();
93 return super.areButtonsValid() && !GroovyRefactoringUtil.KEYWORDS.contains(newName);
98 public void invoke(@NotNull Project project, @NotNull PsiElement[] elements, @Nullable DataContext dataContext) {
99 final GrField property = getProperty(dataContext);
100 if (dataContext == null || property == null) return;
101 Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
102 new PropertyRenameDialog(property, editor).show();