0N/A/*
2362N/A * Copyright (c) 1997, 2003, 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 javax.swing.plaf.basic;
0N/A
0N/Aimport sun.swing.DefaultLookup;
0N/Aimport sun.swing.UIAction;
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.event.*;
0N/Aimport java.awt.Color;
0N/Aimport java.awt.Component;
0N/Aimport java.awt.Container;
0N/Aimport java.awt.Dimension;
0N/Aimport java.awt.Graphics;
0N/Aimport java.awt.Insets;
0N/Aimport java.awt.Point;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.event.*;
0N/Aimport java.beans.PropertyChangeEvent;
0N/Aimport java.beans.PropertyChangeListener;
0N/A
0N/Aimport javax.swing.border.*;
0N/Aimport javax.swing.plaf.*;
0N/A
0N/A
0N/A/**
0N/A * A default L&F implementation of MenuBarUI. This implementation
0N/A * is a "combined" view/controller.
0N/A *
0N/A * @author Georges Saab
0N/A * @author David Karlton
0N/A * @author Arnaud Weber
0N/A */
0N/Apublic class BasicMenuBarUI extends MenuBarUI {
0N/A protected JMenuBar menuBar = null;
0N/A protected ContainerListener containerListener;
0N/A protected ChangeListener changeListener;
0N/A private Handler handler;
0N/A
0N/A public static ComponentUI createUI(JComponent x) {
0N/A return new BasicMenuBarUI();
0N/A }
0N/A
0N/A static void loadActionMap(LazyActionMap map) {
0N/A map.put(new Actions(Actions.TAKE_FOCUS));
0N/A }
0N/A
0N/A public void installUI(JComponent c) {
0N/A menuBar = (JMenuBar) c;
0N/A
0N/A installDefaults();
0N/A installListeners();
0N/A installKeyboardActions();
0N/A
0N/A }
0N/A
0N/A protected void installDefaults() {
0N/A if (menuBar.getLayout() == null ||
0N/A menuBar.getLayout() instanceof UIResource) {
0N/A menuBar.setLayout(new DefaultMenuLayout(menuBar,BoxLayout.LINE_AXIS));
0N/A }
0N/A
0N/A LookAndFeel.installProperty(menuBar, "opaque", Boolean.TRUE);
0N/A LookAndFeel.installBorder(menuBar,"MenuBar.border");
0N/A LookAndFeel.installColorsAndFont(menuBar,
0N/A "MenuBar.background",
0N/A "MenuBar.foreground",
0N/A "MenuBar.font");
0N/A }
0N/A
0N/A protected void installListeners() {
0N/A containerListener = createContainerListener();
0N/A changeListener = createChangeListener();
0N/A
0N/A for (int i = 0; i < menuBar.getMenuCount(); i++) {
0N/A JMenu menu = menuBar.getMenu(i);
0N/A if (menu!=null)
0N/A menu.getModel().addChangeListener(changeListener);
0N/A }
0N/A menuBar.addContainerListener(containerListener);
0N/A }
0N/A
0N/A protected void installKeyboardActions() {
0N/A InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
0N/A
0N/A SwingUtilities.replaceUIInputMap(menuBar,
0N/A JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
0N/A
0N/A LazyActionMap.installLazyActionMap(menuBar, BasicMenuBarUI.class,
0N/A "MenuBar.actionMap");
0N/A }
0N/A
0N/A InputMap getInputMap(int condition) {
0N/A if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
0N/A Object[] bindings = (Object[])DefaultLookup.get
0N/A (menuBar, this, "MenuBar.windowBindings");
0N/A if (bindings != null) {
0N/A return LookAndFeel.makeComponentInputMap(menuBar, bindings);
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A public void uninstallUI(JComponent c) {
0N/A uninstallDefaults();
0N/A uninstallListeners();
0N/A uninstallKeyboardActions();
0N/A
0N/A menuBar = null;
0N/A }
0N/A
0N/A protected void uninstallDefaults() {
0N/A if (menuBar!=null) {
0N/A LookAndFeel.uninstallBorder(menuBar);
0N/A }
0N/A }
0N/A
0N/A protected void uninstallListeners() {
0N/A menuBar.removeContainerListener(containerListener);
0N/A
0N/A for (int i = 0; i < menuBar.getMenuCount(); i++) {
0N/A JMenu menu = menuBar.getMenu(i);
0N/A if (menu !=null)
0N/A menu.getModel().removeChangeListener(changeListener);
0N/A }
0N/A
0N/A containerListener = null;
0N/A changeListener = null;
0N/A handler = null;
0N/A }
0N/A
0N/A protected void uninstallKeyboardActions() {
0N/A SwingUtilities.replaceUIInputMap(menuBar, JComponent.
0N/A WHEN_IN_FOCUSED_WINDOW, null);
0N/A SwingUtilities.replaceUIActionMap(menuBar, null);
0N/A }
0N/A
0N/A protected ContainerListener createContainerListener() {
0N/A return getHandler();
0N/A }
0N/A
0N/A protected ChangeListener createChangeListener() {
0N/A return getHandler();
0N/A }
0N/A
0N/A private Handler getHandler() {
0N/A if (handler == null) {
0N/A handler = new Handler();
0N/A }
0N/A return handler;
0N/A }
0N/A
0N/A
0N/A public Dimension getMinimumSize(JComponent c) {
0N/A return null;
0N/A }
0N/A
0N/A public Dimension getMaximumSize(JComponent c) {
0N/A return null;
0N/A }
0N/A
0N/A private class Handler implements ChangeListener, ContainerListener {
0N/A //
0N/A // ChangeListener
0N/A //
0N/A public void stateChanged(ChangeEvent e) {
0N/A int i,c;
0N/A for(i=0,c = menuBar.getMenuCount() ; i < c ; i++) {
0N/A JMenu menu = menuBar.getMenu(i);
0N/A if(menu !=null && menu.isSelected()) {
0N/A menuBar.getSelectionModel().setSelectedIndex(i);
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A //
0N/A // ContainerListener
0N/A //
0N/A public void componentAdded(ContainerEvent e) {
0N/A Component c = e.getChild();
0N/A if (c instanceof JMenu)
0N/A ((JMenu)c).getModel().addChangeListener(changeListener);
0N/A }
0N/A public void componentRemoved(ContainerEvent e) {
0N/A Component c = e.getChild();
0N/A if (c instanceof JMenu)
0N/A ((JMenu)c).getModel().removeChangeListener(changeListener);
0N/A }
0N/A }
0N/A
0N/A
0N/A private static class Actions extends UIAction {
0N/A private static final String TAKE_FOCUS = "takeFocus";
0N/A
0N/A Actions(String key) {
0N/A super(key);
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A // TAKE_FOCUS
0N/A JMenuBar menuBar = (JMenuBar)e.getSource();
0N/A MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager();
0N/A MenuElement me[];
0N/A MenuElement subElements[];
0N/A JMenu menu = menuBar.getMenu(0);
0N/A if (menu!=null) {
0N/A me = new MenuElement[3];
0N/A me[0] = (MenuElement) menuBar;
0N/A me[1] = (MenuElement) menu;
0N/A me[2] = (MenuElement) menu.getPopupMenu();
0N/A defaultManager.setSelectedPath(me);
0N/A }
0N/A }
0N/A }
0N/A}