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     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() {
 122             public void stateChanged(ChangeEvent e) {}
 123         };
 124     }
 125 
 126     public boolean isPopupTrigger(MouseEvent e) {
 127         return ((e.getID()==MouseEvent.MOUSE_PRESSED)
 128                 && ((e.getModifiers() & MouseEvent.BUTTON3_MASK)!=0));
 129     }
 130 }