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.Container;
034: import java.awt.Graphics;
035: import java.awt.Rectangle;
036:
037: import javax.swing.*;
038: import javax.swing.plaf.ComponentUI;
039: import javax.swing.plaf.metal.MetalButtonUI;
040:
041: /**
042: * The JGoodies Plastic L&F implementation of <code>ButtonUI</code>.
043: * It differs from the superclass <code>MetalButtonUI</code> in that
044: * it can add a pseudo 3D effect and that the border can paint the focus.
045: *
046: * @author Karsten Lentzsch
047: * @version $Revision: 1.4 $
048: */
049: public class PlasticButtonUI extends MetalButtonUI {
050:
051: private static final PlasticButtonUI INSTANCE = new PlasticButtonUI();
052:
053: private boolean borderPaintsFocus;
054:
055: public static ComponentUI createUI(JComponent b) {
056: return INSTANCE;
057: }
058:
059: /**
060: * Installs defaults.
061: */
062: public void installDefaults(AbstractButton b) {
063: super .installDefaults(b);
064: borderPaintsFocus = Boolean.TRUE.equals(UIManager
065: .get("Button.borderPaintsFocus"));
066: }
067:
068: // Painting ***************************************************************
069:
070: public void update(Graphics g, JComponent c) {
071: if (c.isOpaque()) {
072: AbstractButton b = (AbstractButton) c;
073: if (isToolBarButton(b)) {
074: c.setOpaque(false);
075: } else if (b.isContentAreaFilled()) {
076: g.setColor(c.getBackground());
077: g.fillRect(0, 0, c.getWidth(), c.getHeight());
078:
079: if (is3D(b)) {
080: Rectangle r = new Rectangle(1, 1, c.getWidth() - 2,
081: c.getHeight() - 1);
082: PlasticUtils.add3DEffekt(g, r);
083: }
084: }
085: }
086: paint(g, c);
087: }
088:
089: /**
090: * Paints the focus with close to the button's border.
091: */
092: protected void paintFocus(Graphics g, AbstractButton b,
093: Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
094:
095: if (borderPaintsFocus) {
096: return;
097: }
098:
099: boolean isDefault = b instanceof JButton
100: && ((JButton) b).isDefaultButton();
101: int topLeftInset = isDefault ? 3 : 2;
102: int width = b.getWidth() - 1 - topLeftInset * 2;
103: int height = b.getHeight() - 1 - topLeftInset * 2;
104:
105: g.setColor(getFocusColor());
106: g.drawRect(topLeftInset, topLeftInset, width - 1, height - 1);
107: }
108:
109: // Private Helper Code **************************************************************
110:
111: /**
112: * Checks and answers if this is button is in a tool bar.
113: *
114: * @param b the button to check
115: * @return true if in tool bar, false otherwise
116: */
117: protected boolean isToolBarButton(AbstractButton b) {
118: Container parent = b.getParent();
119: return parent != null
120: && (parent instanceof JToolBar || parent.getParent() instanceof JToolBar);
121: }
122:
123: /**
124: * Checks and answers if this button shall use a pseudo 3D effect.
125: *
126: * @param b the button to check
127: * @return true indicates a 3D effect, false flat
128: */
129: protected boolean is3D(AbstractButton b) {
130: if (PlasticUtils.force3D(b))
131: return true;
132: if (PlasticUtils.forceFlat(b))
133: return false;
134: ButtonModel model = b.getModel();
135: return PlasticUtils.is3D("Button.") && b.isBorderPainted()
136: && model.isEnabled()
137: && !(model.isPressed() && model.isArmed());
138: }
139:
140: }
|