src/macosx/classes/com/apple/laf/AquaMenuBarUI.java

Print this page




   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     }


 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 }


   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 import java.security.AccessController;
  30 
  31 import javax.swing.*;
  32 import javax.swing.plaf.ComponentUI;
  33 import javax.swing.plaf.basic.BasicMenuBarUI;
  34 
  35 import sun.lwawt.macosx.LWCToolkit;
  36 import sun.security.action.GetBooleanAction;
  37 import sun.security.action.GetPropertyAction;
  38 
  39 // MenuBar implementation for Mac L&F
  40 public class AquaMenuBarUI extends BasicMenuBarUI implements ScreenMenuBarProvider {
  41     // Utilities
  42     public void uninstallUI(final JComponent c) {
  43         if (fScreenMenuBar != null) {
  44             final JFrame frame = (JFrame)(c.getTopLevelAncestor());
  45             if (frame.getMenuBar() == fScreenMenuBar) {
  46                 frame.setMenuBar((MenuBar)null);
  47             }
  48             fScreenMenuBar = null;
  49         }
  50         super.uninstallUI(c);
  51     }
  52 
  53     // Create PLAF
  54     public static ComponentUI createUI(final JComponent c) {
  55         return new AquaMenuBarUI();
  56     }


 117     // Other JMenuBars appear where the programmer puts them - top of window or elsewhere
 118     public static final boolean isScreenMenuBar(final JMenuBar c) {
 119         final javax.swing.plaf.ComponentUI ui = c.getUI();
 120         if (ui instanceof AquaMenuBarUI) {
 121             if (!((AquaMenuBarUI)ui).useScreenMenuBar) return false;
 122 
 123             final Component parent = c.getTopLevelAncestor();
 124             if (parent instanceof JFrame) {
 125                 final MenuBar mb = ((JFrame)parent).getMenuBar();
 126                 final boolean thisIsTheJMenuBar = (((JFrame)parent).getJMenuBar() == c);
 127                 if (mb == null) return thisIsTheJMenuBar;
 128                 return (mb instanceof ScreenMenuBar && thisIsTheJMenuBar);
 129             }
 130         }
 131         return false;
 132     }
 133 
 134     ScreenMenuBar fScreenMenuBar;
 135     boolean useScreenMenuBar = getScreenMenuBarProperty();
 136 




 137     static boolean getScreenMenuBarProperty() {
 138         // Do not allow AWT to set the screen menu bar if it's embedded in another UI toolkit
 139         if (LWCToolkit.isEmbedded()) return false;
 140         if (AccessController.doPrivileged(
 141                 new GetBooleanAction(AquaLookAndFeel.sPropertyPrefix + "useScreenMenuBar"))) {
 142             return true;








 143         }
 144         if (AccessController.doPrivileged(
 145                 new GetBooleanAction(AquaLookAndFeel.sOldPropertyPrefix + "useScreenMenuBar"))) {
 146                 System.err.println(AquaLookAndFeel.sOldPropertyPrefix +
 147                         "useScreenMenuBar has been deprecated. Please switch to " +
 148                         AquaLookAndFeel.sPropertyPrefix + "useScreenMenuBar");
 149                 return true;
 150         }
 151         return false;


 152     }
 153 }