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