001: /*
002: * GradientLabel.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;
023:
024: import java.awt.AlphaComposite;
025: import java.awt.Color;
026: import java.awt.Composite;
027: import java.awt.Dimension;
028: import java.awt.Font;
029: import java.awt.FontMetrics;
030: import java.awt.GradientPaint;
031: import java.awt.Graphics;
032: import java.awt.Graphics2D;
033: import java.awt.Insets;
034: import java.awt.Paint;
035: import java.awt.RenderingHints;
036:
037: import javax.swing.ImageIcon;
038: import javax.swing.JComponent;
039: import javax.swing.UIManager;
040: import org.underworldlabs.swing.plaf.UIUtils;
041:
042: /* ----------------------------------------------------------
043: * CVS NOTE: Changes to the CVS repository prior to the
044: * release of version 3.0.0beta1 has meant a
045: * resetting of CVS revision numbers.
046: * ----------------------------------------------------------
047: */
048:
049: /**
050: *
051: * @author Takis Diakoumis
052: * @version $Revision: 1.7 $
053: * @date $Date: 2006/07/09 14:34:51 $
054: */
055: public class GradientLabel extends JComponent {
056:
057: /** The text to be displayed */
058: private String text;
059:
060: /** The font */
061: private Font font;
062:
063: /** the image icon displayed */
064: private ImageIcon icon;
065:
066: /** The component's left-hand gradient colour */
067: private Color leftGradientColor;
068:
069: /** The component's right-hand gradient colour */
070: private Color rightGradientColor;
071:
072: /** The text label foreground colour */
073: private Color foregroundColor;
074:
075: /** indicates that a shadow should be dropped on the text */
076: private boolean shadowDropped;
077:
078: /** The component's insets */
079: private static Insets insets;
080:
081: /** the default font size - 14pts */
082: public static final int DEFAULT_FONT_SIZE = 14;
083:
084: /**
085: * Creates a new instance with default component settings.
086: */
087: public GradientLabel() {
088: this ("", null, new Font("dialog", Font.BOLD, DEFAULT_FONT_SIZE));
089: }
090:
091: /**
092: * Creates a new instance with the specified text, icon and font.
093: *
094: * @param text - the label text
095: * @param icon - the label image icon
096: * @param font - the label font
097: */
098: public GradientLabel(String text, ImageIcon icon, Font font) {
099: this (text, icon, font, null, null);
100: }
101:
102: public GradientLabel(String text, ImageIcon icon, Font font,
103: Color leftGradientColor, Color rightGradientColor) {
104: this (text, icon, font, leftGradientColor, rightGradientColor,
105: null, false);
106: }
107:
108: public GradientLabel(String text, ImageIcon icon, Font font,
109: Color leftGradientColor, Color rightGradientColor,
110: Color foregroundColor, boolean shadowDropped) {
111: this .text = text;
112: this .icon = icon;
113: this .font = font;
114: this .setLeftGradientColor(leftGradientColor);
115: this .setRightGradientColor(rightGradientColor);
116: this .shadowDropped = shadowDropped;
117:
118: if (foregroundColor == null) {
119: if (!UIUtils.isWindowsLookAndFeel()) {
120: foregroundColor = Color.WHITE;
121: } else {
122: if (leftGradientColor == null) {
123: leftGradientColor = getLeftGradientColor();
124: }
125: foregroundColor = UIUtils.getInverse(leftGradientColor)
126: .darker();
127: }
128: }
129: setForeground(foregroundColor);
130: }
131:
132: /*
133: public GradientLabel(String text, ImageIcon icon, int fontSize,
134: Color foreground, int fontStyle) {
135:
136: if (text != null) {
137: this.text = text;
138: }
139:
140: font = new Font("dialog", fontStyle, fontSize);
141:
142: if (insets == null) {
143: insets = new Insets(3,2,3,2);
144: }
145:
146: label = new JLabel(text);
147: label.setFont(font);
148:
149: /*
150:
151: if (!UIUtils.isWindowsLookAndFeel()) {
152: label.setForeground(foreground);
153: } else {
154: label.setForeground(UIManager.getColor("TitledBorder.titleColor"));
155: }
156: * /
157:
158: if (icon != null) {
159: label.setIcon(icon);
160: }
161:
162: //leftGradientColor = UIManager.getColor("controlShadow");
163: //rightGradientColor = UIManager.getColor("control");
164:
165: if (leftGradientColor == null) {
166: //leftGradientColor = UIManager.getColor("InternalFrame.activeTitleBackground");
167: leftGradientColor = UIUtils.getDefaultActiveBackgroundColour();
168: }
169:
170: if (rightGradientColor == null) {
171: rightGradientColor = UIUtils.getDefaultInactiveBackgroundColour();
172: //UIManager.getColor("InternalFrame.inactiveTitleBackground");
173: }
174:
175: if (!UIUtils.isWindowsLookAndFeel()) {
176: label.setForeground(foreground);
177: } else {
178: label.setForeground(UIUtils.getInverse(leftGradientColor).darker());
179: //label.setForeground(UIManager.getColor("TitledBorder.titleColor"));
180: }
181:
182: setLayout(new FlowLayout(FlowLayout.LEFT, 5, 3));
183: add(label);
184: }
185: */
186:
187: /**
188: * Overides to return <code>true</code>.
189: *
190: * @return <code>true</code>
191: */
192: public boolean isOpaque() {
193: return true;
194: }
195:
196: /**
197: * Returns the component's inset margin.
198: *
199: * @return the insets
200: */
201: public Insets getInsets() {
202: if (insets == null) {
203: insets = new Insets(3, 2, 3, 2);
204: }
205: return insets;
206: }
207:
208: /**
209: * Returns the label text foreground colour.
210: *
211: * @return the label foreground colour
212: */
213: public Color getForeground() {
214: return foregroundColor;
215: }
216:
217: /**
218: * Returns the label text font.
219: *
220: * @return the font
221: */
222: public Font getFont() {
223: if (font == null) {
224: return super .getFont();
225: }
226: return font;
227: }
228:
229: /**
230: * Performs the painting for this component.
231: *
232: * @param the <code>Graphics</code> object to
233: * perform the painting
234: */
235: protected void paintComponent(Graphics g) {
236: super .paintComponent(g);
237:
238: int width = getWidth();
239: int height = getHeight();
240:
241: Graphics2D g2 = (Graphics2D) g;
242:
243: // draw the gradient background
244: Color color1 = getLeftGradientColor();
245: Color color2 = getRightGradientColor();
246:
247: if (color1 != null && color2 != null) {
248: Paint paint = g2.getPaint();
249: GradientPaint fade = new GradientPaint(0, 0, color1,
250: (int) (width * 0.9), 0, color2);
251: g2.setPaint(fade);
252: g2.fillRect(0, 0, width, height);
253: g2.setPaint(paint);
254: }
255:
256: Color lineColour = getSeparatorColour();
257: if (lineColour != null) {
258: g2.setColor(lineColour);
259: g2.drawLine(0, 0, width, 0);
260: }
261:
262: // draw the icon and text
263: Insets _insets = getInsets();
264: int x = _insets.left + 5;
265: int y = _insets.top + 1;
266: if (icon != null) {
267: icon.paintIcon(this , g2, x, y);
268: x += icon.getIconWidth() + 10;
269: }
270:
271: if (text != null) {
272: Font _font = getFont();
273: g2.setFont(_font);
274: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
275: RenderingHints.VALUE_ANTIALIAS_ON);
276:
277: FontMetrics metrics = getFontMetrics(_font);
278: int fontHeight = metrics.getHeight();
279: y = ((height - fontHeight) / 2) + fontHeight - 2;
280:
281: if (isShadowDropped()) {
282: Composite composite = g2.getComposite();
283: g2.setComposite(AlphaComposite.getInstance(
284: AlphaComposite.SRC_OVER, 0.8f));
285: g2.setColor(Color.BLACK);
286: g2.drawString(text, x + 2, y + 2);
287: g2.setComposite(composite);
288: }
289:
290: g2.setColor(getForeground());
291: g2.drawString(text, x, y);
292: }
293: }
294:
295: /**
296: * Returns the colour of the top border line.
297: * This is the only border line painted by the component.
298: * The default value returned is the value retrieved from
299: * <code>UIManager.getColor("controlShadow")</code>.
300: * <br>Override to return null and not have the line painted.
301: *
302: * @return the top-line separator colour
303: */
304: public Color getSeparatorColour() {
305: return UIManager.getColor("controlShadow");
306: }
307:
308: public Dimension getPreferredSize() {
309: return new Dimension(getWidth(), getHeight());
310: }
311:
312: /** the label height */
313: private int height;
314:
315: /**
316: * Calculates the height based on the font and icon size specified.
317: *
318: * @return the component's height
319: */
320: public int getHeight() {
321: if (height == 0) {
322: Insets _insets = getInsets();
323: height = _insets.top + _insets.bottom;
324:
325: // check icon height
326: int iconHeight = 0;
327: if (icon != null) {
328: iconHeight = icon.getIconHeight();
329: }
330:
331: // get font height
332: FontMetrics metrics = getFontMetrics(getFont());
333: int fontHeight = metrics.getHeight();
334:
335: height += Math.max(iconHeight, fontHeight) + 2;
336: }
337: return height;
338: }
339:
340: public void setIcon(ImageIcon icon) {
341: this .icon = icon;
342: }
343:
344: /**
345: * Returns the right hand gradient colour.
346: * If this is null, the default colour used is retrieved using
347: * <code>UIUtils.getDefaultInactiveBackgroundColour()</code> and
348: * is usually the unmodified component brackground.
349: *
350: * @return the right-hand gradient colour
351: */
352: public Color getRightGradientColor() {
353: if (rightGradientColor == null) {
354: setRightGradientColor(UIUtils
355: .getDefaultInactiveBackgroundColour());
356: }
357: return rightGradientColor;
358: }
359:
360: /**
361: * Returns the right hand gradient colour.
362: * If this is null, the default colour used is retrieved using
363: * <code>UIUtils.getDefaultActiveBackgroundColour()</code>.
364: *
365: * @return the left-hand gradient colour
366: */
367: public Color getLeftGradientColor() {
368: if (leftGradientColor == null) {
369: setLeftGradientColor(UIUtils
370: .getDefaultActiveBackgroundColour());
371: }
372: return leftGradientColor;
373: }
374:
375: public void setText(String _text) {
376: String oldValue = text;
377:
378: if (_text == null) {
379: text = "";
380: } else {
381: text = _text;
382: }
383:
384: firePropertyChange("text", oldValue, text);
385: if (oldValue == null || !text.equals(oldValue)) {
386: revalidate();
387: repaint();
388: }
389:
390: }
391:
392: /**
393: * Returns the text string that this component displays.
394: *
395: * @return the text displayed
396: */
397: public String getText() {
398: return text;
399: }
400:
401: public boolean isShadowDropped() {
402: return shadowDropped;
403: }
404:
405: public void setShadowDropped(boolean shadowDropped) {
406: this .shadowDropped = shadowDropped;
407: }
408:
409: public void setLeftGradientColor(Color leftGradientColor) {
410: this .leftGradientColor = leftGradientColor;
411: }
412:
413: public void setRightGradientColor(Color rightGradientColor) {
414: this .rightGradientColor = rightGradientColor;
415: }
416:
417: public void setForeground(Color foregroundColor) {
418: super.setForeground(foregroundColor);
419: this.foregroundColor = foregroundColor;
420: }
421:
422: }
|