inplace introduce field: split dialog to reuse controls

This commit is contained in:
anna
2011-03-17 15:02:38 +01:00
parent 18f9d16078
commit a36e66ba11
3 changed files with 412 additions and 326 deletions
@@ -0,0 +1,210 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.refactoring.introduceField;
import com.intellij.codeInsight.TestUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.ui.IdeBorderFactory;
import javax.swing.*;
import java.awt.event.ItemListener;
public class InitializerPlaceChooser {
private static final Logger LOG = Logger.getInstance("#" + InitializerPlaceChooser.class.getName());
static BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace;
PsiClass myParentClass;
PsiExpression myInitializerExpression;
boolean myAllowInitInMethod;
boolean myAllowInitInMethodIfAll;
JRadioButton myRbInConstructor;
JRadioButton myRbInCurrentMethod;
JRadioButton myRbInFieldDeclaration;
JRadioButton myRbInSetUp;
public InitializerPlaceChooser(PsiClass parentClass,
PsiExpression initializerExpression,
boolean allowInitInMethod, boolean allowInitInMethodIfAll) {
myParentClass = parentClass;
myInitializerExpression = initializerExpression;
myAllowInitInMethod = allowInitInMethod;
myAllowInitInMethodIfAll = allowInitInMethodIfAll;
}
void updateInitializerPlace(boolean replaceAll) {
if (myAllowInitInMethod) {
myRbInCurrentMethod.setEnabled(myAllowInitInMethodIfAll || !replaceAll);
if (!myRbInCurrentMethod.isEnabled() && myRbInCurrentMethod.isSelected()) {
myRbInCurrentMethod.setSelected(false);
myRbInFieldDeclaration.setSelected(true);
}
}
}
void initializeControls(PsiExpression initializerExpression) {
if (initializerExpression != null) {
if (!setEnabledInitializationPlaces(initializerExpression, initializerExpression)) {
myRbInFieldDeclaration.setEnabled(false);
myRbInConstructor.setEnabled(false);
if (myRbInSetUp != null) myRbInSetUp.setEnabled(false);
}
if (!myAllowInitInMethod) {
myRbInCurrentMethod.setEnabled(false);
}
}
else {
myRbInConstructor.setEnabled(false);
myRbInCurrentMethod.setEnabled(false);
myRbInFieldDeclaration.setEnabled(false);
if (myRbInSetUp != null) myRbInSetUp.setEnabled(false);
}
final PsiMethod setUpMethod = TestUtil.findSetUpMethod(myParentClass);
if (myInitializerExpression != null && PsiTreeUtil.isAncestor(setUpMethod, myInitializerExpression, false) && myRbInSetUp.isEnabled() ||
ourLastInitializerPlace == BaseExpressionToFieldHandler.InitializationPlace.IN_SETUP_METHOD &&
TestUtil.isTestClass(myParentClass) &&
myRbInSetUp.isEnabled()) {
myRbInSetUp.setSelected(true);
}
else if (ourLastInitializerPlace == BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR) {
if (myRbInConstructor.isEnabled()) {
myRbInConstructor.setSelected(true);
}
else {
selectInCurrentMethod();
}
}
else if (ourLastInitializerPlace == BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION) {
if (myRbInFieldDeclaration.isEnabled()) {
myRbInFieldDeclaration.setSelected(true);
}
else {
selectInCurrentMethod();
}
}
else {
selectInCurrentMethod();
}
}
void selectInCurrentMethod() {
if (myRbInCurrentMethod.isEnabled()) {
myRbInCurrentMethod.setSelected(true);
}
else if (myRbInFieldDeclaration.isEnabled()) {
myRbInFieldDeclaration.setSelected(true);
}
else {
myRbInCurrentMethod.setSelected(true);
}
}
public BaseExpressionToFieldHandler.InitializationPlace getInitializerPlace() {
if (myRbInConstructor.isSelected()) {
return BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR;
}
if (myRbInCurrentMethod.isSelected()) {
return BaseExpressionToFieldHandler.InitializationPlace.IN_CURRENT_METHOD;
}
if (myRbInFieldDeclaration.isSelected()) {
return BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION;
}
if (myRbInSetUp != null && myRbInSetUp.isSelected()) {
return BaseExpressionToFieldHandler.InitializationPlace.IN_SETUP_METHOD;
}
LOG.assertTrue(false);
return BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION;
}
JComponent createInitializerPlacePanel() {
final JPanel initializationPanel = new JPanel();
initializationPanel.setBorder(IdeBorderFactory.createTitledBorder(RefactoringBundle.message("initialize.in.border.title")));
initializationPanel.setLayout(new BoxLayout(initializationPanel, BoxLayout.Y_AXIS));
myRbInCurrentMethod = new JRadioButton();
myRbInCurrentMethod.setFocusable(false);
myRbInCurrentMethod.setText(RefactoringBundle.message("current.method.radio"));
myRbInCurrentMethod.setEnabled(myAllowInitInMethod);
myRbInFieldDeclaration = new JRadioButton();
myRbInFieldDeclaration.setFocusable(false);
myRbInFieldDeclaration.setText(RefactoringBundle.message("field.declaration.radio"));
myRbInConstructor = new JRadioButton();
myRbInConstructor.setFocusable(false);
myRbInConstructor.setText(RefactoringBundle.message("class.constructors.radio"));
initializationPanel.add(myRbInCurrentMethod);
initializationPanel.add(myRbInFieldDeclaration);
initializationPanel.add(myRbInConstructor);
if (TestUtil.isTestClass(myParentClass)) {
myRbInSetUp = new JRadioButton();
myRbInSetUp.setFocusable(false);
myRbInSetUp.setText(RefactoringBundle.message("setup.method.radio"));
initializationPanel.add(myRbInSetUp);
}
ButtonGroup bg = new ButtonGroup();
bg.add(myRbInCurrentMethod);
bg.add(myRbInFieldDeclaration);
bg.add(myRbInConstructor);
if (myRbInSetUp != null) bg.add(myRbInSetUp);
return initializationPanel;
}
static boolean setEnabledInitializationPlaces(PsiElement initializerPart, PsiElement initializer) {
if (initializerPart instanceof PsiReferenceExpression) {
PsiReferenceExpression refExpr = (PsiReferenceExpression)initializerPart;
if (refExpr.getQualifierExpression() == null) {
PsiElement refElement = refExpr.resolve();
if (refElement == null ||
(refElement instanceof PsiLocalVariable || refElement instanceof PsiParameter) &&
!PsiTreeUtil.isAncestor(initializer, refElement, true)) {
return false;
}
}
}
PsiElement[] children = initializerPart.getChildren();
for (PsiElement child : children) {
if (!setEnabledInitializationPlaces(child, initializer)) return false;
}
return true;
}
boolean allowFinal(boolean willBeDeclaredStatic, boolean isCurrentMethodConstructor) {
boolean allowFinal = myRbInFieldDeclaration.isSelected() || (myRbInConstructor.isSelected() && !willBeDeclaredStatic);
if (myRbInCurrentMethod.isSelected() && isCurrentMethodConstructor) {
final PsiMethod[] constructors = myParentClass.getConstructors();
allowFinal = constructors.length <= 1;
}
return allowFinal;
}
void addItemListener(ItemListener listener) {
myRbInConstructor.addItemListener(listener);
myRbInCurrentMethod.addItemListener(listener);
myRbInFieldDeclaration.addItemListener(listener);
if (myRbInSetUp != null) myRbInSetUp.addItemListener(listener);
}
}
@@ -15,9 +15,7 @@
*/
package com.intellij.refactoring.introduceField;
import com.intellij.codeInsight.TestUtil;
import com.intellij.codeInsight.completion.JavaCompletionUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.help.HelpManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
@@ -26,29 +24,20 @@ import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.codeStyle.SuggestedNameInfo;
import com.intellij.psi.codeStyle.VariableKind;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.HelpID;
import com.intellij.refactoring.JavaRefactoringSettings;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.ui.*;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.refactoring.util.RefactoringMessageUtil;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.NonFocusableCheckBox;
import com.intellij.ui.StateRestoringCheckBox;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import static com.intellij.refactoring.introduceField.BaseExpressionToFieldHandler.InitializationPlace.*;
class IntroduceFieldDialog extends DialogWrapper {
private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.introduceField.IntroduceFieldDialog");
private static boolean ourLastCbFinalState = false;
private static BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace;
private final Project myProject;
private final PsiClass myParentClass;
@@ -57,28 +46,17 @@ class IntroduceFieldDialog extends DialogWrapper {
private final boolean myIsCurrentMethodConstructor;
private final boolean myIsInvokedOnDeclaration;
private final boolean myWillBeDeclaredStatic;
private final int myOccurrencesCount;
private final boolean myAllowInitInMethod;
private final boolean myAllowInitInMethodIfAll;
private final TypeSelectorManager myTypeSelectorManager;
private NameSuggestionsField myNameField;
private JCheckBox myCbReplaceAll;
private StateRestoringCheckBox myCbDeleteVariable;
private StateRestoringCheckBox myCbFinal;
private JRadioButton myRbInConstructor;
private JRadioButton myRbInCurrentMethod;
private JRadioButton myRbInFieldDeclaration;
private JRadioButton myRbInSetUp;
private JRadioButton myRbPrivate;
private JRadioButton myRbProtected;
private JRadioButton myRbPackageLocal;
private JRadioButton myRbPublic;
private TypeSelector myTypeSelector;
private NameSuggestionsManager myNameSuggestionsManager;
private static final String REFACTORING_NAME = RefactoringBundle.message("introduce.field.title");
private final InitializerPlaceChooser myInitializerPlaceChooser;
private JavaVisibilityPanel myVisibilityPanel;
private IntroduceFieldPanel myIntroduceFieldPanel;
public IntroduceFieldDialog(Project project,
PsiClass parentClass,
@@ -91,13 +69,13 @@ class IntroduceFieldDialog extends DialogWrapper {
myProject = project;
myParentClass = parentClass;
myInitializerExpression = initializerExpression;
myInitializerPlaceChooser = new InitializerPlaceChooser(parentClass, initializerExpression, allowInitInMethod, allowInitInMethodIfAll);
myIntroduceFieldPanel = new IntroduceFieldPanel(isInvokedOnDeclaration, occurrencesCount, localVariable);
myLocalVariable = localVariable;
myIsCurrentMethodConstructor = isCurrentMethodConstructor;
myIsInvokedOnDeclaration = isInvokedOnDeclaration;
myWillBeDeclaredStatic = willBeDeclaredStatic;
myOccurrencesCount = occurrencesCount;
myAllowInitInMethod = allowInitInMethod;
myAllowInitInMethodIfAll = allowInitInMethodIfAll;
myTypeSelectorManager = typeSelectorManager;
setTitle(REFACTORING_NAME);
@@ -108,61 +86,11 @@ class IntroduceFieldDialog extends DialogWrapper {
}
private void initializeControls(PsiExpression initializerExpression) {
if (initializerExpression != null) {
setEnabledInitializationPlaces(initializerExpression, initializerExpression);
if (!myAllowInitInMethod) {
myRbInCurrentMethod.setEnabled(false);
}
} else {
myRbInConstructor.setEnabled(false);
myRbInCurrentMethod.setEnabled(false);
myRbInFieldDeclaration.setEnabled(false);
if (myRbInSetUp != null) myRbInSetUp.setEnabled(false);
}
myInitializerPlaceChooser.initializeControls(initializerExpression);
myIntroduceFieldPanel.initializeControls(initializerExpression);
final PsiMethod setUpMethod = TestUtil.findSetUpMethod(myParentClass);
if (myInitializerExpression != null && PsiTreeUtil.isAncestor(setUpMethod, myInitializerExpression, false) && myRbInSetUp.isEnabled() ||
ourLastInitializerPlace == IN_SETUP_METHOD && TestUtil.isTestClass(myParentClass) && myRbInSetUp.isEnabled()) {
myRbInSetUp.setSelected(true);
}
else if (ourLastInitializerPlace == IN_CONSTRUCTOR) {
if (myRbInConstructor.isEnabled()) {
myRbInConstructor.setSelected(true);
} else {
selectInCurrentMethod();
}
} else if (ourLastInitializerPlace == IN_FIELD_DECLARATION) {
if (myRbInFieldDeclaration.isEnabled()) {
myRbInFieldDeclaration.setSelected(true);
} else {
selectInCurrentMethod();
}
} else {
selectInCurrentMethod();
}
String ourLastVisibility = JavaRefactoringSettings.getInstance().INTRODUCE_FIELD_VISIBILITY;
if (PsiModifier.PUBLIC.equals(ourLastVisibility)) {
myRbPublic.setSelected(true);
} else if (PsiModifier.PROTECTED.equals(ourLastVisibility)) {
myRbProtected.setSelected(true);
} else if (PsiModifier.PACKAGE_LOCAL.equals(ourLastVisibility)) {
myRbPackageLocal.setSelected(true);
} else {
myRbPrivate.setSelected(true);
}
myCbFinal.setSelected(myCbFinal.isEnabled() && ourLastCbFinalState);
}
private void selectInCurrentMethod() {
if (myRbInCurrentMethod.isEnabled()) {
myRbInCurrentMethod.setSelected(true);
}
else if (myRbInFieldDeclaration.isEnabled()) {
myRbInFieldDeclaration.setSelected(true);
}
else {
myRbInCurrentMethod.setSelected(true);
}
myVisibilityPanel.setVisibility(ourLastVisibility);
}
public String getEnteredName() {
@@ -170,55 +98,27 @@ class IntroduceFieldDialog extends DialogWrapper {
}
public BaseExpressionToFieldHandler.InitializationPlace getInitializerPlace() {
if (myRbInConstructor.isSelected()) {
return IN_CONSTRUCTOR;
}
if (myRbInCurrentMethod.isSelected()) {
return IN_CURRENT_METHOD;
}
if (myRbInFieldDeclaration.isSelected()) {
return IN_FIELD_DECLARATION;
}
if (myRbInSetUp != null && myRbInSetUp.isSelected()) {
return IN_SETUP_METHOD;
}
LOG.assertTrue(false);
return IN_FIELD_DECLARATION;
return myInitializerPlaceChooser.getInitializerPlace();
}
@Modifier
public String getFieldVisibility() {
if (myRbPublic.isSelected()) {
return PsiModifier.PUBLIC;
}
if (myRbPackageLocal.isSelected()) {
return PsiModifier.PACKAGE_LOCAL;
}
if (myRbProtected.isSelected()) {
return PsiModifier.PROTECTED;
}
if (myRbPrivate.isSelected()) {
return PsiModifier.PRIVATE;
}
LOG.assertTrue(false);
return null;
return myVisibilityPanel.getVisibility();
}
public boolean isReplaceAllOccurrences() {
if (myIsInvokedOnDeclaration) return true;
if (myOccurrencesCount <= 1) return false;
return myCbReplaceAll.isSelected();
return myIntroduceFieldPanel.isReplaceAllOccurrences();
}
public boolean isDeleteVariable() {
if (myIsInvokedOnDeclaration) return true;
if (myCbDeleteVariable == null) return false;
return myCbDeleteVariable.isSelected();
return myIntroduceFieldPanel.isDeleteVariable();
}
public boolean isDeclareFinal() {
return myCbFinal.isSelected();
return myIntroduceFieldPanel.isDeclareFinal();
}
public PsiType getFieldType() {
@@ -296,201 +196,48 @@ class IntroduceFieldDialog extends DialogWrapper {
}
protected JComponent createCenterPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbConstraints = new GridBagConstraints();
gbConstraints.fill = GridBagConstraints.HORIZONTAL;
gbConstraints.weightx = 1;
gbConstraints.weighty = 0;
gbConstraints.gridwidth = 1;
gbConstraints.gridx = 0;
gbConstraints.gridy = 0;
final Insets standardInsets = new Insets(0, 0, 0, 0);
gbConstraints.insets = standardInsets;
final JPanel panel = new JPanel(new GridBagLayout());
final GridBagConstraints gbConstraints = new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0);
panel.add(createInitializerPlacePanel(), gbConstraints);
ItemListener itemListener = new ItemListener() {
final JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel groupPanel = new JPanel(new GridLayout(1, 2));
final JComponent initializerPlacePanel = myInitializerPlaceChooser.createInitializerPlacePanel();
myInitializerPlaceChooser.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (myCbReplaceAll != null && myAllowInitInMethod) {
myRbInCurrentMethod.setEnabled(myAllowInitInMethodIfAll || !myCbReplaceAll.isSelected());
if (!myRbInCurrentMethod.isEnabled() && myRbInCurrentMethod.isSelected()) {
myRbInCurrentMethod.setSelected(false);
myRbInFieldDeclaration.setSelected(true);
}
}
updateTypeSelector();
myIntroduceFieldPanel.updateTypeSelector(myTypeSelectorManager);
myIntroduceFieldPanel.updateCbFinal(myInitializerPlaceChooser.allowFinal(myWillBeDeclaredStatic, myIsCurrentMethodConstructor));
}
});
groupPanel.add(initializerPlacePanel);
myVisibilityPanel = new JavaVisibilityPanel(false, false);
groupPanel.add(myVisibilityPanel);
mainPanel.add(groupPanel, BorderLayout.CENTER);
final ItemListener itemListener = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (myIntroduceFieldPanel.hasOccurrences()) {
myInitializerPlaceChooser.updateInitializerPlace(myIntroduceFieldPanel.isReplaceAllOccurrences());
}
myIntroduceFieldPanel.updateTypeSelector(myTypeSelectorManager);
myNameField.requestFocusInWindow();
}
};
ItemListener finalUpdater = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
updateCbFinal();
}
};
myRbInConstructor.addItemListener(itemListener);
myRbInCurrentMethod.addItemListener(itemListener);
myRbInFieldDeclaration.addItemListener(itemListener);
myRbInConstructor.addItemListener(finalUpdater);
myRbInCurrentMethod.addItemListener(finalUpdater);
myRbInFieldDeclaration.addItemListener(finalUpdater);
if (myRbInSetUp != null) myRbInSetUp.addItemListener(finalUpdater);
if (myOccurrencesCount > 1) {
myCbReplaceAll = new NonFocusableCheckBox();
myCbReplaceAll.setText(RefactoringBundle.message("replace.all.occurrences.of.expression.0.occurrences", myOccurrencesCount));
gbConstraints.gridy++;
panel.add(myCbReplaceAll, gbConstraints);
myCbReplaceAll.addItemListener(itemListener);
if (myIsInvokedOnDeclaration) {
myCbReplaceAll.setEnabled(false);
myCbReplaceAll.setSelected(true);
}
}
panel.add(mainPanel, gbConstraints);
myIntroduceFieldPanel.appendFinalCb(panel, gbConstraints, itemListener);
myIntroduceFieldPanel.appendOccurrencesCb(panel, gbConstraints, itemListener);
myIntroduceFieldPanel.appendDeleteVariableDeclarationCb(panel, gbConstraints);
if (myLocalVariable != null) {
gbConstraints.gridy++;
if (myCbReplaceAll != null) {
gbConstraints.insets = new Insets(0, 8, 0, 0);
}
myCbDeleteVariable = new StateRestoringCheckBox();
myCbDeleteVariable.setText(RefactoringBundle.message("delete.variable.declaration"));
panel.add(myCbDeleteVariable, gbConstraints);
if (myIsInvokedOnDeclaration) {
myCbDeleteVariable.setEnabled(false);
myCbDeleteVariable.setSelected(true);
} else if (myCbReplaceAll != null) {
updateCbDeleteVariable();
myCbReplaceAll.addItemListener(
new ItemListener() {
public void itemStateChanged(ItemEvent e) {
updateCbDeleteVariable();
}
}
);
}
gbConstraints.insets = standardInsets;
}
myCbFinal.addItemListener(itemListener);
// myCbStatic.addItemListener(itemListener);
// myCbStatic.addItemListener(finalUpdater);
// myCbStatic.addItemListener(
// new ItemListener() {
// public void itemStateChanged(ItemEvent e) {
// updateNameList();
// }
// }
// );
updateTypeSelector();
myIntroduceFieldPanel.updateTypeSelector(myTypeSelectorManager);
return panel;
}
private void updateTypeSelector() {
if (myCbReplaceAll != null) {
myTypeSelectorManager.setAllOccurences(myCbReplaceAll.isSelected());
} else {
myTypeSelectorManager.setAllOccurences(false);
}
}
private void updateCbDeleteVariable() {
if (!myCbReplaceAll.isSelected()) {
myCbDeleteVariable.makeUnselectable(false);
} else {
myCbDeleteVariable.makeSelectable();
}
}
private JComponent createInitializerPlacePanel() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel initializationPanel = new JPanel();
initializationPanel.setBorder(IdeBorderFactory.createTitledBorder(RefactoringBundle.message("initialize.in.border.title")));
initializationPanel.setLayout(new BoxLayout(initializationPanel, BoxLayout.Y_AXIS));
JPanel visibilityPanel = new JPanel();
visibilityPanel.setBorder(IdeBorderFactory.createTitledBorder(RefactoringBundle.message("visibility.border.title")));
visibilityPanel.setLayout(new BoxLayout(visibilityPanel, BoxLayout.Y_AXIS));
/*JPanel modifiersPanel = new GroupPanel();
modifiersPanel.setBorder(BorderFactory.createTitledBorder("Other Modifiers"));
modifiersPanel.setLayout(new BoxLayout(modifiersPanel, BoxLayout.Y_AXIS));*/
myRbInCurrentMethod = new JRadioButton();
myRbInCurrentMethod.setText(RefactoringBundle.message("current.method.radio"));
myRbInCurrentMethod.setEnabled(myAllowInitInMethod);
myRbInFieldDeclaration = new JRadioButton();
myRbInFieldDeclaration.setText(RefactoringBundle.message("field.declaration.radio"));
myRbInConstructor = new JRadioButton();
myRbInConstructor.setText(RefactoringBundle.message("class.constructors.radio"));
myRbPrivate = new JRadioButton();
myRbPrivate.setText(RefactoringBundle.message("visibility.private"));
myRbPrivate.setFocusable(false);
myRbPackageLocal = new JRadioButton();
myRbPackageLocal.setText(RefactoringBundle.message("visibility.package.local"));
myRbPackageLocal.setFocusable(false);
myRbProtected = new JRadioButton();
myRbProtected.setText(RefactoringBundle.message("visibility.protected"));
myRbProtected.setFocusable(false);
myRbPublic = new JRadioButton();
myRbPublic.setText(RefactoringBundle.message("visibility.public"));
myRbPublic.setFocusable(false);
myCbFinal = new StateRestoringCheckBox();
myCbFinal.setText(RefactoringBundle.message("declare.final"));
initializationPanel.add(myRbInCurrentMethod);
initializationPanel.add(myRbInFieldDeclaration);
initializationPanel.add(myRbInConstructor);
if (TestUtil.isTestClass(myParentClass)) {
myRbInSetUp = new JRadioButton();
myRbInSetUp.setText(RefactoringBundle.message("setup.method.radio"));
initializationPanel.add(myRbInSetUp);
}
ButtonGroup bg = new ButtonGroup();
bg.add(myRbInCurrentMethod);
bg.add(myRbInFieldDeclaration);
bg.add(myRbInConstructor);
if (myRbInSetUp != null) bg.add(myRbInSetUp);
visibilityPanel.add(myRbPrivate);
visibilityPanel.add(myRbPackageLocal);
visibilityPanel.add(myRbProtected);
visibilityPanel.add(myRbPublic);
bg = new ButtonGroup();
bg.add(myRbPrivate);
bg.add(myRbPackageLocal);
bg.add(myRbProtected);
bg.add(myRbPublic);
// modifiersPanel.add(myCbFinal);
// modifiersPanel.add(myCbStatic);
JPanel groupPanel = new JPanel(new GridLayout(1, 2));
groupPanel.add(initializationPanel);
groupPanel.add(visibilityPanel);
mainPanel.add(groupPanel, BorderLayout.CENTER);
mainPanel.add(myCbFinal, BorderLayout.SOUTH);
return mainPanel;
}
private void updateCbFinal() {
boolean allowFinal = myRbInFieldDeclaration.isSelected() || (myRbInConstructor.isSelected() && !myWillBeDeclaredStatic);
if (myRbInCurrentMethod.isSelected() && myIsCurrentMethodConstructor) {
final PsiMethod[] constructors = myParentClass.getConstructors();
allowFinal = constructors.length <= 1;
}
if (!allowFinal) {
myCbFinal.makeUnselectable(false);
} else {
myCbFinal.makeSelectable();
}
}
private NameSuggestionsGenerator createGenerator() {
return new NameSuggestionsGenerator() {
@@ -546,8 +293,8 @@ class IntroduceFieldDialog extends DialogWrapper {
}
}
ourLastCbFinalState = myCbFinal.isSelected();
ourLastInitializerPlace = getInitializerPlace();
IntroduceFieldPanel.ourLastCbFinalState = myIntroduceFieldPanel.isFinal();
InitializerPlaceChooser.ourLastInitializerPlace = myInitializerPlaceChooser.getInitializerPlace();
JavaRefactoringSettings.getInstance().INTRODUCE_FIELD_VISIBILITY = getFieldVisibility();
myNameSuggestionsManager.nameSelected();
@@ -562,27 +309,4 @@ class IntroduceFieldDialog extends DialogWrapper {
protected void doHelpAction() {
HelpManager.getInstance().invokeHelp(HelpID.INTRODUCE_FIELD);
}
private boolean setEnabledInitializationPlaces(PsiElement initializerPart, PsiElement initializer) {
if (initializerPart instanceof PsiReferenceExpression) {
PsiReferenceExpression refExpr = (PsiReferenceExpression) initializerPart;
if (refExpr.getQualifierExpression() == null) {
PsiElement refElement = refExpr.resolve();
if (refElement == null ||
(refElement instanceof PsiLocalVariable || refElement instanceof PsiParameter) &&
!PsiTreeUtil.isAncestor(initializer, refElement, true)) {
myRbInFieldDeclaration.setEnabled(false);
myRbInConstructor.setEnabled(false);
if (myRbInSetUp != null) myRbInSetUp.setEnabled(false);
myCbFinal.setEnabled(false);
return false;
}
}
}
PsiElement[] children = initializerPart.getChildren();
for (PsiElement child : children) {
if (!setEnabledInitializationPlaces(child, initializer)) return false;
}
return true;
}
}
@@ -0,0 +1,152 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.refactoring.introduceField;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiLocalVariable;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.ui.TypeSelectorManager;
import com.intellij.ui.NonFocusableCheckBox;
import com.intellij.ui.StateRestoringCheckBox;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/**
* User: anna
* Date: 3/15/11
*/
public class IntroduceFieldPanel {
static boolean ourLastCbFinalState = false;
private JCheckBox myCbReplaceAll;
private StateRestoringCheckBox myCbDeleteVariable;
private StateRestoringCheckBox myCbFinal;
private boolean myIsInvokedOnDeclaration;
private int myOccurrencesCount;
private PsiLocalVariable myLocalVariable;
public IntroduceFieldPanel(boolean isInvokedOnDeclaration, int occurrencesCount, PsiLocalVariable localVariable) {
myIsInvokedOnDeclaration = isInvokedOnDeclaration;
myOccurrencesCount = occurrencesCount;
myLocalVariable = localVariable;
}
public void initializeControls(PsiExpression initializerExpression) {
if (initializerExpression != null && !InitializerPlaceChooser.setEnabledInitializationPlaces(initializerExpression, initializerExpression)) {
myCbFinal.setEnabled(false);
}
myCbFinal.setSelected(myCbFinal.isEnabled() && ourLastCbFinalState);
}
public boolean isReplaceAllOccurrences() {
if (myIsInvokedOnDeclaration) return true;
if (myOccurrencesCount <= 1) return false;
return myCbReplaceAll.isSelected();
}
public boolean isDeclareFinal() {
return myCbFinal.isSelected();
}
public boolean isDeleteVariable() {
if (myIsInvokedOnDeclaration) return true;
if (myCbDeleteVariable == null) return false;
return myCbDeleteVariable.isSelected();
}
void appendFinalCb(JPanel mainPanel, GridBagConstraints gbConstraints, ItemListener itemListener) {
myCbFinal = new StateRestoringCheckBox();
myCbFinal.setText(RefactoringBundle.message("declare.final"));
myCbFinal.addItemListener(itemListener);
gbConstraints.gridy++;
mainPanel.add(myCbFinal, gbConstraints);
}
void appendOccurrencesCb(JPanel panel, GridBagConstraints gbConstraints, ItemListener itemListener) {
if (myOccurrencesCount > 1) {
myCbReplaceAll = new NonFocusableCheckBox();
myCbReplaceAll.setText(RefactoringBundle.message("replace.all.occurrences.of.expression.0.occurrences", myOccurrencesCount));
gbConstraints.gridy++;
panel.add(myCbReplaceAll, gbConstraints);
myCbReplaceAll.addItemListener(itemListener);
if (myIsInvokedOnDeclaration) {
myCbReplaceAll.setEnabled(false);
myCbReplaceAll.setSelected(true);
}
}
}
void appendDeleteVariableDeclarationCb(JPanel panel, GridBagConstraints gbConstraints) {
if (myLocalVariable != null) {
gbConstraints.gridy++;
if (myCbReplaceAll != null) {
gbConstraints.insets = new Insets(0, 8, 0, 0);
}
myCbDeleteVariable = new StateRestoringCheckBox();
myCbDeleteVariable.setText(RefactoringBundle.message("delete.variable.declaration"));
panel.add(myCbDeleteVariable, gbConstraints);
if (myIsInvokedOnDeclaration) {
myCbDeleteVariable.setEnabled(false);
myCbDeleteVariable.setSelected(true);
} else if (myCbReplaceAll != null) {
updateCbDeleteVariable();
myCbReplaceAll.addItemListener(
new ItemListener() {
public void itemStateChanged(ItemEvent e) {
updateCbDeleteVariable();
}
}
);
}
gbConstraints.insets = new Insets(0, 0, 0, 0);
}
}
private void updateCbDeleteVariable() {
if (!myCbReplaceAll.isSelected()) {
myCbDeleteVariable.makeUnselectable(false);
} else {
myCbDeleteVariable.makeSelectable();
}
}
void updateCbFinal(boolean allowFinal) {
if (!allowFinal) {
myCbFinal.makeUnselectable(false);
} else {
myCbFinal.makeSelectable();
}
}
public boolean isFinal() {
return myCbFinal.isSelected();
}
void updateTypeSelector(TypeSelectorManager typeSelectorManager) {
if (myCbReplaceAll != null) {
typeSelectorManager.setAllOccurences(myCbReplaceAll.isSelected());
} else {
typeSelectorManager.setAllOccurences(false);
}
}
public boolean hasOccurrences() {
return myCbReplaceAll != null;
}
}