add ComboBoxWithWidePopup#getMinimumPopupWidth to simplify API (IDEA-CR-31064)

This commit is contained in:
Sergey Simonchik
2018-03-26 17:41:28 +03:00
parent 070a65cee4
commit 50a58a9678
2 changed files with 10 additions and 9 deletions
@@ -69,12 +69,15 @@ public class ComboBoxWithWidePopup<E> extends JComboBox<E> {
}
/**
* @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<E> {
@@ -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);