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.Component;
034: import java.awt.Container;
035: import java.awt.Graphics;
036: import java.awt.Rectangle;
037: import java.beans.PropertyChangeEvent;
038: import java.beans.PropertyChangeListener;
039:
040: import javax.swing.AbstractButton;
041: import javax.swing.JComponent;
042: import javax.swing.JToolBar;
043: import javax.swing.LookAndFeel;
044: import javax.swing.SwingConstants;
045: import javax.swing.border.Border;
046: import javax.swing.plaf.ComponentUI;
047: import javax.swing.plaf.metal.MetalToolBarUI;
048:
049: import com.jgoodies.looks.BorderStyle;
050: import com.jgoodies.looks.HeaderStyle;
051: import com.jgoodies.looks.Options;
052:
053: /**
054: * The JGoodies Plastic look and feel implementation of <code>ToolBarUI</code>.<p>
055: *
056: * Corrects the rollover borders and can handle optional <code>Border</code> types,
057: * as specified by the <code>BorderStyle</code> or <code>HeaderStyle</code>
058: * client properties.
059: *
060: * @author Karsten Lentzsch
061: * @version $Revision: 1.6 $
062: */
063:
064: public class PlasticToolBarUI extends MetalToolBarUI {
065:
066: private static final String PROPERTY_PREFIX = "ToolBar.";
067:
068: private PropertyChangeListener listener;
069:
070: public static ComponentUI createUI(JComponent b) {
071: return new PlasticToolBarUI();
072: }
073:
074: // Rollover Borders *****************************************************
075:
076: protected Border createRolloverBorder() {
077: return PlasticBorders.getRolloverButtonBorder();
078: }
079:
080: protected void setBorderToRollover(Component c) {
081: if (c instanceof AbstractButton) {
082: super .setBorderToRollover(c);
083: } else if (c instanceof Container) {
084: Container cont = (Container) c;
085: for (int i = 0; i < cont.getComponentCount(); i++)
086: super .setBorderToRollover(cont.getComponent(i));
087: }
088: }
089:
090: // Handling Special Borders *********************************************
091:
092: /**
093: * Installs a special border, if indicated by the <code>HeaderStyle</code>.
094: */
095: protected void installDefaults() {
096: super .installDefaults();
097: installSpecialBorder();
098: }
099:
100: protected void installListeners() {
101: super .installListeners();
102: listener = createBorderStyleListener();
103: toolBar.addPropertyChangeListener(listener);
104: }
105:
106: protected void uninstallListeners() {
107: toolBar.removePropertyChangeListener(listener);
108: super .uninstallListeners();
109: }
110:
111: private PropertyChangeListener createBorderStyleListener() {
112: return new PropertyChangeListener() {
113:
114: public void propertyChange(PropertyChangeEvent e) {
115: String prop = e.getPropertyName();
116: if (prop.equals(Options.HEADER_STYLE_KEY)
117: || prop
118: .equals(PlasticLookAndFeel.BORDER_STYLE_KEY)) {
119: PlasticToolBarUI.this .installSpecialBorder();
120: }
121: }
122:
123: };
124: }
125:
126: /**
127: * Installs a special border, if either a look-dependent
128: * <code>BorderStyle</code> or a look-independent
129: * <code>HeaderStyle</code> has been specified.
130: * A look specific <code>BorderStyle</code> shadows
131: * a <code>HeaderStyle</code>.<p>
132: *
133: * Specifying a <code>HeaderStyle</code> is recommend.
134: */
135: private void installSpecialBorder() {
136: String suffix;
137: BorderStyle borderStyle = BorderStyle.from(toolBar,
138: PlasticLookAndFeel.BORDER_STYLE_KEY);
139: if (borderStyle == BorderStyle.EMPTY)
140: suffix = "emptyBorder";
141: else if (borderStyle == BorderStyle.ETCHED)
142: suffix = "etchedBorder";
143: else if (borderStyle == BorderStyle.SEPARATOR)
144: suffix = "separatorBorder";
145: else {
146: HeaderStyle headerStyle = HeaderStyle.from(toolBar);
147: if (headerStyle == HeaderStyle.BOTH)
148: suffix = "headerBorder";
149: else if (headerStyle == HeaderStyle.SINGLE && is3D())
150: suffix = "etchedBorder";
151: else
152: suffix = "border";
153: }
154: LookAndFeel.installBorder(toolBar, PROPERTY_PREFIX + suffix);
155: }
156:
157: // 3D Effect ************************************************************
158:
159: public void update(Graphics g, JComponent c) {
160: if (c.isOpaque()) {
161: g.setColor(c.getBackground());
162: g.fillRect(0, 0, c.getWidth(), c.getHeight());
163: if (is3D()) {
164: Rectangle bounds = new Rectangle(0, 0, c.getWidth(), c
165: .getHeight());
166: boolean isHorizontal = ((JToolBar) c).getOrientation() == SwingConstants.HORIZONTAL;
167: PlasticUtils.addLight3DEffekt(g, bounds, isHorizontal);
168: }
169: }
170: paint(g, c);
171: }
172:
173: /**
174: * Checks and answers if we should add a pseudo 3D effect.
175: */
176: private boolean is3D() {
177: if (PlasticUtils.force3D(toolBar))
178: return true;
179: if (PlasticUtils.forceFlat(toolBar))
180: return false;
181: return PlasticUtils.is3D(PROPERTY_PREFIX)
182: && (HeaderStyle.from(toolBar) != null)
183: && (BorderStyle.from(toolBar,
184: PlasticLookAndFeel.BORDER_STYLE_KEY) != BorderStyle.EMPTY);
185: }
186:
187: }
|