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