jdk/src/share/classes/javax/swing/plaf/synth/SynthMenuBarUI.java
author darcy
Sun, 23 Mar 2014 13:49:48 -0700
changeset 23697 e556a715949f
parent 23010 6dadb192ad81
child 25100 d527cc827d7d
permissions -rw-r--r--
8034169: Fix serial lint warnings in javax.swing Reviewed-by: alanb, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20168
diff changeset
     2
 * Copyright (c) 2002, 2013, 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: 4848
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: 4848
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: 4848
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4848
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4848
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.plaf.synth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.plaf.basic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
20168
137788883a22 8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents: 7668
diff changeset
    35
 * Provides the Synth L&F UI delegate for
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    36
 * {@link javax.swing.JMenuBar}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author Scott Violet
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    39
 * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    41
public class SynthMenuBarUI extends BasicMenuBarUI
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    42
                            implements PropertyChangeListener, SynthUI {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private SynthStyle style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    45
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    46
     * Creates a new UI object for the given component.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    47
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    48
     * @param x component to create UI object for
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    49
     * @return the UI object
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    50
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static ComponentUI createUI(JComponent x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return new SynthMenuBarUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    55
    /**
20168
137788883a22 8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents: 7668
diff changeset
    56
     * {@inheritDoc}
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    57
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    58
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (menuBar.getLayout() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            menuBar.getLayout() instanceof UIResource) {
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    62
            menuBar.setLayout(new SynthMenuLayout(menuBar,BoxLayout.LINE_AXIS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        updateStyle(menuBar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    67
    /**
20168
137788883a22 8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents: 7668
diff changeset
    68
     * {@inheritDoc}
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    69
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    70
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        super.installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        menuBar.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private void updateStyle(JMenuBar c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        SynthContext context = getContext(c, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        SynthStyle oldStyle = style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        style = SynthLookAndFeel.updateStyle(context, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (style != oldStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            if (oldStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    89
    /**
20168
137788883a22 8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents: 7668
diff changeset
    90
     * {@inheritDoc}
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    91
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    92
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        SynthContext context = getContext(menuBar, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        style.uninstallDefaults(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        style = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   101
    /**
20168
137788883a22 8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents: 7668
diff changeset
   102
     * {@inheritDoc}
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   103
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   104
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        super.uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        menuBar.removePropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   110
    /**
20168
137788883a22 8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents: 7668
diff changeset
   111
     * {@inheritDoc}
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   112
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   113
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public SynthContext getContext(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return getContext(c, getComponentState(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private SynthContext getContext(JComponent c, int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return SynthContext.getContext(SynthContext.class, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    SynthLookAndFeel.getRegion(c), style, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private int getComponentState(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return SynthLookAndFeel.getComponentState(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   127
    /**
4848
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   128
     * Notifies this UI delegate to repaint the specified component.
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   129
     * This method paints the component background, then calls
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   130
     * the {@link #paint(SynthContext,Graphics)} method.
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   131
     *
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   132
     * <p>In general, this method does not need to be overridden by subclasses.
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   133
     * All Look and Feel rendering code should reside in the {@code paint} method.
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   134
     *
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   135
     * @param g the {@code Graphics} object used for painting
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   136
     * @param c the component being painted
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   137
     * @see #paint(SynthContext,Graphics)
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   138
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   139
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public void update(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        SynthLookAndFeel.update(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        context.getPainter().paintMenuBarBackground(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                          g, 0, 0, c.getWidth(), c.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   150
    /**
4848
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   151
     * Paints the specified component according to the Look and Feel.
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   152
     * <p>This method is not used by Synth Look and Feel.
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   153
     * Painting is handled by the {@link #paint(SynthContext,Graphics)} method.
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   154
     *
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   155
     * @param g the {@code Graphics} object used for painting
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   156
     * @param c the component being painted
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   157
     * @see #paint(SynthContext,Graphics)
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   158
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   159
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   167
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   168
     * Paints the specified component. This implementation does nothing.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   169
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   170
     * @param context context for the component being painted
4848
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   171
     * @param g the {@code Graphics} object used for painting
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   172
     * @see #update(Graphics,JComponent)
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   173
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    protected void paint(SynthContext context, Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   177
    /**
20168
137788883a22 8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents: 7668
diff changeset
   178
     * {@inheritDoc}
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   179
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   180
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public void paintBorder(SynthContext context, Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                            int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        context.getPainter().paintMenuBarBorder(context, g, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   186
    /**
20168
137788883a22 8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents: 7668
diff changeset
   187
     * {@inheritDoc}
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   188
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   189
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (SynthLookAndFeel.shouldUpdateStyle(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            updateStyle((JMenuBar)e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}