rev 47462 : Fixed JDK-8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated.
1 /* 2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.swing.plaf.basic; 27 28 import java.awt.*; 29 import java.awt.event.*; 30 import java.beans.PropertyChangeEvent; 31 import java.beans.PropertyChangeListener; 32 33 import javax.swing.*; 34 import javax.swing.event.*; 35 import javax.swing.plaf.*; 36 import javax.swing.text.View; 37 38 import sun.swing.*; 39 40 /** 41 * BasicMenuItem implementation 42 * 43 * @author Georges Saab 44 * @author David Karlton 45 * @author Arnaud Weber 46 * @author Fredrik Lagerblad 47 */ 48 public class BasicMenuItemUI extends MenuItemUI 49 { 50 /** 51 * The instance of {@code JMenuItem}. 52 */ 53 protected JMenuItem menuItem = null; 54 /** 55 * The color of the selection background. 56 */ 57 protected Color selectionBackground; 58 /** 59 * The color of the selection foreground. 60 */ 61 protected Color selectionForeground; 62 /** 63 * The color of the disabled foreground. 64 */ 65 protected Color disabledForeground; 66 /** 67 * The color of the accelerator foreground. 68 */ 69 protected Color acceleratorForeground; 70 /** 71 * The color of the accelerator selection. 72 */ 73 protected Color acceleratorSelectionForeground; 74 75 /** 76 * Accelerator delimiter string, such as {@code '+'} in {@code 'Ctrl+C'}. 77 * @since 1.7 78 */ 79 protected String acceleratorDelimiter; 80 81 /** 82 * The gap between the text and the icon. 83 */ 84 protected int defaultTextIconGap; 85 /** 86 * The accelerator font. 87 */ 88 protected Font acceleratorFont; 89 90 /** 91 * The instance of {@code MouseInputListener}. 92 */ 93 protected MouseInputListener mouseInputListener; 94 /** 95 * The instance of {@code MenuDragMouseListener}. 96 */ 97 protected MenuDragMouseListener menuDragMouseListener; 98 /** 99 * The instance of {@code MenuKeyListener}. 100 */ 101 protected MenuKeyListener menuKeyListener; 102 /** 103 * {@code PropertyChangeListener} returned from 104 * {@code createPropertyChangeListener}. You should not 105 * need to access this field, rather if you want to customize the 106 * {@code PropertyChangeListener} override 107 * {@code createPropertyChangeListener}. 108 * 109 * @since 1.6 110 * @see #createPropertyChangeListener 111 */ 112 protected PropertyChangeListener propertyChangeListener; 113 // BasicMenuUI also uses this. 114 Handler handler; 115 /** 116 * The arrow icon. 117 */ 118 protected Icon arrowIcon = null; 119 /** 120 * The check icon. 121 */ 122 protected Icon checkIcon = null; 123 /** 124 * The value represents if the old border is painted. 125 */ 126 protected boolean oldBorderPainted; 127 128 /* diagnostic aids -- should be false for production builds. */ 129 private static final boolean TRACE = false; // trace creates and disposes 130 131 private static final boolean VERBOSE = false; // show reuse hits/misses 132 private static final boolean DEBUG = false; // show bad params, misc. 133 134 static void loadActionMap(LazyActionMap map) { 135 // NOTE: BasicMenuUI also calls into this method. 136 map.put(new Actions(Actions.CLICK)); 137 BasicLookAndFeel.installAudioActionMap(map); 138 } 139 140 /** 141 * Returns a new instance of {@code BasicMenuItemUI}. 142 * 143 * @param c a component 144 * @return a new instance of {@code BasicMenuItemUI} 145 */ 146 public static ComponentUI createUI(JComponent c) { 147 return new BasicMenuItemUI(); 148 } 149 150 public void installUI(JComponent c) { 151 menuItem = (JMenuItem) c; 152 153 installDefaults(); 154 installComponents(menuItem); 155 installListeners(); 156 installKeyboardActions(); 157 } 158 159 /** 160 * Installs default properties. 161 */ 162 protected void installDefaults() { 163 String prefix = getPropertyPrefix(); 164 165 acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont"); 166 // use default if missing so that BasicMenuItemUI can be used in other 167 // LAFs like Nimbus 168 if (acceleratorFont == null) { 169 acceleratorFont = UIManager.getFont("MenuItem.font"); 170 } 171 172 Object opaque = UIManager.get(getPropertyPrefix() + ".opaque"); 173 if (opaque != null) { 174 LookAndFeel.installProperty(menuItem, "opaque", opaque); 175 } 176 else { 177 LookAndFeel.installProperty(menuItem, "opaque", Boolean.TRUE); 178 } 179 if(menuItem.getMargin() == null || 180 (menuItem.getMargin() instanceof UIResource)) { 181 menuItem.setMargin(UIManager.getInsets(prefix + ".margin")); 182 } 183 184 LookAndFeel.installProperty(menuItem, "iconTextGap", Integer.valueOf(4)); 185 defaultTextIconGap = menuItem.getIconTextGap(); 186 187 LookAndFeel.installBorder(menuItem, prefix + ".border"); 188 oldBorderPainted = menuItem.isBorderPainted(); 189 LookAndFeel.installProperty(menuItem, "borderPainted", 190 UIManager.getBoolean(prefix + ".borderPainted")); 191 LookAndFeel.installColorsAndFont(menuItem, 192 prefix + ".background", 193 prefix + ".foreground", 194 prefix + ".font"); 195 196 // MenuItem specific defaults 197 if (selectionBackground == null || 198 selectionBackground instanceof UIResource) { 199 selectionBackground = 200 UIManager.getColor(prefix + ".selectionBackground"); 201 } 202 if (selectionForeground == null || 203 selectionForeground instanceof UIResource) { 204 selectionForeground = 205 UIManager.getColor(prefix + ".selectionForeground"); 206 } 207 if (disabledForeground == null || 208 disabledForeground instanceof UIResource) { 209 disabledForeground = 210 UIManager.getColor(prefix + ".disabledForeground"); 211 } 212 if (acceleratorForeground == null || 213 acceleratorForeground instanceof UIResource) { 214 acceleratorForeground = 215 UIManager.getColor(prefix + ".acceleratorForeground"); 216 } 217 if (acceleratorSelectionForeground == null || 218 acceleratorSelectionForeground instanceof UIResource) { 219 acceleratorSelectionForeground = 220 UIManager.getColor(prefix + ".acceleratorSelectionForeground"); 221 } 222 // Get accelerator delimiter 223 acceleratorDelimiter = 224 UIManager.getString("MenuItem.acceleratorDelimiter"); 225 if (acceleratorDelimiter == null) { acceleratorDelimiter = "+"; } 226 // Icons 227 if (arrowIcon == null || 228 arrowIcon instanceof UIResource) { 229 arrowIcon = UIManager.getIcon(prefix + ".arrowIcon"); 230 } 231 updateCheckIcon(); 232 } 233 234 /** 235 * Updates check Icon based on column layout 236 */ 237 private void updateCheckIcon() { 238 String prefix = getPropertyPrefix(); 239 240 if (checkIcon == null || 241 checkIcon instanceof UIResource) { 242 checkIcon = UIManager.getIcon(prefix + ".checkIcon"); 243 //In case of column layout, .checkIconFactory is defined for this UI, 244 //the icon is compatible with it and useCheckAndArrow() is true, 245 //then the icon is handled by the checkIcon. 246 boolean isColumnLayout = MenuItemLayoutHelper.isColumnLayout( 247 BasicGraphicsUtils.isLeftToRight(menuItem), menuItem); 248 if (isColumnLayout) { 249 MenuItemCheckIconFactory iconFactory = 250 (MenuItemCheckIconFactory) UIManager.get(prefix 251 + ".checkIconFactory"); 252 if (iconFactory != null 253 && MenuItemLayoutHelper.useCheckAndArrow(menuItem) 254 && iconFactory.isCompatible(checkIcon, prefix)) { 255 checkIcon = iconFactory.getIcon(menuItem); 256 } 257 } 258 } 259 } 260 261 /** 262 * 263 * @param menuItem a menu item 264 * @since 1.3 265 */ 266 protected void installComponents(JMenuItem menuItem){ 267 BasicHTML.updateRenderer(menuItem, menuItem.getText()); 268 } 269 270 /** 271 * Returns a property prefix. 272 * 273 * @return a property prefix 274 */ 275 protected String getPropertyPrefix() { 276 return "MenuItem"; 277 } 278 279 /** 280 * Registers listeners. 281 */ 282 protected void installListeners() { 283 if ((mouseInputListener = createMouseInputListener(menuItem)) != null) { 284 menuItem.addMouseListener(mouseInputListener); 285 menuItem.addMouseMotionListener(mouseInputListener); 286 } 287 if ((menuDragMouseListener = createMenuDragMouseListener(menuItem)) != null) { 288 menuItem.addMenuDragMouseListener(menuDragMouseListener); 289 } 290 if ((menuKeyListener = createMenuKeyListener(menuItem)) != null) { 291 menuItem.addMenuKeyListener(menuKeyListener); 292 } 293 if ((propertyChangeListener = createPropertyChangeListener(menuItem)) != null) { 294 menuItem.addPropertyChangeListener(propertyChangeListener); 295 } 296 } 297 298 /** 299 * Registers keyboard action. 300 */ 301 protected void installKeyboardActions() { 302 installLazyActionMap(); 303 updateAcceleratorBinding(); 304 } 305 306 void installLazyActionMap() { 307 LazyActionMap.installLazyActionMap(menuItem, BasicMenuItemUI.class, 308 getPropertyPrefix() + ".actionMap"); 309 } 310 311 public void uninstallUI(JComponent c) { 312 menuItem = (JMenuItem)c; 313 uninstallDefaults(); 314 uninstallComponents(menuItem); 315 uninstallListeners(); 316 uninstallKeyboardActions(); 317 MenuItemLayoutHelper.clearUsedParentClientProperties(menuItem); 318 menuItem = null; 319 } 320 321 /** 322 * Uninstalls default properties. 323 */ 324 protected void uninstallDefaults() { 325 LookAndFeel.uninstallBorder(menuItem); 326 LookAndFeel.installProperty(menuItem, "borderPainted", oldBorderPainted); 327 if (menuItem.getMargin() instanceof UIResource) 328 menuItem.setMargin(null); 329 if (arrowIcon instanceof UIResource) 330 arrowIcon = null; 331 if (checkIcon instanceof UIResource) 332 checkIcon = null; 333 } 334 335 /** 336 * Unregisters components. 337 * 338 * @param menuItem a menu item 339 * @since 1.3 340 */ 341 protected void uninstallComponents(JMenuItem menuItem){ 342 BasicHTML.updateRenderer(menuItem, ""); 343 } 344 345 /** 346 * Unregisters listeners. 347 */ 348 protected void uninstallListeners() { 349 if (mouseInputListener != null) { 350 menuItem.removeMouseListener(mouseInputListener); 351 menuItem.removeMouseMotionListener(mouseInputListener); 352 } 353 if (menuDragMouseListener != null) { 354 menuItem.removeMenuDragMouseListener(menuDragMouseListener); 355 } 356 if (menuKeyListener != null) { 357 menuItem.removeMenuKeyListener(menuKeyListener); 358 } 359 if (propertyChangeListener != null) { 360 menuItem.removePropertyChangeListener(propertyChangeListener); 361 } 362 363 mouseInputListener = null; 364 menuDragMouseListener = null; 365 menuKeyListener = null; 366 propertyChangeListener = null; 367 handler = null; 368 } 369 370 /** 371 * Unregisters keyboard actions. 372 */ 373 protected void uninstallKeyboardActions() { 374 SwingUtilities.replaceUIActionMap(menuItem, null); 375 SwingUtilities.replaceUIInputMap(menuItem, JComponent. 376 WHEN_IN_FOCUSED_WINDOW, null); 377 } 378 379 /** 380 * Returns an instance of {@code MouseInputListener}. 381 * 382 * @param c a component 383 * @return an instance of {@code MouseInputListener} 384 */ 385 protected MouseInputListener createMouseInputListener(JComponent c) { 386 return getHandler(); 387 } 388 389 /** 390 * Returns an instance of {@code MenuDragMouseListener}. 391 * 392 * @param c a component 393 * @return an instance of {@code MenuDragMouseListener} 394 */ 395 protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) { 396 return getHandler(); 397 } 398 399 /** 400 * Returns an instance of {@code MenuKeyListener}. 401 * 402 * @param c a component 403 * @return an instance of {@code MenuKeyListener} 404 */ 405 protected MenuKeyListener createMenuKeyListener(JComponent c) { 406 return null; 407 } 408 409 /** 410 * Creates a {@code PropertyChangeListener} which will be added to 411 * the menu item. 412 * If this method returns null then it will not be added to the menu item. 413 * 414 * @param c a component 415 * @return an instance of a {@code PropertyChangeListener} or null 416 * @since 1.6 417 */ 418 protected PropertyChangeListener 419 createPropertyChangeListener(JComponent c) { 420 return getHandler(); 421 } 422 423 Handler getHandler() { 424 if (handler == null) { 425 handler = new Handler(); 426 } 427 return handler; 428 } 429 430 InputMap createInputMap(int condition) { 431 if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) { 432 return new ComponentInputMapUIResource(menuItem); 433 } 434 return null; 435 } 436 437 void updateAcceleratorBinding() { 438 KeyStroke accelerator = menuItem.getAccelerator(); 439 InputMap windowInputMap = SwingUtilities.getUIInputMap( 440 menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW); 441 442 if (windowInputMap != null) { 443 windowInputMap.clear(); 444 } 445 if (accelerator != null) { 446 if (windowInputMap == null) { 447 windowInputMap = createInputMap(JComponent. 448 WHEN_IN_FOCUSED_WINDOW); 449 SwingUtilities.replaceUIInputMap(menuItem, 450 JComponent.WHEN_IN_FOCUSED_WINDOW, windowInputMap); 451 } 452 windowInputMap.put(accelerator, "doClick"); 453 } 454 } 455 456 public Dimension getMinimumSize(JComponent c) { 457 Dimension d = null; 458 View v = (View) c.getClientProperty(BasicHTML.propertyKey); 459 if (v != null) { 460 d = getPreferredSize(c); 461 d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS); 462 } else if (c.getParent() instanceof JMenuBar) { 463 //if the menu is a toplevel item, then return preferred size. 464 d = getPreferredSize(c); 465 } 466 467 return d; 468 } 469 470 public Dimension getPreferredSize(JComponent c) { 471 return getPreferredMenuItemSize(c, 472 checkIcon, 473 arrowIcon, 474 defaultTextIconGap); 475 } 476 477 public Dimension getMaximumSize(JComponent c) { 478 Dimension d = null; 479 View v = (View) c.getClientProperty(BasicHTML.propertyKey); 480 if (v != null) { 481 d = getPreferredSize(c); 482 d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS); 483 } 484 return d; 485 } 486 487 /** 488 * Returns the preferred size of a menu item. 489 * 490 * @param c a component 491 * @param checkIcon a check icon 492 * @param arrowIcon an arrow icon 493 * @param defaultTextIconGap a gap between a text and an icon 494 * @return the preferred size of a menu item 495 */ 496 protected Dimension getPreferredMenuItemSize(JComponent c, 497 Icon checkIcon, 498 Icon arrowIcon, 499 int defaultTextIconGap) { 500 501 // The method also determines the preferred width of the 502 // parent popup menu (through DefaultMenuLayout class). 503 // The menu width equals to the maximal width 504 // among child menu items. 505 506 // Menu item width will be a sum of the widest check icon, label, 507 // arrow icon and accelerator text among neighbor menu items. 508 // For the latest menu item we will know the maximal widths exactly. 509 // It will be the widest menu item and it will determine 510 // the width of the parent popup menu. 511 512 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 513 // There is a conceptual problem: if user sets preferred size manually 514 // for a menu item, this method won't be called for it 515 // (see JComponent.getPreferredSize()), 516 // maximal widths won't be calculated, other menu items won't be able 517 // to take them into account and will be layouted in such a way, 518 // as there is no the item with manual preferred size. 519 // But after the first paint() method call, all maximal widths 520 // will be correctly calculated and layout of some menu items 521 // can be changed. For example, it can cause a shift of 522 // the icon and text when user points a menu item by mouse. 523 524 JMenuItem mi = (JMenuItem) c; 525 MenuItemLayoutHelper lh = new MenuItemLayoutHelper(mi, checkIcon, 526 arrowIcon, MenuItemLayoutHelper.createMaxRect(), defaultTextIconGap, 527 acceleratorDelimiter, BasicGraphicsUtils.isLeftToRight(mi), 528 mi.getFont(), acceleratorFont, 529 MenuItemLayoutHelper.useCheckAndArrow(menuItem), 530 getPropertyPrefix()); 531 532 Dimension result = new Dimension(); 533 534 // Calculate the result width 535 result.width = lh.getLeadingGap(); 536 MenuItemLayoutHelper.addMaxWidth(lh.getCheckSize(), 537 lh.getAfterCheckIconGap(), result); 538 // Take into account mimimal text offset. 539 if ((!lh.isTopLevelMenu()) 540 && (lh.getMinTextOffset() > 0) 541 && (result.width < lh.getMinTextOffset())) { 542 result.width = lh.getMinTextOffset(); 543 } 544 MenuItemLayoutHelper.addMaxWidth(lh.getLabelSize(), lh.getGap(), result); 545 MenuItemLayoutHelper.addMaxWidth(lh.getAccSize(), lh.getGap(), result); 546 MenuItemLayoutHelper.addMaxWidth(lh.getArrowSize(), lh.getGap(), result); 547 548 // Calculate the result height 549 result.height = MenuItemLayoutHelper.max(lh.getCheckSize().getHeight(), 550 lh.getLabelSize().getHeight(), lh.getAccSize().getHeight(), 551 lh.getArrowSize().getHeight()); 552 553 // Take into account menu item insets 554 Insets insets = lh.getMenuItem().getInsets(); 555 if(insets != null) { 556 result.width += insets.left + insets.right; 557 result.height += insets.top + insets.bottom; 558 } 559 560 // if the width is even, bump it up one. This is critical 561 // for the focus dash line to draw properly 562 if(result.width%2 == 0) { 563 result.width++; 564 } 565 566 // if the height is even, bump it up one. This is critical 567 // for the text to center properly 568 if(result.height%2 == 0 569 && Boolean.TRUE != 570 UIManager.get(getPropertyPrefix() + ".evenHeight")) { 571 result.height++; 572 } 573 574 return result; 575 } 576 577 /** 578 * We draw the background in paintMenuItem() 579 * so override update (which fills the background of opaque 580 * components by default) to just call paint(). 581 * 582 */ 583 public void update(Graphics g, JComponent c) { 584 paint(g, c); 585 } 586 587 public void paint(Graphics g, JComponent c) { 588 paintMenuItem(g, c, checkIcon, arrowIcon, 589 selectionBackground, selectionForeground, 590 defaultTextIconGap); 591 } 592 593 /** 594 * Paints a menu item. 595 * 596 * @param g an instance of {@code Graphics} 597 * @param c a component 598 * @param checkIcon a check icon 599 * @param arrowIcon an arrow icon 600 * @param background a background color 601 * @param foreground a foreground color 602 * @param defaultTextIconGap a gap between a text and an icon 603 */ 604 protected void paintMenuItem(Graphics g, JComponent c, 605 Icon checkIcon, Icon arrowIcon, 606 Color background, Color foreground, 607 int defaultTextIconGap) { 608 // Save original graphics font and color 609 Font holdf = g.getFont(); 610 Color holdc = g.getColor(); 611 612 JMenuItem mi = (JMenuItem) c; 613 g.setFont(mi.getFont()); 614 615 Rectangle viewRect = new Rectangle(0, 0, mi.getWidth(), mi.getHeight()); 616 applyInsets(viewRect, mi.getInsets()); 617 618 MenuItemLayoutHelper lh = new MenuItemLayoutHelper(mi, checkIcon, 619 arrowIcon, viewRect, defaultTextIconGap, acceleratorDelimiter, 620 BasicGraphicsUtils.isLeftToRight(mi), mi.getFont(), 621 acceleratorFont, MenuItemLayoutHelper.useCheckAndArrow(menuItem), 622 getPropertyPrefix()); 623 MenuItemLayoutHelper.LayoutResult lr = lh.layoutMenuItem(); 624 625 paintBackground(g, mi, background); 626 paintCheckIcon(g, lh, lr, holdc, foreground); 627 paintIcon(g, lh, lr, holdc); 628 paintText(g, lh, lr); 629 paintAccText(g, lh, lr); 630 paintArrowIcon(g, lh, lr, foreground); 631 632 // Restore original graphics font and color 633 g.setColor(holdc); 634 g.setFont(holdf); 635 } 636 637 private void paintIcon(Graphics g, MenuItemLayoutHelper lh, 638 MenuItemLayoutHelper.LayoutResult lr, Color holdc) { 639 if (lh.getIcon() != null) { 640 Icon icon; 641 ButtonModel model = lh.getMenuItem().getModel(); 642 if (!model.isEnabled()) { 643 icon = lh.getMenuItem().getDisabledIcon(); 644 } else if (model.isPressed() && model.isArmed()) { 645 icon = lh.getMenuItem().getPressedIcon(); 646 if (icon == null) { 647 // Use default icon 648 icon = lh.getMenuItem().getIcon(); 649 } 650 } else { 651 icon = lh.getMenuItem().getIcon(); 652 } 653 654 if (icon != null) { 655 icon.paintIcon(lh.getMenuItem(), g, lr.getIconRect().x, 656 lr.getIconRect().y); 657 g.setColor(holdc); 658 } 659 } 660 } 661 662 private void paintCheckIcon(Graphics g, MenuItemLayoutHelper lh, 663 MenuItemLayoutHelper.LayoutResult lr, 664 Color holdc, Color foreground) { 665 if (lh.getCheckIcon() != null) { 666 ButtonModel model = lh.getMenuItem().getModel(); 667 if (model.isArmed() || (lh.getMenuItem() instanceof JMenu 668 && model.isSelected())) { 669 g.setColor(foreground); 670 } else { 671 g.setColor(holdc); 672 } 673 if (lh.useCheckAndArrow()) { 674 lh.getCheckIcon().paintIcon(lh.getMenuItem(), g, 675 lr.getCheckRect().x, lr.getCheckRect().y); 676 } 677 g.setColor(holdc); 678 } 679 } 680 681 private void paintAccText(Graphics g, MenuItemLayoutHelper lh, 682 MenuItemLayoutHelper.LayoutResult lr) { 683 if (!lh.getAccText().equals("")) { 684 ButtonModel model = lh.getMenuItem().getModel(); 685 g.setFont(lh.getAccFontMetrics().getFont()); 686 if (!model.isEnabled()) { 687 // *** paint the accText disabled 688 if (disabledForeground != null) { 689 g.setColor(disabledForeground); 690 SwingUtilities2.drawString(lh.getMenuItem(), g, 691 lh.getAccText(), lr.getAccRect().x, 692 lr.getAccRect().y + lh.getAccFontMetrics().getAscent()); 693 } else { 694 g.setColor(lh.getMenuItem().getBackground().brighter()); 695 SwingUtilities2.drawString(lh.getMenuItem(), g, 696 lh.getAccText(), lr.getAccRect().x, 697 lr.getAccRect().y + lh.getAccFontMetrics().getAscent()); 698 g.setColor(lh.getMenuItem().getBackground().darker()); 699 SwingUtilities2.drawString(lh.getMenuItem(), g, 700 lh.getAccText(), lr.getAccRect().x - 1, 701 lr.getAccRect().y + lh.getFontMetrics().getAscent() - 1); 702 } 703 } else { 704 // *** paint the accText normally 705 if (model.isArmed() 706 || (lh.getMenuItem() instanceof JMenu 707 && model.isSelected())) { 708 g.setColor(acceleratorSelectionForeground); 709 } else { 710 g.setColor(acceleratorForeground); 711 } 712 SwingUtilities2.drawString(lh.getMenuItem(), g, lh.getAccText(), 713 lr.getAccRect().x, lr.getAccRect().y + 714 lh.getAccFontMetrics().getAscent()); 715 } 716 } 717 } 718 719 private void paintText(Graphics g, MenuItemLayoutHelper lh, 720 MenuItemLayoutHelper.LayoutResult lr) { 721 if (!lh.getText().equals("")) { 722 if (lh.getHtmlView() != null) { 723 // Text is HTML 724 lh.getHtmlView().paint(g, lr.getTextRect()); 725 } else { 726 // Text isn't HTML 727 paintText(g, lh.getMenuItem(), lr.getTextRect(), lh.getText()); 728 } 729 } 730 } 731 732 private void paintArrowIcon(Graphics g, MenuItemLayoutHelper lh, 733 MenuItemLayoutHelper.LayoutResult lr, 734 Color foreground) { 735 if (lh.getArrowIcon() != null) { 736 ButtonModel model = lh.getMenuItem().getModel(); 737 if (model.isArmed() || (lh.getMenuItem() instanceof JMenu 738 && model.isSelected())) { 739 g.setColor(foreground); 740 } 741 if (lh.useCheckAndArrow()) { 742 lh.getArrowIcon().paintIcon(lh.getMenuItem(), g, 743 lr.getArrowRect().x, lr.getArrowRect().y); 744 } 745 } 746 } 747 748 private void applyInsets(Rectangle rect, Insets insets) { 749 if(insets != null) { 750 rect.x += insets.left; 751 rect.y += insets.top; 752 rect.width -= (insets.right + rect.x); 753 rect.height -= (insets.bottom + rect.y); 754 } 755 } 756 757 /** 758 * Draws the background of the menu item. 759 * 760 * @param g the paint graphics 761 * @param menuItem menu item to be painted 762 * @param bgColor selection background color 763 * @since 1.4 764 */ 765 protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) { 766 ButtonModel model = menuItem.getModel(); 767 Color oldColor = g.getColor(); 768 int menuWidth = menuItem.getWidth(); 769 int menuHeight = menuItem.getHeight(); 770 771 if(menuItem.isOpaque()) { 772 if (model.isArmed()|| (menuItem instanceof JMenu && model.isSelected())) { 773 g.setColor(bgColor); 774 g.fillRect(0,0, menuWidth, menuHeight); 775 } else { 776 g.setColor(menuItem.getBackground()); 777 g.fillRect(0,0, menuWidth, menuHeight); 778 } 779 g.setColor(oldColor); 780 } 781 else if (model.isArmed() || (menuItem instanceof JMenu && 782 model.isSelected())) { 783 g.setColor(bgColor); 784 g.fillRect(0,0, menuWidth, menuHeight); 785 g.setColor(oldColor); 786 } 787 } 788 789 /** 790 * Renders the text of the current menu item. 791 * 792 * @param g graphics context 793 * @param menuItem menu item to render 794 * @param textRect bounding rectangle for rendering the text 795 * @param text string to render 796 * @since 1.4 797 */ 798 protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect, String text) { 799 ButtonModel model = menuItem.getModel(); 800 FontMetrics fm = SwingUtilities2.getFontMetrics(menuItem, g); 801 int mnemIndex = menuItem.getDisplayedMnemonicIndex(); 802 803 if(!model.isEnabled()) { 804 // *** paint the text disabled 805 if ( UIManager.get("MenuItem.disabledForeground") instanceof Color ) { 806 g.setColor( UIManager.getColor("MenuItem.disabledForeground") ); 807 SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text, 808 mnemIndex, textRect.x, textRect.y + fm.getAscent()); 809 } else { 810 g.setColor(menuItem.getBackground().brighter()); 811 SwingUtilities2.drawStringUnderlineCharAt(menuItem, g, text, 812 mnemIndex, textRect.x, textRect.y + fm.getAscent()); 813 g.setColor(menuItem.getBackground().darker()); 814 SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text, 815 mnemIndex, textRect.x - 1, textRect.y + 816 fm.getAscent() - 1); 817 } 818 } else { 819 // *** paint the text normally 820 if (model.isArmed()|| (menuItem instanceof JMenu && model.isSelected())) { 821 g.setColor(selectionForeground); // Uses protected field. 822 } 823 SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text, 824 mnemIndex, textRect.x, textRect.y + fm.getAscent()); 825 } 826 } 827 828 /** 829 * Returns a menu element path. 830 * 831 * @return a menu element path 832 */ 833 public MenuElement[] getPath() { 834 MenuSelectionManager m = MenuSelectionManager.defaultManager(); 835 MenuElement oldPath[] = m.getSelectedPath(); 836 MenuElement newPath[]; 837 int i = oldPath.length; 838 if (i == 0) 839 return new MenuElement[0]; 840 Component parent = menuItem.getParent(); 841 if (oldPath[i-1].getComponent() == parent) { 842 // The parent popup menu is the last so far 843 newPath = new MenuElement[i+1]; 844 System.arraycopy(oldPath, 0, newPath, 0, i); 845 newPath[i] = menuItem; 846 } else { 847 // A sibling menuitem is the current selection 848 // 849 // This probably needs to handle 'exit submenu into 850 // a menu item. Search backwards along the current 851 // selection until you find the parent popup menu, 852 // then copy up to that and add yourself... 853 int j; 854 for (j = oldPath.length-1; j >= 0; j--) { 855 if (oldPath[j].getComponent() == parent) 856 break; 857 } 858 newPath = new MenuElement[j+2]; 859 System.arraycopy(oldPath, 0, newPath, 0, j+1); 860 newPath[j+1] = menuItem; 861 /* 862 System.out.println("Sibling condition -- "); 863 System.out.println("Old array : "); 864 printMenuElementArray(oldPath, false); 865 System.out.println("New array : "); 866 printMenuElementArray(newPath, false); 867 */ 868 } 869 return newPath; 870 } 871 872 void printMenuElementArray(MenuElement path[], boolean dumpStack) { 873 System.out.println("Path is("); 874 int i, j; 875 for(i=0,j=path.length; i<j ;i++){ 876 for (int k=0; k<=i; k++) 877 System.out.print(" "); 878 MenuElement me = path[i]; 879 if(me instanceof JMenuItem) 880 System.out.println(((JMenuItem)me).getText() + ", "); 881 else if (me == null) 882 System.out.println("NULL , "); 883 else 884 System.out.println("" + me + ", "); 885 } 886 System.out.println(")"); 887 888 if (dumpStack == true) 889 Thread.dumpStack(); 890 } 891 /** Mouse input handler */ 892 protected class MouseInputHandler implements MouseInputListener { 893 // NOTE: This class exists only for backward compatibility. All 894 // its functionality has been moved into Handler. If you need to add 895 // new functionality add it to the Handler, but make sure this 896 // class calls into the Handler. 897 898 /** {@inheritDoc} */ 899 public void mouseClicked(MouseEvent e) { 900 getHandler().mouseClicked(e); 901 } 902 /** {@inheritDoc} */ 903 public void mousePressed(MouseEvent e) { 904 getHandler().mousePressed(e); 905 } 906 /** {@inheritDoc} */ 907 public void mouseReleased(MouseEvent e) { 908 getHandler().mouseReleased(e); 909 } 910 /** {@inheritDoc} */ 911 public void mouseEntered(MouseEvent e) { 912 getHandler().mouseEntered(e); 913 } 914 /** {@inheritDoc} */ 915 public void mouseExited(MouseEvent e) { 916 getHandler().mouseExited(e); 917 } 918 /** {@inheritDoc} */ 919 public void mouseDragged(MouseEvent e) { 920 getHandler().mouseDragged(e); 921 } 922 /** {@inheritDoc} */ 923 public void mouseMoved(MouseEvent e) { 924 getHandler().mouseMoved(e); 925 } 926 } 927 928 929 private static class Actions extends UIAction { 930 private static final String CLICK = "doClick"; 931 932 Actions(String key) { 933 super(key); 934 } 935 936 public void actionPerformed(ActionEvent e) { 937 JMenuItem mi = (JMenuItem)e.getSource(); 938 MenuSelectionManager.defaultManager().clearSelectedPath(); 939 mi.doClick(); 940 } 941 } 942 943 boolean doNotCloseOnMouseClick() { 944 if (menuItem instanceof JCheckBoxMenuItem) { 945 String property = "CheckBoxMenuItem.doNotCloseOnMouseClick"; 946 return SwingUtilities2.getBoolean(menuItem, property); 947 } else if (menuItem instanceof JRadioButtonMenuItem) { 948 String property = "RadioButtonMenuItem.doNotCloseOnMouseClick"; 949 return SwingUtilities2.getBoolean(menuItem, property); 950 } 951 return false; 952 } 953 954 /** 955 * Call this method when a menu item is to be activated. 956 * This method handles some of the details of menu item activation 957 * such as clearing the selected path and messaging the 958 * JMenuItem's doClick() method. 959 * 960 * @param msm A MenuSelectionManager. The visual feedback and 961 * internal bookkeeping tasks are delegated to 962 * this MenuSelectionManager. If <code>null</code> is 963 * passed as this argument, the 964 * <code>MenuSelectionManager.defaultManager</code> is 965 * used. 966 * @see MenuSelectionManager 967 * @see JMenuItem#doClick(int) 968 * @since 1.4 969 */ 970 protected void doClick(MenuSelectionManager msm) { 971 // Auditory cue 972 if (! isInternalFrameSystemMenu()) { 973 BasicLookAndFeel.playSound(menuItem, getPropertyPrefix() + 974 ".commandSound"); 975 } 976 if (!doNotCloseOnMouseClick()) { 977 // Visual feedback 978 if (msm == null) { 979 msm = MenuSelectionManager.defaultManager(); 980 } 981 982 msm.clearSelectedPath(); 983 } 984 menuItem.doClick(0); 985 } 986 987 /** 988 * This is to see if the menu item in question is part of the 989 * system menu on an internal frame. 990 * The Strings that are being checked can be found in 991 * MetalInternalFrameTitlePaneUI.java, 992 * WindowsInternalFrameTitlePaneUI.java, and 993 * MotifInternalFrameTitlePaneUI.java. 994 * 995 * @since 1.4 996 */ 997 private boolean isInternalFrameSystemMenu() { 998 String actionCommand = menuItem.getActionCommand(); 999 if ((actionCommand == "Close") || 1000 (actionCommand == "Minimize") || 1001 (actionCommand == "Restore") || 1002 (actionCommand == "Maximize")) { 1003 return true; 1004 } else { 1005 return false; 1006 } 1007 } 1008 1009 1010 // BasicMenuUI subclasses this. 1011 class Handler implements MenuDragMouseListener, 1012 MouseInputListener, PropertyChangeListener { 1013 // 1014 // MouseInputListener 1015 // 1016 public void mouseClicked(MouseEvent e) {} 1017 public void mousePressed(MouseEvent e) { 1018 } 1019 public void mouseReleased(MouseEvent e) { 1020 if (!menuItem.isEnabled()) { 1021 return; 1022 } 1023 MenuSelectionManager manager = 1024 MenuSelectionManager.defaultManager(); 1025 Point p = e.getPoint(); 1026 if(p.x >= 0 && p.x < menuItem.getWidth() && 1027 p.y >= 0 && p.y < menuItem.getHeight()) { 1028 doClick(manager); 1029 } else { 1030 manager.processMouseEvent(e); 1031 } 1032 } 1033 @SuppressWarnings("deprecation") 1034 public void mouseEntered(MouseEvent e) { 1035 MenuSelectionManager manager = MenuSelectionManager.defaultManager(); 1036 int modifiers = e.getModifiers(); 1037 // 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2 1038 if ((modifiers & (InputEvent.BUTTON1_MASK | 1039 InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) !=0 ) { 1040 MenuSelectionManager.defaultManager().processMouseEvent(e); 1041 } else { 1042 manager.setSelectedPath(getPath()); 1043 } 1044 } 1045 @SuppressWarnings("deprecation") 1046 public void mouseExited(MouseEvent e) { 1047 MenuSelectionManager manager = MenuSelectionManager.defaultManager(); 1048 1049 int modifiers = e.getModifiers(); 1050 // 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2 1051 if ((modifiers & (InputEvent.BUTTON1_MASK | 1052 InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) !=0 ) { 1053 MenuSelectionManager.defaultManager().processMouseEvent(e); 1054 } else { 1055 1056 MenuElement path[] = manager.getSelectedPath(); 1057 if (path.length > 1 && path[path.length-1] == menuItem) { 1058 MenuElement newPath[] = new MenuElement[path.length-1]; 1059 int i,c; 1060 for(i=0,c=path.length-1;i<c;i++) 1061 newPath[i] = path[i]; 1062 manager.setSelectedPath(newPath); 1063 } 1064 } 1065 } 1066 1067 public void mouseDragged(MouseEvent e) { 1068 MenuSelectionManager.defaultManager().processMouseEvent(e); 1069 } 1070 public void mouseMoved(MouseEvent e) { 1071 } 1072 1073 // 1074 // MenuDragListener 1075 // 1076 public void menuDragMouseEntered(MenuDragMouseEvent e) { 1077 MenuSelectionManager manager = e.getMenuSelectionManager(); 1078 MenuElement path[] = e.getPath(); 1079 manager.setSelectedPath(path); 1080 } 1081 public void menuDragMouseDragged(MenuDragMouseEvent e) { 1082 MenuSelectionManager manager = e.getMenuSelectionManager(); 1083 MenuElement path[] = e.getPath(); 1084 manager.setSelectedPath(path); 1085 } 1086 public void menuDragMouseExited(MenuDragMouseEvent e) {} 1087 public void menuDragMouseReleased(MenuDragMouseEvent e) { 1088 if (!menuItem.isEnabled()) { 1089 return; 1090 } 1091 MenuSelectionManager manager = e.getMenuSelectionManager(); 1092 MenuElement path[] = e.getPath(); 1093 Point p = e.getPoint(); 1094 if (p.x >= 0 && p.x < menuItem.getWidth() && 1095 p.y >= 0 && p.y < menuItem.getHeight()) { 1096 doClick(manager); 1097 } else { 1098 manager.clearSelectedPath(); 1099 } 1100 } 1101 1102 1103 // 1104 // PropertyChangeListener 1105 // 1106 public void propertyChange(PropertyChangeEvent e) { 1107 String name = e.getPropertyName(); 1108 1109 if (name == "labelFor" || name == "displayedMnemonic" || 1110 name == "accelerator") { 1111 updateAcceleratorBinding(); 1112 } else if (name == "text" || "font" == name || 1113 "foreground" == name) { 1114 // remove the old html view client property if one 1115 // existed, and install a new one if the text installed 1116 // into the JLabel is html source. 1117 JMenuItem lbl = ((JMenuItem) e.getSource()); 1118 String text = lbl.getText(); 1119 BasicHTML.updateRenderer(lbl, text); 1120 } else if (name == "iconTextGap") { 1121 defaultTextIconGap = ((Number)e.getNewValue()).intValue(); 1122 } else if (name == "horizontalTextPosition") { 1123 updateCheckIcon(); 1124 } 1125 } 1126 } 1127 } --- EOF ---