src/java.desktop/macosx/classes/com/apple/eawt/_AppMenuBarHandler.java

Print this page


   1 /*
   2  * Copyright (c) 2011, 2013, 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.eawt;
  27 
  28 import java.awt.Frame;
  29 import java.awt.peer.MenuComponentPeer;
  30 
  31 import javax.swing.*;
  32 import javax.swing.plaf.MenuBarUI;
  33 
  34 import com.apple.laf.ScreenMenuBar;

  35 import sun.lwawt.macosx.CMenuBar;
  36 
  37 import com.apple.laf.AquaMenuBarUI;
  38 
  39 class _AppMenuBarHandler {
  40     private static final int MENU_ABOUT = 1;
  41     private static final int MENU_PREFS = 2;
  42 
  43     private static native void nativeSetMenuState(final int menu, final boolean visible, final boolean enabled);
  44     private static native void nativeSetDefaultMenuBar(final long menuBarPeer);
  45 
  46     static final _AppMenuBarHandler instance = new _AppMenuBarHandler();
  47     static _AppMenuBarHandler getInstance() {
  48         return instance;
  49     }
  50 
  51     // callback from the native delegate -init function
  52     private static void initMenuStates(final boolean aboutMenuItemVisible, final boolean aboutMenuItemEnabled, final boolean prefsMenuItemVisible, final boolean prefsMenuItemEnabled) {



  53         synchronized (instance) {
  54             instance.aboutMenuItemVisible = aboutMenuItemVisible;
  55             instance.aboutMenuItemEnabled = aboutMenuItemEnabled;
  56             instance.prefsMenuItemVisible = prefsMenuItemVisible;
  57             instance.prefsMenuItemEnabled = prefsMenuItemEnabled;
  58         }
  59     }
  60 
  61     _AppMenuBarHandler() { }
  62 
  63     boolean aboutMenuItemVisible;
  64     boolean aboutMenuItemEnabled;
  65 
  66     boolean prefsMenuItemVisible;
  67     boolean prefsMenuItemEnabled;
  68     boolean prefsMenuItemExplicitlySet;
  69 
  70     void setDefaultMenuBar(final JMenuBar menuBar) {
  71         installDefaultMenuBar(menuBar);
  72 


  75         for (final Frame frame : frames) {
  76             if (frame.isVisible() && !isFrameMinimized(frame)) {
  77                 return;
  78             }
  79         }
  80 
  81         // if we have no foreground frames, then we have to "kick" the menubar
  82         final JFrame pingFrame = new JFrame();
  83         pingFrame.getRootPane().putClientProperty("Window.alpha", new Float(0.0f));
  84         pingFrame.setUndecorated(true);
  85         pingFrame.setVisible(true);
  86         pingFrame.toFront();
  87         pingFrame.setVisible(false);
  88         pingFrame.dispose();
  89     }
  90 
  91     static boolean isFrameMinimized(final Frame frame) {
  92         return (frame.getExtendedState() & Frame.ICONIFIED) != 0;
  93     }
  94 
  95     @SuppressWarnings("deprecation")
  96     static void installDefaultMenuBar(final JMenuBar menuBar) {
  97         if (menuBar == null) {
  98             // intentionally clearing the default menu
  99             nativeSetDefaultMenuBar(0);
 100             return;
 101         }
 102 
 103         final MenuBarUI ui = menuBar.getUI();
 104         if (!(ui instanceof AquaMenuBarUI)) {
 105             // Aqua was not installed
 106             throw new IllegalStateException("Application.setDefaultMenuBar() only works with the Aqua Look and Feel");
 107         }
 108 
 109         final AquaMenuBarUI aquaUI = (AquaMenuBarUI)ui;
 110         final ScreenMenuBar screenMenuBar = aquaUI.getScreenMenuBar();
 111         if (screenMenuBar == null) {
 112             // Aqua is installed, but we aren't using the screen menu bar
 113             throw new IllegalStateException("Application.setDefaultMenuBar() only works if apple.laf.useScreenMenuBar=true");
 114         }
 115 
 116         screenMenuBar.addNotify();
 117         final MenuComponentPeer peer = screenMenuBar.getPeer();
 118         if (!(peer instanceof CMenuBar)) {
 119             // such a thing should not be possible
 120             throw new IllegalStateException("Unable to determine native menu bar from provided JMenuBar");
 121         }
 122 
 123         // grab the pointer to the CMenuBar, and retain it in native
 124         nativeSetDefaultMenuBar(((CMenuBar)peer).getModel());
 125     }
 126 
 127     void setAboutMenuItemVisible(final boolean present) {
 128         synchronized (this) {
 129             if (aboutMenuItemVisible == present) return;
 130             aboutMenuItemVisible = present;
 131         }
 132 
 133         nativeSetMenuState(MENU_ABOUT, aboutMenuItemVisible, aboutMenuItemEnabled);
 134     }
 135 
 136     void setPreferencesMenuItemVisible(final boolean present) {
 137         synchronized (this) {


   1 /*
   2  * Copyright (c) 2011, 2015, 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.eawt;
  27 
  28 import java.awt.Frame;
  29 import java.awt.peer.MenuComponentPeer;
  30 
  31 import javax.swing.*;
  32 import javax.swing.plaf.MenuBarUI;
  33 
  34 import com.apple.laf.ScreenMenuBar;
  35 import sun.awt.AWTAccessor;
  36 import sun.lwawt.macosx.CMenuBar;
  37 
  38 import com.apple.laf.AquaMenuBarUI;
  39 
  40 class _AppMenuBarHandler {
  41     private static final int MENU_ABOUT = 1;
  42     private static final int MENU_PREFS = 2;
  43 
  44     private static native void nativeSetMenuState(final int menu, final boolean visible, final boolean enabled);
  45     private static native void nativeSetDefaultMenuBar(final long menuBarPeer);
  46 
  47     static final _AppMenuBarHandler instance = new _AppMenuBarHandler();
  48     static _AppMenuBarHandler getInstance() {
  49         return instance;
  50     }
  51 
  52     // callback from the native delegate -init function
  53     private static void initMenuStates(final boolean aboutMenuItemVisible,
  54                                        final boolean aboutMenuItemEnabled,
  55                                        final boolean prefsMenuItemVisible,
  56                                        final boolean prefsMenuItemEnabled) {
  57         synchronized (instance) {
  58             instance.aboutMenuItemVisible = aboutMenuItemVisible;
  59             instance.aboutMenuItemEnabled = aboutMenuItemEnabled;
  60             instance.prefsMenuItemVisible = prefsMenuItemVisible;
  61             instance.prefsMenuItemEnabled = prefsMenuItemEnabled;
  62         }
  63     }
  64 
  65     _AppMenuBarHandler() { }
  66 
  67     boolean aboutMenuItemVisible;
  68     boolean aboutMenuItemEnabled;
  69 
  70     boolean prefsMenuItemVisible;
  71     boolean prefsMenuItemEnabled;
  72     boolean prefsMenuItemExplicitlySet;
  73 
  74     void setDefaultMenuBar(final JMenuBar menuBar) {
  75         installDefaultMenuBar(menuBar);
  76 


  79         for (final Frame frame : frames) {
  80             if (frame.isVisible() && !isFrameMinimized(frame)) {
  81                 return;
  82             }
  83         }
  84 
  85         // if we have no foreground frames, then we have to "kick" the menubar
  86         final JFrame pingFrame = new JFrame();
  87         pingFrame.getRootPane().putClientProperty("Window.alpha", new Float(0.0f));
  88         pingFrame.setUndecorated(true);
  89         pingFrame.setVisible(true);
  90         pingFrame.toFront();
  91         pingFrame.setVisible(false);
  92         pingFrame.dispose();
  93     }
  94 
  95     static boolean isFrameMinimized(final Frame frame) {
  96         return (frame.getExtendedState() & Frame.ICONIFIED) != 0;
  97     }
  98 

  99     static void installDefaultMenuBar(final JMenuBar menuBar) {
 100         if (menuBar == null) {
 101             // intentionally clearing the default menu
 102             nativeSetDefaultMenuBar(0);
 103             return;
 104         }
 105 
 106         final MenuBarUI ui = menuBar.getUI();
 107         if (!(ui instanceof AquaMenuBarUI)) {
 108             // Aqua was not installed
 109             throw new IllegalStateException("Application.setDefaultMenuBar() only works with the Aqua Look and Feel");
 110         }
 111 
 112         final AquaMenuBarUI aquaUI = (AquaMenuBarUI)ui;
 113         final ScreenMenuBar screenMenuBar = aquaUI.getScreenMenuBar();
 114         if (screenMenuBar == null) {
 115             // Aqua is installed, but we aren't using the screen menu bar
 116             throw new IllegalStateException("Application.setDefaultMenuBar() only works if apple.laf.useScreenMenuBar=true");
 117         }
 118 
 119         screenMenuBar.addNotify();
 120         final Object peer = AWTAccessor.getMenuComponentAccessor().getPeer(screenMenuBar);
 121         if (!(peer instanceof CMenuBar)) {
 122             // such a thing should not be possible
 123             throw new IllegalStateException("Unable to determine native menu bar from provided JMenuBar");
 124         }
 125 
 126         // grab the pointer to the CMenuBar, and retain it in native
 127         nativeSetDefaultMenuBar(((CMenuBar)peer).getModel());
 128     }
 129 
 130     void setAboutMenuItemVisible(final boolean present) {
 131         synchronized (this) {
 132             if (aboutMenuItemVisible == present) return;
 133             aboutMenuItemVisible = present;
 134         }
 135 
 136         nativeSetMenuState(MENU_ABOUT, aboutMenuItemVisible, aboutMenuItemEnabled);
 137     }
 138 
 139     void setPreferencesMenuItemVisible(final boolean present) {
 140         synchronized (this) {