| 1 |
/* |
| 2 |
* @(#)VsnetMenuUI.java |
| 3 |
* |
| 4 |
* Copyright 2002 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
package com.jidesoft.plaf.vsnet; |
| 8 |
|
| 9 |
import com.jidesoft.plaf.LookAndFeelFactory; |
| 10 |
import com.jidesoft.plaf.UIDefaultsLookup; |
| 11 |
import com.jidesoft.plaf.basic.ThemePainter; |
| 12 |
import com.jidesoft.plaf.windows.WindowsGraphicsUtilsPort; |
| 13 |
import com.jidesoft.swing.ButtonStyle; |
| 14 |
import com.jidesoft.swing.JideSwingUtilities; |
| 15 |
import com.jidesoft.swing.TopLevelMenuContainer; |
| 16 |
|
| 17 |
import javax.swing.*; |
| 18 |
import javax.swing.event.*; |
| 19 |
import javax.swing.plaf.ComponentUI; |
| 20 |
import java.awt.*; |
| 21 |
import java.awt.event.ActionEvent; |
| 22 |
import java.awt.event.KeyEvent; |
| 23 |
import java.awt.event.MouseEvent; |
| 24 |
import java.awt.event.MouseMotionListener; |
| 25 |
import java.beans.PropertyChangeEvent; |
| 26 |
import java.beans.PropertyChangeListener; |
| 27 |
import java.util.ArrayList; |
| 28 |
import java.util.Arrays; |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Menu UI implementation |
| 33 |
*/ |
| 34 |
public class VsnetMenuUI extends VsnetMenuItemUI { |
| 35 |
protected ChangeListener changeListener; |
| 36 |
protected PropertyChangeListener propertyChangeListener; |
| 37 |
protected MenuListener menuListener; |
| 38 |
|
| 39 |
private int lastMnemonic = 0; |
| 40 |
|
| 41 |
private static final boolean DEBUG = false; // show bad params, misc. |
| 42 |
|
| 43 |
private static boolean crossMenuMnemonic = true; |
| 44 |
|
| 45 |
private boolean isMouseOver = false; |
| 46 |
private PopupMenuListener popupMenuListener; |
| 47 |
|
| 48 |
public static ComponentUI createUI(JComponent x) { |
| 49 |
return new VsnetMenuUI(); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
protected void installDefaults() { |
| 54 |
super.installDefaults(); |
| 55 |
if (menuItem instanceof JMenu) { |
| 56 |
((JMenu) menuItem).setDelay(200); |
| 57 |
} |
| 58 |
crossMenuMnemonic = UIDefaultsLookup.getBoolean("Menu.crossMenuMnemonic"); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
protected String getPropertyPrefix() { |
| 63 |
return "Menu"; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) { |
| 68 |
if (!(menuItem instanceof JMenu) || !((JMenu) menuItem).isTopLevelMenu()) { |
| 69 |
super.paintBackground(g, menuItem, bgColor); |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
ButtonModel model = menuItem.getModel(); |
| 74 |
Color oldColor = g.getColor(); |
| 75 |
int menuWidth; |
| 76 |
int menuHeight; |
| 77 |
if (JideSwingUtilities.getOrientationOf(menuItem) == SwingConstants.HORIZONTAL) { |
| 78 |
menuWidth = menuItem.getWidth(); |
| 79 |
menuHeight = menuItem.getHeight(); |
| 80 |
} |
| 81 |
else { |
| 82 |
menuWidth = menuItem.getHeight(); |
| 83 |
menuHeight = menuItem.getWidth(); |
| 84 |
} |
| 85 |
|
| 86 |
Color borderColor = getPainter().getMenuItemBorderColor(); |
| 87 |
// Color mouseHoverBackground = UIDefaultsLookup.getColor("Menu.mouseHoverBackground"); |
| 88 |
// Border mouseHoverBorder = UIDefaultsLookup.getBorder("Menu.mouseHoverBorder"); |
| 89 |
|
| 90 |
if (menuItem.isOpaque()) { |
| 91 |
if (menuItem.getParent() != null) { |
| 92 |
g.setColor(menuItem.getParent().getBackground()); |
| 93 |
} |
| 94 |
else { |
| 95 |
g.setColor(menuItem.getBackground()); |
| 96 |
} |
| 97 |
g.fillRect(0, 0, menuWidth, menuHeight); |
| 98 |
} |
| 99 |
|
| 100 |
Rectangle rect = new Rectangle(0, 0, menuWidth, menuHeight); |
| 101 |
if (menuItem instanceof JMenu && model.isSelected()) { |
| 102 |
// Draw a dark shadow border without bottom |
| 103 |
// TODO: we should consider the location of popup menu and decide which side not paint. |
| 104 |
// TODO: for example if the menu is going upward, we shouldn't paint the top. |
| 105 |
if (((JMenu) menuItem).getPopupMenu().isVisible()) { // paint differently if popup menu is visible |
| 106 |
if (JideSwingUtilities.getOrientationOf(menuItem) == SwingConstants.HORIZONTAL) { |
| 107 |
getPainter().paintMenuItemBackground(menuItem, g, rect, JideSwingUtilities.getOrientationOf(menuItem), ThemePainter.STATE_DEFAULT, false); |
| 108 |
g.setColor(borderColor); |
| 109 |
g.drawLine(0, 0, menuWidth - 1, 0); |
| 110 |
g.drawLine(menuWidth - 1, 0, menuWidth - 1, menuHeight - 1); |
| 111 |
g.drawLine(0, menuHeight - 1, 0, 1); |
| 112 |
g.drawLine(0, menuHeight - 1, menuWidth - 1, menuHeight - 1); |
| 113 |
} |
| 114 |
else { |
| 115 |
getPainter().paintMenuItemBackground(menuItem, g, rect, JideSwingUtilities.getOrientationOf(menuItem), ThemePainter.STATE_DEFAULT, false); |
| 116 |
g.setColor(borderColor); |
| 117 |
g.drawLine(0, menuHeight - 1, menuWidth - 1, menuHeight - 1); |
| 118 |
g.drawLine(menuWidth - 1, 0, menuWidth - 1, menuHeight); |
| 119 |
g.drawLine(0, menuHeight - 1, 0, 1); |
| 120 |
g.drawLine(0, 0, menuWidth - 1, 0); |
| 121 |
} |
| 122 |
} |
| 123 |
else { |
| 124 |
getPainter().paintMenuItemBackground(menuItem, g, rect, JideSwingUtilities.getOrientationOf(menuItem), ThemePainter.STATE_ROLLOVER); |
| 125 |
} |
| 126 |
} |
| 127 |
else if (isMouseOver() && model.isEnabled()) { |
| 128 |
getPainter().paintMenuItemBackground(menuItem, g, rect, JideSwingUtilities.getOrientationOf(menuItem), ThemePainter.STATE_ROLLOVER); |
| 129 |
} |
| 130 |
|
| 131 |
if (isDownArrowVisible(menuItem.getParent())) { |
| 132 |
g.setColor(menuItem.isEnabled() ? Color.BLACK : Color.GRAY); |
| 133 |
int middle = menuWidth - 9; |
| 134 |
g.drawLine(middle - 2, menuHeight / 2 - 1, middle + 2, menuHeight / 2 - 1); |
| 135 |
g.drawLine(middle - 1, menuHeight / 2, middle + 1, menuHeight / 2); |
| 136 |
g.drawLine(middle, menuHeight / 2 + 1, middle, menuHeight / 2 + 1); |
| 137 |
} |
| 138 |
g.setColor(oldColor); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
protected void installListeners() { |
| 143 |
super.installListeners(); |
| 144 |
|
| 145 |
if (changeListener == null) |
| 146 |
changeListener = createChangeListener(menuItem); |
| 147 |
|
| 148 |
if (changeListener != null) |
| 149 |
menuItem.addChangeListener(changeListener); |
| 150 |
|
| 151 |
if (propertyChangeListener == null) |
| 152 |
propertyChangeListener = createPropertyChangeListener(menuItem); |
| 153 |
|
| 154 |
if (propertyChangeListener != null) |
| 155 |
menuItem.addPropertyChangeListener(propertyChangeListener); |
| 156 |
|
| 157 |
if (menuListener == null) |
| 158 |
menuListener = createMenuListener(menuItem); |
| 159 |
|
| 160 |
if (menuListener != null && menuItem instanceof JMenu) { |
| 161 |
((JMenu) menuItem).addMenuListener(menuListener); |
| 162 |
} |
| 163 |
|
| 164 |
if (popupMenuListener == null) { |
| 165 |
popupMenuListener = createPopupMenuListener(); |
| 166 |
} |
| 167 |
if (popupMenuListener != null && menuItem instanceof JMenu) { |
| 168 |
((JMenu) menuItem).getPopupMenu().addPopupMenuListener(popupMenuListener); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
protected PopupMenuListener createPopupMenuListener() { |
| 173 |
return new PopupMenuListener() { |
| 174 |
public void popupMenuWillBecomeVisible(PopupMenuEvent e) { |
| 175 |
repaint(); |
| 176 |
} |
| 177 |
|
| 178 |
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { |
| 179 |
repaint(); |
| 180 |
} |
| 181 |
|
| 182 |
public void popupMenuCanceled(PopupMenuEvent e) { |
| 183 |
repaint(); |
| 184 |
} |
| 185 |
|
| 186 |
private void repaint() { |
| 187 |
if (menuItem != null) { |
| 188 |
menuItem.repaint(); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
}; |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
protected void installKeyboardActions() { |
| 197 |
super.installKeyboardActions(); |
| 198 |
updateMnemonicBinding(); |
| 199 |
} |
| 200 |
|
| 201 |
protected void updateMnemonicBinding() { |
| 202 |
int mnemonic = menuItem.getModel().getMnemonic(); |
| 203 |
int[] shortcutKeys = (int[]) UIDefaultsLookup.get("Menu.shortcutKeys"); |
| 204 |
if (shortcutKeys == null) { |
| 205 |
shortcutKeys = new int[]{KeyEvent.ALT_MASK}; |
| 206 |
} |
| 207 |
if (mnemonic == lastMnemonic) { |
| 208 |
return; |
| 209 |
} |
| 210 |
if (lastMnemonic != 0 && windowInputMap != null) { |
| 211 |
for (int shortcutKey : shortcutKeys) { |
| 212 |
windowInputMap.remove(KeyStroke.getKeyStroke |
| 213 |
(lastMnemonic, shortcutKey, false)); |
| 214 |
windowInputMap.remove(KeyStroke.getKeyStroke |
| 215 |
(lastMnemonic, shortcutKey, true)); |
| 216 |
} |
| 217 |
} |
| 218 |
if (mnemonic != 0) { |
| 219 |
if (windowInputMap == null) { |
| 220 |
windowInputMap = createInputMap(JComponent. |
| 221 |
WHEN_IN_FOCUSED_WINDOW); |
| 222 |
SwingUtilities.replaceUIInputMap(menuItem, JComponent. |
| 223 |
WHEN_IN_FOCUSED_WINDOW, windowInputMap); |
| 224 |
} |
| 225 |
for (int shortcutKey : shortcutKeys) { |
| 226 |
windowInputMap.put(KeyStroke.getKeyStroke(mnemonic, |
| 227 |
shortcutKey, false), "selectMenu"); |
| 228 |
} |
| 229 |
} |
| 230 |
lastMnemonic = mnemonic; |
| 231 |
} |
| 232 |
|
| 233 |
@Override |
| 234 |
protected void uninstallKeyboardActions() { |
| 235 |
super.uninstallKeyboardActions(); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* The ActionMap for BasicMenUI can not be shared, this is subclassed to create a new one for each invocation. |
| 240 |
*/ |
| 241 |
@Override |
| 242 |
protected ActionMap getActionMap() { |
| 243 |
return createActionMap(); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Invoked to create the ActionMap. |
| 248 |
*/ |
| 249 |
@Override |
| 250 |
protected ActionMap createActionMap() { |
| 251 |
ActionMap am = super.createActionMap(); |
| 252 |
if (am != null && menuItem instanceof JMenu) { |
| 253 |
am.put("selectMenu", new PostAction((JMenu) menuItem, true)); |
| 254 |
} |
| 255 |
return am; |
| 256 |
} |
| 257 |
|
| 258 |
@Override |
| 259 |
protected MouseInputListener createMouseInputListener(JComponent c) { |
| 260 |
return new MouseInputHandler(); |
| 261 |
} |
| 262 |
|
| 263 |
protected MenuListener createMenuListener(JComponent c) { |
| 264 |
return new MenuHandler(); |
| 265 |
} |
| 266 |
|
| 267 |
protected ChangeListener createChangeListener(JComponent c) { |
| 268 |
return null; |
| 269 |
} |
| 270 |
|
| 271 |
protected PropertyChangeListener createPropertyChangeListener(JComponent c) { |
| 272 |
return new PropertyChangeHandler(); |
| 273 |
} |
| 274 |
|
| 275 |
@Override |
| 276 |
protected void uninstallDefaults() { |
| 277 |
menuItem.setArmed(false); |
| 278 |
menuItem.setSelected(false); |
| 279 |
menuItem.resetKeyboardActions(); |
| 280 |
super.uninstallDefaults(); |
| 281 |
} |
| 282 |
|
| 283 |
@Override |
| 284 |
protected void uninstallListeners() { |
| 285 |
super.uninstallListeners(); |
| 286 |
|
| 287 |
if (changeListener != null) |
| 288 |
menuItem.removeChangeListener(changeListener); |
| 289 |
|
| 290 |
changeListener = null; |
| 291 |
|
| 292 |
if (propertyChangeListener != null) |
| 293 |
menuItem.removePropertyChangeListener(propertyChangeListener); |
| 294 |
|
| 295 |
propertyChangeListener = null; |
| 296 |
|
| 297 |
if (menuListener != null && menuItem instanceof JMenu) { |
| 298 |
((JMenu) menuItem).removeMenuListener(menuListener); |
| 299 |
} |
| 300 |
|
| 301 |
menuListener = null; |
| 302 |
|
| 303 |
if (popupMenuListener != null && menuItem instanceof JMenu) { |
| 304 |
((JMenu) menuItem).getPopupMenu().removePopupMenuListener(popupMenuListener); |
| 305 |
} |
| 306 |
|
| 307 |
popupMenuListener = null; |
| 308 |
} |
| 309 |
|
| 310 |
@Override |
| 311 |
protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) { |
| 312 |
return new MenuDragMouseHandler(); |
| 313 |
} |
| 314 |
|
| 315 |
@Override |
| 316 |
protected MenuKeyListener createMenuKeyListener(JComponent c) { |
| 317 |
return new MenuKeyHandler(); |
| 318 |
} |
| 319 |
|
| 320 |
@Override |
| 321 |
public Dimension getMaximumSize(JComponent c) { |
| 322 |
if (menuItem instanceof JMenu && ((JMenu) menuItem).isTopLevelMenu()) { |
| 323 |
Dimension d = c.getPreferredSize(); |
| 324 |
if (JideSwingUtilities.getOrientationOf(menuItem) == SwingConstants.HORIZONTAL) { |
| 325 |
return new Dimension(d.width, Short.MAX_VALUE); |
| 326 |
} |
| 327 |
else { |
| 328 |
return new Dimension(Short.MAX_VALUE, d.height); |
| 329 |
} |
| 330 |
} |
| 331 |
return null; |
| 332 |
} |
| 333 |
|
| 334 |
protected static void setupPostTimer(JMenu menu) { |
| 335 |
Timer timer = new Timer(menu.getDelay(), new PostAction(menu, false)); |
| 336 |
timer.setRepeats(false); |
| 337 |
timer.start(); |
| 338 |
} |
| 339 |
|
| 340 |
protected static void appendPath(MenuElement[] path, MenuElement elem) { |
| 341 |
MenuElement newPath[] = new MenuElement[path.length + 1]; |
| 342 |
System.arraycopy(path, 0, newPath, 0, path.length); |
| 343 |
newPath[path.length] = elem; |
| 344 |
MenuSelectionManager.defaultManager().setSelectedPath(newPath); |
| 345 |
} |
| 346 |
|
| 347 |
protected static class PostAction extends AbstractAction { |
| 348 |
JMenu menu; |
| 349 |
boolean force = false; |
| 350 |
private static final long serialVersionUID = 8723756757971612903L; |
| 351 |
|
| 352 |
PostAction(JMenu menu, boolean shouldForce) { |
| 353 |
this.menu = menu; |
| 354 |
this.force = shouldForce; |
| 355 |
} |
| 356 |
|
| 357 |
public void actionPerformed(ActionEvent e) { |
| 358 |
if (!crossMenuMnemonic) { |
| 359 |
JPopupMenu pm = getActivePopupMenu(); |
| 360 |
if (pm != null && pm != menu.getParent()) { |
| 361 |
return; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
final MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager(); |
| 366 |
if (force) { |
| 367 |
Container cnt = menu.getParent(); |
| 368 |
if (cnt != null && cnt instanceof JMenuBar) { |
| 369 |
MenuElement me[]; |
| 370 |
MenuElement subElements[]; |
| 371 |
|
| 372 |
subElements = menu.getPopupMenu().getSubElements(); |
| 373 |
if (subElements.length > 0) { |
| 374 |
me = new MenuElement[4]; |
| 375 |
me[0] = (MenuElement) cnt; |
| 376 |
me[1] = menu; |
| 377 |
me[2] = menu.getPopupMenu(); |
| 378 |
me[3] = subElements[0]; |
| 379 |
} |
| 380 |
else { |
| 381 |
me = new MenuElement[3]; |
| 382 |
me[0] = (MenuElement) cnt; |
| 383 |
me[1] = menu; |
| 384 |
me[2] = menu.getPopupMenu(); |
| 385 |
} |
| 386 |
defaultManager.setSelectedPath(me); |
| 387 |
} |
| 388 |
} |
| 389 |
else { |
| 390 |
MenuElement path[] = defaultManager.getSelectedPath(); |
| 391 |
if (path.length > 0 && path[path.length - 1] == menu) { |
| 392 |
appendPath(path, menu.getPopupMenu()); |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
@Override |
| 398 |
public boolean isEnabled() { |
| 399 |
return menu.getModel().isEnabled(); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
private class PropertyChangeHandler implements PropertyChangeListener { |
| 405 |
public void propertyChange(PropertyChangeEvent e) { |
| 406 |
String prop = e.getPropertyName(); |
| 407 |
if (prop.equals(AbstractButton.MNEMONIC_CHANGED_PROPERTY)) { |
| 408 |
updateMnemonicBinding(); |
| 409 |
} |
| 410 |
else if (prop.equals(ButtonStyle.BUTTON_STYLE_PROPERTY)) { |
| 411 |
AbstractButton b = (AbstractButton) e.getSource(); |
| 412 |
b.repaint(); |
| 413 |
} |
| 414 |
else if ("verticalTextPosition".equals(prop) |
| 415 |
|| "horizontalTextPosition".equals(prop)) { |
| 416 |
AbstractButton b = (AbstractButton) e.getSource(); |
| 417 |
b.updateUI(); |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
/** |
| 423 |
* Instantiated and used by a menu item to handle the current menu selection from mouse events. A MouseInputHandler |
| 424 |
* processes and forwards all mouse events to a shared instance of the MenuSelectionManager. |
| 425 |
* <p/> |
| 426 |
* This class is protected so that it can be subclassed by other look and feels to implement their own mouse |
| 427 |
* handling behavior. All overridden methods should call the parent methods so that the menu selection is correct. |
| 428 |
* |
| 429 |
* @see MenuSelectionManager |
| 430 |
* @since 1.4 |
| 431 |
*/ |
| 432 |
protected class MouseInputHandler implements MouseInputListener { |
| 433 |
|
| 434 |
public void mouseClicked(MouseEvent e) { |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Invoked when the mouse has been clicked on the menu. This method clears or sets the selection path of the |
| 439 |
* MenuSelectionManager. |
| 440 |
* |
| 441 |
* @param e the mouse event |
| 442 |
*/ |
| 443 |
public void mousePressed(MouseEvent e) { |
| 444 |
if (!(menuItem instanceof JMenu)) { |
| 445 |
return; |
| 446 |
} |
| 447 |
|
| 448 |
JMenu menu = (JMenu) menuItem; |
| 449 |
if (!menu.isEnabled()) |
| 450 |
return; |
| 451 |
|
| 452 |
MenuSelectionManager manager = |
| 453 |
MenuSelectionManager.defaultManager(); |
| 454 |
if (menu.getParent() instanceof JMenuBar || menu.getParent() instanceof TopLevelMenuContainer) { |
| 455 |
if (menu.isSelected()) { |
| 456 |
manager.clearSelectedPath(); |
| 457 |
} |
| 458 |
else { |
| 459 |
//Container cnt = menu.getParent(); |
| 460 |
Container cnt = getFirstParentMenuElement(menu); |
| 461 |
|
| 462 |
if (cnt != null && cnt instanceof MenuElement) { |
| 463 |
ArrayList<Component> parents = new ArrayList<Component>(); |
| 464 |
while (cnt instanceof MenuElement) { |
| 465 |
parents.add(0, cnt); |
| 466 |
if (cnt instanceof JPopupMenu) { |
| 467 |
cnt = (Container) ((JPopupMenu) cnt).getInvoker(); |
| 468 |
} |
| 469 |
else { |
| 470 |
//cnt = cnt.getParent(); |
| 471 |
cnt = getFirstParentMenuElement(cnt); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
MenuElement me[] = new MenuElement[parents.size() + 1]; |
| 476 |
for (int i = 0; i < parents.size(); i++) { |
| 477 |
Container container = (Container) parents.get(i); |
| 478 |
me[i] = (MenuElement) container; |
| 479 |
} |
| 480 |
me[parents.size()] = menu; |
| 481 |
manager.setSelectedPath(me); |
| 482 |
} |
| 483 |
else { |
| 484 |
MenuElement me[] = new MenuElement[1]; |
| 485 |
me[0] = menu; |
| 486 |
manager.setSelectedPath(me); |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
MenuElement selectedPath[] = manager.getSelectedPath(); |
| 492 |
if (selectedPath.length > 0 && |
| 493 |
selectedPath[selectedPath.length - 1] != menu.getPopupMenu()) { |
| 494 |
|
| 495 |
if (menu.isTopLevelMenu() || |
| 496 |
menu.getDelay() == 0) { |
| 497 |
appendPath(selectedPath, menu.getPopupMenu()); |
| 498 |
} |
| 499 |
else { |
| 500 |
setupPostTimer(menu); |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
protected Container getFirstParentMenuElement(Component comp) { |
| 506 |
Container parent = comp.getParent(); |
| 507 |
|
| 508 |
while (parent != null) { |
| 509 |
if (parent instanceof MenuElement) |
| 510 |
return parent; |
| 511 |
|
| 512 |
parent = parent.getParent(); |
| 513 |
} |
| 514 |
|
| 515 |
return null; |
| 516 |
} |
| 517 |
|
| 518 |
/** |
| 519 |
* Invoked when the mouse has been released on the menu. Delegates the mouse event to the MenuSelectionManager. |
| 520 |
* |
| 521 |
* @param e the mouse event |
| 522 |
*/ |
| 523 |
public void mouseReleased(MouseEvent e) { |
| 524 |
if (!SwingUtilities.isLeftMouseButton(e)) { |
| 525 |
return; |
| 526 |
} |
| 527 |
|
| 528 |
if (!(menuItem instanceof JMenu)) { |
| 529 |
return; |
| 530 |
} |
| 531 |
|
| 532 |
JMenu menu = (JMenu) menuItem; |
| 533 |
if (!menu.isEnabled()) |
| 534 |
return; |
| 535 |
MenuSelectionManager manager = |
| 536 |
MenuSelectionManager.defaultManager(); |
| 537 |
manager.processMouseEvent(e); |
| 538 |
if (!e.isConsumed()) |
| 539 |
manager.clearSelectedPath(); |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
* Invoked when the cursor enters the menu. This method sets the selected path for the MenuSelectionManager and |
| 544 |
* handles the case in which a menu item is used to pop up an additional menu, as in a hierarchical menu |
| 545 |
* system. |
| 546 |
* |
| 547 |
* @param e the mouse event; not used |
| 548 |
*/ |
| 549 |
public void mouseEntered(MouseEvent e) { |
| 550 |
if (!(menuItem instanceof JMenu)) { |
| 551 |
return; |
| 552 |
} |
| 553 |
JMenu menu = (JMenu) menuItem; |
| 554 |
if (!menu.isEnabled()) |
| 555 |
return; |
| 556 |
|
| 557 |
MenuSelectionManager manager = |
| 558 |
MenuSelectionManager.defaultManager(); |
| 559 |
MenuElement selectedPath[] = manager.getSelectedPath(); |
| 560 |
if (!menu.isTopLevelMenu()) { |
| 561 |
if (!(selectedPath.length > 0 && |
| 562 |
selectedPath[selectedPath.length - 1] == |
| 563 |
menu.getPopupMenu())) { |
| 564 |
if (menu.getDelay() == 0) { |
| 565 |
appendPath(getPath(), menu.getPopupMenu()); |
| 566 |
} |
| 567 |
else { |
| 568 |
manager.setSelectedPath(getPath()); |
| 569 |
setupPostTimer(menu); |
| 570 |
} |
| 571 |
} |
| 572 |
} |
| 573 |
else { |
| 574 |
if (selectedPath.length > 0 && |
| 575 |
selectedPath[0] == menu.getParent()) { |
| 576 |
MenuElement newPath[] = new MenuElement[3]; |
| 577 |
// A top level menu's parent is by definition |
| 578 |
// a JMenuBar |
| 579 |
newPath[0] = (MenuElement) menu.getParent(); |
| 580 |
newPath[1] = menu; |
| 581 |
newPath[2] = menu.getPopupMenu(); |
| 582 |
manager.setSelectedPath(newPath); |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
if (!SwingUtilities.isLeftMouseButton(e)) { |
| 587 |
setMouseOver(true); |
| 588 |
} |
| 589 |
menuItem.repaint(); |
| 590 |
} |
| 591 |
|
| 592 |
public void mouseExited(MouseEvent e) { |
| 593 |
setMouseOver(false); |
| 594 |
menuItem.repaint(); |
| 595 |
} |
| 596 |
|
| 597 |
/** |
| 598 |
* Invoked when a mouse button is pressed on the menu and then dragged. Delegates the mouse event to the |
| 599 |
* MenuSelectionManager. |
| 600 |
* |
| 601 |
* @param e the mouse event |
| 602 |
* @see MouseMotionListener#mouseDragged |
| 603 |
*/ |
| 604 |
public void mouseDragged(MouseEvent e) { |
| 605 |
if (!(menuItem instanceof JMenu)) { |
| 606 |
return; |
| 607 |
} |
| 608 |
JMenu menu = (JMenu) menuItem; |
| 609 |
if (!menu.isEnabled()) |
| 610 |
return; |
| 611 |
MenuSelectionManager.defaultManager().processMouseEvent(e); |
| 612 |
} |
| 613 |
|
| 614 |
public void mouseMoved(MouseEvent e) { |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
private class MenuHandler implements MenuListener { |
| 619 |
public void menuSelected(MenuEvent e) { |
| 620 |
} |
| 621 |
|
| 622 |
public void menuDeselected(MenuEvent e) { |
| 623 |
} |
| 624 |
|
| 625 |
public void menuCanceled(MenuEvent e) { |
| 626 |
if (!(menuItem instanceof JMenu)) { |
| 627 |
return; |
| 628 |
} |
| 629 |
JMenu m = (JMenu) e.getSource(); |
| 630 |
MenuSelectionManager manager = MenuSelectionManager.defaultManager(); |
| 631 |
if (manager.isComponentPartOfCurrentMenu(m)) |
| 632 |
MenuSelectionManager.defaultManager().clearSelectedPath(); |
| 633 |
} |
| 634 |
|
| 635 |
} |
| 636 |
|
| 637 |
private class MenuDragMouseHandler implements MenuDragMouseListener { |
| 638 |
public void menuDragMouseEntered(MenuDragMouseEvent e) { |
| 639 |
} |
| 640 |
|
| 641 |
public void menuDragMouseDragged(MenuDragMouseEvent e) { |
| 642 |
if (!(menuItem instanceof JMenu)) { |
| 643 |
return; |
| 644 |
} |
| 645 |
if (!menuItem.isEnabled()) |
| 646 |
return; |
| 647 |
|
| 648 |
MenuSelectionManager manager = e.getMenuSelectionManager(); |
| 649 |
MenuElement path[] = e.getPath(); |
| 650 |
|
| 651 |
Point p = e.getPoint(); |
| 652 |
if (p.x >= 0 && p.x < menuItem.getWidth() && |
| 653 |
p.y >= 0 && p.y < menuItem.getHeight()) { |
| 654 |
JMenu menu = (JMenu) menuItem; |
| 655 |
MenuElement selectedPath[] = manager.getSelectedPath(); |
| 656 |
if (!(selectedPath.length > 0 && |
| 657 |
selectedPath[selectedPath.length - 1] == |
| 658 |
menu.getPopupMenu())) { |
| 659 |
if (menu.isTopLevelMenu() || |
| 660 |
menu.getDelay() == 0 || |
| 661 |
e.getID() == MouseEvent.MOUSE_DRAGGED) { |
| 662 |
appendPath(path, menu.getPopupMenu()); |
| 663 |
} |
| 664 |
else { |
| 665 |
manager.setSelectedPath(path); |
| 666 |
setupPostTimer(menu); |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
else if (e.getID() == MouseEvent.MOUSE_RELEASED) { |
| 671 |
Component comp = manager.componentForPoint(e.getComponent(), e.getPoint()); |
| 672 |
if (comp == null) |
| 673 |
manager.clearSelectedPath(); |
| 674 |
} |
| 675 |
|
| 676 |
} |
| 677 |
|
| 678 |
public void menuDragMouseExited(MenuDragMouseEvent e) { |
| 679 |
} |
| 680 |
|
| 681 |
public void menuDragMouseReleased(MenuDragMouseEvent e) { |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
static JPopupMenu getActivePopupMenu() { |
| 686 |
MenuElement[] path = MenuSelectionManager.defaultManager(). |
| 687 |
getSelectedPath(); |
| 688 |
for (int i = path.length - 1; i >= 0; i--) { |
| 689 |
MenuElement elem = path[i]; |
| 690 |
if (elem instanceof JPopupMenu) { |
| 691 |
return (JPopupMenu) elem; |
| 692 |
} |
| 693 |
} |
| 694 |
return null; |
| 695 |
} |
| 696 |
|
| 697 |
/** |
| 698 |
* Handles the mnemonic handling for the JMenu and JMenuItems. |
| 699 |
*/ |
| 700 |
private class MenuKeyHandler implements MenuKeyListener { |
| 701 |
|
| 702 |
/** |
| 703 |
* Opens the SubMenu |
| 704 |
*/ |
| 705 |
public void menuKeyTyped(MenuKeyEvent e) { |
| 706 |
if (DEBUG) { |
| 707 |
System.out.println("in BasicMenuUI.menuKeyTyped for " + menuItem.getText()); |
| 708 |
} |
| 709 |
if (!crossMenuMnemonic) { |
| 710 |
JPopupMenu pm = getActivePopupMenu(); |
| 711 |
if (pm != null && pm != menuItem.getParent()) { |
| 712 |
return; |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
int key = menuItem.getMnemonic(); |
| 717 |
if (key == 0) |
| 718 |
return; |
| 719 |
MenuElement path[] = e.getPath(); |
| 720 |
if (lower((char) key) == lower(e.getKeyChar())) { |
| 721 |
JPopupMenu popupMenu = ((JMenu) menuItem).getPopupMenu(); |
| 722 |
ArrayList newList = new ArrayList(Arrays.asList(path)); |
| 723 |
newList.add(popupMenu); |
| 724 |
MenuElement sub[] = popupMenu.getSubElements(); |
| 725 |
if (sub.length > 0) { |
| 726 |
newList.add(sub[0]); |
| 727 |
} |
| 728 |
MenuSelectionManager manager = e.getMenuSelectionManager(); |
| 729 |
MenuElement newPath[] = new MenuElement[0]; |
| 730 |
newPath = (MenuElement[]) newList.toArray(newPath); |
| 731 |
manager.setSelectedPath(newPath); |
| 732 |
e.consume(); |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
/** |
| 737 |
* Handles the mnemonics for the menu items. Will also handle duplicate mnemonics. Perhaps this should be moved |
| 738 |
* into BasicPopupMenuUI. See 4670831 |
| 739 |
*/ |
| 740 |
public void menuKeyPressed(MenuKeyEvent e) { |
| 741 |
if (DEBUG) { |
| 742 |
System.out.println("in BasicMenuUI.menuKeyPressed for " + menuItem.getText()); |
| 743 |
} |
| 744 |
// Handle the case for Escape or Enter... |
| 745 |
char keyChar = e.getKeyChar(); |
| 746 |
if (!Character.isLetterOrDigit(keyChar)) |
| 747 |
return; |
| 748 |
|
| 749 |
MenuSelectionManager manager = e.getMenuSelectionManager(); |
| 750 |
MenuElement path[] = e.getPath(); |
| 751 |
MenuElement selectedPath[] = manager.getSelectedPath(); |
| 752 |
|
| 753 |
for (int i = selectedPath.length - 1; i >= 0; i--) { |
| 754 |
if (selectedPath[i] == menuItem) { |
| 755 |
JPopupMenu popupMenu = ((JMenu) menuItem).getPopupMenu(); |
| 756 |
if (!popupMenu.isVisible()) { |
| 757 |
return; // Do not invoke items from invisible popup |
| 758 |
} |
| 759 |
MenuElement items[] = popupMenu.getSubElements(); |
| 760 |
|
| 761 |
MenuElement currentItem = selectedPath[selectedPath.length - 1]; |
| 762 |
int currentIndex = -1; |
| 763 |
int matches = 0; |
| 764 |
int firstMatch = -1; |
| 765 |
int indexes[] = null; |
| 766 |
|
| 767 |
for (int j = 0; j < items.length; j++) { |
| 768 |
if (!(items[j] instanceof JMenuItem)) { |
| 769 |
continue; |
| 770 |
} |
| 771 |
int key = ((JMenuItem) items[j]).getMnemonic(); |
| 772 |
if (lower((char) key) == lower(keyChar)) { |
| 773 |
if (matches == 0) { |
| 774 |
firstMatch = j; |
| 775 |
matches++; |
| 776 |
} |
| 777 |
else { |
| 778 |
if (indexes == null) { |
| 779 |
indexes = new int[items.length]; |
| 780 |
indexes[0] = firstMatch; |
| 781 |
} |
| 782 |
indexes[matches++] = j; |
| 783 |
} |
| 784 |
} |
| 785 |
if (currentItem == items[j]) { |
| 786 |
currentIndex = matches - 1; |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
if (matches == 0) { |
| 791 |
// no op (consume) |
| 792 |
} |
| 793 |
else if (matches == 1) { |
| 794 |
// Invoke the menu action |
| 795 |
JMenuItem item = (JMenuItem) items[firstMatch]; |
| 796 |
if (!(item instanceof JMenu)) { |
| 797 |
// Let Submenus be handled by menuKeyTyped |
| 798 |
manager.clearSelectedPath(); |
| 799 |
item.doClick(); |
| 800 |
} |
| 801 |
} |
| 802 |
else { |
| 803 |
// Select the menu item with the matching mnemonic. If |
| 804 |
// the same mnemonic has been invoked then select the next |
| 805 |
// menu item in the cycle. |
| 806 |
MenuElement newItem = null; |
| 807 |
if (indexes != null) { |
| 808 |
newItem = items[indexes[(currentIndex + 1) % matches]]; |
| 809 |
} |
| 810 |
|
| 811 |
MenuElement newPath[] = new MenuElement[path.length + 2]; |
| 812 |
System.arraycopy(path, 0, newPath, 0, path.length); |
| 813 |
newPath[path.length] = popupMenu; |
| 814 |
newPath[path.length + 1] = newItem; |
| 815 |
manager.setSelectedPath(newPath); |
| 816 |
} |
| 817 |
e.consume(); |
| 818 |
return; |
| 819 |
} |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
public void menuKeyReleased(MenuKeyEvent e) { |
| 824 |
} |
| 825 |
|
| 826 |
private char lower(char keyChar) { |
| 827 |
return Character.toLowerCase(keyChar); |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
/** |
| 832 |
* Method which renders the text of the current menu item. |
| 833 |
* <p/> |
| 834 |
* |
| 835 |
* @param g Graphics context |
| 836 |
* @param menuItem Current menu item to render |
| 837 |
* @param textRect Bounding rectangle to render the text. |
| 838 |
* @param text String to render |
| 839 |
* @since 1.4 |
| 840 |
*/ |
| 841 |
@Override |
| 842 |
protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect, String text) { |
| 843 |
// Note: This method is almost identical to the same method in WindowsMenuItemUI |
| 844 |
ButtonModel model = menuItem.getModel(); |
| 845 |
|
| 846 |
if (!(menuItem instanceof JMenu) || !((JMenu) menuItem).isTopLevelMenu()) { |
| 847 |
if (menuItem.getComponentOrientation().isLeftToRight()) { |
| 848 |
int defaultTextIconGap = UIDefaultsLookup.getInt("MenuItem.textIconGap"); |
| 849 |
int defaultShadowWidth = UIDefaultsLookup.getInt("MenuItem.shadowWidth"); |
| 850 |
textRect.x = defaultShadowWidth + defaultTextIconGap; |
| 851 |
} |
| 852 |
else { |
| 853 |
int defaultTextIconGap = UIDefaultsLookup.getInt("MenuItem.textIconGap"); |
| 854 |
int defaultShadowWidth = UIDefaultsLookup.getInt("MenuItem.shadowWidth"); |
| 855 |
textRect.x = viewRect.width - (defaultShadowWidth + defaultTextIconGap + textRect.width); |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
if (!model.isEnabled()) { |
| 860 |
// *** paint the text disabled |
| 861 |
WindowsGraphicsUtilsPort.paintText(g, menuItem, textRect, text, 0); |
| 862 |
} |
| 863 |
else { |
| 864 |
FontMetrics fm = g.getFontMetrics(); |
| 865 |
int mnemonicIndex = menuItem.getDisplayedMnemonicIndex(); |
| 866 |
// W2K Feature: Check to see if the Underscore should be rendered. |
| 867 |
if (LookAndFeelFactory.isMnemonicHidden()) { |
| 868 |
mnemonicIndex = -1; |
| 869 |
} |
| 870 |
|
| 871 |
Color oldColor = g.getColor(); |
| 872 |
|
| 873 |
// For Win95, the selected text color is the selection foreground color |
| 874 |
if (model.isSelected()) { |
| 875 |
if (/*SystemInfo.isClassicWindows() || |
| 876 |
*/menuItem instanceof JMenu && !((JMenu) menuItem).isTopLevelMenu()) { |
| 877 |
|
| 878 |
g.setColor(selectionForeground); // Uses protected field. |
| 879 |
} |
| 880 |
} |
| 881 |
JideSwingUtilities.drawStringUnderlineCharAt(menuItem, g, text, |
| 882 |
mnemonicIndex, |
| 883 |
textRect.x, |
| 884 |
textRect.y + fm.getAscent()); |
| 885 |
g.setColor(oldColor); |
| 886 |
} |
| 887 |
// } |
| 888 |
} |
| 889 |
|
| 890 |
/** |
| 891 |
* Set the temporary flag to indicate if the mouse has entered the menu. |
| 892 |
* |
| 893 |
* @param over the flag |
| 894 |
*/ |
| 895 |
protected void setMouseOver(boolean over) { |
| 896 |
isMouseOver = over; |
| 897 |
menuItem.getModel().setRollover(isMouseOver); |
| 898 |
} |
| 899 |
|
| 900 |
/** |
| 901 |
* Get the temporary flag to indicate if the mouse has entered the menu. |
| 902 |
* |
| 903 |
* @return the flag. |
| 904 |
*/ |
| 905 |
protected boolean isMouseOver() { |
| 906 |
return isMouseOver; |
| 907 |
} |
| 908 |
|
| 909 |
@Override |
| 910 |
public Dimension getPreferredSize(JComponent c) { |
| 911 |
Dimension size = super.getPreferredSize(c); |
| 912 |
if (menuItem instanceof JMenu && ((JMenu) menuItem).isTopLevelMenu() && |
| 913 |
isDownArrowVisible(menuItem.getParent())) { |
| 914 |
if (JideSwingUtilities.getOrientationOf(menuItem) == SwingConstants.HORIZONTAL) |
| 915 |
size.width += 11; |
| 916 |
else |
| 917 |
size.height += 11; |
| 918 |
} |
| 919 |
return size; |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|