1. /*
  2. * @(#)BasicMenuItemUI.java 1.101 00/04/05
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to licence terms.
  8. */
  9. package javax.swing.plaf.basic;
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import java.beans.PropertyChangeEvent;
  13. import java.beans.PropertyChangeListener;
  14. import javax.swing.*;
  15. import javax.swing.event.*;
  16. import javax.swing.border.*;
  17. import javax.swing.plaf.*;
  18. import javax.swing.text.View;
  19. /**
  20. * BasicMenuItem implementation
  21. *
  22. * @version 1.101 04/05/00
  23. * @author Georges Saab
  24. * @author David Karlton
  25. * @author Arnaud Weber
  26. * @author Fredrik Lagerblad
  27. */
  28. public class BasicMenuItemUI extends MenuItemUI
  29. {
  30. protected JMenuItem menuItem = null;
  31. protected Color selectionBackground;
  32. protected Color selectionForeground;
  33. protected Color disabledForeground;
  34. protected Color acceleratorForeground;
  35. protected Color acceleratorSelectionForeground;
  36. private String acceleratorDelimiter;
  37. protected int defaultTextIconGap;
  38. protected Font acceleratorFont;
  39. protected MouseInputListener mouseInputListener;
  40. protected MenuDragMouseListener menuDragMouseListener;
  41. protected MenuKeyListener menuKeyListener;
  42. private PropertyChangeListener propertyChangeListener;
  43. protected Icon arrowIcon = null;
  44. protected Icon checkIcon = null;
  45. protected boolean oldBorderPainted;
  46. /** Used for accelerator binding, lazily created. */
  47. InputMap windowInputMap;
  48. /* diagnostic aids -- should be false for production builds. */
  49. private static final boolean TRACE = false; // trace creates and disposes
  50. private static final boolean VERBOSE = false; // show reuse hits/misses
  51. private static final boolean DEBUG = false; // show bad params, misc.
  52. /* Client Property keys for text and accelerator text widths */
  53. private static final String MAX_TEXT_WIDTH = "maxTextWidth";
  54. private static final String MAX_ACC_WIDTH = "maxAccWidth";
  55. public static ComponentUI createUI(JComponent c) {
  56. return new BasicMenuItemUI();
  57. }
  58. public void installUI(JComponent c) {
  59. menuItem = (JMenuItem) c;
  60. installDefaults();
  61. installComponents(menuItem);
  62. installListeners();
  63. installKeyboardActions();
  64. }
  65. protected void installDefaults() {
  66. String prefix = getPropertyPrefix();
  67. acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
  68. menuItem.setOpaque(true);
  69. if(menuItem.getMargin() == null ||
  70. (menuItem.getMargin() instanceof UIResource)) {
  71. menuItem.setMargin(UIManager.getInsets(prefix + ".margin"));
  72. }
  73. defaultTextIconGap = 4; // Should be from table
  74. LookAndFeel.installBorder(menuItem, prefix + ".border");
  75. oldBorderPainted = menuItem.isBorderPainted();
  76. menuItem.setBorderPainted( ( (Boolean) (UIManager.get(prefix + ".borderPainted")) ).booleanValue() );
  77. LookAndFeel.installColorsAndFont(menuItem,
  78. prefix + ".background",
  79. prefix + ".foreground",
  80. prefix + ".font");
  81. // MenuItem specific defaults
  82. if (selectionBackground == null ||
  83. selectionBackground instanceof UIResource) {
  84. selectionBackground =
  85. UIManager.getColor(prefix + ".selectionBackground");
  86. }
  87. if (selectionForeground == null ||
  88. selectionForeground instanceof UIResource) {
  89. selectionForeground =
  90. UIManager.getColor(prefix + ".selectionForeground");
  91. }
  92. if (disabledForeground == null ||
  93. disabledForeground instanceof UIResource) {
  94. disabledForeground =
  95. UIManager.getColor(prefix + ".disabledForeground");
  96. }
  97. if (acceleratorForeground == null ||
  98. acceleratorForeground instanceof UIResource) {
  99. acceleratorForeground =
  100. UIManager.getColor(prefix + ".acceleratorForeground");
  101. }
  102. if (acceleratorSelectionForeground == null ||
  103. acceleratorSelectionForeground instanceof UIResource) {
  104. acceleratorSelectionForeground =
  105. UIManager.getColor(prefix + ".acceleratorSelectionForeground");
  106. }
  107. // Get accelerator delimiter
  108. acceleratorDelimiter =
  109. UIManager.getString("MenuItem.acceleratorDelimiter");
  110. if (acceleratorDelimiter == null) { acceleratorDelimiter = "+"; }
  111. // Icons
  112. if (arrowIcon == null ||
  113. arrowIcon instanceof UIResource) {
  114. arrowIcon = UIManager.getIcon(prefix + ".arrowIcon");
  115. }
  116. if (checkIcon == null ||
  117. checkIcon instanceof UIResource) {
  118. checkIcon = UIManager.getIcon(prefix + ".checkIcon");
  119. }
  120. }
  121. /**
  122. * @since 1.3
  123. */
  124. protected void installComponents(JMenuItem menuItem){
  125. BasicHTML.updateRenderer(menuItem, menuItem.getText());
  126. }
  127. protected String getPropertyPrefix() {
  128. return "MenuItem";
  129. }
  130. protected void installListeners() {
  131. mouseInputListener = createMouseInputListener(menuItem);
  132. menuDragMouseListener = createMenuDragMouseListener(menuItem);
  133. menuKeyListener = createMenuKeyListener(menuItem);
  134. propertyChangeListener = createPropertyChangeListener(menuItem);
  135. menuItem.addMouseListener(mouseInputListener);
  136. menuItem.addMouseMotionListener(mouseInputListener);
  137. menuItem.addMenuDragMouseListener(menuDragMouseListener);
  138. menuItem.addMenuKeyListener(menuKeyListener);
  139. menuItem.addPropertyChangeListener(propertyChangeListener);
  140. }
  141. protected void installKeyboardActions() {
  142. ActionMap actionMap = getActionMap();
  143. SwingUtilities.replaceUIActionMap(menuItem, actionMap);
  144. updateAcceleratorBinding();
  145. }
  146. public void uninstallUI(JComponent c) {
  147. menuItem = (JMenuItem)c;
  148. uninstallDefaults();
  149. uninstallComponents(menuItem);
  150. uninstallListeners();
  151. uninstallKeyboardActions();
  152. //Remove the textWidth and accWidth values from the parent's Client Properties.
  153. Container parent = menuItem.getParent();
  154. if ( (parent != null && parent instanceof JComponent) &&
  155. !(menuItem instanceof JMenu && ((JMenu) menuItem).isTopLevelMenu())) {
  156. JComponent p = (JComponent) parent;
  157. p.putClientProperty(BasicMenuItemUI.MAX_ACC_WIDTH, null );
  158. p.putClientProperty(BasicMenuItemUI.MAX_TEXT_WIDTH, null );
  159. }
  160. menuItem = null;
  161. }
  162. protected void uninstallDefaults() {
  163. LookAndFeel.uninstallBorder(menuItem);
  164. menuItem.setBorderPainted( oldBorderPainted );
  165. if (menuItem.getMargin() instanceof UIResource)
  166. menuItem.setMargin(null);
  167. if (arrowIcon instanceof UIResource)
  168. arrowIcon = null;
  169. if (checkIcon instanceof UIResource)
  170. checkIcon = null;
  171. }
  172. /**
  173. * @since 1.3
  174. */
  175. protected void uninstallComponents(JMenuItem menuItem){
  176. BasicHTML.updateRenderer(menuItem, "");
  177. }
  178. protected void uninstallListeners() {
  179. menuItem.removeMouseListener(mouseInputListener);
  180. menuItem.removeMouseMotionListener(mouseInputListener);
  181. menuItem.removeMenuDragMouseListener(menuDragMouseListener);
  182. menuItem.removeMenuKeyListener(menuKeyListener);
  183. menuItem.removePropertyChangeListener(propertyChangeListener);
  184. mouseInputListener = null;
  185. menuDragMouseListener = null;
  186. menuKeyListener = null;
  187. propertyChangeListener = null;
  188. }
  189. protected void uninstallKeyboardActions() {
  190. SwingUtilities.replaceUIActionMap(menuItem, null);
  191. if (windowInputMap != null) {
  192. SwingUtilities.replaceUIInputMap(menuItem, JComponent.
  193. WHEN_IN_FOCUSED_WINDOW, null);
  194. windowInputMap = null;
  195. }
  196. }
  197. protected MouseInputListener createMouseInputListener(JComponent c) {
  198. return new MouseInputHandler();
  199. }
  200. protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) {
  201. return new MenuDragMouseHandler();
  202. }
  203. protected MenuKeyListener createMenuKeyListener(JComponent c) {
  204. return new MenuKeyHandler();
  205. }
  206. private PropertyChangeListener createPropertyChangeListener(JComponent c) {
  207. return new PropertyChangeHandler();
  208. }
  209. ActionMap getActionMap() {
  210. String propertyPrefix = getPropertyPrefix();
  211. String uiKey = propertyPrefix + ".actionMap";
  212. ActionMap am = (ActionMap)UIManager.get(uiKey);
  213. if (am == null) {
  214. am = createActionMap();
  215. UIManager.put(uiKey, am);
  216. }
  217. return am;
  218. }
  219. ActionMap createActionMap() {
  220. ActionMap map = new ActionMapUIResource();
  221. map.put("doClick", new ClickAction());
  222. return map;
  223. }
  224. InputMap createInputMap(int condition) {
  225. if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
  226. return new ComponentInputMapUIResource(menuItem);
  227. }
  228. return null;
  229. }
  230. void updateAcceleratorBinding() {
  231. KeyStroke accelerator = menuItem.getAccelerator();
  232. if (accelerator != null) {
  233. if (windowInputMap == null) {
  234. windowInputMap = createInputMap(JComponent.
  235. WHEN_IN_FOCUSED_WINDOW);
  236. SwingUtilities.replaceUIInputMap(menuItem,
  237. JComponent.WHEN_IN_FOCUSED_WINDOW, windowInputMap);
  238. }
  239. windowInputMap.put(accelerator, "doClick");
  240. }
  241. else if (windowInputMap != null) {
  242. windowInputMap.clear();
  243. }
  244. }
  245. public Dimension getMinimumSize(JComponent c) {
  246. Dimension d = null;
  247. View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  248. if (v != null) {
  249. d = getPreferredSize(c);
  250. d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
  251. }
  252. return d;
  253. }
  254. public Dimension getPreferredSize(JComponent c) {
  255. return getPreferredMenuItemSize(c,
  256. checkIcon,
  257. arrowIcon,
  258. defaultTextIconGap);
  259. }
  260. public Dimension getMaximumSize(JComponent c) {
  261. Dimension d = null;
  262. View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  263. if (v != null) {
  264. d = getPreferredSize(c);
  265. d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
  266. }
  267. return d;
  268. }
  269. // these rects are used for painting and preferredsize calculations.
  270. // they used to be regenerated constantly. Now they are reused.
  271. static Rectangle zeroRect = new Rectangle(0,0,0,0);
  272. static Rectangle iconRect = new Rectangle();
  273. static Rectangle textRect = new Rectangle();
  274. static Rectangle acceleratorRect = new Rectangle();
  275. static Rectangle checkIconRect = new Rectangle();
  276. static Rectangle arrowIconRect = new Rectangle();
  277. static Rectangle viewRect = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);
  278. static Rectangle r = new Rectangle();
  279. private void resetRects() {
  280. iconRect.setBounds(zeroRect);
  281. textRect.setBounds(zeroRect);
  282. acceleratorRect.setBounds(zeroRect);
  283. checkIconRect.setBounds(zeroRect);
  284. arrowIconRect.setBounds(zeroRect);
  285. viewRect.setBounds(0,0,Short.MAX_VALUE, Short.MAX_VALUE);
  286. r.setBounds(zeroRect);
  287. }
  288. protected Dimension getPreferredMenuItemSize(JComponent c,
  289. Icon checkIcon,
  290. Icon arrowIcon,
  291. int defaultTextIconGap) {
  292. JMenuItem b = (JMenuItem) c;
  293. Icon icon = (Icon) b.getIcon();
  294. String text = b.getText();
  295. KeyStroke accelerator = b.getAccelerator();
  296. String acceleratorText = "";
  297. if (accelerator != null) {
  298. int modifiers = accelerator.getModifiers();
  299. if (modifiers > 0) {
  300. acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
  301. //acceleratorText += "-";
  302. acceleratorText += acceleratorDelimiter;
  303. }
  304. acceleratorText += KeyEvent.getKeyText(accelerator.getKeyCode());
  305. }
  306. Font font = b.getFont();
  307. FontMetrics fm = b.getToolkit().getFontMetrics(font);
  308. FontMetrics fmAccel = b.getToolkit().getFontMetrics( acceleratorFont );
  309. resetRects();
  310. layoutMenuItem(
  311. fm, text, fmAccel, acceleratorText, icon, checkIcon, arrowIcon,
  312. b.getVerticalAlignment(), b.getHorizontalAlignment(),
  313. b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  314. viewRect, iconRect, textRect, acceleratorRect, checkIconRect, arrowIconRect,
  315. text == null ? 0 : defaultTextIconGap,
  316. defaultTextIconGap
  317. );
  318. // find the union of the icon and text rects
  319. r.setBounds(textRect);
  320. r = SwingUtilities.computeUnion(iconRect.x,
  321. iconRect.y,
  322. iconRect.width,
  323. iconRect.height,
  324. r);
  325. // r = iconRect.union(textRect);
  326. // To make the accelerator texts appear in a column, find the widest MenuItem text
  327. // and the widest accelerator text.
  328. //Get the parent, which stores the information.
  329. Container parent = menuItem.getParent();
  330. //Check the parent, and see that it is not a top-level menu.
  331. if (parent != null && parent instanceof JComponent &&
  332. !(menuItem instanceof JMenu && ((JMenu) menuItem).isTopLevelMenu())) {
  333. JComponent p = (JComponent) parent;
  334. //Get widest text so far from parent, if no one exists null is returned.
  335. Integer maxTextWidth = (Integer) p.getClientProperty(BasicMenuItemUI.MAX_TEXT_WIDTH);
  336. Integer maxAccWidth = (Integer) p.getClientProperty(BasicMenuItemUI.MAX_ACC_WIDTH);
  337. int maxTextValue = maxTextWidth!=null ? maxTextWidth.intValue() : 0;
  338. int maxAccValue = maxAccWidth!=null ? maxAccWidth.intValue() : 0;
  339. //Compare the text widths, and adjust the r.width to the widest.
  340. if (r.width < maxTextValue) {
  341. r.width = maxTextValue;
  342. } else {
  343. p.putClientProperty(BasicMenuItemUI.MAX_TEXT_WIDTH, new Integer(r.width) );
  344. }
  345. //Compare the accelarator widths.
  346. if (acceleratorRect.width > maxAccValue) {
  347. maxAccValue = acceleratorRect.width;
  348. p.putClientProperty(BasicMenuItemUI.MAX_ACC_WIDTH, new Integer(acceleratorRect.width) );
  349. }
  350. //Add on the widest accelerator
  351. r.width += maxAccValue;
  352. r.width += defaultTextIconGap;
  353. }
  354. if( useCheckAndArrow() ) {
  355. // Add in the checkIcon
  356. r.width += checkIconRect.width;
  357. r.width += defaultTextIconGap;
  358. // Add in the arrowIcon
  359. r.width += defaultTextIconGap;
  360. r.width += arrowIconRect.width;
  361. }
  362. r.width += 2*defaultTextIconGap;
  363. Insets insets = b.getInsets();
  364. if(insets != null) {
  365. r.width += insets.left + insets.right;
  366. r.height += insets.top + insets.bottom;
  367. }
  368. // if the width is even, bump it up one. This is critical
  369. // for the focus dash line to draw properly
  370. if(r.width%2 == 0) {
  371. r.width++;
  372. }
  373. // if the height is even, bump it up one. This is critical
  374. // for the text to center properly
  375. if(r.height%2 == 0) {
  376. r.height++;
  377. }
  378. /*
  379. if(!(b instanceof JMenu && ((JMenu) b).isTopLevelMenu()) ) {
  380. // Container parent = menuItem.getParent();
  381. JComponent p = (JComponent) parent;
  382. System.out.println("MaxText: "+p.getClientProperty(BasicMenuItemUI.MAX_TEXT_WIDTH));
  383. System.out.println("MaxACC"+p.getClientProperty(BasicMenuItemUI.MAX_ACC_WIDTH));
  384. System.out.println("returning pref.width: " + r.width);
  385. System.out.println("Current getSize: " + b.getSize() + "\n");
  386. }*/
  387. return r.getSize();
  388. }
  389. /**
  390. * We draw the background in paintMenuItem()
  391. * so override update (which fills the background of opaque
  392. * components by default) to just call paint().
  393. *
  394. */
  395. public void update(Graphics g, JComponent c) {
  396. paint(g, c);
  397. }
  398. public void paint(Graphics g, JComponent c) {
  399. paintMenuItem(g, c, checkIcon, arrowIcon,
  400. selectionBackground, selectionForeground,
  401. defaultTextIconGap);
  402. }
  403. protected void paintMenuItem(Graphics g, JComponent c,
  404. Icon checkIcon, Icon arrowIcon,
  405. Color background, Color foreground,
  406. int defaultTextIconGap) {
  407. JMenuItem b = (JMenuItem) c;
  408. ButtonModel model = b.getModel();
  409. // Dimension size = b.getSize();
  410. int menuWidth = b.getWidth();
  411. int menuHeight = b.getHeight();
  412. Insets i = c.getInsets();
  413. resetRects();
  414. viewRect.setBounds( 0, 0, menuWidth, menuHeight );
  415. viewRect.x += i.left;
  416. viewRect.y += i.top;
  417. viewRect.width -= (i.right + viewRect.x);
  418. viewRect.height -= (i.bottom + viewRect.y);
  419. Font holdf = g.getFont();
  420. Font f = c.getFont();
  421. g.setFont( f );
  422. FontMetrics fm = g.getFontMetrics( f );
  423. FontMetrics fmAccel = g.getFontMetrics( acceleratorFont );
  424. // Paint background
  425. Color holdc = g.getColor();
  426. if(c.isOpaque()) {
  427. if (model.isArmed()|| (c instanceof JMenu && model.isSelected())) {
  428. g.setColor(background);
  429. g.fillRect(0,0, menuWidth, menuHeight);
  430. } else {
  431. g.setColor(b.getBackground());
  432. g.fillRect(0,0, menuWidth, menuHeight);
  433. }
  434. g.setColor(holdc);
  435. }
  436. // get Accelerator text
  437. KeyStroke accelerator = b.getAccelerator();
  438. String acceleratorText = "";
  439. if (accelerator != null) {
  440. int modifiers = accelerator.getModifiers();
  441. if (modifiers > 0) {
  442. acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
  443. //acceleratorText += "-";
  444. acceleratorText += acceleratorDelimiter;
  445. }
  446. acceleratorText += KeyEvent.getKeyText(accelerator.getKeyCode());
  447. }
  448. // layout the text and icon
  449. String text = layoutMenuItem(
  450. fm, b.getText(), fmAccel, acceleratorText, b.getIcon(),
  451. checkIcon, arrowIcon,
  452. b.getVerticalAlignment(), b.getHorizontalAlignment(),
  453. b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  454. viewRect, iconRect, textRect, acceleratorRect,
  455. checkIconRect, arrowIconRect,
  456. b.getText() == null ? 0 : defaultTextIconGap,
  457. defaultTextIconGap
  458. );
  459. // Paint the Check
  460. if (checkIcon != null) {
  461. if(model.isArmed() || (c instanceof JMenu && model.isSelected())) {
  462. g.setColor(foreground);
  463. } else {
  464. g.setColor(holdc);
  465. }
  466. if( useCheckAndArrow() )
  467. checkIcon.paintIcon(c, g, checkIconRect.x, checkIconRect.y);
  468. g.setColor(holdc);
  469. }
  470. // Paint the Icon
  471. if(b.getIcon() != null) {
  472. Icon icon;
  473. if(!model.isEnabled()) {
  474. icon = (Icon) b.getDisabledIcon();
  475. } else if(model.isPressed() && model.isArmed()) {
  476. icon = (Icon) b.getPressedIcon();
  477. if(icon == null) {
  478. // Use default icon
  479. icon = (Icon) b.getIcon();
  480. }
  481. } else {
  482. icon = (Icon) b.getIcon();
  483. }
  484. if (icon!=null)
  485. icon.paintIcon(c, g, iconRect.x, iconRect.y);
  486. }
  487. // Draw the Text
  488. if(text != null) {
  489. View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  490. if (v != null) {
  491. v.paint(g, textRect);
  492. } else {
  493. if(!model.isEnabled()) {
  494. // *** paint the text disabled
  495. if ( UIManager.get("MenuItem.disabledForeground") instanceof Color )
  496. {
  497. g.setColor( UIManager.getColor("MenuItem.disabledForeground") );
  498. BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  499. textRect.x, textRect.y + fm.getAscent());
  500. }
  501. else
  502. {
  503. g.setColor(b.getBackground().brighter());
  504. BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  505. textRect.x, textRect.y + fm.getAscent());
  506. g.setColor(b.getBackground().darker());
  507. BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  508. textRect.x - 1, textRect.y + fm.getAscent() - 1);
  509. }
  510. } else {
  511. // *** paint the text normally
  512. if (model.isArmed()|| (c instanceof JMenu && model.isSelected())) {
  513. g.setColor(foreground);
  514. } else {
  515. g.setColor(holdc);
  516. }
  517. BasicGraphicsUtils.drawString(g,text,
  518. model.getMnemonic(),
  519. textRect.x,
  520. textRect.y + fm.getAscent());
  521. }
  522. }
  523. }
  524. // Draw the Accelerator Text
  525. if(acceleratorText != null && !acceleratorText.equals("")) {
  526. //Get the maxAccWidth from the parent to calculate the offset.
  527. int accOffset = 0;
  528. Container parent = menuItem.getParent();
  529. if (parent != null && parent instanceof JComponent) {
  530. JComponent p = (JComponent) parent;
  531. Integer maxValueInt = (Integer) p.getClientProperty(BasicMenuItemUI.MAX_ACC_WIDTH);
  532. int maxValue = maxValueInt!=null ? maxValueInt.intValue() : 0;
  533. //Calculate the offset, with which the accelerator texts will be drawn with.
  534. accOffset = maxValue - acceleratorRect.width;
  535. }
  536. g.setFont( acceleratorFont );
  537. if(!model.isEnabled()) {
  538. // *** paint the acceleratorText disabled
  539. if ( disabledForeground != null )
  540. {
  541. g.setColor( disabledForeground );
  542. BasicGraphicsUtils.drawString(g,acceleratorText,0,
  543. acceleratorRect.x - accOffset,
  544. acceleratorRect.y + fmAccel.getAscent());
  545. }
  546. else
  547. {
  548. g.setColor(b.getBackground().brighter());
  549. BasicGraphicsUtils.drawString(g,acceleratorText,0,
  550. acceleratorRect.x - accOffset,
  551. acceleratorRect.y + fmAccel.getAscent());
  552. g.setColor(b.getBackground().darker());
  553. BasicGraphicsUtils.drawString(g,acceleratorText,0,
  554. acceleratorRect.x - accOffset - 1,
  555. acceleratorRect.y + fmAccel.getAscent() - 1);
  556. }
  557. } else {
  558. // *** paint the acceleratorText normally
  559. if (model.isArmed()|| (c instanceof JMenu && model.isSelected())) {
  560. g.setColor( acceleratorSelectionForeground );
  561. } else {
  562. g.setColor( acceleratorForeground );
  563. }
  564. BasicGraphicsUtils.drawString(g,acceleratorText, 0,
  565. acceleratorRect.x - accOffset,
  566. acceleratorRect.y + fmAccel.getAscent());
  567. }
  568. }
  569. // Paint the Arrow
  570. if (arrowIcon != null) {
  571. if(model.isArmed() || (c instanceof JMenu &&model.isSelected()))
  572. g.setColor(foreground);
  573. if(useCheckAndArrow())
  574. arrowIcon.paintIcon(c, g, arrowIconRect.x, arrowIconRect.y);
  575. }
  576. g.setColor(holdc);
  577. g.setFont(holdf);
  578. }
  579. /**
  580. * Compute and return the location of the icons origin, the
  581. * location of origin of the text baseline, and a possibly clipped
  582. * version of the compound labels string. Locations are computed
  583. * relative to the viewRect rectangle.
  584. */
  585. private String layoutMenuItem(
  586. FontMetrics fm,
  587. String text,
  588. FontMetrics fmAccel,
  589. String acceleratorText,
  590. Icon icon,
  591. Icon checkIcon,
  592. Icon arrowIcon,
  593. int verticalAlignment,
  594. int horizontalAlignment,
  595. int verticalTextPosition,
  596. int horizontalTextPosition,
  597. Rectangle viewRect,
  598. Rectangle iconRect,
  599. Rectangle textRect,
  600. Rectangle acceleratorRect,
  601. Rectangle checkIconRect,
  602. Rectangle arrowIconRect,
  603. int textIconGap,
  604. int menuItemGap
  605. )
  606. {
  607. SwingUtilities.layoutCompoundLabel(
  608. menuItem, fm, text, icon, verticalAlignment,
  609. horizontalAlignment, verticalTextPosition,
  610. horizontalTextPosition, viewRect, iconRect, textRect,
  611. textIconGap);
  612. /* Initialize the acceelratorText bounds rectangle textRect. If a null
  613. * or and empty String was specified we substitute "" here
  614. * and use 0,0,0,0 for acceleratorTextRect.
  615. */
  616. if( (acceleratorText == null) || acceleratorText.equals("") ) {
  617. acceleratorRect.width = acceleratorRect.height = 0;
  618. acceleratorText = "";
  619. }
  620. else {
  621. acceleratorRect.width = SwingUtilities.computeStringWidth( fmAccel, acceleratorText );
  622. acceleratorRect.height = fmAccel.getHeight();
  623. }
  624. /* Initialize the checkIcon bounds rectangle's width & height.
  625. */
  626. if( useCheckAndArrow()) {
  627. if (checkIcon != null) {
  628. checkIconRect.width = checkIcon.getIconWidth();
  629. checkIconRect.height = checkIcon.getIconHeight();
  630. }
  631. else {
  632. checkIconRect.width = checkIconRect.height = 0;
  633. }
  634. /* Initialize the arrowIcon bounds rectangle width & height.
  635. */
  636. if (arrowIcon != null) {
  637. arrowIconRect.width = arrowIcon.getIconWidth();
  638. arrowIconRect.height = arrowIcon.getIconHeight();
  639. } else {
  640. arrowIconRect.width = arrowIconRect.height = 0;
  641. }
  642. }
  643. Rectangle labelRect = iconRect.union(textRect);
  644. if( BasicGraphicsUtils.isLeftToRight(menuItem) ) {
  645. textRect.x += menuItemGap;
  646. iconRect.x += menuItemGap;
  647. // Position the Accelerator text rect
  648. acceleratorRect.x = viewRect.x + viewRect.width - arrowIconRect.width
  649. - menuItemGap - acceleratorRect.width;
  650. // Position the Check and Arrow Icons
  651. if (useCheckAndArrow()) {
  652. checkIconRect.x = viewRect.x + menuItemGap;
  653. textRect.x += menuItemGap + checkIconRect.width;
  654. iconRect.x += menuItemGap + checkIconRect.width;
  655. arrowIconRect.x = viewRect.x + viewRect.width - menuItemGap
  656. - arrowIconRect.width;
  657. }
  658. } else {
  659. textRect.x -= menuItemGap;
  660. iconRect.x -= menuItemGap;
  661. // Position the Accelerator text rect
  662. acceleratorRect.x = viewRect.x + arrowIconRect.width + menuItemGap;
  663. // Position the Check and Arrow Icons
  664. if (useCheckAndArrow()) {
  665. checkIconRect.x = viewRect.x + viewRect.width - menuItemGap
  666. - checkIconRect.width;
  667. textRect.x -= menuItemGap + checkIconRect.width;
  668. iconRect.x -= menuItemGap + checkIconRect.width;
  669. arrowIconRect.x = viewRect.x + menuItemGap;
  670. }
  671. }
  672. // Align the accelertor text and the check and arrow icons vertically
  673. // with the center of the label rect.
  674. acceleratorRect.y = labelRect.y + (labelRect.height2) - (acceleratorRect.height2);
  675. if( useCheckAndArrow() ) {
  676. arrowIconRect.y = labelRect.y + (labelRect.height2) - (arrowIconRect.height2);
  677. checkIconRect.y = labelRect.y + (labelRect.height2) - (checkIconRect.height2);
  678. }
  679. /*
  680. System.out.println("Layout: text="+menuItem.getText()+"\n\tv="
  681. +viewRect+"\n\tc="+checkIconRect+"\n\ti="
  682. +iconRect+"\n\tt="+textRect+"\n\tacc="
  683. +acceleratorRect+"\n\ta="+arrowIconRect+"\n");
  684. */
  685. return text;
  686. }
  687. /*
  688. * Returns false if the component is a JMenu and it is a top
  689. * level menu (on the menubar).
  690. */
  691. private boolean useCheckAndArrow(){
  692. boolean b = true;
  693. if((menuItem instanceof JMenu) &&
  694. (((JMenu)menuItem).isTopLevelMenu())) {
  695. b = false;
  696. }
  697. return b;
  698. }
  699. public MenuElement[] getPath() {
  700. MenuSelectionManager m = MenuSelectionManager.defaultManager();
  701. MenuElement oldPath[] = m.getSelectedPath();
  702. MenuElement newPath[];
  703. int i = oldPath.length;
  704. if (i == 0)
  705. return new MenuElement[0];
  706. Component parent = menuItem.getParent();
  707. if (oldPath[i-1].getComponent() == parent) {
  708. // The parent popup menu is the last so far
  709. newPath = new MenuElement[i+1];
  710. System.arraycopy(oldPath, 0, newPath, 0, i);
  711. newPath[i] = menuItem;
  712. } else {
  713. // A sibling menuitem is the current selection
  714. //
  715. // This probably needs to handle 'exit submenu into
  716. // a menu item. Search backwards along the current
  717. // selection until you find the parent popup menu,
  718. // then copy up to that and add yourself...
  719. int j;
  720. for (j = oldPath.length-1; j >= 0; j--) {
  721. if (oldPath[j].getComponent() == parent)
  722. break;
  723. }
  724. newPath = new MenuElement[j+2];
  725. System.arraycopy(oldPath, 0, newPath, 0, j+1);
  726. newPath[j+1] = menuItem;
  727. /*
  728. System.out.println("Sibling condition -- ");
  729. System.out.println("Old array : ");
  730. printMenuElementArray(oldPath, false);
  731. System.out.println("New array : ");
  732. printMenuElementArray(newPath, false);
  733. */
  734. }
  735. return newPath;
  736. }
  737. void printMenuElementArray(MenuElement path[], boolean dumpStack) {
  738. System.out.println("Path is(");
  739. int i, j;
  740. for(i=0,j=path.length; i<j ;i++){
  741. for (int k=0; k<=i; k++)
  742. System.out.print(" ");
  743. MenuElement me = (MenuElement) path[i];
  744. if(me instanceof JMenuItem)
  745. System.out.println(((JMenuItem)me).getText() + ", ");
  746. else if (me == null)
  747. System.out.println("NULL , ");
  748. else
  749. System.out.println("" + me + ", ");
  750. }
  751. System.out.println(")");
  752. if (dumpStack == true)
  753. Thread.dumpStack();
  754. }
  755. protected class MouseInputHandler implements MouseInputListener {
  756. public void mouseClicked(MouseEvent e) {}
  757. public void mousePressed(MouseEvent e) {
  758. }
  759. public void mouseReleased(MouseEvent e) {
  760. MenuSelectionManager manager =
  761. MenuSelectionManager.defaultManager();
  762. Point p = e.getPoint();
  763. if(p.x >= 0 && p.x < menuItem.getWidth() &&
  764. p.y >= 0 && p.y < menuItem.getHeight()) {
  765. manager.clearSelectedPath();
  766. menuItem.doClick(0);
  767. } else {
  768. manager.processMouseEvent(e);
  769. }
  770. }
  771. public void mouseEntered(MouseEvent e) {
  772. MenuSelectionManager manager = MenuSelectionManager.defaultManager();
  773. int modifiers = e.getModifiers();
  774. // 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2
  775. if ((modifiers & (InputEvent.BUTTON1_MASK |
  776. InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) !=0 ) {
  777. MenuSelectionManager.defaultManager().processMouseEvent(e);
  778. } else {
  779. manager.setSelectedPath(getPath());
  780. }
  781. }
  782. public void mouseExited(MouseEvent e) {
  783. MenuSelectionManager manager = MenuSelectionManager.defaultManager();
  784. int modifiers = e.getModifiers();
  785. // 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2
  786. if ((modifiers & (InputEvent.BUTTON1_MASK |
  787. InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) !=0 ) {
  788. MenuSelectionManager.defaultManager().processMouseEvent(e);
  789. } else {
  790. MenuElement path[] = manager.getSelectedPath();
  791. if (path.length > 1) {
  792. MenuElement newPath[] = new MenuElement[path.length-1];
  793. int i,c;
  794. for(i=0,c=path.length-1;i<c;i++)
  795. newPath[i] = path[i];
  796. manager.setSelectedPath(newPath);
  797. }
  798. }
  799. }
  800. public void mouseDragged(MouseEvent e) {
  801. MenuSelectionManager.defaultManager().processMouseEvent(e);
  802. }
  803. public void mouseMoved(MouseEvent e) {
  804. }
  805. }
  806. private class MenuDragMouseHandler implements MenuDragMouseListener {
  807. public void menuDragMouseEntered(MenuDragMouseEvent e) {}
  808. public void menuDragMouseDragged(MenuDragMouseEvent e) {
  809. MenuSelectionManager manager = e.getMenuSelectionManager();
  810. MenuElement path[] = e.getPath();
  811. manager.setSelectedPath(path);
  812. }
  813. public void menuDragMouseExited(MenuDragMouseEvent e) {}
  814. public void menuDragMouseReleased(MenuDragMouseEvent e) {
  815. MenuSelectionManager manager = e.getMenuSelectionManager();
  816. MenuElement path[] = e.getPath();
  817. Point p = e.getPoint();
  818. if(p.x >= 0 && p.x < menuItem.getWidth() &&
  819. p.y >= 0 && p.y < menuItem.getHeight()) {
  820. manager.clearSelectedPath();
  821. menuItem.doClick(0);
  822. } else {
  823. manager.clearSelectedPath();
  824. }
  825. }
  826. }
  827. private class MenuKeyHandler implements MenuKeyListener {
  828. public void menuKeyTyped(MenuKeyEvent e) {
  829. if (DEBUG) {
  830. System.out.println("in BasicMenuItemUI.menuKeyTyped for " + menuItem.getText());
  831. }
  832. int key = menuItem.getMnemonic();
  833. if(key == 0)
  834. return;
  835. if(lower(key) == lower((int)(e.getKeyChar()))) {
  836. MenuSelectionManager manager =
  837. e.getMenuSelectionManager();
  838. manager.clearSelectedPath();
  839. menuItem.doClick(0);
  840. e.consume();
  841. }
  842. }
  843. public void menuKeyPressed(MenuKeyEvent e) {
  844. if (DEBUG) {
  845. System.out.println("in BasicMenuItemUI.menuKeyPressed for " + menuItem.getText());
  846. }
  847. }
  848. public void menuKeyReleased(MenuKeyEvent e) {}
  849. private int lower(int ascii) {
  850. if(ascii >= 'A' && ascii <= 'Z')
  851. return ascii + 'a' - 'A';
  852. else
  853. return ascii;
  854. }
  855. }
  856. private class PropertyChangeHandler implements PropertyChangeListener {
  857. public void propertyChange(PropertyChangeEvent e) {
  858. String name = e.getPropertyName();
  859. if (name.equals("labelFor") || name.equals("displayedMnemonic") ||
  860. name.equals("accelerator")) {
  861. updateAcceleratorBinding();
  862. } else if (name.equals("text")) {
  863. // remove the old html view client property if one
  864. // existed, and install a new one if the text installed
  865. // into the JLabel is html source.
  866. JMenuItem lbl = ((JMenuItem) e.getSource());
  867. String text = lbl.getText();
  868. BasicHTML.updateRenderer(lbl, text);
  869. }
  870. }
  871. }
  872. private static class ClickAction extends AbstractAction {
  873. public void actionPerformed(ActionEvent e) {
  874. JMenuItem mi = (JMenuItem)e.getSource();
  875. MenuSelectionManager.defaultManager().clearSelectedPath();
  876. mi.doClick();
  877. }
  878. }
  879. }