jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicMenuBarUI.java
author weijun
Thu, 11 Dec 2014 15:19:12 +0800
changeset 28228 be83f404724d
parent 25859 3317bb8137f4
permissions -rw-r--r--
8067207: Replace concat String to append in StringBuilder parameters (client) Reviewed-by: serb Contributed-by: Otavio Santana <otaviojava@java.net>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.swing.UIAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
    49
 * A default L&amp;F implementation of MenuBarUI.  This implementation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * is a "combined" view/controller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Georges Saab
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author David Karlton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Arnaud Weber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class BasicMenuBarUI extends MenuBarUI  {
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    57
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    58
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    59
     * The instance of {@code JMenuBar}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    60
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    protected JMenuBar              menuBar = null;
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    62
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    63
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    64
     * The instance of {@code ContainerListener}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    65
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected ContainerListener     containerListener;
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    67
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    68
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    69
     * The instance of {@code ChangeListener}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    70
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected ChangeListener        changeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    74
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    75
     * Returns a new instance of {@code BasicMenuBarUI}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    76
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    77
     * @param x a component
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    78
     * @return a new instance of {@code BasicMenuBarUI}
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    79
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static ComponentUI createUI(JComponent x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return new BasicMenuBarUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        map.put(new Actions(Actions.TAKE_FOCUS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        menuBar = (JMenuBar) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    97
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    98
     * Installs default properties.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    99
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (menuBar.getLayout() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            menuBar.getLayout() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            menuBar.setLayout(new DefaultMenuLayout(menuBar,BoxLayout.LINE_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        LookAndFeel.installProperty(menuBar, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        LookAndFeel.installBorder(menuBar,"MenuBar.border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        LookAndFeel.installColorsAndFont(menuBar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                              "MenuBar.background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                              "MenuBar.foreground",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                              "MenuBar.font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   114
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   115
     * Registers listeners.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   116
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        containerListener = createContainerListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        changeListener = createChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        for (int i = 0; i < menuBar.getMenuCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            JMenu menu = menuBar.getMenu(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (menu!=null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                menu.getModel().addChangeListener(changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        menuBar.addContainerListener(containerListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   129
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   130
     * Registers keyboard actions.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   131
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        SwingUtilities.replaceUIInputMap(menuBar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                           JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        LazyActionMap.installLazyActionMap(menuBar, BasicMenuBarUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                           "MenuBar.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    InputMap getInputMap(int condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            Object[] bindings = (Object[])DefaultLookup.get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                (menuBar, this, "MenuBar.windowBindings");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (bindings != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                return LookAndFeel.makeComponentInputMap(menuBar, bindings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        menuBar = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   161
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   162
     * Uninstalls default properties.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   163
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (menuBar!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            LookAndFeel.uninstallBorder(menuBar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   170
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   171
     * Unregisters listeners.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   172
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        menuBar.removeContainerListener(containerListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        for (int i = 0; i < menuBar.getMenuCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            JMenu menu = menuBar.getMenu(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if (menu !=null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                menu.getModel().removeChangeListener(changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        containerListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        changeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   187
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   188
     * Unregisters keyboard actions.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   189
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    protected void uninstallKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        SwingUtilities.replaceUIInputMap(menuBar, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                       WHEN_IN_FOCUSED_WINDOW, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        SwingUtilities.replaceUIActionMap(menuBar, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   196
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   197
     * Returns an instance of {@code ContainerListener}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   198
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   199
     * @return an instance of {@code ContainerListener}
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   200
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    protected ContainerListener createContainerListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   205
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   206
     * Returns an instance of {@code ChangeListener}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   207
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   208
     * @return an instance of {@code ChangeListener}
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   209
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    protected ChangeListener createChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    private class Handler implements ChangeListener, ContainerListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // ChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            int i,c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            for(i=0,c = menuBar.getMenuCount() ; i < c ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                JMenu menu = menuBar.getMenu(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                if(menu !=null && menu.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    menuBar.getSelectionModel().setSelectedIndex(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // ContainerListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        public void componentAdded(ContainerEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            Component c = e.getChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (c instanceof JMenu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                ((JMenu)c).getModel().addChangeListener(changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        public void componentRemoved(ContainerEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            Component c = e.getChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if (c instanceof JMenu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                ((JMenu)c).getModel().removeChangeListener(changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        private static final String TAKE_FOCUS = "takeFocus";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        Actions(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            super(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            // TAKE_FOCUS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            JMenuBar menuBar = (JMenuBar)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            MenuElement me[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            MenuElement subElements[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            JMenu menu = menuBar.getMenu(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (menu!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    me = new MenuElement[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    me[0] = (MenuElement) menuBar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    me[1] = (MenuElement) menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    me[2] = (MenuElement) menu.getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    defaultManager.setSelectedPath(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
}