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