1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
package com.explodingpixels.widgets.plaf;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.MenuElement;
import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.plaf.basic.ComboPopup;
/**
* An implementation of {@link ComboPopup} that uses actual {@link JMenuItem}s rather than a
* {@link JList} to display it's contents.
*/
public class EPComboPopup implements ComboPopup {
private final JComboBox fComboBox;
private JPopupMenu fPopupMenu = new JPopupMenu();
private Font fFont;
private ComboBoxVerticalCenterProvider fComboBoxVerticalCenterProvider =
new DefaultVerticalCenterProvider();
private static final int LEFT_SHIFT = 5;
public EPComboPopup(JComboBox comboBox) {
fComboBox = comboBox;
fFont = comboBox.getFont();
fPopupMenu.addPopupMenuListener(createPopupMenuListener());
}
/**
* Creates a {@link PopupMenuListener} on the underlying {@link JPopupMenu} which forwards along the popup event
* notification to the associated {@link JComboBox}.
*/
private PopupMenuListener createPopupMenuListener() {
return new PopupMenuListener() {
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
fComboBox.firePopupMenuWillBecomeVisible();
}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
fComboBox.firePopupMenuWillBecomeInvisible();
}
public void popupMenuCanceled(PopupMenuEvent e) {
fComboBox.firePopupMenuCanceled();
}
};
}
public void setFont(Font font) {
fFont = font;
}
public void setVerticalComponentCenterProvider(
ComboBoxVerticalCenterProvider comboBoxVerticalCenterProvider) {
if (comboBoxVerticalCenterProvider == null) {
throw new IllegalArgumentException("The given CompnonentCenterProvider cannot be null.");
}
fComboBoxVerticalCenterProvider = comboBoxVerticalCenterProvider;
}
private void togglePopup() {
if (isVisible()) {
hide();
} else {
show();
}
}
private void clearAndFillMenu() {
fPopupMenu.removeAll();
ButtonGroup buttonGroup = new ButtonGroup();
// add the given items to the popup menu.
for (int i = 0; i < fComboBox.getModel().getSize(); i++) {
Object item = fComboBox.getModel().getElementAt(i);
JMenuItem menuItem = new JCheckBoxMenuItem(item.toString());
menuItem.setFont(fFont);
menuItem.addActionListener(createMenuItemListener(item));
buttonGroup.add(menuItem);
fPopupMenu.add(menuItem);
// if the current item is selected, make the menu item reflect that.
if (item.equals(fComboBox.getModel().getSelectedItem())) {
menuItem.setSelected(true);
fPopupMenu.setSelected(menuItem);
}
}
fPopupMenu.pack();
int popupWidth = fComboBox.getWidth() + 5;
// adjust the width to be slightly wider than the associated combo box.
fPopupMenu.setSize(popupWidth, fPopupMenu.getHeight());
}
private Point placePopupOnScreen() {
// grab the right most location of the button.
int buttonRightX = fComboBox.getWidth();
// figure out how the height of a menu item.
Insets insets = fPopupMenu.getInsets();
// calculate the x and y value at which to place the popup menu.
// by default, this will place the selected menu item in the
// popup item directly over the button.
int x = buttonRightX - fPopupMenu.getPreferredSize().width - LEFT_SHIFT;
int selectedItemIndex = fPopupMenu.getSelectionModel().getSelectedIndex();
int componentCenter = fComboBoxVerticalCenterProvider.provideCenter(fComboBox);
int menuItemHeight = fPopupMenu.getComponent(selectedItemIndex).getPreferredSize().height;
int menuItemCenter = insets.top + (selectedItemIndex * menuItemHeight) + menuItemHeight / 2;
int y = componentCenter - menuItemCenter;
// do a cursory check to make sure we're not placing the popup
// off the bottom of the screen. note that Java on Mac won't
// let the popup show up off screen no matter where you place it.
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Point bottomOfMenuOnScreen = new Point(0, y + fPopupMenu.getPreferredSize().height);
SwingUtilities.convertPointToScreen(bottomOfMenuOnScreen, fComboBox);
if (bottomOfMenuOnScreen.y > size.height) {
y = fComboBox.getHeight() - fPopupMenu.getPreferredSize().height;
}
return new Point(x, y);
}
private ActionListener createMenuItemListener(final Object comboBoxItem) {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
fComboBox.setSelectedItem(comboBoxItem);
}
};
}
private void forceCorrectPopupSelectionIfNeccessary() {
if (fComboBox.getSelectedIndex() >= 0) {
forceCorrectPopupSelection();
}
}
private void forceCorrectPopupSelection() {
assert fPopupMenu.isShowing() : "The popup must be showing for this method to work properly.";
// force the correct item to be shown as selected. this is a
// work around for Java bug 4740942, which has been fixed by
// Sun, but not by Apple.
int index = fPopupMenu.getSelectionModel().getSelectedIndex();
MenuElement[] menuPath = new MenuElement[2];
menuPath[0] = fPopupMenu;
menuPath[1] = fPopupMenu.getSubElements()[index];
MenuSelectionManager.defaultManager().setSelectedPath(menuPath);
}
// ComboPopup implementation. /////////////////////////////////////////////////////////////////
public void show() {
clearAndFillMenu();
// if there are combo box items, then show the popup menu.
if (fComboBox.getModel().getSize() > 0) {
// Point popupLocation = placePopupOnScreen();
Rectangle popupBounds = calculateInitialPopupBounds();
// fPopupMenu.show(fComboBox, popupLocation.x, popupLocation.y);
fPopupMenu.show(fComboBox, popupBounds.x, popupBounds.y);
forceCorrectPopupSelectionIfNeccessary();
}
}
public void hide() {
fPopupMenu.setVisible(false);
}
public boolean isVisible() {
return fPopupMenu.isVisible();
}
/**
* This method is not implemented and would throw an {@link UnsupportedOperationException} if
* {@link javax.swing.plaf.basic.BasicComboBoxUI} didn't call it. Thus, this method should not
* be used, as it always returns null.
*
* @return null.
*/
public JList getList() {
return null;
}
public MouseListener getMouseListener() {
return new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (fComboBox.isEnabled()) {
togglePopup();
}
}
@Override
public void mouseReleased(MouseEvent e) {
MenuSelectionManager.defaultManager().processMouseEvent(e);
}
};
}
public MouseMotionListener getMouseMotionListener() {
return new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
MenuSelectionManager.defaultManager().processMouseEvent(e);
}
};
}
public KeyListener getKeyListener() {
return null;
}
public void uninstallingUI() {
// TODO implement, if necessary.
}
// An interface to allow a third-party to provide the center of a given compoennt. ////////////
public interface ComboBoxVerticalCenterProvider {
int provideCenter(JComboBox comboBox);
}
// A default implementation of ComboBoxVerticalCenterProvider. ////////////////////////////////
private static class DefaultVerticalCenterProvider implements ComboBoxVerticalCenterProvider {
public int provideCenter(JComboBox comboBox) {
return comboBox.getHeight() / 2;
}
}
// Utility methods. ///////////////////////////////////////////////////////////////////////////
private Rectangle calculateInitialPopupBounds() {
// grab the right most location of the button.
int comboBoxRightEdge = fComboBox.getWidth();
// figure out how the height of a menu item.
Insets insets = fPopupMenu.getInsets();
// calculate the x and y value at which to place the popup menu. by default, this will place
// the selected menu item in the popup item directly over the button.
int x = comboBoxRightEdge - fPopupMenu.getPreferredSize().width - LEFT_SHIFT;
int selectedItemIndex = fPopupMenu.getSelectionModel().getSelectedIndex();
int componentCenter = fComboBoxVerticalCenterProvider.provideCenter(fComboBox);
int menuItemHeight = fPopupMenu.getSelectionModel().getSelectedIndex() >= 0
? fPopupMenu.getComponent(selectedItemIndex).getPreferredSize().height
: 0;
int menuItemCenter = insets.top + (selectedItemIndex * menuItemHeight) + menuItemHeight / 2;
int y = componentCenter - menuItemCenter;
// TODO this method doesn't robustly handle multiple monitors.
Rectangle bounds = new Rectangle(new Point(x, y), fPopupMenu.getPreferredSize());
Dimension preferredSize = fPopupMenu.getPreferredSize();
// do a cursory check to make sure we're not placing the popup
// off the bottom of the screen. note that Java on Mac won't
// let the popup show up off screen no matter where you place it.
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Point bottomOfMenuOnScreen = new Point(0, y + preferredSize.height);
SwingUtilities.convertPointToScreen(bottomOfMenuOnScreen, fComboBox);
if (bottomOfMenuOnScreen.y > size.height) {
y = fComboBox.getHeight() - preferredSize.height;
}
Point position = new Point(x, y);
return bounds;
}
}
|