45 import javax.swing.plaf.*;
46 import javax.swing.plaf.basic.BasicPopupMenuUI;
47
48
49 /**
50 * A Motif {@literal L&F} implementation of PopupMenuUI.
51 * <p>
52 * <strong>Warning:</strong>
53 * Serialized objects of this class will not be compatible with
54 * future Swing releases. The current serialization support is appropriate
55 * for short term storage or RMI between applications running the same
56 * version of Swing. A future release of Swing will provide support for
57 * long term persistence.
58 *
59 * @author Georges Saab
60 * @author Rich Schiavi
61 */
62 public class MotifPopupMenuUI extends BasicPopupMenuUI {
63 private static Border border = null;
64 private Font titleFont = null;
65
66 public static ComponentUI createUI(JComponent x) {
67 return new MotifPopupMenuUI();
68 }
69
70 /* This has to deal with the fact that the title may be wider than
71 the widest child component.
72 */
73 public Dimension getPreferredSize(JComponent c) {
74 LayoutManager layout = c.getLayout();
75 Dimension d = layout.preferredLayoutSize(c);
76 String title = ((JPopupMenu)c).getLabel();
77 if (titleFont == null) {
78 UIDefaults table = UIManager.getLookAndFeelDefaults();
79 titleFont = table.getFont("PopupMenu.font");
80 }
81 FontMetrics fm = c.getFontMetrics(titleFont);
82 int stringWidth = 0;
83
84 if (title!=null) {
85 stringWidth += SwingUtilities2.stringWidth(c, fm, title);
86 }
87
88 if (d.width < stringWidth) {
89 d.width = stringWidth + 8;
90 Insets i = c.getInsets();
91 if (i!=null) {
92 d.width += i.left + i.right;
93 }
94 if (border != null) {
95 i = border.getBorderInsets(c);
96 d.width += i.left + i.right;
97 }
98
99 return d;
100 }
101 return null;
102 }
103
104 protected ChangeListener createChangeListener(JPopupMenu m) {
105 return new ChangeListener() {
|
45 import javax.swing.plaf.*;
46 import javax.swing.plaf.basic.BasicPopupMenuUI;
47
48
49 /**
50 * A Motif {@literal L&F} implementation of PopupMenuUI.
51 * <p>
52 * <strong>Warning:</strong>
53 * Serialized objects of this class will not be compatible with
54 * future Swing releases. The current serialization support is appropriate
55 * for short term storage or RMI between applications running the same
56 * version of Swing. A future release of Swing will provide support for
57 * long term persistence.
58 *
59 * @author Georges Saab
60 * @author Rich Schiavi
61 */
62 public class MotifPopupMenuUI extends BasicPopupMenuUI {
63 private static Border border = null;
64 private Font titleFont = null;
65 private TextUIDrawing textUIDrawing;
66
67 public static ComponentUI createUI(JComponent x) {
68 return new MotifPopupMenuUI();
69 }
70
71 @Override
72 public void installDefaults() {
73 super.installDefaults();
74 textUIDrawing = SwingUtilities2.getTextUIDrawing(textUIDrawing);
75 }
76
77 @Override
78 protected void uninstallDefaults() {
79 super.uninstallDefaults();
80 if (textUIDrawing != SwingUtilities2.DEFAULT_UI_TEXT_DRAWING
81 && textUIDrawing instanceof UIResource) {
82 textUIDrawing = SwingUtilities2.DEFAULT_UI_TEXT_DRAWING;
83 }
84 }
85
86 /* This has to deal with the fact that the title may be wider than
87 the widest child component.
88 */
89 public Dimension getPreferredSize(JComponent c) {
90 LayoutManager layout = c.getLayout();
91 Dimension d = layout.preferredLayoutSize(c);
92 String title = ((JPopupMenu)c).getLabel();
93 if (titleFont == null) {
94 UIDefaults table = UIManager.getLookAndFeelDefaults();
95 titleFont = table.getFont("PopupMenu.font");
96 }
97 FontMetrics fm = c.getFontMetrics(titleFont);
98 int stringWidth = 0;
99
100 if (title!=null) {
101 stringWidth += textUIDrawing.getStringWidth(c, fm, title);
102 }
103
104 if (d.width < stringWidth) {
105 d.width = stringWidth + 8;
106 Insets i = c.getInsets();
107 if (i!=null) {
108 d.width += i.left + i.right;
109 }
110 if (border != null) {
111 i = border.getBorderInsets(c);
112 d.width += i.left + i.right;
113 }
114
115 return d;
116 }
117 return null;
118 }
119
120 protected ChangeListener createChangeListener(JPopupMenu m) {
121 return new ChangeListener() {
|