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