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.laf;
  27 
  28 import java.awt.*;
  29 
  30 import javax.swing.*;
  31 import javax.swing.plaf.ComponentUI;
  32 import javax.swing.plaf.basic.BasicMenuBarUI;
  33 
  34 import sun.security.action.GetPropertyAction;
  35 
  36 // MenuBar implementation for Mac L&F
  37 public class AquaMenuBarUI extends BasicMenuBarUI implements ScreenMenuBarProvider {
  38     // Utilities
  39     public void uninstallUI(final JComponent c) {
  40         if (fScreenMenuBar != null) {
  41             final JFrame frame = (JFrame)(c.getTopLevelAncestor());
  42             if (frame.getMenuBar() == fScreenMenuBar) {
  43                 frame.setMenuBar((MenuBar)null);
  44             }
  45             fScreenMenuBar = null;
  46         }
  47         super.uninstallUI(c);
  48     }
  49 
  50     // Create PLAF
  51     public static ComponentUI createUI(final JComponent c) {
  52         return new AquaMenuBarUI();
  53     }
  54 
  55     // [3320390] -- If the screen menu bar is in use, don't register keyboard actions that
  56     // show the menus when F10 is pressed.
  57     protected void installKeyboardActions() {
  58         if (!useScreenMenuBar) {
  59             super.installKeyboardActions();
  60         }
  61     }
  62 
  63     protected void uninstallKeyboardActions() {
  64         if (!useScreenMenuBar) {
  65             super.uninstallKeyboardActions();
  66         }
  67     }
  68 
  69     // Paint Methods
  70     public void paint(final Graphics g, final JComponent c) {
  71         AquaMenuPainter.instance().paintMenuBarBackground(g, c.getWidth(), c.getHeight(), c);
  72     }
  73 
  74     public Dimension getPreferredSize(final JComponent c) {
  75         if (isScreenMenuBar((JMenuBar)c)) {
  76             if (setScreenMenuBar((JFrame)(c.getTopLevelAncestor()))) {
  77                 return new Dimension(0, 0);
  78             }
  79         }
  80         return null;
  81     }
  82 
  83     void clearScreenMenuBar(final JFrame frame) {
  84         if (useScreenMenuBar) {
  85             frame.setMenuBar(null);
  86         }
  87     }
  88 
  89     boolean setScreenMenuBar(final JFrame frame) {
  90         if (useScreenMenuBar) {
  91             try {
  92                 getScreenMenuBar();
  93             } catch(final Throwable t) {
  94                 return false;
  95             }
  96 
  97             frame.setMenuBar(fScreenMenuBar);
  98         }
  99 
 100         return true;
 101     }
 102 
 103     public ScreenMenuBar getScreenMenuBar() {
 104         // Lazy init of member variables means we should use a synchronized block.
 105         synchronized(this) {
 106             if (fScreenMenuBar == null) fScreenMenuBar = new ScreenMenuBar(this.menuBar);
 107         }
 108         return fScreenMenuBar;
 109     }
 110 
 111     // JMenuBars are in frame unless we're using ScreenMenuBars *and* it's
 112     //   been set by JFrame.setJMenuBar
 113     // unless the JFrame has a normal java.awt.MenuBar (it's possible!)
 114     // Other JMenuBars appear where the programmer puts them - top of window or elsewhere
 115     public static final boolean isScreenMenuBar(final JMenuBar c) {
 116         final javax.swing.plaf.ComponentUI ui = c.getUI();
 117         if (ui instanceof AquaMenuBarUI) {
 118             if (!((AquaMenuBarUI)ui).useScreenMenuBar) return false;
 119 
 120             final Component parent = c.getTopLevelAncestor();
 121             if (parent instanceof JFrame) {
 122                 final MenuBar mb = ((JFrame)parent).getMenuBar();
 123                 final boolean thisIsTheJMenuBar = (((JFrame)parent).getJMenuBar() == c);
 124                 if (mb == null) return thisIsTheJMenuBar;
 125                 return (mb instanceof ScreenMenuBar && thisIsTheJMenuBar);
 126             }
 127         }
 128         return false;
 129     }
 130 
 131     ScreenMenuBar fScreenMenuBar;
 132     boolean useScreenMenuBar = getScreenMenuBarProperty();
 133 
 134     private static String getPrivSysProp(final String propName) {
 135         return java.security.AccessController.doPrivileged(new GetPropertyAction(propName));
 136     }
 137 
 138     static boolean getScreenMenuBarProperty() {
 139         final String props[] = new String[]{""};
 140 
 141         boolean useScreenMenuBar = false;
 142         try {
 143             props[0] = getPrivSysProp(AquaLookAndFeel.sPropertyPrefix + "useScreenMenuBar");
 144 
 145             if (props[0] != null && props[0].equals("true")) useScreenMenuBar = true;
 146             else {
 147                 props[0] = getPrivSysProp(AquaLookAndFeel.sOldPropertyPrefix + "useScreenMenuBar");
 148 
 149                 if (props[0] != null && props[0].equals("true")) {
 150                     System.err.println(AquaLookAndFeel.sOldPropertyPrefix + "useScreenMenuBar has been deprecated. Please switch to " + AquaLookAndFeel.sPropertyPrefix + "useScreenMenuBar");
 151                     useScreenMenuBar = true;
 152                 }
 153             }
 154         } catch(final Throwable t) { };
 155 
 156         return useScreenMenuBar;
 157     }
 158 }