| 1 |
/* |
| 2 |
* @(#)BasicJidePopupMenuUI.java 12/13/2006 |
| 3 |
* |
| 4 |
* Copyright 2002 - 2006 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
package com.jidesoft.plaf.basic; |
| 8 |
|
| 9 |
import com.jidesoft.swing.JidePopupMenu; |
| 10 |
import com.jidesoft.swing.SimpleScrollPane; |
| 11 |
import com.jidesoft.utils.SystemInfo; |
| 12 |
|
| 13 |
import javax.swing.*; |
| 14 |
import javax.swing.event.ChangeListener; |
| 15 |
import javax.swing.event.ChangeEvent; |
| 16 |
import javax.swing.plaf.ComponentUI; |
| 17 |
import javax.swing.plaf.basic.BasicPopupMenuUI; |
| 18 |
import javax.swing.plaf.basic.DefaultMenuLayout; |
| 19 |
import java.awt.*; |
| 20 |
|
| 21 |
public class BasicJidePopupMenuUI extends BasicPopupMenuUI { |
| 22 |
public BasicJidePopupMenuUI() { |
| 23 |
} |
| 24 |
|
| 25 |
@SuppressWarnings({"UnusedDeclaration"}) |
| 26 |
public static ComponentUI createUI(JComponent c) { |
| 27 |
return new BasicJidePopupMenuUI(); |
| 28 |
} |
| 29 |
|
| 30 |
@Override |
| 31 |
public Popup getPopup(JPopupMenu popupMenu, int x, int y) { |
| 32 |
Popup popup = BasicJidePopupMenuUI.addScrollPaneIfNecessary(popupMenu, x, y); |
| 33 |
return popup == null ? super.getPopup(popupMenu, x, y) : popup; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Adds a scroll pane to the popup menu if the popup menu is taller than the screen boundary. |
| 38 |
* |
| 39 |
* @param popupMenu the popup menu. |
| 40 |
* @param x the x origin |
| 41 |
* @param y the y origin |
| 42 |
* @return Popup |
| 43 |
*/ |
| 44 |
public static Popup addScrollPaneIfNecessary(final JPopupMenu popupMenu, int x, int y) { |
| 45 |
final SimpleScrollPane contents = new SimpleScrollPane(popupMenu, SimpleScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, SimpleScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
| 46 |
if (popupMenu instanceof JidePopupMenu && popupMenu.getPreferredSize().height != ((JidePopupMenu) popupMenu).getPreferredScrollableViewportSize().height) { |
| 47 |
if (popupMenu.getLayout() instanceof DefaultMenuLayout && SystemInfo.isJdk6Above()) { |
| 48 |
popupMenu.setLayout(new BoxLayout(popupMenu, ((DefaultMenuLayout) popupMenu.getLayout()).getAxis())); |
| 49 |
} |
| 50 |
PopupFactory popupFactory = PopupFactory.getSharedInstance(); |
| 51 |
contents.getScrollUpButton().setOpaque(true); |
| 52 |
contents.getScrollDownButton().setOpaque(true); |
| 53 |
contents.setBorder(BorderFactory.createEmptyBorder()); |
| 54 |
Component[] components = popupMenu.getComponents(); |
| 55 |
for (Component component : components) { |
| 56 |
if (component instanceof JMenuItem) { |
| 57 |
((JMenuItem) component).addChangeListener(new ChangeListener() { |
| 58 |
public void stateChanged(ChangeEvent e) { |
| 59 |
if (e.getSource() instanceof JMenuItem) { |
| 60 |
if (((JMenuItem) e.getSource()).getModel().isArmed()) { |
| 61 |
popupMenu.scrollRectToVisible(((JMenuItem) e.getSource()).getBounds()); |
| 62 |
Point position = contents.getViewport().getViewPosition(); |
| 63 |
contents.getScrollUpButton().setEnabled(position.y > 2); |
| 64 |
contents.getScrollDownButton().setEnabled(position.y < contents.getViewport().getViewSize().height - contents.getViewport().getViewRect().height - 2); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
}); |
| 69 |
} |
| 70 |
} |
| 71 |
return popupFactory.getPopup(popupMenu.getInvoker(), contents, x, y); |
| 72 |
} |
| 73 |
else { |
| 74 |
return null; |
| 75 |
} |
| 76 |
} |
| 77 |
} |