0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.java.swing.plaf.windows;
0N/A
0N/Aimport javax.swing.plaf.basic.*;
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.plaf.ActionMapUIResource;
0N/Aimport javax.swing.plaf.ComponentUI;
0N/Aimport java.awt.event.ActionEvent;
0N/Aimport java.awt.event.HierarchyEvent;
0N/Aimport java.awt.event.HierarchyListener;
0N/Aimport java.awt.event.WindowAdapter;
0N/Aimport java.awt.event.WindowEvent;
0N/Aimport java.awt.event.WindowListener;
0N/Aimport java.awt.event.WindowStateListener;
0N/A
0N/Aimport java.awt.*;
0N/A
0N/Aimport com.sun.java.swing.plaf.windows.TMSchema.*;
0N/Aimport com.sun.java.swing.plaf.windows.XPStyle.*;
0N/A
0N/A/**
0N/A * Windows rendition of the component.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is appropriate
0N/A * for short term storage or RMI between applications running the same
0N/A * version of Swing. A future release of Swing will provide support for
0N/A * long term persistence.
0N/A */
0N/Apublic class WindowsMenuBarUI extends BasicMenuBarUI
0N/A{
0N/A /* to be accessed on the EDT only */
0N/A private WindowListener windowListener = null;
0N/A private HierarchyListener hierarchyListener = null;
0N/A private Window window = null;
0N/A
0N/A public static ComponentUI createUI(JComponent x) {
0N/A return new WindowsMenuBarUI();
0N/A }
0N/A
0N/A @Override
0N/A protected void uninstallListeners() {
0N/A uninstallWindowListener();
0N/A if (hierarchyListener != null) {
0N/A menuBar.removeHierarchyListener(hierarchyListener);
0N/A hierarchyListener = null;
0N/A }
0N/A super.uninstallListeners();
0N/A }
0N/A private void installWindowListener() {
0N/A if (windowListener == null) {
0N/A Component component = menuBar.getTopLevelAncestor();
0N/A if (component instanceof Window) {
0N/A window = (Window) component;
0N/A windowListener = new WindowAdapter() {
0N/A @Override
0N/A public void windowActivated(WindowEvent e) {
0N/A menuBar.repaint();
0N/A }
0N/A @Override
0N/A public void windowDeactivated(WindowEvent e) {
0N/A menuBar.repaint();
0N/A }
0N/A };
0N/A ((Window) component).addWindowListener(windowListener);
0N/A }
0N/A }
0N/A }
0N/A private void uninstallWindowListener() {
0N/A if (windowListener != null && window != null) {
0N/A window.removeWindowListener(windowListener);
0N/A }
0N/A window = null;
0N/A windowListener = null;
0N/A }
0N/A @Override
0N/A protected void installListeners() {
0N/A if (WindowsLookAndFeel.isOnVista()) {
0N/A installWindowListener();
0N/A hierarchyListener =
0N/A new HierarchyListener() {
0N/A public void hierarchyChanged(HierarchyEvent e) {
0N/A if ((e.getChangeFlags()
0N/A & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
0N/A if (menuBar.isDisplayable()) {
0N/A installWindowListener();
0N/A } else {
0N/A uninstallWindowListener();
0N/A }
0N/A }
0N/A }
0N/A };
0N/A menuBar.addHierarchyListener(hierarchyListener);
0N/A }
0N/A super.installListeners();
0N/A }
0N/A
0N/A protected void installKeyboardActions() {
0N/A super.installKeyboardActions();
0N/A ActionMap map = SwingUtilities.getUIActionMap(menuBar);
0N/A if (map == null) {
0N/A map = new ActionMapUIResource();
0N/A SwingUtilities.replaceUIActionMap(menuBar, map);
0N/A }
0N/A map.put("takeFocus", new TakeFocus());
0N/A }
0N/A
0N/A /**
0N/A * Action that activates the menu (e.g. when F10 is pressed).
0N/A * Unlike BasicMenuBarUI.TakeFocus, this Action will not show menu popup.
0N/A */
0N/A private static class TakeFocus extends AbstractAction {
0N/A public void actionPerformed(ActionEvent e) {
0N/A JMenuBar menuBar = (JMenuBar)e.getSource();
0N/A JMenu menu = menuBar.getMenu(0);
0N/A if (menu != null) {
0N/A MenuSelectionManager msm =
0N/A MenuSelectionManager.defaultManager();
0N/A MenuElement path[] = new MenuElement[2];
0N/A path[0] = (MenuElement)menuBar;
0N/A path[1] = (MenuElement)menu;
0N/A msm.setSelectedPath(path);
0N/A
0N/A // show mnemonics
0N/A WindowsLookAndFeel.setMnemonicHidden(false);
0N/A WindowsLookAndFeel.repaintRootPane(menuBar);
0N/A }
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public void paint(Graphics g, JComponent c) {
0N/A if (WindowsMenuItemUI.isVistaPainting()) {
0N/A XPStyle xp = XPStyle.getXP();
0N/A Skin skin;
0N/A skin = xp.getSkin(c, Part.MP_BARBACKGROUND);
0N/A int width = c.getWidth();
0N/A int height = c.getHeight();
0N/A State state = isActive(c) ? State.ACTIVE : State.INACTIVE;
0N/A skin.paintSkin(g, 0, 0, width, height, state);
0N/A } else {
0N/A super.paint(g, c);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Checks if component belongs to an active window.
0N/A * @param c component to check
0N/A * @return true if component belongs to an active window
0N/A */
0N/A static boolean isActive(JComponent c) {
0N/A JRootPane rootPane = c.getRootPane();
0N/A if (rootPane != null) {
0N/A Component component = rootPane.getParent();
0N/A if (component instanceof Window) {
0N/A return ((Window) component).isActive();
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A}