1 /*
   2  * Copyright (c) 2002, 2010, 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 package javax.swing.plaf.synth;
  26 
  27 import javax.swing.*;
  28 import java.awt.Graphics;
  29 import java.beans.PropertyChangeEvent;
  30 import java.beans.PropertyChangeListener;
  31 import javax.swing.plaf.*;
  32 import javax.swing.plaf.basic.*;
  33 
  34 /**
  35  * Provides the Synth L&F UI delegate for
  36  * {@link javax.swing.JMenuBar}.
  37  *
  38  * @author Scott Violet
  39  * @since 1.7
  40  */
  41 public class SynthMenuBarUI extends BasicMenuBarUI
  42                             implements PropertyChangeListener, SynthUI {
  43     private SynthStyle style;
  44 
  45     /**
  46      * Creates a new UI object for the given component.
  47      *
  48      * @param x component to create UI object for
  49      * @return the UI object
  50      */
  51     public static ComponentUI createUI(JComponent x) {
  52         return new SynthMenuBarUI();
  53     }
  54 
  55     /**
  56      * @inheritDoc
  57      */
  58     @Override
  59     protected void installDefaults() {
  60         if (menuBar.getLayout() == null ||
  61             menuBar.getLayout() instanceof UIResource) {
  62             menuBar.setLayout(new SynthMenuLayout(menuBar,BoxLayout.LINE_AXIS));
  63         }
  64         updateStyle(menuBar);
  65     }
  66 
  67     /**
  68      * @inheritDoc
  69      */
  70     @Override
  71     protected void installListeners() {
  72         super.installListeners();
  73         menuBar.addPropertyChangeListener(this);
  74     }
  75 
  76     private void updateStyle(JMenuBar c) {
  77         SynthContext context = getContext(c, ENABLED);
  78         SynthStyle oldStyle = style;
  79         style = SynthLookAndFeel.updateStyle(context, this);
  80         if (style != oldStyle) {
  81             if (oldStyle != null) {
  82                 uninstallKeyboardActions();
  83                 installKeyboardActions();
  84             }
  85         }
  86         context.dispose();
  87     }
  88 
  89     /**
  90      * @inheritDoc
  91      */
  92     @Override
  93     protected void uninstallDefaults() {
  94         SynthContext context = getContext(menuBar, ENABLED);
  95 
  96         style.uninstallDefaults(context);
  97         context.dispose();
  98         style = null;
  99     }
 100 
 101     /**
 102      * @inheritDoc
 103      */
 104     @Override
 105     protected void uninstallListeners() {
 106         super.uninstallListeners();
 107         menuBar.removePropertyChangeListener(this);
 108     }
 109 
 110     /**
 111      * @inheritDoc
 112      */
 113     @Override
 114     public SynthContext getContext(JComponent c) {
 115         return getContext(c, getComponentState(c));
 116     }
 117 
 118     private SynthContext getContext(JComponent c, int state) {
 119         return SynthContext.getContext(SynthContext.class, c,
 120                     SynthLookAndFeel.getRegion(c), style, state);
 121     }
 122 
 123     private int getComponentState(JComponent c) {
 124         return SynthLookAndFeel.getComponentState(c);
 125     }
 126 
 127     /**
 128      * Notifies this UI delegate to repaint the specified component.
 129      * This method paints the component background, then calls
 130      * the {@link #paint(SynthContext,Graphics)} method.
 131      *
 132      * <p>In general, this method does not need to be overridden by subclasses.
 133      * All Look and Feel rendering code should reside in the {@code paint} method.
 134      *
 135      * @param g the {@code Graphics} object used for painting
 136      * @param c the component being painted
 137      * @see #paint(SynthContext,Graphics)
 138      */
 139     @Override
 140     public void update(Graphics g, JComponent c) {
 141         SynthContext context = getContext(c);
 142 
 143         SynthLookAndFeel.update(context, g);
 144         context.getPainter().paintMenuBarBackground(context,
 145                           g, 0, 0, c.getWidth(), c.getHeight());
 146         paint(context, g);
 147         context.dispose();
 148     }
 149 
 150     /**
 151      * Paints the specified component according to the Look and Feel.
 152      * <p>This method is not used by Synth Look and Feel.
 153      * Painting is handled by the {@link #paint(SynthContext,Graphics)} method.
 154      *
 155      * @param g the {@code Graphics} object used for painting
 156      * @param c the component being painted
 157      * @see #paint(SynthContext,Graphics)
 158      */
 159     @Override
 160     public void paint(Graphics g, JComponent c) {
 161         SynthContext context = getContext(c);
 162 
 163         paint(context, g);
 164         context.dispose();
 165     }
 166 
 167     /**
 168      * Paints the specified component. This implementation does nothing.
 169      *
 170      * @param context context for the component being painted
 171      * @param g the {@code Graphics} object used for painting
 172      * @see #update(Graphics,JComponent)
 173      */
 174     protected void paint(SynthContext context, Graphics g) {
 175     }
 176 
 177     /**
 178      * @inheritDoc
 179      */
 180     @Override
 181     public void paintBorder(SynthContext context, Graphics g, int x,
 182                             int y, int w, int h) {
 183         context.getPainter().paintMenuBarBorder(context, g, x, y, w, h);
 184     }
 185 
 186     /**
 187      * @inheritDoc
 188      */
 189     @Override
 190     public void propertyChange(PropertyChangeEvent e) {
 191         if (SynthLookAndFeel.shouldUpdateStyle(e)) {
 192             updateStyle((JMenuBar)e.getSource());
 193         }
 194     }
 195 }