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

Print this page




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


  55             instance.prefsMenuItemVisible = prefsMenuItemVisible;
  56             instance.prefsMenuItemEnabled = prefsMenuItemEnabled;
  57         }
  58     }
  59 
  60     _AppMenuBarHandler() { }
  61 
  62     boolean aboutMenuItemVisible;
  63     boolean aboutMenuItemEnabled;
  64 
  65     boolean prefsMenuItemVisible;
  66     boolean prefsMenuItemEnabled;
  67     boolean prefsMenuItemExplicitlySet;
  68 
  69     void setDefaultMenuBar(final JMenuBar menuBar) {
  70         installDefaultMenuBar(menuBar);
  71 
  72         // scan the current frames, and see if any are foreground
  73         final Frame[] frames = Frame.getFrames();
  74         for (final Frame frame : frames) {
  75             if (frame.isVisible() && !isFrameMinimized(frame)) return;


  76         }
  77 
  78         // if we have no foreground frames, then we have to "kick" the menubar
  79         final JFrame pingFrame = new JFrame();
  80         pingFrame.getRootPane().putClientProperty("Window.alpha", new Float(0.0f));

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




  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;


  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 
  73         // scan the current frames, and see if any are foreground
  74         final Frame[] frames = Frame.getFrames();
  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) {
 138             prefsMenuItemExplicitlySet = true;
 139             if (prefsMenuItemVisible == present) return;
 140             prefsMenuItemVisible = present;
 141         }
 142         nativeSetMenuState(MENU_PREFS, prefsMenuItemVisible, prefsMenuItemEnabled);
 143     }
 144