jdk/src/share/classes/javax/swing/plaf/metal/MetalToolBarUI.java
author yan
Mon, 30 Jun 2014 16:32:36 +0400
changeset 25178 dbab904451e9
parent 23715 54ae9dd9df73
child 25565 ce603b34c98d
permissions -rw-r--r--
8046434: Fix doclint warnings from javax.swing.plaf.metal package Reviewed-by: alexsch Contributed-by: Andrei Eremeev <andrei.eremeev@oracle.com>
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: 21278
diff changeset
     2
 * Copyright (c) 1998, 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: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
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.metal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.GraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.swing.plaf.basic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * A Metal Look and Feel implementation of ToolBarUI.  This implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * is a "combined" view/controller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Jeff Shapiro
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class MetalToolBarUI extends BasicToolBarUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * An array of WeakReferences that point to JComponents. This will contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * instances of JToolBars and JMenuBars and is used to find
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * JToolBars/JMenuBars that border each other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
    63
    private static List<WeakReference<JComponent>> components = new ArrayList<WeakReference<JComponent>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
    66
     * This protected field is implementation specific. Do not access directly
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * or override. Use the create method instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @see #createContainerListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected ContainerListener contListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
    74
     * This protected field is implementation specific. Do not access directly
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * or override. Use the create method instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @see #createRolloverListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected PropertyChangeListener rolloverListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static Border nonRolloverBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Last menubar the toolbar touched.  This is only useful for ocean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private JMenuBar lastMenuBar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Registers the specified component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    synchronized static void register(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // Exception is thrown as convenience for callers that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            // typed to throw an NPE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new NullPointerException("JComponent must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
    97
        components.add(new WeakReference<JComponent>(c));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Unregisters the specified component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    synchronized static void unregister(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        for (int counter = components.size() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            // Search for the component, removing any flushed references
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            // along the way.
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   107
            JComponent target = components.get(counter).get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (target == c || target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                components.remove(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Finds a previously registered component of class <code>target</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * that shares the JRootPane ancestor of <code>from</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    synchronized static Object findRegisteredComponentOfType(JComponent from,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                                             Class target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        JRootPane rp = SwingUtilities.getRootPane(from);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (rp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            for (int counter = components.size() - 1; counter >= 0; counter--){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                Object component = ((WeakReference)components.get(counter)).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                   get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (component == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    // WeakReference has gone away, remove the WeakReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    components.remove(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                else if (target.isInstance(component) && SwingUtilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                         getRootPane((Component)component) == rp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    return component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Returns true if the passed in JMenuBar is above a horizontal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * JToolBar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static boolean doesMenuBarBorderToolBar(JMenuBar c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        JToolBar tb = (JToolBar)MetalToolBarUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    findRegisteredComponentOfType(c, JToolBar.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (tb != null && tb.getOrientation() == JToolBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            JRootPane rp = SwingUtilities.getRootPane(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            Point point = new Point(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            point = SwingUtilities.convertPoint(c, point, rp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            int menuX = point.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            int menuY = point.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            point.x = point.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            point = SwingUtilities.convertPoint(tb, point, rp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return (point.x == menuX && menuY + c.getHeight() == point.y &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    c.getWidth() == tb.getWidth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
25178
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   161
    /**
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   162
     * Constructs an instance of {@code MetalToolBarUI}.
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   163
     *
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   164
     * @param c a component
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   165
     * @return an instance of {@code MetalToolBarUI}
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   166
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public static ComponentUI createUI( JComponent c )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return new MetalToolBarUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public void installUI( JComponent c )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        super.installUI( c );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        register(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public void uninstallUI( JComponent c )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        super.uninstallUI( c );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        nonRolloverBorder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        unregister(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        super.installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        contListener = createContainerListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (contListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            toolBar.addContainerListener(contListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        rolloverListener = createRolloverListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (rolloverListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            toolBar.addPropertyChangeListener(rolloverListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        super.uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (contListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            toolBar.removeContainerListener(contListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        rolloverListener = createRolloverListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (rolloverListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            toolBar.removePropertyChangeListener(rolloverListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    protected Border createRolloverBorder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return super.createRolloverBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    protected Border createNonRolloverBorder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return super.createNonRolloverBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Creates a non rollover border for Toggle buttons in the toolbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private Border createNonRolloverToggleBorder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return createNonRolloverBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    protected void setBorderToNonRollover(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (c instanceof JToggleButton && !(c instanceof JCheckBox)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            // 4735514, 4886944: The method createNonRolloverToggleBorder() is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            // private in BasicToolBarUI so we can't override it. We still need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            // to call super from this method so that it can save away the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            // original border and then we install ours.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            // Before calling super we get a handle to the old border, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            // super will install a non-UIResource border that we can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            // distinguish from one provided by an application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            JToggleButton b = (JToggleButton)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            Border border = b.getBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            super.setBorderToNonRollover(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (border instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                if (nonRolloverBorder == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    nonRolloverBorder = createNonRolloverToggleBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                b.setBorder(nonRolloverBorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            super.setBorderToNonRollover(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Creates a container listener that will be added to the JToolBar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * If this method returns null then it will not be added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * toolbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @return an instance of a <code>ContainerListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    protected ContainerListener createContainerListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Creates a property change listener that will be added to the JToolBar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * If this method returns null then it will not be added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * toolbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return an instance of a <code>PropertyChangeListener</code> or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    protected PropertyChangeListener createRolloverListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    protected MouseInputListener createDockingListener( )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return new MetalDockingListener( toolBar );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
25178
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   278
    /**
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   279
     * Sets the offset of the mouse cursor inside the DragWindow.
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   280
     *
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   281
     * @param p the offset
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   282
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    protected void setDragOffset(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (dragWindow == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                dragWindow = createDragWindow(toolBar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            dragWindow.setOffset(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * If necessary paints the background of the component, then invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <code>paint</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @param g Graphics to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param c JComponent painting on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @throws NullPointerException if <code>g</code> or <code>c</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *         null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @see javax.swing.plaf.ComponentUI#update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @see javax.swing.plaf.ComponentUI#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public void update(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (g == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            throw new NullPointerException("graphics must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (c.isOpaque() && (c.getBackground() instanceof UIResource) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                            ((JToolBar)c).getOrientation() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                      JToolBar.HORIZONTAL && UIManager.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                     "MenuBar.gradient") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            JRootPane rp = SwingUtilities.getRootPane(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            JMenuBar mb = (JMenuBar)findRegisteredComponentOfType(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                    c, JMenuBar.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (mb != null && mb.isOpaque() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                              (mb.getBackground() instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                Point point = new Point(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                point = SwingUtilities.convertPoint(c, point, rp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                int x = point.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                int y = point.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                point.x = point.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                point = SwingUtilities.convertPoint(mb, point, rp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                if (point.x == x && y == point.y + mb.getHeight() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                     mb.getWidth() == c.getWidth() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                     MetalUtils.drawGradient(c, g, "MenuBar.gradient",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                     0, -mb.getHeight(), c.getWidth(), c.getHeight() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                     mb.getHeight(), true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    setLastMenuBar(mb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    paint(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (MetalUtils.drawGradient(c, g, "MenuBar.gradient",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                           0, 0, c.getWidth(), c.getHeight(), true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                setLastMenuBar(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                paint(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        setLastMenuBar(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        super.update(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private void setLastMenuBar(JMenuBar lastMenuBar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (MetalLookAndFeel.usingOcean()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (this.lastMenuBar != lastMenuBar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                // The menubar we previously touched has changed, force it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                // to repaint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                if (this.lastMenuBar != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    this.lastMenuBar.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                if (lastMenuBar != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    lastMenuBar.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                this.lastMenuBar = lastMenuBar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
25178
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   360
    /**
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   361
     * No longer used. The class cannot be removed for compatibility reasons.
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   362
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    protected class MetalContainerListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        extends BasicToolBarUI.ToolBarContListener {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
25178
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   366
    /**
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   367
     * No longer used. The class cannot be removed for compatibility reasons.
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   368
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    protected class MetalRolloverListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        extends BasicToolBarUI.PropertyListener {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
25178
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   372
    /**
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   373
     * {@code DockingListener} for {@code MetalToolBarUI}.
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   374
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    protected class MetalDockingListener extends DockingListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        private boolean pressedInBumps = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
25178
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   378
        /**
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   379
         * Constructs the {@code MetalDockingListener}.
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   380
         *
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   381
         * @param t an instance of {@code JToolBar}
dbab904451e9 8046434: Fix doclint warnings from javax.swing.plaf.metal package
yan
parents: 23715
diff changeset
   382
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        public MetalDockingListener(JToolBar t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            super(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            super.mousePressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (!toolBar.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            pressedInBumps = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            Rectangle bumpRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if (toolBar.getOrientation() == JToolBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                int x = MetalUtils.isLeftToRight(toolBar) ? 0 : toolBar.getSize().width-14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                bumpRect.setBounds(x, 0, 14, toolBar.getSize().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            } else {  // vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                bumpRect.setBounds(0, 0, toolBar.getSize().width, 14);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (bumpRect.contains(e.getPoint())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                pressedInBumps = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                Point dragOffset = e.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                if (!MetalUtils.isLeftToRight(toolBar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    dragOffset.x -= (toolBar.getSize().width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                     - toolBar.getPreferredSize().width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                setDragOffset(dragOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (pressedInBumps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                super.mouseDragged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    } // end class MetalDockingListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
}