1 /*
   2  * Copyright (c) 2011, 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.apple.laf;
  27 
  28 import java.awt.*;
  29 import java.security.PrivilegedAction;
  30 
  31 import javax.swing.*;
  32 
  33 import sun.lwawt.macosx.CPlatformWindow;
  34 
  35 class ScreenPopupFactory extends PopupFactory {
  36     static {
  37         java.security.AccessController.doPrivileged((PrivilegedAction<?>)new sun.security.action.LoadLibraryAction("osxui"));
  38     }
  39 
  40     static final Float TRANSLUCENT = new Float(248f/255f);
  41     static final Float OPAQUE = new Float(1.0f);
  42 
  43     boolean fIsActive = true;
  44 
  45     // Only popups generated with the Aqua LaF turned on will be translucent with shadows
  46     void setActive(final boolean b) {
  47         fIsActive = b;
  48     }
  49 
  50     private static Window getWindow(final Component c) {
  51         Component w = c;
  52         while(!(w instanceof Window) && (w!=null)) {
  53             w = w.getParent();
  54         }
  55         return (Window)w;
  56     }
  57 
  58     /*
  59      * Since we can't change the signature of PopupFactory, we have to call the
  60      * private method getPopup(Component, Component, int, int, int) through JNI
  61      * (see AquaLookAndFeel.m)
  62      */
  63     native Popup _getHeavyWeightPopup(Component comp, Component invoker, int x, int y);
  64 
  65     public Popup getPopup(final Component comp, final Component invoker, final int x, final int y) {
  66         if (invoker == null) throw new IllegalArgumentException("Popup.getPopup must be passed non-null contents");
  67 
  68         final Popup popup;
  69         if (fIsActive) {
  70             popup = _getHeavyWeightPopup(comp, invoker, x, y);
  71         } else {
  72             popup = super.getPopup(comp, invoker, x, y);
  73         }
  74 
  75         // Make the popup semi-translucent if it is a heavy weight
  76         // see <rdar://problem/3547670> JPopupMenus have incorrect background
  77         final Window w = getWindow(invoker);
  78         if (w == null) return popup;
  79 
  80         if (!(w instanceof RootPaneContainer)) return popup;
  81         final JRootPane popupRootPane = ((RootPaneContainer)w).getRootPane();
  82 
  83         // we need to set every time, because PopupFactory caches the heavy weight
  84         // TODO: CPlatformWindow constants?
  85         if (fIsActive) {
  86             popupRootPane.putClientProperty(CPlatformWindow.WINDOW_ALPHA, TRANSLUCENT);
  87             popupRootPane.putClientProperty(CPlatformWindow.WINDOW_SHADOW, Boolean.TRUE);
  88             popupRootPane.putClientProperty(CPlatformWindow.WINDOW_FADE_DELEGATE, invoker);
  89 
  90             w.setBackground(UIManager.getColor("PopupMenu.translucentBackground"));
  91             popupRootPane.putClientProperty(CPlatformWindow.WINDOW_DRAGGABLE_BACKGROUND, Boolean.FALSE);
  92             SwingUtilities.invokeLater(new Runnable() {
  93                 public void run() {
  94                     popupRootPane.putClientProperty(CPlatformWindow.WINDOW_SHADOW_REVALIDATE_NOW, Double.valueOf(Math.random()));
  95                 }
  96             });
  97         } else {
  98             popupRootPane.putClientProperty(CPlatformWindow.WINDOW_ALPHA, OPAQUE);
  99             popupRootPane.putClientProperty(CPlatformWindow.WINDOW_SHADOW, Boolean.FALSE);
 100         }
 101 
 102         return popup;
 103     }
 104 }