001: /*
002: * Copyright (c) 2001-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.looks.plastic;
032:
033: import java.awt.Graphics;
034: import java.awt.Rectangle;
035: import java.beans.PropertyChangeEvent;
036: import java.beans.PropertyChangeListener;
037:
038: import javax.swing.JComponent;
039: import javax.swing.LookAndFeel;
040: import javax.swing.plaf.ComponentUI;
041: import javax.swing.plaf.basic.BasicMenuBarUI;
042:
043: import com.jgoodies.looks.BorderStyle;
044: import com.jgoodies.looks.HeaderStyle;
045: import com.jgoodies.looks.Options;
046:
047: /**
048: * The JGoodies Plastic look and feel implemenation of <code>MenuBarUI</code>.
049: * Can handle optional <code>Border</code> types as specified by the
050: * <code>BorderStyle</code> or <code>HeaderStyle</code> client properties.
051: *
052: * @author Karsten Lentzsch
053: * @version $Revision: 1.3 $
054: */
055: public final class PlasticMenuBarUI extends BasicMenuBarUI {
056:
057: private PropertyChangeListener listener;
058:
059: public static ComponentUI createUI(JComponent b) {
060: return new PlasticMenuBarUI();
061: }
062:
063: // Handling Special Borders *********************************************
064:
065: protected void installDefaults() {
066: super .installDefaults();
067: installSpecialBorder();
068: }
069:
070: protected void installListeners() {
071: super .installListeners();
072: listener = createBorderStyleListener();
073: menuBar.addPropertyChangeListener(listener);
074: }
075:
076: protected void uninstallListeners() {
077: menuBar.removePropertyChangeListener(listener);
078: super .uninstallListeners();
079: }
080:
081: private PropertyChangeListener createBorderStyleListener() {
082: return new PropertyChangeListener() {
083:
084: public void propertyChange(PropertyChangeEvent e) {
085: String prop = e.getPropertyName();
086: if (prop.equals(Options.HEADER_STYLE_KEY)
087: || prop
088: .equals(PlasticLookAndFeel.BORDER_STYLE_KEY)) {
089: PlasticMenuBarUI.this .installSpecialBorder();
090: }
091: }
092:
093: };
094: }
095:
096: /**
097: * Installs a special border, if either a look-dependent <code>BorderStyle</code>
098: * or a look-independent <code>HeaderStyle</code> has been specified.
099: * A look specific <code>BorderStyle</code> shadows a <code>HeaderStyle</code>.<p>
100: *
101: * We recommend to specify a <code>HeaderStyle</code>.
102: */
103: public void installSpecialBorder() {
104: String suffix;
105: BorderStyle borderStyle = BorderStyle.from(menuBar,
106: PlasticLookAndFeel.BORDER_STYLE_KEY);
107: if (borderStyle == BorderStyle.EMPTY)
108: suffix = "emptyBorder";
109: else if (borderStyle == BorderStyle.ETCHED)
110: suffix = "etchedBorder";
111: else if (borderStyle == BorderStyle.SEPARATOR)
112: suffix = "separatorBorder";
113: else {
114: HeaderStyle headerStyle = HeaderStyle.from(menuBar);
115: if (headerStyle == HeaderStyle.BOTH)
116: suffix = "headerBorder";
117: else if (headerStyle == HeaderStyle.SINGLE && is3D())
118: suffix = "etchedBorder";
119: else
120: return;
121: }
122:
123: LookAndFeel.installBorder(menuBar, "MenuBar." + suffix);
124: }
125:
126: // 3D Effect ************************************************************************
127:
128: public void update(Graphics g, JComponent c) {
129: if (c.isOpaque()) {
130: g.setColor(c.getBackground());
131: g.fillRect(0, 0, c.getWidth(), c.getHeight());
132: if (is3D()) {
133: Rectangle bounds = new Rectangle(0, 0, c.getWidth(), c
134: .getHeight());
135: PlasticUtils.addLight3DEffekt(g, bounds, true);
136: }
137: }
138:
139: paint(g, c);
140: }
141:
142: /**
143: * Checks and answers if we should add a pseudo 3D effect.
144: */
145: private boolean is3D() {
146: if (PlasticUtils.force3D(menuBar))
147: return true;
148: if (PlasticUtils.forceFlat(menuBar))
149: return false;
150: return PlasticUtils.is3D("MenuBar.")
151: && (HeaderStyle.from(menuBar) != null)
152: && (BorderStyle.from(menuBar,
153: PlasticLookAndFeel.BORDER_STYLE_KEY) != BorderStyle.EMPTY);
154: }
155:
156: }
|