inplace introduce: show dialog on second keystroke

This commit is contained in:
anna
2011-06-27 20:23:34 +04:00
parent 82a8824ce1
commit f475e7d57a
24 changed files with 219 additions and 361 deletions

View File

@@ -206,7 +206,7 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
final Runnable runnable =
new ConvertToFieldRunnable(selectedExpr, settings, type, occurrences, occurenceManager,
new ConvertToFieldRunnable(settings.getSelectedExpr(), settings, type, settings.getOccurrences(), occurenceManager,
anchorStatementIfAll, tempAnchorElement, editor,
myParentClass);
@@ -482,6 +482,8 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
private final TargetDestination myTargetClass;
private final boolean myAnnotateAsNonNls;
private final boolean myIntroduceEnumConstant;
private PsiExpression mySelectedExpr;
private PsiExpression[] myOccurrences;
public PsiLocalVariable getLocalVariable() {
return myLocalVariable;
@@ -534,7 +536,10 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
return myIntroduceEnumConstant;
}
public Settings(String fieldName, boolean replaceAll,
public Settings(String fieldName,
PsiExpression selectedExpr,
PsiExpression[] occurrences,
boolean replaceAll,
boolean declareStatic, boolean declareFinal,
InitializationPlace initializerPlace, String visibility, PsiLocalVariable localVariableToRemove, PsiType forcedType,
boolean deleteLocalVariable,
@@ -543,6 +548,8 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
final boolean introduceEnumConstant) {
myFieldName = fieldName;
myOccurrences = occurrences;
mySelectedExpr = selectedExpr;
myReplaceAll = replaceAll;
myDeclareStatic = declareStatic;
myDeclareFinal = declareFinal;
@@ -556,7 +563,10 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
myIntroduceEnumConstant = introduceEnumConstant;
}
public Settings(String fieldName, boolean replaceAll,
public Settings(String fieldName,
PsiExpression selectedExpression,
PsiExpression[] occurrences,
boolean replaceAll,
boolean declareStatic, boolean declareFinal,
InitializationPlace initializerPlace, String visibility, PsiLocalVariable localVariableToRemove, PsiType forcedType,
boolean deleteLocalVariable,
@@ -564,9 +574,20 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
final boolean annotateAsNonNls,
final boolean introduceEnumConstant) {
this(fieldName, replaceAll, declareStatic, declareFinal, initializerPlace, visibility, localVariableToRemove, forcedType, deleteLocalVariable, new TargetDestination(targetClass), annotateAsNonNls, introduceEnumConstant);
this(fieldName, selectedExpression, occurrences, replaceAll, declareStatic, declareFinal, initializerPlace, visibility, localVariableToRemove, forcedType, deleteLocalVariable, new TargetDestination(targetClass), annotateAsNonNls, introduceEnumConstant);
}
public PsiExpression getSelectedExpr() {
return mySelectedExpr;
}
public PsiExpression[] getOccurrences() {
return myOccurrences;
}
public void setOccurrences(PsiExpression[] occurrences) {
myOccurrences = occurrences;
}
}
public static class TargetDestination {

View File

@@ -285,6 +285,8 @@ public class InplaceIntroduceConstantPopup extends AbstractJavaInplaceIntroducer
protected void performIntroduce() {
final BaseExpressionToFieldHandler.Settings settings =
new BaseExpressionToFieldHandler.Settings(getInputName(),
getExpr(),
getOccurrences(),
isReplaceAllOccurrences(), true,
true,
BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION,

View File

@@ -62,6 +62,7 @@ public class InplaceIntroduceFieldPopup extends AbstractJavaInplaceIntroducer {
private JPanel myWholePanel;
static BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace;
private JLabel myLabel = new JLabel("###################");
public InplaceIntroduceFieldPopup(PsiLocalVariable localVariable,
PsiClass parentClass,
@@ -98,28 +99,22 @@ public class InplaceIntroduceFieldPopup extends AbstractJavaInplaceIntroducer {
new IntroduceFieldPopupPanel(parentClass, initializerExpression, localVariable, currentMethodConstructor, localVariable != null, aStatic,
myOccurrences, allowInitInMethod, allowInitInMethodIfAll, typeSelectorManager);
myWholePanel = new JPanel(new GridBagLayout());
myWholePanel = new JPanel(new BorderLayout());
myWholePanel.setBorder(null);
myLabel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 0));
myLabel.setFont(myLabel.getFont().deriveFont(Font.BOLD));
myWholePanel.add(myLabel, BorderLayout.NORTH);
GridBagConstraints gc =
new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(0,0,0,0), 0, 0);
final JComponent centerPanel = myIntroduceFieldPanel.createCenterPanel();
myWholePanel.add(centerPanel, BorderLayout.WEST);
gc.gridy++;
gc.insets.top = 5;
myWholePanel.add(myIntroduceFieldPanel.createCenterPanel(), gc);
JComponent typeChooser = typeComponent();
if (typeChooser != null) {
gc.gridy++ ;
gc.insets.left = 5;
gc.insets.right = 5;
myWholePanel.add(typeChooser, gc);
}
myIntroduceFieldPanel.initializeControls(initializerExpression, ourLastInitializerPlace);
}
@Override
protected void updateTitle(PsiVariable variable) {
myLabel.setText(variable.getText());
}
protected PsiField createFieldToStartTemplateOn(final String[] names,
@@ -190,39 +185,6 @@ public class InplaceIntroduceFieldPopup extends AbstractJavaInplaceIntroducer {
@Override
protected JComponent getComponent() {
final VisibilityListener visibilityListener = new VisibilityListener(myEditor) {
@Override
protected String getVisibility() {
return myIntroduceFieldPanel.getFieldVisibility();
}
};
myIntroduceFieldPanel.addVisibilityListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
new WriteCommandAction(myProject, getCommandName(), getCommandName()) {
@Override
protected void run(Result result) throws Throwable {
PsiDocumentManager.getInstance(myProject).commitDocument(myEditor.getDocument());
visibilityListener.perform(getVariable());
updateTitle(getVariable());
}
}.execute();
}
});
final FinalListener finalListener = new FinalListener(myEditor);
myIntroduceFieldPanel.addFinalListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
new WriteCommandAction(myProject, getCommandName(), getCommandName()){
@Override
protected void run(Result result) throws Throwable {
PsiDocumentManager.getInstance(myProject).commitDocument(myEditor.getDocument());
finalListener.perform(myIntroduceFieldPanel.isDeclareFinal(), getVariable());
updateTitle(getVariable());
}
}.execute();
}
});
myIntroduceFieldPanel.addOccurrenceListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
@@ -266,7 +228,10 @@ public class InplaceIntroduceFieldPopup extends AbstractJavaInplaceIntroducer {
protected void performIntroduce() {
ourLastInitializerPlace = myIntroduceFieldPanel.getInitializerPlace();
final BaseExpressionToFieldHandler.Settings settings =
new BaseExpressionToFieldHandler.Settings(getInputName(), myIntroduceFieldPanel.isReplaceAllOccurrences(), myStatic,
new BaseExpressionToFieldHandler.Settings(getInputName(),
getExpr(),
getOccurrences(),
myIntroduceFieldPanel.isReplaceAllOccurrences(), myStatic,
myIntroduceFieldPanel.isDeclareFinal(),
myIntroduceFieldPanel.getInitializerPlace(),
myIntroduceFieldPanel.getFieldVisibility(), (PsiLocalVariable)getLocalVariable(),

View File

@@ -30,6 +30,7 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.refactoring.HelpID;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer;
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.refactoring.util.RefactoringUtil;
@@ -146,10 +147,18 @@ public class IntroduceConstantHandler extends BaseExpressionToFieldHandler {
final TypeSelectorManagerImpl typeSelectorManager = new TypeSelectorManagerImpl(project, type, containingMethod, expr, occurences);
if (editor != null && editor.getSettings().isVariableInplaceRenameEnabled() && (expr == null || expr.isPhysical())) {
if (new InplaceIntroduceConstantPopup(project, editor, parentClass, expr, localVariable, occurences, typeSelectorManager,
anchorElement, anchorElementIfAll,
expr != null ? createOccurenceManager(expr, parentClass) : null).startInplaceIntroduceTemplate() ){
return null;
final AbstractInplaceIntroducer activeIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
if (activeIntroducer == null) {
if (new InplaceIntroduceConstantPopup(project, editor, parentClass, expr, localVariable, occurences, typeSelectorManager,
anchorElement, anchorElementIfAll,
expr != null ? createOccurenceManager(expr, parentClass) : null).startInplaceIntroduceTemplate() ){
return null;
}
} else {
AbstractInplaceIntroducer.stopIntroduce(editor);
expr = (PsiExpression)activeIntroducer.getExpr();
localVariable = (PsiLocalVariable)activeIntroducer.getLocalVariable();
occurences = (PsiExpression[])activeIntroducer.getOccurrences();
}
}
@@ -164,10 +173,11 @@ public class IntroduceConstantHandler extends BaseExpressionToFieldHandler {
}
return null;
}
return new Settings(dialog.getEnteredName(), dialog.isReplaceAllOccurrences(), true, true,
BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION, dialog.getFieldVisibility(), localVariable,
dialog.getSelectedType(), dialog.isDeleteVariable(), dialog.getDestinationClass(), dialog.isAnnotateAsNonNls(),
dialog.introduceEnumConstant());
return new Settings(dialog.getEnteredName(), expr, occurences, dialog.isReplaceAllOccurrences(), true, true,
InitializationPlace.IN_FIELD_DECLARATION, dialog.getFieldVisibility(), localVariable,
dialog.getSelectedType(), dialog.isDeleteVariable(), dialog.getDestinationClass(),
dialog.isAnnotateAsNonNls(),
dialog.introduceEnumConstant());
}
private static void highlightError(Project project, Editor editor, PsiElement errorElement) {

View File

@@ -93,7 +93,6 @@ public abstract class IntroduceFieldCentralPanel {
public abstract void setVisibility(String visibility);
public abstract String getFieldVisibility();
public abstract void addVisibilityListener(ChangeListener changeListener);
protected void initializeControls(PsiExpression initializerExpression,
BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace) {
@@ -152,7 +151,7 @@ public abstract class IntroduceFieldCentralPanel {
return true;
}
private JPanel appendCheckboxes(ItemListener itemListener) {
protected 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();
@@ -161,18 +160,7 @@ public abstract class IntroduceFieldCentralPanel {
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));
gbConstraints.gridy++;
panel.add(myCbReplaceAll, gbConstraints);
myCbReplaceAll.addItemListener(itemListener);
if (myIsInvokedOnDeclaration) {
myCbReplaceAll.setEnabled(false);
myCbReplaceAll.setSelected(true);
}
}
appendOccurrences(itemListener, gbConstraints, panel);
if (myLocalVariable != null) {
gbConstraints.gridy++;
@@ -199,6 +187,20 @@ public abstract class IntroduceFieldCentralPanel {
return panel;
}
public void appendOccurrences(ItemListener itemListener, GridBagConstraints gbConstraints, JPanel panel) {
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);
}
}
}
private void updateTypeSelector() {
if (myCbReplaceAll != null) {
myTypeSelectorManager.setAllOccurences(myCbReplaceAll.isSelected());
@@ -215,7 +217,7 @@ public abstract class IntroduceFieldCentralPanel {
}
}
private void updateCbFinal() {
protected void updateCbFinal() {
if (!allowFinal()) {
myCbFinal.makeUnselectable(false);
} else {
@@ -224,7 +226,8 @@ public abstract class IntroduceFieldCentralPanel {
}
protected boolean allowFinal() {
return !myHasWriteAccess && isReplaceAllOccurrences();
if (myHasWriteAccess && isReplaceAllOccurrences()) return false;
return true;
}
public void addOccurrenceListener(ItemListener itemListener) {
@@ -255,7 +258,7 @@ public abstract class IntroduceFieldCentralPanel {
public void saveFinalState() {
if (myCbFinal.isEnabled()) {
if (myCbFinal != null && myCbFinal.isEnabled()) {
ourLastCbFinalState = myCbFinal.isSelected();
}
}

View File

@@ -25,7 +25,6 @@ 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;
@@ -55,10 +54,10 @@ public class IntroduceFieldDialogPanel extends IntroduceFieldCentralPanel {
}
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);
super.initializeControls(initializerExpression, ourLastInitializerPlace);
}
protected void initializeInitializerPlace(PsiExpression initializerExpression,
@@ -217,10 +216,6 @@ public class IntroduceFieldDialogPanel extends IntroduceFieldCentralPanel {
return true;
}
public void addVisibilityListener(ChangeListener changeListener) {
myVisibilityPanel.addListener(changeListener);
}
public void setInitializeInFieldDeclaration() {
myRbInFieldDeclaration.setSelected(true);
}

View File

@@ -24,6 +24,7 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.refactoring.HelpID;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer;
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.refactoring.util.occurences.*;
@@ -104,12 +105,20 @@ public class IntroduceFieldHandler extends BaseExpressionToFieldHandler {
final TypeSelectorManagerImpl typeSelectorManager = new TypeSelectorManagerImpl(project, type, containingMethod, expr, occurences);
if (editor != null && editor.getSettings().isVariableInplaceRenameEnabled() && (expr == null || expr.isPhysical())) {
myInplaceIntroduceFieldPopup =
new InplaceIntroduceFieldPopup(localVariable, parentClass, declareStatic, currentMethodConstructor, occurences, expr, typeSelectorManager, editor,
allowInitInMethod, allowInitInMethodIfAll, anchorElement, anchorElementIfAll, expr != null ? createOccurenceManager(expr, parentClass) : null,
project);
if (myInplaceIntroduceFieldPopup.startInplaceIntroduceTemplate()) {
return null;
final AbstractInplaceIntroducer activeIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
if (activeIntroducer == null) {
myInplaceIntroduceFieldPopup =
new InplaceIntroduceFieldPopup(localVariable, parentClass, declareStatic, currentMethodConstructor, occurences, expr, typeSelectorManager, editor,
allowInitInMethod, allowInitInMethodIfAll, anchorElement, anchorElementIfAll, expr != null ? createOccurenceManager(expr, parentClass) : null,
project);
if (myInplaceIntroduceFieldPopup.startInplaceIntroduceTemplate()) {
return null;
}
} else {
AbstractInplaceIntroducer.stopIntroduce(editor);
expr = (PsiExpression)activeIntroducer.getExpr();
localVariable = (PsiLocalVariable)activeIntroducer.getLocalVariable();
occurences = (PsiExpression[])activeIntroducer.getOccurrences();
}
}
@@ -134,11 +143,11 @@ public class IntroduceFieldHandler extends BaseExpressionToFieldHandler {
}
return new Settings(dialog.getEnteredName(), dialog.isReplaceAllOccurrences(),
declareStatic, dialog.isDeclareFinal(),
dialog.getInitializerPlace(), dialog.getFieldVisibility(),
localVariable,
dialog.getFieldType(), localVariable != null, (TargetDestination)null, false, false);
return new Settings(dialog.getEnteredName(), expr, occurences, dialog.isReplaceAllOccurrences(),
declareStatic, dialog.isDeclareFinal(),
dialog.getInitializerPlace(), dialog.getFieldVisibility(),
localVariable,
dialog.getFieldType(), localVariable != null, (TargetDestination)null, false, false);
}
public InplaceIntroduceFieldPopup getInplaceIntroduceFieldPopup() {

View File

@@ -25,7 +25,6 @@ import com.intellij.refactoring.ui.TypeSelectorManager;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.*;
@@ -65,28 +64,31 @@ public class IntroduceFieldPopupPanel extends IntroduceFieldCentralPanel {
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) {
if (setupEnabled && (myInitializerExpression != null && PsiTreeUtil.isAncestor(setUpMethod, myInitializerExpression, false) ||
TestUtil.isTestClass(myParentClass))) {
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 {
else if (myInitialisersPlaceModel.getIndexOf(BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR) > -1 && myParentClass.getConstructors().length > 0) {
myInitialisersPlaceModel.setSelectedItem(BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR);
}
else if (myInitialisersPlaceModel.getIndexOf(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION) > -1) {
myInitialisersPlaceModel.setSelectedItem(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION);
}
else {
selectInCurrentMethod();
}
}
@Override
protected void initializeControls(PsiExpression initializerExpression,
BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace) {
}
@Override
public boolean isDeclareFinal() {
return allowFinal();
}
private void selectInCurrentMethod() {
if (myInitialisersPlaceModel.getIndexOf(BaseExpressionToFieldHandler.InitializationPlace.IN_CURRENT_METHOD) > -1) {
myInitialisersPlaceModel.setSelectedItem(BaseExpressionToFieldHandler.InitializationPlace.IN_CURRENT_METHOD);
@@ -107,7 +109,11 @@ public class IntroduceFieldPopupPanel extends IntroduceFieldCentralPanel {
}
public String getFieldVisibility() {
return (String)myVisibilityCombo.getSelectedItem();
String visibility = JavaRefactoringSettings.getInstance().INTRODUCE_FIELD_VISIBILITY;
if (visibility == null) {
visibility = PsiModifier.PRIVATE;
}
return visibility;
}
protected JComponent createInitializerPlacePanel(final ItemListener itemListener, final ItemListener finalUpdater) {
@@ -116,10 +122,8 @@ public class IntroduceFieldPopupPanel extends IntroduceFieldCentralPanel {
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;
new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0);
myInitialisersPlaceModel = new DefaultComboBoxModel();
myInitialisersPlaceModel.addElement(BaseExpressionToFieldHandler.InitializationPlace.IN_CURRENT_METHOD);
@@ -132,6 +136,8 @@ public class IntroduceFieldPopupPanel extends IntroduceFieldCentralPanel {
if (myInitialisersPlaceModel.getSize() > 1) {
final JLabel initLabel = new JLabel(RefactoringBundle.message("initialize.in.border.title") + ":");
initLabel.setDisplayedMnemonic('i');
gridBagConstraints.insets.left = 5;
gridBagConstraints.anchor = GridBagConstraints.WEST;
groupPanel.add(initLabel, gridBagConstraints);
JComboBox initializersCombo = new JComboBox(myInitialisersPlaceModel);
InplaceCombosUtil.appendActions(initializersCombo, myParentClass.getProject());
@@ -156,32 +162,18 @@ public class IntroduceFieldPopupPanel extends IntroduceFieldCentralPanel {
gridBagConstraints.gridx = 1;
gridBagConstraints.insets.top = 0;
gridBagConstraints.insets.left = 0;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
groupPanel.add(initializersCombo, gridBagConstraints);
myInitializerCombo = initializersCombo;
} else if (myInitialisersPlaceModel.getSize() == 1){
} /*else if (myInitialisersPlaceModel.getSize() == 1){
gridBagConstraints.gridwidth = 2;
groupPanel.add(new JLabel("Initialize field in " +
getPresentableText((BaseExpressionToFieldHandler.InitializationPlace)myInitialisersPlaceModel.getElementAt(0))),
gridBagConstraints);
gridBagConstraints.gridwidth = 1;
}
}*/
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.insets.top = 8;
gridBagConstraints.insets.left = 5;
String visibility = JavaRefactoringSettings.getInstance().INTRODUCE_FIELD_VISIBILITY;
if (visibility == null) {
visibility = PsiModifier.PRIVATE;
}
myVisibilityCombo = InplaceCombosUtil.createVisibilityCombo(groupPanel, gridBagConstraints, myParentClass.getProject(),
visibility);
mainPanel.add(groupPanel, BorderLayout.CENTER);
return mainPanel;
return groupPanel;
}
@Nullable
@@ -220,15 +212,6 @@ public class IntroduceFieldPopupPanel extends IntroduceFieldCentralPanel {
return true;
}
public void addVisibilityListener(final ChangeListener changeListener) {
myVisibilityCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
changeListener.stateChanged(null);
}
});
}
public void setInitializeInFieldDeclaration() {
LOG.assertTrue(myInitializerCombo != null);
myInitializerCombo.setSelectedItem(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION);
@@ -238,6 +221,10 @@ public class IntroduceFieldPopupPanel extends IntroduceFieldCentralPanel {
myVisibilityCombo.setSelectedItem(visibility);
}
@Override
protected void updateCbFinal() {
}
@Override
protected boolean allowFinal() {
final Object selectedItem = getInitializerPlace();
@@ -255,14 +242,20 @@ public class IntroduceFieldPopupPanel extends IntroduceFieldCentralPanel {
return false;
}
@Override
protected JPanel appendCheckboxes(ItemListener itemListener) {
final JPanel panel = new JPanel(new GridBagLayout());
appendOccurrences(itemListener, new GridBagConstraints(0,0,1,1,0,0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0,0), panel);
return panel;
}
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 GridBagConstraints(0, 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;
constraints.gridy++;
panel.add(checkboxPanel, constraints);
return panel;
}

View File

@@ -49,7 +49,9 @@ import com.intellij.psi.codeStyle.SuggestedNameInfo;
import com.intellij.psi.codeStyle.VariableKind;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.*;
import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer;
import com.intellij.refactoring.introduceField.ElementToWorkOn;
import com.intellij.refactoring.introduceField.InplaceIntroduceFieldPopup;
import com.intellij.refactoring.ui.MethodCellRenderer;
import com.intellij.refactoring.ui.NameSuggestionsGenerator;
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
@@ -343,7 +345,7 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase implements R
private final Project myProject;
private final PsiExpression myExpr;
private PsiExpression myExpr;
private PsiLocalVariable myLocalVar;
private final Editor myEditor;
@@ -367,12 +369,7 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase implements R
else { // local variable
occurences = CodeInsightUtil.findReferenceExpressions(method, myLocalVar);
}
PsiExpression expressionToRemoveParamFrom = myExpr;
if (myExpr == null) {
expressionToRemoveParamFrom = myLocalVar.getInitializer();
}
TIntArrayList parametersToRemove = expressionToRemoveParamFrom == null ? new TIntArrayList() : Util
.findParametersToRemove(method, expressionToRemoveParamFrom, occurences);
boolean mustBeFinal = false;
if (myLocalVar != null) {
@@ -401,22 +398,30 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase implements R
? new TypeSelectorManagerImpl(myProject, initializerType, myExpr, occurences)
: new TypeSelectorManagerImpl(myProject, initializerType, occurences);
NameSuggestionsGenerator nameSuggestionsGenerator = createNameSuggestionGenerator(myExpr, propName, myProject);
boolean isInplaceAvailableOnDataContext = myEditor != null && myEditor.getSettings().isVariableInplaceRenameEnabled();
if (myExpr != null) {
isInplaceAvailableOnDataContext &= myExpr.isPhysical();
}
if (isInplaceAvailableOnDataContext) {
myInplaceIntroduceParameterPopup =
new InplaceIntroduceParameterPopup(myProject, myEditor, classMemberRefs,
typeSelectorManager,
myExpr, myLocalVar, method, methodToSearchFor, occurences,
parametersToRemove,
mustBeFinal);
if (myInplaceIntroduceParameterPopup.startInplaceIntroduceTemplate()) {
return;
final AbstractInplaceIntroducer activeIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(myEditor);
if (activeIntroducer == null) {
myInplaceIntroduceParameterPopup =
new InplaceIntroduceParameterPopup(myProject, myEditor, classMemberRefs,
typeSelectorManager,
myExpr, myLocalVar, method, methodToSearchFor, occurences,
getParamsToRemove(method, occurences),
mustBeFinal);
if (myInplaceIntroduceParameterPopup.startInplaceIntroduceTemplate()) {
return;
}
}
else {
AbstractInplaceIntroducer.stopIntroduce(myEditor);
myExpr = (PsiExpression)activeIntroducer.getExpr();
myLocalVar = (PsiLocalVariable)activeIntroducer.getLocalVariable();
occurences = (PsiExpression[])activeIntroducer.getOccurrences();
}
}
if (ApplicationManager.getApplication().isUnitTestMode()) {
@@ -427,18 +432,28 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase implements R
new IntroduceParameterProcessor(myProject, method, methodToSearchFor, initializer, myExpr, myLocalVar, isDeleteLocalVariable, parameterName,
replaceAllOccurences, IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, mustBeFinal,
false, null,
parametersToRemove).run();
getParamsToRemove(method, occurences)).run();
} else {
if (myEditor != null) {
RefactoringUtil.highlightAllOccurences(myProject, occurences, myEditor);
}
new IntroduceParameterDialog(myProject, classMemberRefs, occurences, myLocalVar, myExpr, nameSuggestionsGenerator,
typeSelectorManager, methodToSearchFor, method, parametersToRemove, mustBeFinal).show();
new IntroduceParameterDialog(myProject, classMemberRefs, occurences, myLocalVar, myExpr,
createNameSuggestionGenerator(myExpr, propName, myProject),
typeSelectorManager, methodToSearchFor, method, getParamsToRemove(method, occurences), mustBeFinal).show();
if (myEditor != null) {
myEditor.getSelectionModel().removeSelection();
}
}
}
private TIntArrayList getParamsToRemove(PsiMethod method, PsiExpression[] occurences) {
PsiExpression expressionToRemoveParamFrom = myExpr;
if (myExpr == null) {
expressionToRemoveParamFrom = myLocalVar.getInitializer();
}
return expressionToRemoveParamFrom == null ? new TIntArrayList() : Util
.findParametersToRemove(method, expressionToRemoveParamFrom, occurences);
}
}
public InplaceIntroduceParameterPopup getInplaceIntroduceParameterPopup() {

View File

@@ -1,21 +0,0 @@
/*
* 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.
*/
class Test {
void simpleMethod() {
System.out.println("<caret>");
System.out.println("");
}
}

View File

@@ -1,24 +0,0 @@
/*
* 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.
*/
class Test {
private final String x = "";
void simpleMethod() {
System.out.println(x);
System.out.println("");
}
}

View File

@@ -1,21 +0,0 @@
/*
* 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.
*/
class Test {
void simpleMethod() {
System.out.println("<caret>");
System.out.println("");
}
}

View File

@@ -1,22 +0,0 @@
/*
* 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.
*/
class Test {
void simpleMethod() {
String x<caret> = "";
System.out.println(x);
System.out.println("");
}
}

View File

@@ -1,25 +0,0 @@
/*
* 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.
*/
class Test {
protected String x;
void simpleMethod() {
x = "";
System.out.println(x);
System.out.println("");
}
}

View File

@@ -1,25 +0,0 @@
/*
* 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.
*/
class Test {
protected String x;
void simpleMethod() {
x = "";
System.out.println(x);
System.out.println("");
}
}

View File

@@ -15,10 +15,9 @@
*/
class Test {
private String x;
private final String x = "";
void simpleMethod() {
x = "";
System.out.println(x);
System.out.println(x);
}

View File

@@ -48,24 +48,6 @@ public class InplaceIntroduceFieldTest extends LightCodeInsightTestCase {
});
}
public void testMakeFinal() throws Exception {
final boolean oldCreateFinals = IntroduceFieldCentralPanel.ourLastCbFinalState;
try {
IntroduceFieldCentralPanel.ourLastCbFinalState = false;
doTest(new Pass<InplaceIntroduceFieldPopup>() {
@Override
public void pass(InplaceIntroduceFieldPopup inplaceIntroduceFieldPopup) {
inplaceIntroduceFieldPopup.setInitializeInFieldDeclaration();
inplaceIntroduceFieldPopup.setCreateFinal(true);
}
});
assertTrue(IntroduceFieldCentralPanel.ourLastCbFinalState);
}
finally {
IntroduceFieldCentralPanel.ourLastCbFinalState = oldCreateFinals;
}
}
public void testReplaceAll() throws Exception {
doTest(new Pass<InplaceIntroduceFieldPopup>() {
@@ -76,29 +58,6 @@ public class InplaceIntroduceFieldTest extends LightCodeInsightTestCase {
});
}
public void testMakeProtected() throws Exception {
doTest(new Pass<InplaceIntroduceFieldPopup>() {
@Override
public void pass(InplaceIntroduceFieldPopup inplaceIntroduceFieldPopup) {
inplaceIntroduceFieldPopup.setVisibility(PsiModifier.PACKAGE_LOCAL);
inplaceIntroduceFieldPopup.setVisibility(PsiModifier.PRIVATE);
inplaceIntroduceFieldPopup.setVisibility(PsiModifier.PROTECTED);
}
});
}
public void testMakeProtectedFromLocal() throws Exception {
doTest(new Pass<InplaceIntroduceFieldPopup>() {
@Override
public void pass(InplaceIntroduceFieldPopup inplaceIntroduceFieldPopup) {
inplaceIntroduceFieldPopup.setVisibility(PsiModifier.PACKAGE_LOCAL);
inplaceIntroduceFieldPopup.setVisibility(PsiModifier.PRIVATE);
inplaceIntroduceFieldPopup.setVisibility(PsiModifier.PROTECTED);
}
});
}
public void testEscapePosition() throws Exception {
doTestEscape();
}

View File

@@ -125,10 +125,10 @@ public class IntroduceConstantTest extends LightCodeInsightTestCase {
new TypeSelectorManagerImpl(project, type, PsiTreeUtil.getParentOfType(anchorElement, PsiMethod.class), expr, occurences);
final PsiType psiType = selectorManager.getDefaultType();
Assert.assertEquals(psiType.getCanonicalText(), expectedType);
return new Settings("xxx", true, true, true,
return new Settings("xxx", expr, occurences, true, true, true,
InitializationPlace.IN_FIELD_DECLARATION, getVisibility(), null, psiType, false,
parentClass, false, false);
}
}.invoke(getProject(), getEditor(), getFile(), null);
}
}
}

View File

@@ -93,7 +93,7 @@ public class IntroduceFieldWitSetUpInitializationTest extends CodeInsightTestCas
final PsiLocalVariable local,
final PsiExpression[] occurences,
final boolean isStatic) {
return new BaseExpressionToFieldHandler.Settings("i", true, false, false,
return new BaseExpressionToFieldHandler.Settings("i", null, occurences, true, false, false,
BaseExpressionToFieldHandler.InitializationPlace.IN_SETUP_METHOD,
PsiModifier.PRIVATE, local, local.getType(), true, (BaseExpressionToFieldHandler.TargetDestination)null, false,
false);

View File

@@ -20,7 +20,7 @@ public class MockIntroduceConstantHandler extends IntroduceConstantHandler{
protected Settings showRefactoringDialog(final Project project, final Editor editor, final PsiClass parentClass, final PsiExpression expr,
final PsiType type, final PsiExpression[] occurences, final PsiElement anchorElement,
final PsiElement anchorElementIfAll) {
return new Settings("xxx", true, true, true, InitializationPlace.IN_FIELD_DECLARATION, getVisibility(), null, null, false,
return new Settings("xxx", expr, occurences, true, true, true, InitializationPlace.IN_FIELD_DECLARATION, getVisibility(), null, null, false,
myTargetClass != null ? myTargetClass : parentClass, false, false);
}

View File

@@ -24,7 +24,7 @@ public class MockIntroduceFieldHandler extends IntroduceFieldHandler {
protected Settings showRefactoringDialog(Project project, Editor editor, PsiClass parentClass, PsiExpression expr, PsiType type,
PsiExpression[] occurences, PsiElement anchorElement, PsiElement anchorElementIfAll) {
SuggestedNameInfo name = JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.FIELD, null, expr, type);
return new Settings(name.names[0], true, myDeclareStatic, true, myInitializationPlace,
return new Settings(name.names[0], expr, occurences, true, myDeclareStatic, true, myInitializationPlace,
PsiModifier.PUBLIC,
null,
getFieldType(type), true, (TargetDestination)null, false, false);

View File

@@ -21,7 +21,7 @@ public class MockLocalToFieldHandler extends LocalToFieldHandler {
@Override
protected BaseExpressionToFieldHandler.Settings showRefactoringDialog(PsiClass aClass, PsiLocalVariable local, PsiExpression[] occurences,
boolean isStatic) {
return new BaseExpressionToFieldHandler.Settings("xxx", true, isStatic, true, BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION,
return new BaseExpressionToFieldHandler.Settings("xxx", null, occurences, true, isStatic, true, BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION,
PsiModifier.PRIVATE, local, local.getType(), false, aClass, true, myMakeEnumConstant);
}
}

View File

@@ -30,6 +30,7 @@ import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
@@ -37,6 +38,7 @@ import com.intellij.psi.*;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.*;
@@ -53,6 +55,7 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
private final String myLocalName;
protected String myConstantName;
public static final Key<AbstractInplaceIntroducer> ACTIVE_INTRODUCE = Key.create("ACTIVE_INTRODUCE");
public AbstractInplaceIntroducer(Project project,
Editor editor,
@@ -123,6 +126,9 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
setElementToRename(variable);
started = AbstractInplaceIntroducer.super.performInplaceRename(false, nameSuggestions);
updateTitle(variable);
if (TemplateManagerImpl.getTemplateState(myEditor) != null) {
myEditor.putUserData(ACTIVE_INTRODUCE, AbstractInplaceIntroducer.this);
}
}
result.set(started);
if (!started && variable != null) {
@@ -140,9 +146,9 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
}
protected void updateTitle(V variable) {
if (myBalloon != null) {
/*if (myBalloon != null) {
myBalloon.setTitle(variable.getText());
}
}*/
}
public void restartInplaceIntroduceTemplate() {
@@ -177,6 +183,10 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
@Override
public void finish() {
final TemplateState templateState = TemplateManagerImpl.getTemplateState(myEditor);
if (templateState != null) {
myEditor.putUserData(ACTIVE_INTRODUCE, null);
}
super.finish();
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
final V variable = getVariable();
@@ -320,7 +330,7 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
}
}
protected V getLocalVariable() {
public V getLocalVariable() {
if (myLocalVariable != null && myLocalVariable.isValid()) {
return myLocalVariable;
}
@@ -339,4 +349,20 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
}
return myLocalVariable;
}
public static void stopIntroduce(Editor editor) {
final TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
if (templateState != null) {
templateState.gotoEnd(true);
}
}
@Nullable
public static AbstractInplaceIntroducer getActiveIntroducer(Editor editor) {
final TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
if (templateState != null) {
return editor.getUserData(ACTIVE_INTRODUCE);
}
return null;
}
}

View File

@@ -83,11 +83,11 @@ public abstract class AbstractInplaceVariableIntroducer<E extends PsiElement> ex
myExprMarker = exprMarker;
}
protected E getExpr() {
public E getExpr() {
return myExpr != null && myExpr.isValid() && myExpr.isPhysical() ? myExpr : null;
}
protected E[] getOccurrences() {
public E[] getOccurrences() {
return myOccurrences;
}
@@ -133,8 +133,7 @@ public abstract class AbstractInplaceVariableIntroducer<E extends PsiElement> ex
.setHideOnClickOutside(false)
.setHideOnKeyOutside(false)
.setHideOnAction(false)
.setCloseButtonEnabled(true)
.setTitle(myTitle);
.setCloseButtonEnabled(true);
final RelativePoint target = JBPopupFactory.getInstance().guessBestPopupLocation(myEditor);
final Point screenPoint = target.getScreenPoint();