* @param x the x origin
* @param y the y origin
* @return Popup
*/
public static Popup addScrollPaneIfNecessary(final JPopupMenu popupMenu, int x, int y) {
final SimpleScrollPane contents = new SimpleScrollPane(popupMenu, SimpleScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, SimpleScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
if (popupMenu instanceof JidePopupMenu && popupMenu.getPreferredSize().height != ((JidePopupMenu) popupMenu).getPreferredScrollableViewportSize().height) {
if (popupMenu.getLayout() instanceof DefaultMenuLayout && SystemInfo.isJdk6Above()) {
popupMenu.setLayout(new BoxLayout(popupMenu, ((DefaultMenuLayout) popupMenu.getLayout()).getAxis()));
}
PopupFactory popupFactory = PopupFactory.getSharedInstance();
contents.getScrollUpButton().setOpaque(true);
contents.getScrollDownButton().setOpaque(true);
contents.setBorder(BorderFactory.createEmptyBorder());
Component[] components = popupMenu.getComponents();
for (Component component : components) {
if (component instanceof JMenuItem) {
((JMenuItem) component).addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (e.getSource() instanceof JMenuItem) {
if (((JMenuItem) e.getSource()).getModel().isArmed()) {
popupMenu.scrollRectToVisible(((JMenuItem) e.getSource()).getBounds());
Point position = contents.getViewport().getViewPosition();
contents.getScrollUpButton().setEnabled(position.y > 2);
contents.getScrollDownButton().setEnabled(position.y < contents.getViewport().getViewSize().height - contents.getViewport().getViewRect().height - 2);
}
}
}
});
}