Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/com/apple/laf/AquaMenuBarUI.java
38831 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.apple.laf;2627import java.awt.*;28import java.security.AccessController;2930import javax.swing.*;31import javax.swing.plaf.ComponentUI;32import javax.swing.plaf.basic.BasicMenuBarUI;3334import sun.lwawt.macosx.LWCToolkit;35import sun.security.action.GetBooleanAction;36import sun.security.action.GetPropertyAction;3738// MenuBar implementation for Mac L&F39public class AquaMenuBarUI extends BasicMenuBarUI implements ScreenMenuBarProvider {40// Utilities41public void uninstallUI(final JComponent c) {42if (fScreenMenuBar != null) {43final JFrame frame = (JFrame)(c.getTopLevelAncestor());44if (frame.getMenuBar() == fScreenMenuBar) {45frame.setMenuBar((MenuBar)null);46}47fScreenMenuBar = null;48}49super.uninstallUI(c);50}5152// Create PLAF53public static ComponentUI createUI(final JComponent c) {54return new AquaMenuBarUI();55}5657// [3320390] -- If the screen menu bar is in use, don't register keyboard actions that58// show the menus when F10 is pressed.59protected void installKeyboardActions() {60if (!useScreenMenuBar) {61super.installKeyboardActions();62}63}6465protected void uninstallKeyboardActions() {66if (!useScreenMenuBar) {67super.uninstallKeyboardActions();68}69}7071// Paint Methods72public void paint(final Graphics g, final JComponent c) {73AquaMenuPainter.instance().paintMenuBarBackground(g, c.getWidth(), c.getHeight(), c);74}7576public Dimension getPreferredSize(final JComponent c) {77if (isScreenMenuBar((JMenuBar)c)) {78if (setScreenMenuBar((JFrame)(c.getTopLevelAncestor()))) {79return new Dimension(0, 0);80}81}82return null;83}8485void clearScreenMenuBar(final JFrame frame) {86if (useScreenMenuBar) {87frame.setMenuBar(null);88}89}9091boolean setScreenMenuBar(final JFrame frame) {92if (useScreenMenuBar) {93try {94getScreenMenuBar();95} catch(final Throwable t) {96return false;97}9899frame.setMenuBar(fScreenMenuBar);100}101102return true;103}104105public ScreenMenuBar getScreenMenuBar() {106// Lazy init of member variables means we should use a synchronized block.107synchronized(this) {108if (fScreenMenuBar == null) fScreenMenuBar = new ScreenMenuBar(this.menuBar);109}110return fScreenMenuBar;111}112113// JMenuBars are in frame unless we're using ScreenMenuBars *and* it's114// been set by JFrame.setJMenuBar115// unless the JFrame has a normal java.awt.MenuBar (it's possible!)116// Other JMenuBars appear where the programmer puts them - top of window or elsewhere117public static final boolean isScreenMenuBar(final JMenuBar c) {118final javax.swing.plaf.ComponentUI ui = c.getUI();119if (ui instanceof AquaMenuBarUI) {120if (!((AquaMenuBarUI)ui).useScreenMenuBar) return false;121122final Component parent = c.getTopLevelAncestor();123if (parent instanceof JFrame) {124final MenuBar mb = ((JFrame)parent).getMenuBar();125final boolean thisIsTheJMenuBar = (((JFrame)parent).getJMenuBar() == c);126if (mb == null) return thisIsTheJMenuBar;127return (mb instanceof ScreenMenuBar && thisIsTheJMenuBar);128}129}130return false;131}132133ScreenMenuBar fScreenMenuBar;134boolean useScreenMenuBar = getScreenMenuBarProperty();135136static boolean getScreenMenuBarProperty() {137// Do not allow AWT to set the screen menu bar if it's embedded in another UI toolkit138if (LWCToolkit.isEmbedded()) return false;139if (AccessController.doPrivileged(140new GetBooleanAction(AquaLookAndFeel.sPropertyPrefix + "useScreenMenuBar"))) {141return true;142}143if (AccessController.doPrivileged(144new GetBooleanAction(AquaLookAndFeel.sOldPropertyPrefix + "useScreenMenuBar"))) {145System.err.println(AquaLookAndFeel.sOldPropertyPrefix +146"useScreenMenuBar has been deprecated. Please switch to " +147AquaLookAndFeel.sPropertyPrefix + "useScreenMenuBar");148return true;149}150return false;151}152}153154155