001: /*
002: * SmoothGradientButtonUI.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.plaf.smoothgradient;
023:
024: import java.awt.Container;
025: import java.awt.Graphics;
026: import java.awt.Rectangle;
027:
028: import javax.swing.AbstractButton;
029: import javax.swing.ButtonModel;
030: import javax.swing.JButton;
031: import javax.swing.JComponent;
032: import javax.swing.JToolBar;
033: import javax.swing.UIManager;
034: import javax.swing.border.EmptyBorder;
035: import javax.swing.plaf.ComponentUI;
036: import javax.swing.plaf.metal.MetalButtonUI;
037:
038: /* ----------------------------------------------------------
039: * CVS NOTE: Changes to the CVS repository prior to the
040: * release of version 3.0.0beta1 has meant a
041: * resetting of CVS revision numbers.
042: * ----------------------------------------------------------
043: */
044:
045: /**
046: *
047: * @author Takis Diakoumis
048: * @version $Revision: 1.4 $
049: * @date $Date: 2006/05/14 06:56:07 $
050: */
051: public class SmoothGradientButtonUI extends MetalButtonUI {
052:
053: private static final SmoothGradientButtonUI INSTANCE = new SmoothGradientButtonUI();
054:
055: private boolean borderPaintsFocus;
056:
057: public static ComponentUI createUI(JComponent b) {
058: return INSTANCE;
059: }
060:
061: /**
062: * Installs defaults and honors the client property <code>isNarrow</code>.
063: */
064: public void installDefaults(AbstractButton b) {
065: super .installDefaults(b);
066: // PolishedLookUtils.installNarrowMargin(b, getPropertyPrefix());
067: borderPaintsFocus = Boolean.TRUE.equals(UIManager
068: .get("Button.borderPaintsFocus"));
069: }
070:
071: /**
072: * Installs an extra listener for a change of the isNarrow property.
073: */
074: /*
075: public void installListeners(AbstractButton b) {
076: super.installListeners(b);
077: PropertyChangeListener listener =
078: new ButtonMarginListener(getPropertyPrefix());
079: b.putClientProperty(ButtonMarginListener.CLIENT_KEY, listener);
080: b.addPropertyChangeListener(Options.IS_NARROW_KEY, listener);
081: }
082: */
083: /**
084: * Uninstalls the extra listener for a change of the isNarrow property.
085: */
086: /*
087: public void uninstallListeners(AbstractButton b) {
088: super.uninstallListeners(b);
089: PropertyChangeListener listener =
090: (PropertyChangeListener) b.getClientProperty(
091: ButtonMarginListener.CLIENT_KEY);
092: b.removePropertyChangeListener(listener);
093: }
094: */
095: // Painting ***************************************************************
096: public void update(Graphics g, JComponent c) {
097: AbstractButton b = (AbstractButton) c;
098: if (c.isOpaque()) {
099: if (isToolBarButton(b)) {
100: c.setOpaque(false);
101: } else if (b.isContentAreaFilled()) {
102: g.setColor(c.getBackground());
103: g.fillRect(0, 0, c.getWidth(), c.getHeight());
104:
105: if (is3D(b)) {
106: Rectangle r = new Rectangle(1, 1, c.getWidth() - 2,
107: c.getHeight() - 1);
108: SmoothGradientUtils.add3DEffekt(g, r);
109: }
110: }
111: }
112: paint(g, c);
113: }
114:
115: /**
116: * Paints the focus with close to the button's border.
117: */
118: protected void paintFocus(Graphics g, AbstractButton b,
119: Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
120:
121: if (borderPaintsFocus) {
122: return;
123: }
124:
125: boolean isDefault = b instanceof JButton
126: && ((JButton) b).isDefaultButton();
127: int topLeftInset = isDefault ? 3 : 2;
128: int width = b.getWidth() - 1 - topLeftInset * 2;
129: int height = b.getHeight() - 1 - topLeftInset * 2;
130:
131: g.setColor(getFocusColor());
132: g.drawRect(topLeftInset, topLeftInset, width - 1, height - 1);
133: }
134:
135: // Private Helper Code **************************************************************
136:
137: /**
138: * Checks and answers if this is button is in a tool bar.
139: *
140: * @param b the button to check
141: * @return true if in tool bar, false otherwise
142: */
143: protected boolean isToolBarButton(AbstractButton b) {
144: Container parent = b.getParent();
145: return parent != null
146: && (parent instanceof JToolBar || parent.getParent() instanceof JToolBar);
147: }
148:
149: /**
150: * Checks and answers if this button shall use a pseudo 3D effect
151: *
152: * @param b the button to check
153: * @return true indicates a 3D effect, false flat
154: */
155: protected boolean is3D(AbstractButton b) {
156: if (SmoothGradientUtils.force3D(b))
157: return true;
158: if (SmoothGradientUtils.forceFlat(b))
159: return false;
160: ButtonModel model = b.getModel();
161: return SmoothGradientUtils.is3D("Button.")
162: && b.isBorderPainted() && model.isEnabled()
163: && !(model.isPressed() && model.isArmed())
164: && !(b.getBorder() instanceof EmptyBorder);
165:
166: /*
167: * Implementation note regarding the last line: instead of checking
168: * for the EmptyBorder in NetBeans, I'd prefer to just check the
169: * 'borderPainted' property. I'd recommend to the NetBeans developers,
170: * to switch this property on and off, instead of changing the border.
171: */
172: }
173:
174: }
|