1 /*
2  * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package javax.swing.plaf.metal;
27 
28 import java.awt.*;
29 import javax.swing.*;
30 import javax.swing.plaf.ComponentUI;
31 import javax.swing.plaf.UIResource;
32 import javax.swing.plaf.basic.*;
33 
34 /**
35  * Metal implementation of <code>MenuBarUI</code>. This class is responsible
36  * for providing the metal look and feel for <code>JMenuBar</code>s.
37  *
38  * @see javax.swing.plaf.MenuBarUI
39  * @since 1.5
40  */
41 public class MetalMenuBarUI extends BasicMenuBarUI  {
42     /**
43      * Creates the <code>ComponentUI</code> implementation for the passed
44      * in component.
45      *
46      * @param x JComponent to create the ComponentUI implementation for
47      * @return ComponentUI implementation for <code>x</code>
48      * @throws NullPointerException if <code>x</code> is null
49      */
createUI(JComponent x)50     public static ComponentUI createUI(JComponent x) {
51         if (x == null) {
52             throw new NullPointerException("Must pass in a non-null component");
53         }
54         return new MetalMenuBarUI();
55     }
56 
57     /**
58      * Configures the specified component appropriate for the metal look and
59      * feel.
60      *
61      * @param c the component where this UI delegate is being installed
62      * @throws NullPointerException if <code>c</code> is null.
63      */
installUI(JComponent c)64     public void installUI(JComponent c) {
65         super.installUI(c);
66         MetalToolBarUI.register(c);
67     }
68 
69     /**
70      * Reverses configuration which was done on the specified component during
71      * <code>installUI</code>.
72      *
73      * @param c the component where this UI delegate is being installed
74      * @throws NullPointerException if <code>c</code> is null.
75      */
uninstallUI(JComponent c)76     public void uninstallUI(JComponent c) {
77         super.uninstallUI(c);
78         MetalToolBarUI.unregister(c);
79     }
80 
81     /**
82      * If necessary paints the background of the component, then
83      * invokes <code>paint</code>.
84      *
85      * @param g Graphics to paint to
86      * @param c JComponent painting on
87      * @throws NullPointerException if <code>g</code> or <code>c</code> is
88      *         null
89      * @see javax.swing.plaf.ComponentUI#update
90      * @see javax.swing.plaf.ComponentUI#paint
91      * @since 1.5
92      */
update(Graphics g, JComponent c)93     public void update(Graphics g, JComponent c) {
94         boolean isOpaque = c.isOpaque();
95         if (g == null) {
96             throw new NullPointerException("Graphics must be non-null");
97         }
98         if (isOpaque && (c.getBackground() instanceof UIResource) &&
99                         UIManager.get("MenuBar.gradient") != null) {
100             if (MetalToolBarUI.doesMenuBarBorderToolBar((JMenuBar)c)) {
101                 JToolBar tb = (JToolBar)MetalToolBarUI.
102                      findRegisteredComponentOfType(c, JToolBar.class);
103                 if (tb.isOpaque() &&tb.getBackground() instanceof UIResource) {
104                     MetalUtils.drawGradient(c, g, "MenuBar.gradient", 0, 0,
105                                             c.getWidth(), c.getHeight() +
106                                             tb.getHeight(), true);
107                     paint(g, c);
108                     return;
109                 }
110             }
111             MetalUtils.drawGradient(c, g, "MenuBar.gradient", 0, 0,
112                                     c.getWidth(), c.getHeight(),true);
113             paint(g, c);
114         }
115         else {
116             super.update(g, c);
117         }
118     }
119 }
120