1. /*
  2. * @(#)MotifMenuUI.java 1.32 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.motif;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11. import javax.swing.event.*;
  12. import javax.swing.plaf.*;
  13. import javax.swing.border.*;
  14. import javax.swing.plaf.basic.*;
  15. import javax.swing.plaf.basic.BasicMenuUI;
  16. /**
  17. * A Motif L&F implementation of MenuUI.
  18. * <p>
  19. *
  20. * @version 1.32 12/19/03
  21. * @author Georges Saab
  22. * @author Rich Schiavi
  23. */
  24. public class MotifMenuUI extends BasicMenuUI
  25. {
  26. public static ComponentUI createUI( JComponent x ) {
  27. return new MotifMenuUI();
  28. }
  29. // These should not be necessary because BasicMenuUI does this,
  30. // and this class overrides createChangeListener.
  31. // protected void installListeners() {
  32. // super.installListeners();
  33. // changeListener = createChangeListener(menuItem);
  34. // menuItem.addChangeListener(changeListener);
  35. // }
  36. //
  37. // protected void uninstallListeners() {
  38. // super.uninstallListeners();
  39. // menuItem.removeChangeListener(changeListener);
  40. // }
  41. protected ChangeListener createChangeListener(JComponent c) {
  42. return new MotifChangeHandler((JMenu)c, this);
  43. }
  44. public void paint(Graphics g, JComponent c){
  45. MotifGraphicsUtils.paintMenuItem(g,c,checkIcon,arrowIcon,
  46. selectionBackground, selectionForeground,
  47. defaultTextIconGap);
  48. }
  49. private boolean popupIsOpen(JMenu m,MenuElement me[]) {
  50. int i;
  51. JPopupMenu pm = m.getPopupMenu();
  52. for(i=me.length-1;i>=0;i--) {
  53. if(me[i].getComponent() == pm)
  54. return true;
  55. }
  56. return false;
  57. }
  58. protected MouseInputListener createMouseInputListener(JComponent c) {
  59. return new MouseInputHandler();
  60. }
  61. public class MotifChangeHandler extends ChangeHandler {
  62. public MotifChangeHandler(JMenu m, MotifMenuUI ui) {
  63. super(m, ui);
  64. }
  65. public void stateChanged(ChangeEvent e) {
  66. JMenuItem c = (JMenuItem)e.getSource();
  67. if (c.isArmed() || c.isSelected()) {
  68. c.setBorderPainted(true);
  69. // c.repaint();
  70. } else {
  71. c.setBorderPainted(false);
  72. }
  73. super.stateChanged(e);
  74. }
  75. }
  76. protected class MouseInputHandler implements MouseInputListener {
  77. public void mouseClicked(MouseEvent e) {}
  78. public void mousePressed(MouseEvent e) {
  79. MenuSelectionManager manager = MenuSelectionManager.defaultManager();
  80. JMenu menu = (JMenu)e.getComponent();
  81. if(menu.isEnabled()) {
  82. if(menu.isTopLevelMenu()) {
  83. if(menu.isSelected()) {
  84. manager.clearSelectedPath();
  85. } else {
  86. Container cnt = menu.getParent();
  87. if(cnt != null && cnt instanceof JMenuBar) {
  88. MenuElement me[] = new MenuElement[2];
  89. me[0]=(MenuElement)cnt;
  90. me[1]=menu;
  91. manager.setSelectedPath(me);
  92. }
  93. }
  94. }
  95. MenuElement path[] = getPath();
  96. if (path.length > 0) {
  97. MenuElement newPath[] = new MenuElement[path.length+1];
  98. System.arraycopy(path,0,newPath,0,path.length);
  99. newPath[path.length] = menu.getPopupMenu();
  100. manager.setSelectedPath(newPath);
  101. }
  102. }
  103. }
  104. public void mouseReleased(MouseEvent e) {
  105. MenuSelectionManager manager =
  106. MenuSelectionManager.defaultManager();
  107. JMenuItem menuItem = (JMenuItem)e.getComponent();
  108. Point p = e.getPoint();
  109. if(!(p.x >= 0 && p.x < menuItem.getWidth() &&
  110. p.y >= 0 && p.y < menuItem.getHeight())) {
  111. manager.processMouseEvent(e);
  112. }
  113. }
  114. public void mouseEntered(MouseEvent e) {}
  115. public void mouseExited(MouseEvent e) {}
  116. public void mouseDragged(MouseEvent e) {
  117. MenuSelectionManager.defaultManager().processMouseEvent(e);
  118. }
  119. public void mouseMoved(MouseEvent e) { }
  120. }
  121. }