From 7ccd5663a01455dc8a0b4baea6874069a7abf723 Mon Sep 17 00:00:00 2001 From: Eugene Petrenko Date: Wed, 18 Dec 2019 20:59:58 +0100 Subject: [PATCH] SdkPopup - use correct parent to show JdkDownload dialog GitOrigin-RevId: 96cf4f442c652cab21d9322f8820459658b8956d --- .../projectRoot/JdkListConfigurable.java | 2 +- .../impl/UnknownSdkEditorNotification.java | 22 +++++++++-------- .../projectRoots/impl/UnknownSdkTracker.java | 8 +++---- .../ui/configuration/SdkPopupFactory.java | 14 +++++------ .../darcula/ui/DarculaJBPopupComboPopup.java | 3 +-- .../intellij/ui/popup/list/ComboBoxPopup.java | 24 +++++++------------ 6 files changed, 33 insertions(+), 40 deletions(-) diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/JdkListConfigurable.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/JdkListConfigurable.java index d917baf643f9..71dd4fa1dce6 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/JdkListConfigurable.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/JdkListConfigurable.java @@ -236,7 +236,7 @@ public class JdkListConfigurable extends BaseStructureConfigurable { notSimpleJavaSdkType(), null, sdk -> false) - ).createPopup(new SdkPopup.SdkPopupListener() { }) + ).createPopup(myTree, new SdkPopup.SdkPopupListener() { }) .showPopup(e); } } diff --git a/platform/lang-impl/src/com/intellij/openapi/projectRoots/impl/UnknownSdkEditorNotification.java b/platform/lang-impl/src/com/intellij/openapi/projectRoots/impl/UnknownSdkEditorNotification.java index 5b89b8ef6dbe..0ab0c80331e1 100644 --- a/platform/lang-impl/src/com/intellij/openapi/projectRoots/impl/UnknownSdkEditorNotification.java +++ b/platform/lang-impl/src/com/intellij/openapi/projectRoots/impl/UnknownSdkEditorNotification.java @@ -72,17 +72,23 @@ public class UnknownSdkEditorNotification implements Disposable { panel.createActionLabel("Configure...", () -> { - //FileEditorManager#addTopComponent wraps the panel to implement borders, unwrapping - Container container = panel.getParent(); - if (container == null) container = panel; - UnknownSdkTracker .getInstance(myProject) - .showSdkSelectionPopup(sdkName, sdkType, container, () -> removeNotification(panel)); + .showSdkSelectionPopup(sdkName, sdkType, parentJComponentOrSelf(panel), () -> removeNotification(panel)); } ); } + @NotNull + private static JComponent parentJComponentOrSelf(@NotNull JComponent panel) { + //FileEditorManager#addTopComponent wraps the panel to implement borders, unwrapping + Container parent = panel.getParent(); + if (parent instanceof JComponent) { + return (JComponent)parent; + } + return panel; + } + private void setupPanel(@NotNull MissingSdkNotificationPanel panel, @NotNull String source, @Nullable Runnable setProjectSdk, @@ -100,13 +106,9 @@ public class UnknownSdkEditorNotification implements Disposable { if (setSdk != null) { panel.createActionLabel("Configure...", () -> { - //FileEditorManager#addTopComponent wraps the panel to implement borders, unwrapping - Container container = panel.getParent(); - if (container == null) container = panel; - UnknownSdkTracker .getInstance(myProject) - .showSdkSelectionPopup(null, container, sdk -> { + .showSdkSelectionPopup(null, parentJComponentOrSelf(panel), sdk -> { setSdk.accept(sdk); removeNotification(panel); }); diff --git a/platform/lang-impl/src/com/intellij/openapi/projectRoots/impl/UnknownSdkTracker.java b/platform/lang-impl/src/com/intellij/openapi/projectRoots/impl/UnknownSdkTracker.java index 6e03f90d16cb..eeaffc71666e 100644 --- a/platform/lang-impl/src/com/intellij/openapi/projectRoots/impl/UnknownSdkTracker.java +++ b/platform/lang-impl/src/com/intellij/openapi/projectRoots/impl/UnknownSdkTracker.java @@ -38,7 +38,7 @@ import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.awt.*; +import javax.swing.*; import java.util.List; import java.util.*; @@ -191,7 +191,7 @@ public class UnknownSdkTracker { public void showSdkSelectionPopup(@NotNull String sdkName, @Nullable SdkType sdkType, - @NotNull Container underneathRightOfComponent, + @NotNull JComponent underneathRightOfComponent, @NotNull Runnable onSelectionMade) { showSdkSelectionPopup(sdkType, underneathRightOfComponent, sdk -> { registerNewSdkInJdkTable(sdkName, sdk); @@ -200,7 +200,7 @@ public class UnknownSdkTracker { } public void showSdkSelectionPopup(@Nullable SdkType sdkType, - @NotNull Container underneathRightOfComponent, + @NotNull JComponent underneathRightOfComponent, @NotNull Consumer onSelectionMade) { ProjectSdksModel model = new ProjectSdksModel(); SdkListModelBuilder modelBuilder = new SdkListModelBuilder( @@ -216,7 +216,7 @@ public class UnknownSdkTracker { modelBuilder ); - popup.createPopup(new SdkPopup.SdkPopupListener() { + popup.createPopup(underneathRightOfComponent, new SdkPopup.SdkPopupListener() { private void handleNewItem(@NotNull SdkListItem item) { if (item instanceof SdkListItem.SdkItem) { onSelectionMade.consume(((SdkListItem.SdkItem)item).getSdk()); diff --git a/platform/lang-impl/src/com/intellij/openapi/roots/ui/configuration/SdkPopupFactory.java b/platform/lang-impl/src/com/intellij/openapi/roots/ui/configuration/SdkPopupFactory.java index c2a1b5385865..92f6e1a9f025 100644 --- a/platform/lang-impl/src/com/intellij/openapi/roots/ui/configuration/SdkPopupFactory.java +++ b/platform/lang-impl/src/com/intellij/openapi/roots/ui/configuration/SdkPopupFactory.java @@ -11,6 +11,7 @@ import com.intellij.openapi.ui.popup.LightweightWindowEvent; import com.intellij.ui.AnActionButton; import com.intellij.ui.awt.RelativePoint; import com.intellij.ui.popup.list.ComboBoxPopup; +import java.util.function.Consumer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -32,12 +33,11 @@ public class SdkPopupFactory { } @NotNull - public SdkPopup createPopup(@NotNull SdkPopupListener listener) { + public SdkPopup createPopup(@NotNull JComponent parent, + @NotNull SdkPopupListener listener) { SdkListItemContext context = new SdkListItemContext(); - SdkPopupImpl popup = new SdkPopupImpl(context); - - popup.addItemSelectedListener(value -> { - if (myModelBuilder.executeAction(popup.getList(), value, listener::onNewItemAdded)) { + SdkPopupImpl popup = new SdkPopupImpl(context, value -> { + if (myModelBuilder.executeAction(parent, value, listener::onNewItemAdded)) { return; } listener.onExistingItemSelected(value); @@ -71,8 +71,8 @@ public class SdkPopupFactory { } private class SdkPopupImpl extends ComboBoxPopup implements SdkPopup { - SdkPopupImpl(SdkListItemContext context) { - super(context, null); + SdkPopupImpl(SdkListItemContext context, Consumer onItemSelected) { + super(context, null, onItemSelected); } @Override diff --git a/platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/DarculaJBPopupComboPopup.java b/platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/DarculaJBPopupComboPopup.java index d7664b0bf806..0515364ab197 100644 --- a/platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/DarculaJBPopupComboPopup.java +++ b/platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/DarculaJBPopupComboPopup.java @@ -85,7 +85,7 @@ public class DarculaJBPopupComboPopup implements ComboPopup, ComboBoxPopup.Co //noinspection unchecked T selectedItem = (T)myComboBox.getSelectedItem(); - myPopup = new ComboBoxPopup(this, selectedItem) { + myPopup = new ComboBoxPopup(this, selectedItem, value -> myComboBox.setSelectedItem(value)) { @Override public void cancel(InputEvent e) { if (e instanceof MouseEvent) { @@ -98,7 +98,6 @@ public class DarculaJBPopupComboPopup implements ComboPopup, ComboBoxPopup.Co super.cancel(e); } }; - myPopup.addItemSelectedListener(value -> myComboBox.setSelectedItem(value)); myPopup.addListener(new JBPopupListener() { @Override public void beforeShown(@NotNull LightweightWindowEvent event) { diff --git a/platform/platform-impl/src/com/intellij/ui/popup/list/ComboBoxPopup.java b/platform/platform-impl/src/com/intellij/ui/popup/list/ComboBoxPopup.java index ccf059645e8c..e99ebc4ec98c 100644 --- a/platform/platform-impl/src/com/intellij/ui/popup/list/ComboBoxPopup.java +++ b/platform/platform-impl/src/com/intellij/ui/popup/list/ComboBoxPopup.java @@ -28,8 +28,9 @@ public class ComboBoxPopup extends ListPopupImpl { private final Context myContext; public ComboBoxPopup(@NotNull Context context, - @Nullable T selectedItem) { - this(context, null, popupStateFromContext(context, selectedItem), null); + @Nullable T selectedItem, + @NotNull Consumer onItemSelected) { + this(context, null, popupStateFromContext(context, onItemSelected, selectedItem), null); } private ComboBoxPopup(@NotNull Context context, @@ -41,15 +42,11 @@ public class ComboBoxPopup extends ListPopupImpl { configurePopup(); } - public void addItemSelectedListener(@NotNull Consumer callback) { - //noinspection unchecked - ((MyBasePopupState)getStep()).addItemSelectedListener(callback); - } - @NotNull private static MyBasePopupState popupStateFromContext(@NotNull Context context, - @Nullable Object selectedItem) { - MyBasePopupState step = new MyBasePopupState(t -> {}, + @NotNull Consumer onItemSelected, + @Nullable T selectedItem) { + MyBasePopupState step = new MyBasePopupState(onItemSelected, () -> context.getModel(), () -> context.getRenderer()) { @Override @@ -59,7 +56,6 @@ public class ComboBoxPopup extends ListPopupImpl { }; if (selectedItem != null) { - //noinspection SuspiciousMethodCalls step.setDefaultOptionIndex(step.getValues().indexOf(selectedItem)); } return step; @@ -177,7 +173,7 @@ public class ComboBoxPopup extends ListPopupImpl { private static class MyBasePopupState extends BaseListPopupStep { private final JBList myProxyList = new JBList<>(); - private Consumer myOnItemSelected; + private final Consumer myOnItemSelected; private final Supplier> myGetComboboxModel; private final Supplier> myGetRenderer; @@ -190,10 +186,6 @@ public class ComboBoxPopup extends ListPopupImpl { myGetRenderer = getRenderer; } - private void addItemSelectedListener(@NotNull Consumer callback) { - myOnItemSelected = myOnItemSelected.andThen(callback); - } - @Nullable @Override @SuppressWarnings("rawtypes") @@ -203,7 +195,7 @@ public class ComboBoxPopup extends ListPopupImpl { //noinspection unchecked ListModel nextModel = ((ComboBoxPopupState)model).onChosen(selectedValue); if (nextModel != null) { - return new MyBasePopupState<>(t -> myOnItemSelected.accept(t) /* mutable callback! */, + return new MyBasePopupState<>(myOnItemSelected /* mutable callback! */, () -> nextModel, myGetRenderer); } }