diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AnonymousTargetClassPreselectionUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AnonymousTargetClassPreselectionUtil.java new file mode 100644 index 000000000000..421c43cabe88 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AnonymousTargetClassPreselectionUtil.java @@ -0,0 +1,45 @@ +/* + * Copyright 2000-2014 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.codeInsight.daemon.impl.quickfix; + +import com.intellij.ide.util.PropertiesComponent; +import com.intellij.psi.PsiAnonymousClass; +import com.intellij.psi.PsiClass; +import org.jetbrains.annotations.Nullable; + +import java.util.Collection; + +public class AnonymousTargetClassPreselectionUtil { + private static final String PRESELECT_ANONYMOUS = "create.member.preselect.anonymous"; + + public static void rememberSelection(PsiClass aClass, PsiClass firstClass) { + if (firstClass instanceof PsiAnonymousClass) { + PropertiesComponent.getInstance().setValue(PRESELECT_ANONYMOUS, String.valueOf(aClass == firstClass)); + } + } + + @Nullable + public static PsiClass getPreselection(Collection classes, PsiClass firstClass) { + if (firstClass instanceof PsiAnonymousClass && !PropertiesComponent.getInstance().getBoolean(PRESELECT_ANONYMOUS, true)) { + for (PsiClass aClass : classes) { + if (!(aClass instanceof PsiAnonymousClass)) { + return aClass; + } + } + } + return null; + } +} diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageBaseFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageBaseFix.java index 09b83e2a489f..abe9368374e8 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageBaseFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageBaseFix.java @@ -119,7 +119,8 @@ public abstract class CreateFromUsageBaseFix extends BaseIntentionAction { protected abstract PsiElement getElement(); private void chooseTargetClass(List classes, final Editor editor) { - final Project project = classes.get(0).getProject(); + final PsiClass firstClass = classes.get(0); + final Project project = firstClass.getProject(); final JList list = new JBList(classes); PsiElementListCellRenderer renderer = new PsiClassListCellRenderer(); @@ -128,12 +129,18 @@ public abstract class CreateFromUsageBaseFix extends BaseIntentionAction { final PopupChooserBuilder builder = new PopupChooserBuilder(list); renderer.installSpeedSearch(builder); + final PsiClass preselection = AnonymousTargetClassPreselectionUtil.getPreselection(classes, firstClass); + if (preselection != null) { + list.setSelectedValue(preselection, true); + } + Runnable runnable = new Runnable() { @Override public void run() { int index = list.getSelectedIndex(); if (index < 0) return; final PsiClass aClass = (PsiClass) list.getSelectedValue(); + AnonymousTargetClassPreselectionUtil.rememberSelection(aClass, firstClass); CommandProcessor.getInstance().executeCommand(project, new Runnable() { @Override public void run() { diff --git a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java index 75544f9d8ab9..ce52274140bb 100644 --- a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java @@ -19,6 +19,7 @@ import com.intellij.codeInsight.ChangeContextUtil; import com.intellij.codeInsight.ExceptionUtil; import com.intellij.codeInsight.NullableNotNullManager; import com.intellij.codeInsight.daemon.impl.analysis.JavaHighlightUtil; +import com.intellij.codeInsight.daemon.impl.quickfix.AnonymousTargetClassPreselectionUtil; import com.intellij.codeInsight.highlighting.HighlightManager; import com.intellij.codeInsight.intention.impl.AddNullableAnnotationFix; import com.intellij.codeInsight.navigation.NavigationUtil; @@ -1133,6 +1134,7 @@ public class ExtractMethodProcessor implements MatchProvider { final PsiElementProcessor processor = new PsiElementProcessor() { @Override public boolean execute(@NotNull PsiClass selectedClass) { + AnonymousTargetClassPreselectionUtil.rememberSelection(selectedClass, myTargetClass); final List array = classes.get(selectedClass); myNeedChangeContext = myTargetClass != selectedClass; myTargetClass = selectedClass; @@ -1184,7 +1186,9 @@ public class ExtractMethodProcessor implements MatchProvider { if (classes.size() > 1) { final PsiClass[] psiClasses = classes.keySet().toArray(new PsiClass[classes.size()]); - NavigationUtil.getPsiElementPopup(psiClasses, new PsiClassListCellRenderer(), "Choose Destination Class", processor).showInBestPositionFor(myEditor); + final PsiClass preselection = AnonymousTargetClassPreselectionUtil.getPreselection(classes.keySet(), psiClasses[0]); + NavigationUtil.getPsiElementPopup(psiClasses, new PsiClassListCellRenderer(), "Choose Destination Class", processor, preselection) + .showInBestPositionFor(myEditor); return true; } } diff --git a/java/java-impl/src/com/intellij/refactoring/introduceField/BaseExpressionToFieldHandler.java b/java/java-impl/src/com/intellij/refactoring/introduceField/BaseExpressionToFieldHandler.java index a751acea42df..75473959ddcc 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceField/BaseExpressionToFieldHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceField/BaseExpressionToFieldHandler.java @@ -27,6 +27,7 @@ package com.intellij.refactoring.introduceField; import com.intellij.codeInsight.AnnotationUtil; import com.intellij.codeInsight.ChangeContextUtil; import com.intellij.codeInsight.TestFrameworks; +import com.intellij.codeInsight.daemon.impl.quickfix.AnonymousTargetClassPreselectionUtil; import com.intellij.codeInsight.highlighting.HighlightManager; import com.intellij.codeInsight.lookup.LookupManager; import com.intellij.codeInsight.navigation.NavigationUtil; @@ -129,18 +130,13 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase return !convertExpressionToField(selectedExpr, editor, file, project, tempType); } else { - PsiClass selection = null; - for (PsiClass psiClass : classes) { - if (!(psiClass instanceof PsiAnonymousClass)) { - selection = psiClass; - break; - } - } + PsiClass selection = AnonymousTargetClassPreselectionUtil.getPreselection(classes, myParentClass); NavigationUtil.getPsiElementPopup(classes.toArray(new PsiClass[classes.size()]), new PsiClassListCellRenderer(), "Choose class to introduce " + (myIsConstant ? "constant" : "field"), new PsiElementProcessor() { @Override public boolean execute(@NotNull PsiClass aClass) { + AnonymousTargetClassPreselectionUtil.rememberSelection(aClass, myParentClass); myParentClass = aClass; convertExpressionToField(selectedExpr, editor, file, project, tempType); return false; diff --git a/java/java-impl/src/com/intellij/refactoring/introduceField/LocalToFieldHandler.java b/java/java-impl/src/com/intellij/refactoring/introduceField/LocalToFieldHandler.java index 5b5c39fb10a8..0b31eedf8be0 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceField/LocalToFieldHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceField/LocalToFieldHandler.java @@ -18,6 +18,7 @@ package com.intellij.refactoring.introduceField; import com.intellij.codeInsight.ChangeContextUtil; import com.intellij.codeInsight.CodeInsightUtil; import com.intellij.codeInsight.TestFrameworks; +import com.intellij.codeInsight.daemon.impl.quickfix.AnonymousTargetClassPreselectionUtil; import com.intellij.codeInsight.navigation.NavigationUtil; import com.intellij.ide.util.PsiClassListCellRenderer; import com.intellij.openapi.application.ApplicationManager; @@ -66,7 +67,7 @@ public abstract class LocalToFieldHandler { public boolean convertLocalToField(final PsiLocalVariable local, final Editor editor) { boolean tempIsStatic = myIsConstant; PsiElement parent = local.getParent(); - List classes = new ArrayList(); + final List classes = new ArrayList(); while (parent != null && parent.getContainingFile() != null) { if (parent instanceof PsiClass && !(myIsConstant && parent instanceof PsiAnonymousClass)) { classes.add((PsiClass)parent); @@ -90,13 +91,16 @@ public abstract class LocalToFieldHandler { if (convertLocalToField(local, classes.get(getChosenClassIndex(classes)), editor, tempIsStatic)) return false; } else { final boolean isStatic = tempIsStatic; + final PsiClass firstClass = classes.get(0); + final PsiClass preselection = AnonymousTargetClassPreselectionUtil.getPreselection(classes, firstClass); NavigationUtil.getPsiElementPopup(classes.toArray(new PsiClass[classes.size()]), new PsiClassListCellRenderer(), "Choose class to introduce " + (myIsConstant ? "constant" : "field"), new PsiElementProcessor() { @Override public boolean execute(@NotNull PsiClass aClass) { + AnonymousTargetClassPreselectionUtil.rememberSelection(aClass, aClass); convertLocalToField(local, aClass, editor, isStatic); return false; } - }).showInBestPositionFor(editor); + }, preselection).showInBestPositionFor(editor); } return true;