1 /* 2 * Copyright (c) 2002, 2013, 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 */ createUI(JComponent x)51 public static ComponentUI createUI(JComponent x) { 52 return new SynthMenuBarUI(); 53 } 54 55 /** 56 * {@inheritDoc} 57 */ 58 @Override installDefaults()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 installListeners()71 protected void installListeners() { 72 super.installListeners(); 73 menuBar.addPropertyChangeListener(this); 74 } 75 updateStyle(JMenuBar c)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 } 87 88 /** 89 * {@inheritDoc} 90 */ 91 @Override uninstallDefaults()92 protected void uninstallDefaults() { 93 SynthContext context = getContext(menuBar, ENABLED); 94 95 style.uninstallDefaults(context); 96 style = null; 97 } 98 99 /** 100 * {@inheritDoc} 101 */ 102 @Override uninstallListeners()103 protected void uninstallListeners() { 104 super.uninstallListeners(); 105 menuBar.removePropertyChangeListener(this); 106 } 107 108 /** 109 * {@inheritDoc} 110 */ 111 @Override getContext(JComponent c)112 public SynthContext getContext(JComponent c) { 113 return getContext(c, getComponentState(c)); 114 } 115 getContext(JComponent c, int state)116 private SynthContext getContext(JComponent c, int state) { 117 return SynthContext.getContext(c, style, state); 118 } 119 getComponentState(JComponent c)120 private int getComponentState(JComponent c) { 121 return SynthLookAndFeel.getComponentState(c); 122 } 123 124 /** 125 * Notifies this UI delegate to repaint the specified component. 126 * This method paints the component background, then calls 127 * the {@link #paint(SynthContext,Graphics)} method. 128 * 129 * <p>In general, this method does not need to be overridden by subclasses. 130 * All Look and Feel rendering code should reside in the {@code paint} method. 131 * 132 * @param g the {@code Graphics} object used for painting 133 * @param c the component being painted 134 * @see #paint(SynthContext,Graphics) 135 */ 136 @Override update(Graphics g, JComponent c)137 public void update(Graphics g, JComponent c) { 138 SynthContext context = getContext(c); 139 140 SynthLookAndFeel.update(context, g); 141 context.getPainter().paintMenuBarBackground(context, 142 g, 0, 0, c.getWidth(), c.getHeight()); 143 paint(context, g); 144 } 145 146 /** 147 * Paints the specified component according to the Look and Feel. 148 * <p>This method is not used by Synth Look and Feel. 149 * Painting is handled by the {@link #paint(SynthContext,Graphics)} method. 150 * 151 * @param g the {@code Graphics} object used for painting 152 * @param c the component being painted 153 * @see #paint(SynthContext,Graphics) 154 */ 155 @Override paint(Graphics g, JComponent c)156 public void paint(Graphics g, JComponent c) { 157 SynthContext context = getContext(c); 158 159 paint(context, g); 160 } 161 162 /** 163 * Paints the specified component. This implementation does nothing. 164 * 165 * @param context context for the component being painted 166 * @param g the {@code Graphics} object used for painting 167 * @see #update(Graphics,JComponent) 168 */ paint(SynthContext context, Graphics g)169 protected void paint(SynthContext context, Graphics g) { 170 } 171 172 /** 173 * {@inheritDoc} 174 */ 175 @Override paintBorder(SynthContext context, Graphics g, int x, int y, int w, int h)176 public void paintBorder(SynthContext context, Graphics g, int x, 177 int y, int w, int h) { 178 context.getPainter().paintMenuBarBorder(context, g, x, y, w, h); 179 } 180 181 /** 182 * {@inheritDoc} 183 */ 184 @Override propertyChange(PropertyChangeEvent e)185 public void propertyChange(PropertyChangeEvent e) { 186 if (SynthLookAndFeel.shouldUpdateStyle(e)) { 187 updateStyle((JMenuBar)e.getSource()); 188 } 189 } 190 } 191