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 
  26 package com.sun.java.swing.plaf.motif;
  27 
  28 import sun.swing.SwingUtilities2;
  29 import javax.swing.*;
  30 import javax.swing.event.*;
  31 import javax.swing.border.*;
  32 import java.awt.Color;
  33 import java.awt.Component;
  34 import java.awt.Container;
  35 import java.awt.Dimension;
  36 import java.awt.Font;
  37 import java.awt.FontMetrics;
  38 import java.awt.Frame;
  39 import java.awt.Graphics;
  40 import java.awt.Insets;
  41 import java.awt.LayoutManager;
  42 import java.awt.Point;
  43 import java.awt.Rectangle;
  44 import java.awt.event.*;
  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() {
 106             public void stateChanged(ChangeEvent e) {}
 107         };
 108     }
 109 
 110     public boolean isPopupTrigger(MouseEvent e) {
 111         return ((e.getID()==MouseEvent.MOUSE_PRESSED)
 112                 && ((e.getModifiers() & MouseEvent.BUTTON3_MASK)!=0));
 113     }
 114 }