001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
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 (at your option) 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,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: AbstractButtonIcon.java,v 1.10 2005/02/16 11:28:11 jesper Exp $
023: package net.infonode.gui.icon.button;
024:
025: import net.infonode.gui.ComponentUtil;
026: import net.infonode.util.ColorUtil;
027:
028: import javax.swing.*;
029: import java.awt.*;
030: import java.io.Serializable;
031:
032: public abstract class AbstractButtonIcon implements Icon, Serializable {
033: private static final long serialVersionUID = 1;
034:
035: private int size = 10;
036: private Color defaultColor = null;
037: private boolean shadowEnabled = true;
038: private float shadowStrength = 0.3f;
039: private boolean enabled = true;
040:
041: public AbstractButtonIcon() {
042: }
043:
044: public AbstractButtonIcon(Color color) {
045: this .defaultColor = color;
046: }
047:
048: public AbstractButtonIcon(Color color, int size) {
049: this (color);
050: this .size = size;
051: }
052:
053: public AbstractButtonIcon(int size) {
054: this (size, true);
055: }
056:
057: public AbstractButtonIcon(int size, boolean enabled) {
058: this ();
059: this .size = size;
060: this .enabled = enabled;
061: }
062:
063: public int getIconWidth() {
064: return size;
065: }
066:
067: public int getIconHeight() {
068: return size;
069: }
070:
071: public boolean isShadowEnabled() {
072: return shadowEnabled;
073: }
074:
075: public void setShadowEnabled(boolean shadowEnabled) {
076: this .shadowEnabled = shadowEnabled;
077: }
078:
079: public float getShadowStrength() {
080: return shadowStrength;
081: }
082:
083: public void setShadowStrength(float shadowStrength) {
084: this .shadowStrength = shadowStrength;
085: }
086:
087: public void paintIcon(Component c, Graphics g, int x, int y) {
088: Color oldColor = g.getColor();
089: Color color = defaultColor == null ? (enabled ? c
090: .getForeground() : UIManager
091: .getColor("Button.disabledForeground")) : defaultColor;
092: if (color == null)
093: color = ColorUtil.blend(
094: ComponentUtil.getBackgroundColor(c), c
095: .getForeground(), 0.5f);
096:
097: if (shadowEnabled) {
098: Color background = ComponentUtil.getBackgroundColor(c);
099: g.setColor(ColorUtil.blend(background == null ? Color.BLACK
100: : background, Color.BLACK, shadowStrength));
101: paintIcon(c, g, x + 2, y + 2, x + size - 1, y + size - 1,
102: true);
103: g.setColor(color);
104: paintIcon(c, g, x + 1, y + 1, x + size - 2, y + size - 2,
105: false);
106: } else {
107: g.setColor(color);
108: paintIcon(c, g, x, y, x + size - 1, y + size - 1, false);
109: }
110:
111: g.setColor(oldColor);
112: }
113:
114: protected void paintIcon(Component c, Graphics g, int x1, int y1,
115: int x2, int y2, boolean isShadow) {
116: paintIcon(c, g, x1, y1, x2, y2);
117: }
118:
119: protected void paintIcon(Component c, Graphics g, int x1, int y1,
120: int x2, int y2) {
121: }
122: }
|