From 50a58a96781ea97d5cc45c45b3f573ba364d50e7 Mon Sep 17 00:00:00 2001 From: Sergey Simonchik Date: Mon, 26 Mar 2018 17:41:28 +0300 Subject: [PATCH] add ComboBoxWithWidePopup#getMinimumPopupWidth to simplify API (IDEA-CR-31064) --- .../intellij/openapi/ui/ComboBoxWithWidePopup.java | 13 ++++++++----- .../ide/ui/laf/darcula/ui/DarculaComboBoxUI.java | 6 ++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/platform/platform-api/src/com/intellij/openapi/ui/ComboBoxWithWidePopup.java b/platform/platform-api/src/com/intellij/openapi/ui/ComboBoxWithWidePopup.java index 313e6dddd8e2..e0ec1f6a11dc 100644 --- a/platform/platform-api/src/com/intellij/openapi/ui/ComboBoxWithWidePopup.java +++ b/platform/platform-api/src/com/intellij/openapi/ui/ComboBoxWithWidePopup.java @@ -69,12 +69,15 @@ public class ComboBoxWithWidePopup extends JComboBox { } /** - * @return original preferred size of JComboBox. E.g. its width naturally sets minimum - * width of a wide popup as it's calculated as max of all pref sizes of combobox's items. - * See {@link javax.swing.plaf.basic.BasicComboBoxUI#getDisplaySize()}. + * @return minimum width of a popup that is wide enough to show all the combobox's items horizontally */ - public Dimension getOriginalPreferredSize() { - return super.getPreferredSize(); + public int getMinimumPopupWidth() { + // Original preferred size of JComboBox is calculated as max of all pref sizes of combobox's items. + // See javax.swing.plaf.basic.BasicComboBoxUI#getDisplaySize() + // + // Please note that "getPreferredSize().width" cannot be used as getPreferredSize might be overridden + // to return a value different to "max of all pref sizes of comboBox's items". + return super.getPreferredSize().width; } private class AdjustingListCellRenderer implements ListCellRenderer { diff --git a/platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/DarculaComboBoxUI.java b/platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/DarculaComboBoxUI.java index a79dd00676c0..28d33e8d03ba 100644 --- a/platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/DarculaComboBoxUI.java +++ b/platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/DarculaComboBoxUI.java @@ -514,12 +514,10 @@ public class DarculaComboBoxUI extends BasicComboBoxUI implements Border, ErrorB public void show(Component invoker, int x, int y) { if (comboBox instanceof ComboBoxWithWidePopup) { Dimension popupSize = comboBox.getSize(); - // Can't use simply comboBox.getPreferredSize() as it can be overridden and implemented differently. - // Here we need max of all pref sizes of comboBox's items. - Dimension prefSize = ((ComboBoxWithWidePopup)comboBox).getOriginalPreferredSize(); + int minPopupWidth = ((ComboBoxWithWidePopup)comboBox).getMinimumPopupWidth(); Insets insets = getInsets(); - popupSize.width = Math.max(popupSize.width, prefSize.width); + popupSize.width = Math.max(popupSize.width, minPopupWidth); popupSize.setSize(popupSize.width - (insets.right + insets.left), getPopupHeightForRowCount(comboBox.getMaximumRowCount())); scroller.setMaximumSize(popupSize);