introduce field: layout change

This commit is contained in:
anna
2011-04-09 17:57:43 +02:00
parent 481ec612a7
commit 8af1c09ffb
7 changed files with 603 additions and 260 deletions
@@ -44,12 +44,10 @@ import com.intellij.refactoring.JavaRefactoringSettings;
import com.intellij.refactoring.introduceParameter.AbstractInplaceIntroducer;
import com.intellij.refactoring.move.moveMembers.MoveMembersImpl;
import com.intellij.refactoring.rename.inplace.VariableInplaceRenamer;
import com.intellij.refactoring.ui.JavaVisibilityPanel;
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
import com.intellij.refactoring.util.occurences.OccurenceManager;
import com.intellij.ui.StateRestoringCheckBox;
import com.intellij.ui.TitlePanel;
import com.intellij.util.VisibilityUtil;
import javax.swing.*;
import java.awt.*;
@@ -194,28 +192,41 @@ public class InplaceIntroduceConstantPopup {
private JPanel createLeftPanel() {
final JPanel left = new JPanel(new GridBagLayout());
final GridBagConstraints lgc = new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5,5,0,0), 0, 0);
myVisibilityCombo = createVisibilityCombo(left, new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 0, 0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(6,5,0,0), 0, 0),
myProject);
myMoveToAnotherClassCb = new JCheckBox("Move to another class");
myMoveToAnotherClassCb.setMnemonic('m');
myMoveToAnotherClassCb.setFocusable(false);
left.add(myMoveToAnotherClassCb, new GridBagConstraints(0, 1, 2, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
return left;
}
public static JComboBox createVisibilityCombo(final JPanel left,
final GridBagConstraints lgc,
final Project project) {
final JLabel label = new JLabel("Visibility:");
label.setDisplayedMnemonic('V');
left.add(label, lgc);
myVisibilityCombo = new JComboBox(new String[]{PsiModifier.PUBLIC, PsiModifier.PACKAGE_LOCAL, PsiModifier.PROTECTED, PsiModifier.PRIVATE});
myVisibilityCombo.setRenderer(new ListCellRendererWrapper<String>(myVisibilityCombo.getRenderer()){
final JComboBox visibilityCombo = new JComboBox(new String[]{PsiModifier.PUBLIC, PsiModifier.PACKAGE_LOCAL, PsiModifier.PROTECTED, PsiModifier.PRIVATE});
visibilityCombo.setRenderer(new ListCellRendererWrapper<String>(visibilityCombo.getRenderer()) {
@Override
public void customize(JList list, String value, int index, boolean selected, boolean hasFocus) {
setText(PsiBundle.visibilityPresentation(value));
}
});
label.setLabelFor(myVisibilityCombo);
myVisibilityCombo.setSelectedItem(JavaRefactoringSettings.getInstance().INTRODUCE_CONSTANT_VISIBILITY);
myVisibilityCombo.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
ToolWindowManager.getInstance(myProject).activateEditorComponent();
}
}
});
label.setLabelFor(visibilityCombo);
visibilityCombo.setSelectedItem(JavaRefactoringSettings.getInstance().INTRODUCE_CONSTANT_VISIBILITY);
appendActions(visibilityCombo, project);
lgc.gridx++;
lgc.insets.top = 2;
lgc.insets.left = 0;
left.add(visibilityCombo, lgc);
return visibilityCombo;
}
static void appendActions(final JComboBox visibilityCombo, final Project project) {
final AnAction arrow = new AnAction() {
@Override
public void actionPerformed(AnActionEvent e) {
@@ -223,34 +234,29 @@ public class InplaceIntroduceConstantPopup {
final int code = ((KeyEvent)e.getInputEvent()).getKeyCode();
final int delta = code == KeyEvent.VK_DOWN ? 1 : code == KeyEvent.VK_UP ? -1 : 0;
if (delta == 0) return;
final int size = myVisibilityCombo.getModel().getSize();
int next = myVisibilityCombo.getSelectedIndex() + delta;
final int size = visibilityCombo.getModel().getSize();
int next = visibilityCombo.getSelectedIndex() + delta;
if (next < 0 || next >= size) {
if (!UISettings.getInstance().CYCLE_SCROLLING) {
return;
}
next = (next + size) % size;
}
myVisibilityCombo.setSelectedIndex(next);
visibilityCombo.setSelectedIndex(next);
}
}
};
arrow.registerCustomShortcutSet(new CustomShortcutSet(new KeyboardShortcut(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), null),
new KeyboardShortcut(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), null)), myVisibilityCombo);
lgc.gridx = 1;
lgc.insets.top = 2;
lgc.insets.left = 0;
left.add(myVisibilityCombo, lgc);
new KeyboardShortcut(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), null)), visibilityCombo);
myMoveToAnotherClassCb = new JCheckBox("Move to another class");
myMoveToAnotherClassCb.setMnemonic('m');
myMoveToAnotherClassCb.setFocusable(false);
lgc.gridx = 0;
lgc.gridy = 1;
lgc.gridwidth = 2;
lgc.insets.top = 0;
left.add(myMoveToAnotherClassCb, lgc);
return left;
visibilityCombo.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
ToolWindowManager.getInstance(project).activateEditorComponent();
}
}
});
}
public void performInplaceIntroduce() {
@@ -115,7 +115,7 @@ public class InplaceIntroduceFieldPopup {
myEditor = editor;
myIntroduceFieldPanel =
new IntroduceFieldCentralPanel(parentClass, initializerExpression, localVariable, currentMethodConstructor, localVariable != null, aStatic,
new IntroduceFieldPopupPanel(parentClass, initializerExpression, localVariable, currentMethodConstructor, localVariable != null, aStatic,
myOccurrences.length, allowInitInMethod, allowInitInMethodIfAll, typeSelectorManager);
myWholePanel = new JPanel(new GridBagLayout());
@@ -17,7 +17,6 @@ package com.intellij.refactoring.introduceField;
import com.intellij.codeInsight.TestUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.JavaRefactoringSettings;
@@ -38,34 +37,27 @@ import java.awt.event.ItemListener;
* User: anna
* Date: 3/16/11
*/
public class IntroduceFieldCentralPanel {
private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.introduceField.IntroduceFieldDialog");
public abstract class IntroduceFieldCentralPanel {
protected static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.introduceField.IntroduceFieldDialog");
private static boolean ourLastCbFinalState = false;
protected static boolean ourLastCbFinalState = false;
private final PsiClass myParentClass;
private final PsiExpression myInitializerExpression;
private final PsiLocalVariable myLocalVariable;
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;
protected final PsiClass myParentClass;
protected final PsiExpression myInitializerExpression;
protected final PsiLocalVariable myLocalVariable;
protected final boolean myIsCurrentMethodConstructor;
protected final boolean myIsInvokedOnDeclaration;
protected final boolean myWillBeDeclaredStatic;
protected final int myOccurrencesCount;
protected final boolean myAllowInitInMethod;
protected final boolean myAllowInitInMethodIfAll;
protected final TypeSelectorManager myTypeSelectorManager;
private JCheckBox myCbReplaceAll;
private StateRestoringCheckBox myCbDeleteVariable;
private StateRestoringCheckBox myCbFinal;
private JRadioButton myRbInConstructor;
private JRadioButton myRbInCurrentMethod;
private JRadioButton myRbInFieldDeclaration;
private JRadioButton myRbInSetUp;
private JavaVisibilityPanel myVisibilityPanel;
public IntroduceFieldCentralPanel(PsiClass parentClass,
PsiExpression initializerExpression,
PsiLocalVariable localVariable,
@@ -84,79 +76,22 @@ public class IntroduceFieldCentralPanel {
myTypeSelectorManager = typeSelectorManager;
}
void initializeControls(PsiExpression initializerExpression, BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace) {
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);
}
protected abstract boolean setEnabledInitializationPlaces(PsiElement initializerPart, PsiElement initializer);
public abstract BaseExpressionToFieldHandler.InitializationPlace getInitializerPlace();
protected abstract void initializeInitializerPlace(PsiExpression initializerExpression,
BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace);
protected abstract JComponent createInitializerPlacePanel(ItemListener itemListener, ItemListener finalUpdater);
public abstract void setInitializeInFieldDeclaration();
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();
}
String ourLastVisibility = JavaRefactoringSettings.getInstance().INTRODUCE_FIELD_VISIBILITY;
myVisibilityPanel.setVisibility(ourLastVisibility);
public abstract void setVisibility(String visibility);
public abstract String getFieldVisibility();
public abstract void addVisibilityListener(ChangeListener changeListener);
protected void initializeControls(PsiExpression initializerExpression,
BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace) {
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);
}
}
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;
}
public String getFieldVisibility() {
return myVisibilityPanel.getVisibility();
}
public boolean isReplaceAllOccurrences() {
if (myIsInvokedOnDeclaration) return true;
@@ -174,29 +109,12 @@ public class IntroduceFieldCentralPanel {
return myCbFinal.isSelected();
}
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;
panel.add(createInitializerPlacePanel(), gbConstraints);
ItemListener itemListener = new ItemListener() {
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);
}
updateInitializerSelection();
}
updateTypeSelector();
}
@@ -206,13 +124,29 @@ public class IntroduceFieldCentralPanel {
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);
final JComponent initializerPlacePanel = createInitializerPlacePanel(itemListener, finalUpdater);
final JPanel checkboxes = appendCheckboxes(itemListener);
JPanel panel = composeWholePanel(initializerPlacePanel, checkboxes);
updateTypeSelector();
return panel;
}
protected abstract JPanel composeWholePanel(JComponent initializerPlacePanel, JPanel checkboxPanel);
protected void updateInitializerSelection() {
}
private JPanel appendCheckboxes(ItemListener itemListener) {
GridBagConstraints gbConstraints = new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1,1,0,0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0,0);
JPanel panel = new JPanel(new GridBagLayout());
myCbFinal = new StateRestoringCheckBox();
myCbFinal.setFocusable(false);
myCbFinal.setText(RefactoringBundle.message("declare.final"));
myCbFinal.addItemListener(itemListener);
gbConstraints.gridy++;
panel.add(myCbFinal, gbConstraints);
if (myOccurrencesCount > 1) {
myCbReplaceAll = new NonFocusableCheckBox();
myCbReplaceAll.setText(RefactoringBundle.message("replace.all.occurrences.of.expression.0.occurrences", myOccurrencesCount));
@@ -246,20 +180,7 @@ public class IntroduceFieldCentralPanel {
}
);
}
gbConstraints.insets = standardInsets;
}
myCbFinal.addItemListener(itemListener);
// myCbStatic.addItemListener(itemListener);
// myCbStatic.addItemListener(finalUpdater);
// myCbStatic.addItemListener(
// new ItemListener() {
// public void itemStateChanged(ItemEvent e) {
// updateNameList();
// }
// }
// );
updateTypeSelector();
return panel;
}
@@ -279,101 +200,15 @@ public class IntroduceFieldCentralPanel {
}
}
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));
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"));
myVisibilityPanel = new JavaVisibilityPanel(false, false);
myCbFinal = new StateRestoringCheckBox();
myCbFinal.setFocusable(false);
myCbFinal.setText(RefactoringBundle.message("declare.final"));
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);
// modifiersPanel.add(myCbFinal);
// modifiersPanel.add(myCbStatic);
JPanel groupPanel = new JPanel(new GridLayout(1, 2));
groupPanel.add(initializationPanel);
groupPanel.add(myVisibilityPanel);
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) {
if (!allowFinal()) {
myCbFinal.makeUnselectable(false);
} else {
myCbFinal.makeSelectable();
}
}
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;
}
protected boolean allowFinal() {
return true;
}
@@ -383,10 +218,6 @@ public class IntroduceFieldCentralPanel {
}
}
public void addVisibilityListener(ChangeListener changeListener) {
myVisibilityPanel.addListener(changeListener);
}
public void addFinalListener(ItemListener itemListener) {
myCbFinal.addItemListener(itemListener);
}
@@ -401,11 +232,11 @@ public class IntroduceFieldCentralPanel {
myCbFinal.setSelected(createFinal);
}
public void setInitializeInFieldDeclaration() {
myRbInFieldDeclaration.setSelected(true);
protected void enableFinal(boolean enable){
myCbFinal.setEnabled(enable);
}
public void setVisibility(String visibility) {
myVisibilityPanel.setVisibility(visibility);
}
}
@@ -68,7 +68,7 @@ class IntroduceFieldDialog extends DialogWrapper {
myParentClass = parentClass;
myInitializerExpression = initializerExpression;
myCentralPanel =
new IntroduceFieldCentralPanel(parentClass, initializerExpression, localVariable, isCurrentMethodConstructor, isInvokedOnDeclaration,
new IntroduceFieldDialogPanel(parentClass, initializerExpression, localVariable, isCurrentMethodConstructor, isInvokedOnDeclaration,
willBeDeclaredStatic, occurrencesCount, allowInitInMethod, allowInitInMethodIfAll,
typeSelectorManager);
myLocalVariable = localVariable;
@@ -0,0 +1,260 @@
/*
* 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.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.JavaRefactoringSettings;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.ui.JavaVisibilityPanel;
import com.intellij.refactoring.ui.TypeSelectorManager;
import com.intellij.ui.IdeBorderFactory;
import javax.swing.*;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ItemListener;
/**
* User: anna
* Date: 4/8/11
*/
public class IntroduceFieldDialogPanel extends IntroduceFieldCentralPanel {
private JRadioButton myRbInConstructor;
private JRadioButton myRbInCurrentMethod;
private JRadioButton myRbInFieldDeclaration;
private JRadioButton myRbInSetUp;
private JavaVisibilityPanel myVisibilityPanel;
public IntroduceFieldDialogPanel(PsiClass parentClass,
PsiExpression initializerExpression,
PsiLocalVariable localVariable,
boolean isCurrentMethodConstructor,
boolean isInvokedOnDeclaration,
boolean willBeDeclaredStatic,
int occurrencesCount,
boolean allowInitInMethod,
boolean allowInitInMethodIfAll,
TypeSelectorManager typeSelectorManager) {
super(parentClass, initializerExpression, localVariable, isCurrentMethodConstructor, isInvokedOnDeclaration, willBeDeclaredStatic,
occurrencesCount, allowInitInMethod, allowInitInMethodIfAll, typeSelectorManager);
}
protected void initializeControls(PsiExpression initializerExpression, BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace) {
super.initializeControls(initializerExpression, ourLastInitializerPlace);
initializeInitializerPlace(initializerExpression, ourLastInitializerPlace);
String ourLastVisibility = JavaRefactoringSettings.getInstance().INTRODUCE_FIELD_VISIBILITY;
myVisibilityPanel.setVisibility(ourLastVisibility);
}
protected void initializeInitializerPlace(PsiExpression initializerExpression,
BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace) {
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);
}
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();
}
}
private 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;
}
public String getFieldVisibility() {
return myVisibilityPanel.getVisibility();
}
protected JComponent createInitializerPlacePanel(ItemListener itemListener, ItemListener finalUpdater) {
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));
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);
myRbInConstructor.addItemListener(itemListener);
myRbInCurrentMethod.addItemListener(itemListener);
myRbInFieldDeclaration.addItemListener(itemListener);
myRbInConstructor.addItemListener(finalUpdater);
myRbInCurrentMethod.addItemListener(finalUpdater);
myRbInFieldDeclaration.addItemListener(finalUpdater);
if (myRbInSetUp != null) myRbInSetUp.addItemListener(finalUpdater);
// modifiersPanel.add(myCbFinal);
// modifiersPanel.add(myCbStatic);
JPanel groupPanel = new JPanel(new GridLayout(1, 2));
groupPanel.add(initializationPanel);
myVisibilityPanel = new JavaVisibilityPanel(false, false);
groupPanel.add(myVisibilityPanel);
mainPanel.add(groupPanel, BorderLayout.CENTER);
return mainPanel;
}
protected 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);
enableFinal(false);
return false;
}
}
}
PsiElement[] children = initializerPart.getChildren();
for (PsiElement child : children) {
if (!setEnabledInitializationPlaces(child, initializer)) return false;
}
return true;
}
public void addVisibilityListener(ChangeListener changeListener) {
myVisibilityPanel.addListener(changeListener);
}
public void setInitializeInFieldDeclaration() {
myRbInFieldDeclaration.setSelected(true);
}
public void setVisibility(String visibility) {
myVisibilityPanel.setVisibility(visibility);
}
@Override
protected boolean allowFinal() {
boolean allowFinal = myRbInFieldDeclaration.isSelected() || (myRbInConstructor.isSelected() && !myWillBeDeclaredStatic);
if (myRbInCurrentMethod.isSelected() && myIsCurrentMethodConstructor) {
final PsiMethod[] constructors = myParentClass.getConstructors();
allowFinal = constructors.length <= 1;
}
return allowFinal;
}
@Override
protected void updateInitializerSelection() {
myRbInCurrentMethod.setEnabled(myAllowInitInMethodIfAll || !isReplaceAllOccurrences());
if (!myRbInCurrentMethod.isEnabled() && myRbInCurrentMethod.isSelected()) {
myRbInCurrentMethod.setSelected(false);
myRbInFieldDeclaration.setSelected(true);
}
}
protected JPanel composeWholePanel(JComponent initializerPlacePanel, JPanel checkboxPanel) {
JPanel panel = new JPanel(new GridBagLayout());
final GridBagConstraints constraints =
new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0);
panel.add(initializerPlacePanel, constraints);
panel.add(checkboxPanel, constraints);
return panel;
}
}
@@ -0,0 +1,246 @@
/*
* 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.ide.ui.ListCellRendererWrapper;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.JavaRefactoringSettings;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.ui.TypeSelectorManager;
import javax.swing.*;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.*;
/**
* User: anna
* Date: 4/8/11
*/
public class IntroduceFieldPopupPanel extends IntroduceFieldCentralPanel {
private JComboBox myInitializerCombo;
private JComboBox myVisibilityCombo;
private DefaultComboBoxModel myInitialisersPlaceModel;
public IntroduceFieldPopupPanel(PsiClass parentClass,
PsiExpression initializerExpression,
PsiLocalVariable localVariable,
boolean isCurrentMethodConstructor,
boolean isInvokedOnDeclaration,
boolean willBeDeclaredStatic,
int occurrencesCount,
boolean allowInitInMethod,
boolean allowInitInMethodIfAll,
TypeSelectorManager typeSelectorManager) {
super(parentClass, initializerExpression, localVariable, isCurrentMethodConstructor, isInvokedOnDeclaration, willBeDeclaredStatic,
occurrencesCount, allowInitInMethod, allowInitInMethodIfAll, typeSelectorManager);
}
protected void initializeControls(PsiExpression initializerExpression, BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace) {
super.initializeControls(initializerExpression, ourLastInitializerPlace);
initializeInitializerPlace(initializerExpression, ourLastInitializerPlace);
String ourLastVisibility = JavaRefactoringSettings.getInstance().INTRODUCE_FIELD_VISIBILITY;
setVisibility(ourLastVisibility);
}
protected void initializeInitializerPlace(PsiExpression initializerExpression,
BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace) {
if (initializerExpression != null) {
setEnabledInitializationPlaces(initializerExpression, initializerExpression);
if (!myAllowInitInMethod) {
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_CURRENT_METHOD);
}
} else {
myInitialisersPlaceModel.removeAllElements();
}
final PsiMethod setUpMethod = TestUtil.findSetUpMethod(myParentClass);
final boolean setupEnabled = myInitialisersPlaceModel.getIndexOf(BaseExpressionToFieldHandler.InitializationPlace.IN_SETUP_METHOD) > -1;
if (myInitializerExpression != null && PsiTreeUtil.isAncestor(setUpMethod, myInitializerExpression, false) && setupEnabled ||
ourLastInitializerPlace == BaseExpressionToFieldHandler.InitializationPlace.IN_SETUP_METHOD && TestUtil.isTestClass(myParentClass) &&
setupEnabled) {
myInitialisersPlaceModel.setSelectedItem(BaseExpressionToFieldHandler.InitializationPlace.IN_SETUP_METHOD);
}
else if (ourLastInitializerPlace == BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR) {
if (myInitialisersPlaceModel.getIndexOf(BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR) > -1) {
myInitialisersPlaceModel.setSelectedItem(BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR);
} else {
selectInCurrentMethod();
}
} else if (ourLastInitializerPlace == BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION) {
if (myInitialisersPlaceModel.getIndexOf(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION) > -1) {
myInitialisersPlaceModel.setSelectedItem(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION);
} else {
selectInCurrentMethod();
}
} else {
selectInCurrentMethod();
}
}
private void selectInCurrentMethod() {
if (myInitialisersPlaceModel.getIndexOf(BaseExpressionToFieldHandler.InitializationPlace.IN_CURRENT_METHOD) > -1) {
myInitialisersPlaceModel.setSelectedItem(BaseExpressionToFieldHandler.InitializationPlace.IN_CURRENT_METHOD);
}
else if (myInitialisersPlaceModel.getIndexOf(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION) > -1) {
myInitialisersPlaceModel.setSelectedItem(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION);
}
else {
myInitialisersPlaceModel.setSelectedItem(BaseExpressionToFieldHandler.InitializationPlace.IN_CURRENT_METHOD);
}
}
public BaseExpressionToFieldHandler.InitializationPlace getInitializerPlace() {
return (BaseExpressionToFieldHandler.InitializationPlace)myInitializerCombo.getSelectedItem();
}
public String getFieldVisibility() {
return (String)myVisibilityCombo.getSelectedItem();
}
protected JComponent createInitializerPlacePanel(final ItemListener itemListener, final ItemListener finalUpdater) {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel groupPanel = new JPanel(new GridBagLayout());
final GridBagConstraints gridBagConstraints =
new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
new Insets(0, 5, 0, 0), 0, 0);
gridBagConstraints.insets.top = 5;
final JLabel initLabel = new JLabel(RefactoringBundle.message("initialize.in.border.title") + ":");
initLabel.setDisplayedMnemonic('i');
groupPanel.add(initLabel, gridBagConstraints);
myInitialisersPlaceModel = new DefaultComboBoxModel();
myInitialisersPlaceModel.addElement(BaseExpressionToFieldHandler.InitializationPlace.IN_CURRENT_METHOD);
myInitialisersPlaceModel.addElement(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION);
myInitialisersPlaceModel.addElement(BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR);
if (TestUtil.isTestClass(myParentClass)) {
myInitialisersPlaceModel.addElement(BaseExpressionToFieldHandler.InitializationPlace.IN_SETUP_METHOD);
}
myInitializerCombo = new JComboBox(myInitialisersPlaceModel);
InplaceIntroduceConstantPopup.appendActions(myInitializerCombo, myParentClass.getProject());
initLabel.setLabelFor(myInitializerCombo);
myInitializerCombo.setRenderer(new ListCellRendererWrapper<BaseExpressionToFieldHandler.InitializationPlace>(myInitializerCombo) {
@Override
public void customize(JList list,
BaseExpressionToFieldHandler.InitializationPlace value,
int index,
boolean selected,
boolean hasFocus) {
if (value == BaseExpressionToFieldHandler.InitializationPlace.IN_CURRENT_METHOD) {
setText("current method");
} else if (value == BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR) {
setText("constructor");
} else if (value == BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION) {
setText("field declaration");
} else {
setText("setUp");
}
}
});
myInitializerCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
itemListener.itemStateChanged(null);
finalUpdater.itemStateChanged(null);
}
});
gridBagConstraints.gridx = 1;
gridBagConstraints.insets.top = 0;
gridBagConstraints.insets.left = 0;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
groupPanel.add(myInitializerCombo, gridBagConstraints);
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.insets.top = 8;
gridBagConstraints.insets.left = 5;
myVisibilityCombo = InplaceIntroduceConstantPopup.createVisibilityCombo(groupPanel, gridBagConstraints, myParentClass.getProject());
mainPanel.add(groupPanel, BorderLayout.CENTER);
return mainPanel;
}
protected 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)) {
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION);
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR);
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_SETUP_METHOD);
enableFinal(false);
return false;
}
}
}
PsiElement[] children = initializerPart.getChildren();
for (PsiElement child : children) {
if (!setEnabledInitializationPlaces(child, initializer)) return false;
}
return true;
}
public void addVisibilityListener(final ChangeListener changeListener) {
myVisibilityCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
changeListener.stateChanged(null);
}
});
}
public void setInitializeInFieldDeclaration() {
myInitializerCombo.setSelectedItem(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION);
}
public void setVisibility(String visibility) {
myVisibilityCombo.setSelectedItem(visibility);
}
@Override
protected boolean allowFinal() {
final Object selectedItem = myInitializerCombo.getSelectedItem();
boolean allowFinal = selectedItem == BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION ||
(selectedItem == BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR && !myWillBeDeclaredStatic);
if (selectedItem == BaseExpressionToFieldHandler.InitializationPlace.IN_CURRENT_METHOD && myIsCurrentMethodConstructor) {
final PsiMethod[] constructors = myParentClass.getConstructors();
allowFinal = constructors.length <= 1;
}
return allowFinal;
}
protected JPanel composeWholePanel(JComponent initializerPlacePanel, JPanel checkboxPanel) {
final JPanel panel = new JPanel(new GridBagLayout());
final GridBagConstraints constraints =
new GridBagConstraints(GridBagConstraints.RELATIVE, 0, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0);
constraints.insets.bottom = 5;
panel.add(initializerPlacePanel, constraints);
constraints.insets.left = 5;
panel.add(checkboxPanel, constraints);
return panel;
}
}
@@ -368,7 +368,7 @@ replace.all.occurences.checkbox=Replace &all occurrences
introduce.constant.introduce.to.class=Introduce to class (fully qualified name):
introduce.field.static.field.of.type=Static field of &type:
introduce.field.field.of.type=Field of &type:
replace.all.occurrences.of.expression.0.occurrences=Replace &all occurrences of expression ({0} occurrences)
replace.all.occurrences.of.expression.0.occurrences=Replace &all occurrences ({0})
delete.variable.declaration=&Delete variable declaration
initialize.in.border.title=Initialize in
setup.method.radio=&setUp method