4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/Apackage com.apple.eawt;
4632N/A
4632N/Aimport java.awt.Frame;
4632N/Aimport java.awt.peer.MenuComponentPeer;
4632N/A
4632N/Aimport javax.swing.*;
4632N/Aimport javax.swing.plaf.MenuBarUI;
4632N/A
4632N/Aimport sun.lwawt.macosx.CMenuBar;
4632N/A
4632N/Aimport com.apple.laf.AquaMenuBarUI;
4632N/A
4632N/Aclass _AppMenuBarHandler {
4632N/A private static final int MENU_ABOUT = 1;
4632N/A private static final int MENU_PREFS = 2;
4632N/A
4632N/A private static native void nativeSetMenuState(final int menu, final boolean visible, final boolean enabled);
4632N/A private static native void nativeSetDefaultMenuBar(final long menuBarPeer);
4632N/A
4632N/A static final _AppMenuBarHandler instance = new _AppMenuBarHandler();
4632N/A static _AppMenuBarHandler getInstance() {
4632N/A return instance;
4632N/A }
4632N/A
4632N/A // callback from the native delegate -init function
4632N/A private static void initMenuStates(final boolean aboutMenuItemVisible, final boolean aboutMenuItemEnabled, final boolean prefsMenuItemVisible, final boolean prefsMenuItemEnabled) {
4632N/A synchronized (instance) {
4632N/A instance.aboutMenuItemVisible = aboutMenuItemVisible;
4632N/A instance.aboutMenuItemEnabled = aboutMenuItemEnabled;
4632N/A instance.prefsMenuItemVisible = prefsMenuItemVisible;
4632N/A instance.prefsMenuItemEnabled = prefsMenuItemEnabled;
4632N/A }
4632N/A }
4632N/A
4632N/A _AppMenuBarHandler() { }
4632N/A
4632N/A boolean aboutMenuItemVisible;
4632N/A boolean aboutMenuItemEnabled;
4632N/A
4632N/A boolean prefsMenuItemVisible;
4632N/A boolean prefsMenuItemEnabled;
4632N/A boolean prefsMenuItemExplicitlySet;
4632N/A
4632N/A void setDefaultMenuBar(final JMenuBar menuBar) {
4632N/A installDefaultMenuBar(menuBar);
4632N/A
4632N/A // scan the current frames, and see if any are foreground
4632N/A final Frame[] frames = Frame.getFrames();
4632N/A for (final Frame frame : frames) {
4632N/A if (frame.isVisible() && !isFrameMinimized(frame)) return;
4632N/A }
4632N/A
4632N/A // if we have no foreground frames, then we have to "kick" the menubar
4632N/A final JFrame pingFrame = new JFrame();
4632N/A pingFrame.getRootPane().putClientProperty("Window.alpha", new Float(0.0f));
4632N/A pingFrame.setVisible(true);
4632N/A pingFrame.toFront();
4632N/A pingFrame.setVisible(false);
4632N/A pingFrame.dispose();
4632N/A }
4632N/A
4632N/A static boolean isFrameMinimized(final Frame frame) {
4632N/A return (frame.getExtendedState() & Frame.ICONIFIED) != 0;
4632N/A }
4632N/A
4632N/A @SuppressWarnings("deprecation")
4632N/A static void installDefaultMenuBar(final JMenuBar menuBar) {
4632N/A if (menuBar == null) {
4632N/A // intentionally clearing the default menu
4632N/A nativeSetDefaultMenuBar(0);
4632N/A return;
4632N/A }
4632N/A
4632N/A final MenuBarUI ui = menuBar.getUI();
4632N/A if (!(ui instanceof AquaMenuBarUI)) {
4632N/A // Aqua was not installed
4632N/A throw new IllegalStateException("Application.setDefaultMenuBar() only works with the Aqua Look and Feel");
4632N/A }
4632N/A/* TODO: disabled until ScreenMenuBar is working
4632N/A
4632N/A final AquaMenuBarUI aquaUI = (AquaMenuBarUI)ui;
4632N/A final ScreenMenuBar screenMenuBar = aquaUI.getScreenMenuBar();
4632N/A if (screenMenuBar == null) {
4632N/A // Aqua is installed, but we aren't using the screen menu bar
4632N/A throw new IllegalStateException("Application.setDefaultMenuBar() only works if apple.laf.useScreenMenuBar=true");
4632N/A }
4632N/A
4632N/A screenMenuBar.addNotify();
4632N/A final MenuComponentPeer peer = screenMenuBar.getPeer();
4632N/A if (!(peer instanceof CMenuBar)) {
4632N/A // such a thing should not be possible
4632N/A throw new IllegalStateException("Unable to determine native menu bar from provided JMenuBar");
4632N/A }
4632N/A
4632N/A // grab the pointer to the CMenuBar, and retain it in native
4632N/A nativeSetDefaultMenuBar(((CMenuBar)peer).getNativeMenuBarPeer());
4632N/A*/
4632N/A }
4632N/A
4632N/A void setAboutMenuItemVisible(final boolean present) {
4632N/A synchronized (this) {
4632N/A if (aboutMenuItemVisible == present) return;
4632N/A aboutMenuItemVisible = present;
4632N/A }
4632N/A
4632N/A nativeSetMenuState(MENU_ABOUT, aboutMenuItemVisible, aboutMenuItemEnabled);
4632N/A }
4632N/A
4632N/A void setPreferencesMenuItemVisible(final boolean present) {
4632N/A synchronized (this) {
4632N/A prefsMenuItemExplicitlySet = true;
4632N/A if (prefsMenuItemVisible == present) return;
4632N/A prefsMenuItemVisible = present;
4632N/A }
4632N/A nativeSetMenuState(MENU_PREFS, prefsMenuItemVisible, prefsMenuItemEnabled);
4632N/A }
4632N/A
4632N/A void setAboutMenuItemEnabled(final boolean enable) {
4632N/A synchronized (this) {
4632N/A if (aboutMenuItemEnabled == enable) return;
4632N/A aboutMenuItemEnabled = enable;
4632N/A }
4632N/A nativeSetMenuState(MENU_ABOUT, aboutMenuItemVisible, aboutMenuItemEnabled);
4632N/A }
4632N/A
4632N/A void setPreferencesMenuItemEnabled(final boolean enable) {
4632N/A synchronized (this) {
4632N/A prefsMenuItemExplicitlySet = true;
4632N/A if (prefsMenuItemEnabled == enable) return;
4632N/A prefsMenuItemEnabled = enable;
4632N/A }
4632N/A nativeSetMenuState(MENU_PREFS, prefsMenuItemVisible, prefsMenuItemEnabled);
4632N/A }
4632N/A
4632N/A boolean isAboutMenuItemVisible() {
4632N/A return aboutMenuItemVisible;
4632N/A }
4632N/A
4632N/A boolean isPreferencesMenuItemVisible() {
4632N/A return prefsMenuItemVisible;
4632N/A }
4632N/A
4632N/A boolean isAboutMenuItemEnabled() {
4632N/A return aboutMenuItemEnabled;
4632N/A }
4632N/A
4632N/A boolean isPreferencesMenuItemEnabled() {
4632N/A return prefsMenuItemEnabled;
4632N/A }
4632N/A}