update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / extractSuperclass / ExtractSuperBaseDialog.java
blob6eadde933f422d0280c53c04adac08d159ff7f9f
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.extractSuperclass;
18 import com.intellij.ide.util.PackageUtil;
19 import com.intellij.openapi.command.CommandProcessor;
20 import com.intellij.openapi.help.HelpManager;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.roots.ProjectFileIndex;
23 import com.intellij.openapi.roots.ProjectRootManager;
24 import com.intellij.openapi.util.text.StringUtil;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import com.intellij.psi.*;
27 import com.intellij.refactoring.RefactoringBundle;
28 import com.intellij.refactoring.ui.DocCommentPanel;
29 import com.intellij.refactoring.ui.PackageNameReferenceEditorCombo;
30 import com.intellij.refactoring.ui.RefactoringDialog;
31 import com.intellij.refactoring.util.CommonRefactoringUtil;
32 import com.intellij.refactoring.util.RefactoringMessageUtil;
33 import com.intellij.refactoring.util.classMembers.MemberInfo;
34 import com.intellij.util.IncorrectOperationException;
36 import javax.swing.*;
37 import java.awt.event.ItemEvent;
38 import java.awt.event.ItemListener;
39 import java.util.List;
41 /**
42 * @author dsl
44 public abstract class ExtractSuperBaseDialog extends RefactoringDialog {
45 protected String myRefactoringName;
46 protected PsiClass mySourceClass;
47 protected PsiDirectory myTargetDirectory;
48 protected List<MemberInfo> myMemberInfos;
50 protected JRadioButton myRbExtractSuperclass;
51 protected JRadioButton myRbExtractSubclass;
53 protected JTextField mySourceClassField;
54 protected JTextField myExtractedSuperNameField;
55 protected PackageNameReferenceEditorCombo myPackageNameField;
56 protected DocCommentPanel myJavaDocPanel;
59 public ExtractSuperBaseDialog(Project project, PsiClass sourceClass, List<MemberInfo> members, String refactoringName) {
60 super(project, true);
61 myRefactoringName = refactoringName;
63 mySourceClass = sourceClass;
64 myMemberInfos = members;
65 myTargetDirectory = mySourceClass.getContainingFile().getContainingDirectory();
68 @Override
69 protected void init() {
70 setTitle(myRefactoringName);
72 initPackageNameField();
73 initSourceClassField();
74 myExtractedSuperNameField = new JTextField();
76 myJavaDocPanel = new DocCommentPanel(getJavaDocPanelName());
77 myJavaDocPanel.setPolicy(getJavaDocPolicySetting());
79 super.init();
80 updateDialogForExtractSuperclass();
83 private void initPackageNameField() {
84 String name = "";
85 PsiFile file = mySourceClass.getContainingFile();
86 if (file instanceof PsiJavaFile) {
87 name = ((PsiJavaFile)file).getPackageName();
89 myPackageNameField = new PackageNameReferenceEditorCombo(name, myProject, "ExtractSuperBase.RECENT_KEYS", RefactoringBundle.message("choose.destination.package"));
92 private void initSourceClassField() {
93 mySourceClassField = new JTextField();
94 mySourceClassField.setEditable(false);
95 mySourceClassField.setText(mySourceClass.getQualifiedName());
98 protected JComponent createActionComponent() {
99 Box box = Box.createHorizontalBox();
100 final String s = StringUtil.decapitalize(getEntityName());
101 myRbExtractSuperclass = new JRadioButton();
102 myRbExtractSuperclass.setText(RefactoringBundle.message("extractSuper.extract", s));
103 myRbExtractSubclass = new JRadioButton();
104 myRbExtractSubclass.setText(RefactoringBundle.message("extractSuper.rename.original.class", s));
105 box.add(myRbExtractSuperclass);
106 box.add(myRbExtractSubclass);
107 box.add(Box.createHorizontalGlue());
108 final ButtonGroup buttonGroup = new ButtonGroup();
109 buttonGroup.add(myRbExtractSuperclass);
110 buttonGroup.add(myRbExtractSubclass);
111 myRbExtractSuperclass.setSelected(true);
112 myRbExtractSuperclass.addItemListener(new ItemListener() {
113 public void itemStateChanged(ItemEvent e) {
114 updateDialogForExtractSuperclass();
118 myRbExtractSubclass.addItemListener(new ItemListener() {
119 public void itemStateChanged(ItemEvent e) {
120 updateDialogForExtractSubclass();
123 return box;
126 @Override
127 public JComponent getPreferredFocusedComponent() {
128 return myExtractedSuperNameField;
131 protected void updateDialogForExtractSubclass() {
132 getClassNameLabel().setText(RefactoringBundle.message("extractSuper.rename.original.class.to"));
133 getPreviewAction().setEnabled(true);
136 protected void updateDialogForExtractSuperclass() {
137 getClassNameLabel().setText(getClassNameLabelText());
138 getPreviewAction().setEnabled(false);
141 public String getExtractedSuperName() {
142 return myExtractedSuperNameField.getText().trim();
145 protected String getTargetPackageName() {
146 return myPackageNameField.getText().trim();
149 public PsiDirectory getTargetDirectory() {
150 return myTargetDirectory;
153 protected abstract String getClassNameLabelText();
155 protected abstract JLabel getClassNameLabel();
157 protected abstract JLabel getPackageNameLabel();
159 protected abstract String getEntityName();
161 public int getJavaDocPolicy() {
162 return myJavaDocPanel.getPolicy();
165 public boolean isExtractSuperclass() {
166 return myRbExtractSuperclass.isSelected();
169 protected void doAction() {
170 final String[] errorString = new String[]{null};
171 final String extractedSuperName = getExtractedSuperName();
172 final String packageName = getTargetPackageName();
173 final PsiManager manager = PsiManager.getInstance(myProject);
175 if ("".equals(extractedSuperName)) {
176 errorString[0] = getExtractedSuperNameNotSpecifiedKey();
177 myExtractedSuperNameField.requestFocusInWindow();
179 else {
180 if (!JavaPsiFacade.getInstance(manager.getProject()).getNameHelper().isIdentifier(extractedSuperName)) {
181 errorString[0] = RefactoringMessageUtil.getIncorrectIdentifierMessage(extractedSuperName);
182 myExtractedSuperNameField.requestFocusInWindow();
184 else {
185 CommandProcessor.getInstance().executeCommand(myProject, new Runnable() {
186 public void run() {
187 try {
188 final PsiPackage aPackage = JavaPsiFacade.getInstance(manager.getProject()).findPackage(packageName);
189 if (aPackage != null) {
190 final PsiDirectory[] directories = aPackage.getDirectories(mySourceClass.getResolveScope());
191 if (directories.length >= 1) {
192 myTargetDirectory = getDirUnderSameSourceRoot(directories);
195 myTargetDirectory
196 = PackageUtil.findOrCreateDirectoryForPackage(myProject, packageName, myTargetDirectory, true);
197 if (myTargetDirectory == null) {
198 errorString[0] = ""; // message already reported by PackageUtil
199 return;
201 errorString[0] = RefactoringMessageUtil.checkCanCreateClass(myTargetDirectory, extractedSuperName);
203 catch (IncorrectOperationException e) {
204 errorString[0] = e.getMessage();
205 myPackageNameField.requestFocusInWindow();
208 }, RefactoringBundle.message("create.directory"), null);
211 if (errorString[0] != null) {
212 if (errorString[0].length() > 0) {
213 CommonRefactoringUtil.showErrorMessage(myRefactoringName, errorString[0], getHelpId(), myProject);
215 return;
218 if (!checkConflicts()) return;
220 if (!isExtractSuperclass()) {
221 invokeRefactoring(createProcessor());
223 setJavaDocPolicySetting(getJavaDocPolicy());
224 closeOKAction();
227 private PsiDirectory getDirUnderSameSourceRoot(final PsiDirectory[] directories) {
228 final VirtualFile sourceFile = mySourceClass.getContainingFile().getVirtualFile();
229 if (sourceFile != null) {
230 final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
231 final VirtualFile sourceRoot = fileIndex.getSourceRootForFile(sourceFile);
232 if (sourceRoot != null) {
233 for(PsiDirectory dir: directories) {
234 if (fileIndex.getSourceRootForFile(dir.getVirtualFile()) == sourceRoot) {
235 return dir;
240 return directories[0];
243 protected abstract String getJavaDocPanelName();
245 protected abstract String getExtractedSuperNameNotSpecifiedKey();
246 protected boolean checkConflicts() { return true; }
247 protected abstract ExtractSuperBaseProcessor createProcessor();
249 protected abstract int getJavaDocPolicySetting();
250 protected abstract void setJavaDocPolicySetting(int policy);
252 @Override
253 protected void doHelpAction() {
254 HelpManager.getInstance().invokeHelp(getHelpId());
257 protected abstract String getHelpId();