1/*
2 * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.java.swing.plaf.motif;
27
28import sun.swing.SwingUtilities2;
29import javax.swing.*;
30import javax.swing.event.*;
31import javax.swing.border.*;
32import java.awt.Color;
33import java.awt.Component;
34import java.awt.Container;
35import java.awt.Dimension;
36import java.awt.Font;
37import java.awt.FontMetrics;
38import java.awt.Frame;
39import java.awt.Graphics;
40import java.awt.Insets;
41import java.awt.LayoutManager;
42import java.awt.Point;
43import java.awt.Rectangle;
44import java.awt.event.*;
45import javax.swing.plaf.*;
46import 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 */
62public 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() {
106            public void stateChanged(ChangeEvent e) {}
107        };
108    }
109
110    @SuppressWarnings("deprecation")
111    public boolean isPopupTrigger(MouseEvent e) {
112        return ((e.getID()==MouseEvent.MOUSE_PRESSED)
113                && ((e.getModifiers() & MouseEvent.BUTTON3_MASK)!=0));
114    }
115}
116