1. /*
  2. * @(#)MotifPopupMenuUI.java 1.23 03/01/23
  3. *
  4. * Copyright 2003 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 javax.swing.*;
  9. import javax.swing.event.*;
  10. import javax.swing.border.*;
  11. import java.awt.Color;
  12. import java.awt.Component;
  13. import java.awt.Container;
  14. import java.awt.Dimension;
  15. import java.awt.Font;
  16. import java.awt.FontMetrics;
  17. import java.awt.Frame;
  18. import java.awt.Graphics;
  19. import java.awt.Insets;
  20. import java.awt.LayoutManager;
  21. import java.awt.Point;
  22. import java.awt.Rectangle;
  23. import java.awt.event.*;
  24. import javax.swing.plaf.*;
  25. import javax.swing.plaf.basic.BasicPopupMenuUI;
  26. /**
  27. * A Motif L&F implementation of PopupMenuUI.
  28. * <p>
  29. * <strong>Warning:</strong>
  30. * Serialized objects of this class will not be compatible with
  31. * future Swing releases. The current serialization support is appropriate
  32. * for short term storage or RMI between applications running the same
  33. * version of Swing. A future release of Swing will provide support for
  34. * long term persistence.
  35. *
  36. * @version 1.23 01/23/03
  37. * @author Georges Saab
  38. * @author Rich Schiavi
  39. */
  40. public class MotifPopupMenuUI extends BasicPopupMenuUI {
  41. private static Border border = null;
  42. private Font titleFont = null;
  43. public static ComponentUI createUI(JComponent x) {
  44. return new MotifPopupMenuUI();
  45. }
  46. /* This has to deal with the fact that the title may be wider than
  47. the widest child component.
  48. */
  49. public Dimension getPreferredSize(JComponent c) {
  50. LayoutManager layout = c.getLayout();
  51. Dimension d = layout.preferredLayoutSize(c);
  52. String title = ((JPopupMenu)c).getLabel();
  53. if (titleFont == null) {
  54. UIDefaults table = UIManager.getLookAndFeelDefaults();
  55. titleFont = table.getFont("PopupMenu.font");
  56. }
  57. FontMetrics fm = c.getFontMetrics(titleFont);
  58. int stringWidth = 0;
  59. if (title!=null) {
  60. stringWidth +=fm.stringWidth(title);
  61. }
  62. if (d.width < stringWidth) {
  63. d.width = stringWidth + 8;
  64. Insets i = c.getInsets();
  65. if (i!=null) {
  66. d.width += i.left + i.right;
  67. }
  68. if (border != null) {
  69. i = border.getBorderInsets(c);
  70. d.width += i.left + i.right;
  71. }
  72. return d;
  73. }
  74. return null;
  75. }
  76. protected ChangeListener createChangeListener(JPopupMenu m) {
  77. return new ChangeListener() {
  78. public void stateChanged(ChangeEvent e) {}
  79. };
  80. }
  81. public boolean isPopupTrigger(MouseEvent e) {
  82. return ((e.getID()==MouseEvent.MOUSE_PRESSED)
  83. && ((e.getModifiers() & MouseEvent.BUTTON3_MASK)!=0));
  84. }
  85. }