Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/com/apple/eawt/_AppMenuBarHandler.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.eawt;2627import java.awt.Frame;28import java.awt.peer.MenuComponentPeer;2930import javax.swing.*;31import javax.swing.plaf.MenuBarUI;3233import com.apple.laf.ScreenMenuBar;34import sun.lwawt.macosx.CMenuBar;3536import com.apple.laf.AquaMenuBarUI;3738class _AppMenuBarHandler {39private static final int MENU_ABOUT = 1;40private static final int MENU_PREFS = 2;4142private static native void nativeSetMenuState(final int menu, final boolean visible, final boolean enabled);43private static native void nativeSetDefaultMenuBar(final long menuBarPeer);4445static final _AppMenuBarHandler instance = new _AppMenuBarHandler();46static _AppMenuBarHandler getInstance() {47return instance;48}4950// callback from the native delegate -init function51private static void initMenuStates(final boolean aboutMenuItemVisible, final boolean aboutMenuItemEnabled, final boolean prefsMenuItemVisible, final boolean prefsMenuItemEnabled) {52synchronized (instance) {53instance.aboutMenuItemVisible = aboutMenuItemVisible;54instance.aboutMenuItemEnabled = aboutMenuItemEnabled;55instance.prefsMenuItemVisible = prefsMenuItemVisible;56instance.prefsMenuItemEnabled = prefsMenuItemEnabled;57}58}5960_AppMenuBarHandler() { }6162boolean aboutMenuItemVisible;63boolean aboutMenuItemEnabled;6465boolean prefsMenuItemVisible;66boolean prefsMenuItemEnabled;67boolean prefsMenuItemExplicitlySet;6869void setDefaultMenuBar(final JMenuBar menuBar) {70installDefaultMenuBar(menuBar);7172// scan the current frames, and see if any are foreground73final Frame[] frames = Frame.getFrames();74for (final Frame frame : frames) {75if (frame.isVisible() && !isFrameMinimized(frame)) {76return;77}78}7980// if we have no foreground frames, then we have to "kick" the menubar81final JFrame pingFrame = new JFrame();82pingFrame.getRootPane().putClientProperty("Window.alpha", new Float(0.0f));83pingFrame.setUndecorated(true);84pingFrame.setVisible(true);85pingFrame.toFront();86pingFrame.setVisible(false);87pingFrame.dispose();88}8990static boolean isFrameMinimized(final Frame frame) {91return (frame.getExtendedState() & Frame.ICONIFIED) != 0;92}9394@SuppressWarnings("deprecation")95static void installDefaultMenuBar(final JMenuBar menuBar) {96if (menuBar == null) {97// intentionally clearing the default menu98nativeSetDefaultMenuBar(0);99return;100}101102final MenuBarUI ui = menuBar.getUI();103if (!(ui instanceof AquaMenuBarUI)) {104// Aqua was not installed105throw new IllegalStateException("Application.setDefaultMenuBar() only works with the Aqua Look and Feel");106}107108final AquaMenuBarUI aquaUI = (AquaMenuBarUI)ui;109final ScreenMenuBar screenMenuBar = aquaUI.getScreenMenuBar();110if (screenMenuBar == null) {111// Aqua is installed, but we aren't using the screen menu bar112throw new IllegalStateException("Application.setDefaultMenuBar() only works if apple.laf.useScreenMenuBar=true");113}114115screenMenuBar.addNotify();116final MenuComponentPeer peer = screenMenuBar.getPeer();117if (!(peer instanceof CMenuBar)) {118// such a thing should not be possible119throw new IllegalStateException("Unable to determine native menu bar from provided JMenuBar");120}121122// grab the pointer to the CMenuBar, and retain it in native123((CMenuBar) peer).execute(_AppMenuBarHandler::nativeSetDefaultMenuBar);124}125126void setAboutMenuItemVisible(final boolean present) {127synchronized (this) {128if (aboutMenuItemVisible == present) return;129aboutMenuItemVisible = present;130}131132nativeSetMenuState(MENU_ABOUT, aboutMenuItemVisible, aboutMenuItemEnabled);133}134135void setPreferencesMenuItemVisible(final boolean present) {136synchronized (this) {137prefsMenuItemExplicitlySet = true;138if (prefsMenuItemVisible == present) return;139prefsMenuItemVisible = present;140}141nativeSetMenuState(MENU_PREFS, prefsMenuItemVisible, prefsMenuItemEnabled);142}143144void setAboutMenuItemEnabled(final boolean enable) {145synchronized (this) {146if (aboutMenuItemEnabled == enable) return;147aboutMenuItemEnabled = enable;148}149nativeSetMenuState(MENU_ABOUT, aboutMenuItemVisible, aboutMenuItemEnabled);150}151152void setPreferencesMenuItemEnabled(final boolean enable) {153synchronized (this) {154prefsMenuItemExplicitlySet = true;155if (prefsMenuItemEnabled == enable) return;156prefsMenuItemEnabled = enable;157}158nativeSetMenuState(MENU_PREFS, prefsMenuItemVisible, prefsMenuItemEnabled);159}160161boolean isAboutMenuItemVisible() {162return aboutMenuItemVisible;163}164165boolean isPreferencesMenuItemVisible() {166return prefsMenuItemVisible;167}168169boolean isAboutMenuItemEnabled() {170return aboutMenuItemEnabled;171}172173boolean isPreferencesMenuItemEnabled() {174return prefsMenuItemEnabled;175}176}177178179