001: /*
002: * SmoothGradientToggleButtonUI.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.Dimension;
026: import java.awt.Font;
027: import java.awt.FontMetrics;
028: import java.awt.Graphics;
029: import java.awt.Insets;
030: import java.awt.Rectangle;
031:
032: import javax.swing.AbstractButton;
033: import javax.swing.ButtonModel;
034: import javax.swing.JComponent;
035: import javax.swing.JToolBar;
036: import javax.swing.SwingUtilities;
037: import javax.swing.UIManager;
038: import javax.swing.plaf.ComponentUI;
039: import javax.swing.plaf.basic.BasicHTML;
040: import javax.swing.plaf.metal.MetalToggleButtonUI;
041: import javax.swing.text.View;
042:
043: /* ----------------------------------------------------------
044: * CVS NOTE: Changes to the CVS repository prior to the
045: * release of version 3.0.0beta1 has meant a
046: * resetting of CVS revision numbers.
047: * ----------------------------------------------------------
048: */
049:
050: /**
051: *
052: * @author Takis Diakoumis
053: * @version $Revision: 1.4 $
054: * @date $Date: 2006/05/14 06:56:07 $
055: */
056: public class SmoothGradientToggleButtonUI extends MetalToggleButtonUI {
057:
058: private static final SmoothGradientToggleButtonUI INSTANCE = new SmoothGradientToggleButtonUI();
059:
060: /*
061: * Implementation note: The protected visibility prevents
062: * the String value from being encrypted by the obfuscator.
063: * An encrypted String key would break the client property lookup
064: * in the #paint method below.
065: */
066: protected static final String HTML_KEY = BasicHTML.propertyKey;
067:
068: private boolean borderPaintsFocus;
069:
070: public static ComponentUI createUI(JComponent b) {
071: return INSTANCE;
072: }
073:
074: /**
075: * Installs defaults and honors the client property <code>isNarrow</code>.
076: */
077: public void installDefaults(AbstractButton b) {
078: super .installDefaults(b);
079: borderPaintsFocus = Boolean.TRUE.equals(UIManager
080: .get("ToggleButton.borderPaintsFocus"));
081: }
082:
083: public void update(Graphics g, JComponent c) {
084: AbstractButton b = (AbstractButton) c;
085: if (c.isOpaque()) {
086: if (isToolBarButton(b)) {
087: c.setOpaque(false);
088: } else if (b.isContentAreaFilled()) {
089: g.setColor(c.getBackground());
090: g.fillRect(0, 0, c.getWidth(), c.getHeight());
091:
092: Rectangle r = new Rectangle(1, 1, c.getWidth() - 2, c
093: .getHeight() - 1);
094: SmoothGradientUtils.add3DEffekt(g, r);
095:
096: }
097: }
098: paint(g, c);
099: }
100:
101: /**
102: * Paints the focus close to the button's border.
103: */
104: protected void paintFocus(Graphics g, AbstractButton b,
105: Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
106:
107: if (borderPaintsFocus)
108: return;
109:
110: boolean isDefault = false;
111: int topLeftInset = isDefault ? 3 : 2;
112: int width = b.getWidth() - 1 - topLeftInset * 2;
113: int height = b.getHeight() - 1 - topLeftInset * 2;
114:
115: g.setColor(getFocusColor());
116: g.drawRect(topLeftInset, topLeftInset, width - 1, height - 1);
117: }
118:
119: /**
120: * Unlike the BasicToggleButtonUI.paint, we don't fill the content area;
121: * this has been done by the update method before.
122: */
123: public void paint(Graphics g, JComponent c) {
124: AbstractButton b = (AbstractButton) c;
125: ButtonModel model = b.getModel();
126:
127: Dimension size = b.getSize();
128: FontMetrics fm = g.getFontMetrics();
129:
130: Insets i = c.getInsets();
131:
132: Rectangle viewRect = new Rectangle(size);
133:
134: viewRect.x += i.left;
135: viewRect.y += i.top;
136: viewRect.width -= (i.right + viewRect.x);
137: viewRect.height -= (i.bottom + viewRect.y);
138:
139: Rectangle iconRect = new Rectangle();
140: Rectangle textRect = new Rectangle();
141:
142: Font f = c.getFont();
143: g.setFont(f);
144:
145: // layout the text and icon
146: String text = SwingUtilities.layoutCompoundLabel(c, fm, b
147: .getText(), b.getIcon(), b.getVerticalAlignment(), b
148: .getHorizontalAlignment(), b.getVerticalTextPosition(),
149: b.getHorizontalTextPosition(), viewRect, iconRect,
150: textRect, b.getText() == null ? 0
151: : getDefaultTextIconGap(b));
152: // [Pending 1.4]: b.getIconTextGap());
153:
154: g.setColor(b.getBackground());
155:
156: if (model.isArmed() && model.isPressed() || model.isSelected())
157: paintButtonPressed(g, b);
158:
159: // Paint the Icon
160: if (b.getIcon() != null)
161: paintIcon(g, b, iconRect);
162:
163: // Draw the Text
164: if (text != null && !text.equals("")) {
165: View v = (View) c.getClientProperty(HTML_KEY);
166: if (v != null) {
167: v.paint(g, textRect);
168: } else {
169: paintText(g, c, textRect, text);
170: }
171: }
172:
173: // draw the dashed focus line.
174: if (b.isFocusPainted() && b.hasFocus()) {
175: paintFocus(g, b, viewRect, textRect, iconRect);
176: }
177: }
178:
179: // Private Helper Code **************************************************************
180:
181: /**
182: * Checks and answers if this is button is in a tool bar.
183: *
184: * @param b the button to check
185: * @return true if in tool bar, false otherwise
186: */
187: protected boolean isToolBarButton(AbstractButton b) {
188: Container parent = b.getParent();
189: return parent != null
190: && (parent instanceof JToolBar || parent.getParent() instanceof JToolBar);
191: }
192:
193: }
|