J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2003 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | package javax.swing.plaf.synth; |
| 26 | |
| 27 | import javax.swing.*; |
| 28 | import javax.swing.event.*; |
| 29 | import java.awt.Color; |
| 30 | import java.awt.Component; |
| 31 | import java.awt.Container; |
| 32 | import java.awt.Dimension; |
| 33 | import java.awt.Graphics; |
| 34 | import java.awt.Insets; |
| 35 | import java.awt.Point; |
| 36 | import java.awt.Rectangle; |
| 37 | import java.awt.event.*; |
| 38 | import java.beans.PropertyChangeEvent; |
| 39 | import java.beans.PropertyChangeListener; |
| 40 | |
| 41 | import javax.swing.border.*; |
| 42 | import javax.swing.plaf.*; |
| 43 | import javax.swing.plaf.basic.*; |
| 44 | import sun.swing.plaf.synth.SynthUI; |
| 45 | |
| 46 | /** |
| 47 | * Synth's MenuBarUI. |
| 48 | * |
| 49 | * @author Scott Violet |
| 50 | */ |
| 51 | class SynthMenuBarUI extends BasicMenuBarUI implements PropertyChangeListener, |
| 52 | SynthUI { |
| 53 | private SynthStyle style; |
| 54 | |
| 55 | public static ComponentUI createUI(JComponent x) { |
| 56 | return new SynthMenuBarUI(); |
| 57 | } |
| 58 | |
| 59 | protected void installDefaults() { |
| 60 | if (menuBar.getLayout() == null || |
| 61 | menuBar.getLayout() instanceof UIResource) { |
| 62 | menuBar.setLayout(new DefaultMenuLayout(menuBar,BoxLayout.LINE_AXIS)); |
| 63 | } |
| 64 | updateStyle(menuBar); |
| 65 | } |
| 66 | |
| 67 | protected void installListeners() { |
| 68 | super.installListeners(); |
| 69 | menuBar.addPropertyChangeListener(this); |
| 70 | } |
| 71 | |
| 72 | private void updateStyle(JMenuBar c) { |
| 73 | SynthContext context = getContext(c, ENABLED); |
| 74 | SynthStyle oldStyle = style; |
| 75 | style = SynthLookAndFeel.updateStyle(context, this); |
| 76 | if (style != oldStyle) { |
| 77 | if (oldStyle != null) { |
| 78 | uninstallKeyboardActions(); |
| 79 | installKeyboardActions(); |
| 80 | } |
| 81 | } |
| 82 | context.dispose(); |
| 83 | } |
| 84 | |
| 85 | protected void uninstallDefaults() { |
| 86 | SynthContext context = getContext(menuBar, ENABLED); |
| 87 | |
| 88 | style.uninstallDefaults(context); |
| 89 | context.dispose(); |
| 90 | style = null; |
| 91 | } |
| 92 | |
| 93 | protected void uninstallListeners() { |
| 94 | super.uninstallListeners(); |
| 95 | menuBar.removePropertyChangeListener(this); |
| 96 | } |
| 97 | |
| 98 | public SynthContext getContext(JComponent c) { |
| 99 | return getContext(c, getComponentState(c)); |
| 100 | } |
| 101 | |
| 102 | private SynthContext getContext(JComponent c, int state) { |
| 103 | return SynthContext.getContext(SynthContext.class, c, |
| 104 | SynthLookAndFeel.getRegion(c), style, state); |
| 105 | } |
| 106 | |
| 107 | private Region getRegion(JComponent c) { |
| 108 | return SynthLookAndFeel.getRegion(c); |
| 109 | } |
| 110 | |
| 111 | private int getComponentState(JComponent c) { |
| 112 | return SynthLookAndFeel.getComponentState(c); |
| 113 | } |
| 114 | |
| 115 | public void update(Graphics g, JComponent c) { |
| 116 | SynthContext context = getContext(c); |
| 117 | |
| 118 | SynthLookAndFeel.update(context, g); |
| 119 | context.getPainter().paintMenuBarBackground(context, |
| 120 | g, 0, 0, c.getWidth(), c.getHeight()); |
| 121 | paint(context, g); |
| 122 | context.dispose(); |
| 123 | } |
| 124 | |
| 125 | public void paint(Graphics g, JComponent c) { |
| 126 | SynthContext context = getContext(c); |
| 127 | |
| 128 | paint(context, g); |
| 129 | context.dispose(); |
| 130 | } |
| 131 | |
| 132 | protected void paint(SynthContext context, Graphics g) { |
| 133 | } |
| 134 | |
| 135 | public void paintBorder(SynthContext context, Graphics g, int x, |
| 136 | int y, int w, int h) { |
| 137 | context.getPainter().paintMenuBarBorder(context, g, x, y, w, h); |
| 138 | } |
| 139 | |
| 140 | public void propertyChange(PropertyChangeEvent e) { |
| 141 | if (SynthLookAndFeel.shouldUpdateStyle(e)) { |
| 142 | updateStyle((JMenuBar)e.getSource()); |
| 143 | } |
| 144 | } |
| 145 | } |