update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / encapsulateFields / EncapsulateFieldsHandler.java
blobfa2a1433fb10a8409abbd3e6a4304cd5571daaf0
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.encapsulateFields;
18 import com.intellij.openapi.actionSystem.DataContext;
19 import com.intellij.openapi.actionSystem.PlatformDataKeys;
20 import com.intellij.openapi.diagnostic.Logger;
21 import com.intellij.openapi.editor.Editor;
22 import com.intellij.openapi.editor.ScrollType;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.psi.PsiClass;
25 import com.intellij.psi.PsiElement;
26 import com.intellij.psi.PsiField;
27 import com.intellij.psi.PsiFile;
28 import com.intellij.refactoring.HelpID;
29 import com.intellij.refactoring.RefactoringActionHandler;
30 import com.intellij.refactoring.RefactoringBundle;
31 import com.intellij.refactoring.util.CommonRefactoringUtil;
32 import org.jetbrains.annotations.NotNull;
34 import java.util.HashSet;
36 public class EncapsulateFieldsHandler implements RefactoringActionHandler {
37 private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.encapsulateFields.EncapsulateFieldsHandler");
38 public static final String REFACTORING_NAME = RefactoringBundle.message("encapsulate.fields.title");
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.ENCAPSULATE_FIELDS);
48 return;
50 if (element instanceof PsiField) {
51 if (((PsiField) element).getContainingClass() == null) {
52 String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("the.field.should.be.declared.in.a.class"));
53 CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.ENCAPSULATE_FIELDS);
54 return;
56 invoke(project, new PsiElement[]{element}, dataContext);
57 return;
59 if (element instanceof PsiClass) {
60 invoke(project, new PsiElement[]{element}, dataContext);
61 return;
63 element = element.getParent();
67 /**
68 * if elements.length == 1 the expected value is either PsiClass or PsiField
69 * if elements.length > 1 the expected values are PsiField objects only
71 public void invoke(@NotNull final Project project, @NotNull final PsiElement[] elements, DataContext dataContext) {
72 PsiClass aClass = null;
73 final HashSet<PsiField> preselectedFields = new HashSet<PsiField>();
74 if (elements.length == 1) {
75 if (elements[0] instanceof PsiClass) {
76 aClass = (PsiClass) elements[0];
77 } else if (elements[0] instanceof PsiField) {
78 PsiField field = (PsiField) elements[0];
79 aClass = field.getContainingClass();
80 preselectedFields.add(field);
81 } else {
82 return;
84 } else {
85 for (PsiElement element : elements) {
86 if (!(element instanceof PsiField)) {
87 return;
89 PsiField field = (PsiField)element;
90 if (aClass == null) {
91 aClass = field.getContainingClass();
92 preselectedFields.add(field);
94 else {
95 if (aClass.equals(field.getContainingClass())) {
96 preselectedFields.add(field);
98 else {
99 String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("fields.to.be.refactored.should.belong.to.the.same.class"));
100 Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
101 CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.ENCAPSULATE_FIELDS);
102 return;
108 LOG.assertTrue(aClass != null);
110 if (aClass.isInterface()) {
111 String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("encapsulate.fields.refactoring.cannot.be.applied.to.interface"));
112 Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
113 CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.ENCAPSULATE_FIELDS);
114 return;
117 if (!CommonRefactoringUtil.checkReadOnlyStatus(project, aClass)) return;
119 EncapsulateFieldsDialog dialog = new EncapsulateFieldsDialog(
120 project,
121 aClass,
122 preselectedFields);
123 dialog.show();