/*
   *  Adjust the width of the scrollpane used by the popup
   */
  protected void popupWider(BasicComboPopup popup) {
    JList list = popup.getList();

    //  Determine the maximimum width to use:
    //  a) determine the popup preferred width
    //  b) limit width to the maximum if specified
    //  c) ensure width is not less than the scroll pane width

    int popupWidth =
        list.getPreferredSize().width
            + 5 // make sure horizontal scrollbar doesn't appear
            + getScrollBarWidth(popup, scrollPane);

    if (maximumWidth != -1) {
      popupWidth = Math.min(popupWidth, maximumWidth);
    }

    Dimension scrollPaneSize = scrollPane.getPreferredSize();
    popupWidth = Math.max(popupWidth, scrollPaneSize.width);

    //  Adjust the width

    scrollPaneSize.width = popupWidth;
    scrollPane.setPreferredSize(scrollPaneSize);
    scrollPane.setMaximumSize(scrollPaneSize);
  }
  /*
   *  I can't find any property on the scrollBar to determine if it will be
   *  displayed or not so use brute force to determine this.
   */
  protected boolean horizontalScrollBarWillBeVisible(
      BasicComboPopup popup, JScrollPane scrollPane) {
    JList list = popup.getList();
    int scrollBarWidth = getScrollBarWidth(popup, scrollPane);
    int popupWidth = list.getPreferredSize().width + scrollBarWidth;

    return popupWidth > scrollPane.getPreferredSize().width;
  }
  protected void customizePopup(BasicComboPopup popup) {
    scrollPane = getScrollPane(popup);

    if (popupWider) popupWider(popup);

    checkHorizontalScrollBar(popup);

    //  For some reason in JDK7 the popup will not display at its preferred
    //  width unless its location has been changed from its default
    //  (ie. for normal "pop down" shift the popup and reset)

    Component comboBox = popup.getInvoker();
    Point location = comboBox.getLocationOnScreen();

    if (popupAbove) {
      int height = popup.getPreferredSize().height;
      popup.setLocation(location.x, location.y - height);
    } else {
      int height = comboBox.getPreferredSize().height;
      popup.setLocation(location.x, location.y + height - 1);
      popup.setLocation(location.x, location.y + height);
    }
  }
  /*
   *  Get the scroll pane used by the popup so its bounds can be adjusted
   */
  protected JScrollPane getScrollPane(BasicComboPopup popup) {
    JList list = popup.getList();
    Container c = SwingUtilities.getAncestorOfClass(JScrollPane.class, list);

    return (JScrollPane) c;
  }
  /*
   *  I can't find any property on the scrollBar to determine if it will be
   *  displayed or not so use brute force to determine this.
   */
  protected int getScrollBarWidth(BasicComboPopup popup, JScrollPane scrollPane) {
    int scrollBarWidth = 0;
    JComboBox comboBox = (JComboBox) popup.getInvoker();

    if (comboBox.getItemCount() > comboBox.getMaximumRowCount()) {
      JScrollBar vertical = scrollPane.getVerticalScrollBar();
      scrollBarWidth = vertical.getPreferredSize().width;
    }

    return scrollBarWidth;
  }