001: /*
002: This library is free software; you can redistribute it and/or
003: modify it under the terms of the GNU General Public
004: License as published by the Free Software Foundation; either
005: version 2 of the license, or (at your option) any later version.
006: */
007:
008: package org.gjt.jclasslib.util;
009:
010: import javax.swing.*;
011: import java.awt.*;
012:
013: /**
014: A <tt>JLabel</tt> that can be underlined, implements <tt>Scrollable</tt>,
015: may have a tooltip text equal to its text and exposes a few convenience methods
016: for <tt>setText()</tt>.
017:
018: @author <a href="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
019: @version $Revision: 1.3 $ $Date: 2003/08/18 07:47:20 $
020: */
021: public class ExtendedJLabel extends JLabel implements Scrollable {
022:
023: private boolean underlined = false;
024: private boolean autoTooltip = false;
025:
026: /**
027: Constructor.
028: */
029: public ExtendedJLabel() {
030: }
031:
032: /**
033: Constructor.
034: @param text the label text.
035: */
036: public ExtendedJLabel(String text) {
037: super (text);
038: }
039:
040: public Dimension getPreferredScrollableViewportSize() {
041: return getSize();
042: }
043:
044: public int getScrollableBlockIncrement(Rectangle visibleRect,
045: int orientation, int direction) {
046: return getWidth() / 10;
047: }
048:
049: public boolean getScrollableTracksViewportWidth() {
050: return false;
051: }
052:
053: public boolean getScrollableTracksViewportHeight() {
054: return false;
055: }
056:
057: public int getScrollableUnitIncrement(Rectangle visibleRect,
058: int orientation, int direction) {
059: return 10;
060: }
061:
062: /**
063: Check whether this label is underlined.
064: @return underlined or not
065: */
066: public boolean isUnderlined() {
067: return underlined;
068: }
069:
070: /**
071: Set whether this label is underlined.
072: @param underlined underlined or not
073: */
074: public void setUnderlined(boolean underlined) {
075: this .underlined = underlined;
076: repaint();
077: }
078:
079: /**
080: Check whether the tooltip text is automatically equal
081: to the text of this label or not.
082: @return equal or not
083: */
084: public boolean getAutoTooltip() {
085: return autoTooltip;
086: }
087:
088: /**
089: Set whether the tooltip text is automatically equal
090: to the text of this label or not.
091: @param autoTooltip equal or not
092: */
093: public void setAutoTooltip(boolean autoTooltip) {
094: this .autoTooltip = autoTooltip;
095: setToolTipText(getText());
096: }
097:
098: public void setText(String text) {
099: super .setText(text);
100: if (autoTooltip) {
101: setToolTipText(text);
102: }
103: }
104:
105: /**
106: Convenience method for calling <tt>setText()</tt> with a <tt>short</tt>.
107: @param number the <tt>short</tt>
108: */
109: public void setText(short number) {
110: setText(String.valueOf(number));
111: }
112:
113: /**
114: Convenience method for calling <tt>setText()</tt> with a <tt>int</tt>.
115: @param number the <tt>int</tt>
116: */
117: public void setText(int number) {
118: setText(String.valueOf(number));
119: }
120:
121: /**
122: Convenience method for calling <tt>setText()</tt> with a <tt>double</tt>.
123: @param number the <tt>double</tt>
124: */
125: public void setText(double number) {
126: setText(String.valueOf(number));
127: }
128:
129: /**
130: Convenience method for calling <tt>setText()</tt> with a <tt>float</tt>.
131: @param number the <tt>float</tt>
132: */
133: public void setText(float number) {
134: setText(String.valueOf(number));
135: }
136:
137: /**
138: Convenience method for calling <tt>setText()</tt> with a <tt>long</tt>.
139: @param number the <tt>long</tt>
140: */
141: public void setText(long number) {
142: setText(String.valueOf(number));
143: }
144:
145: public void paint(Graphics g) {
146: super .paint(g);
147:
148: if (underlined) {
149: Insets i = getInsets();
150: FontMetrics fm = g.getFontMetrics();
151:
152: Rectangle textRect = new Rectangle();
153: Rectangle viewRect = new Rectangle(i.left, i.top,
154: getWidth() - (i.right + i.left), getHeight()
155: - (i.bottom + i.top));
156:
157: SwingUtilities.layoutCompoundLabel(this , fm, getText(),
158: getIcon(), getVerticalAlignment(),
159: getHorizontalAlignment(),
160: getVerticalTextPosition(),
161: getHorizontalTextPosition(), viewRect,
162: new Rectangle(), textRect, getText() == null ? 0
163: : ((Integer) UIManager
164: .get("Button.textIconGap"))
165: .intValue());
166:
167: int offset = 2;
168: if (UIManager.getLookAndFeel().isNativeLookAndFeel()
169: && System.getProperty("os.name").startsWith(
170: "Windows")) {
171: offset = 1;
172: }
173: g.fillRect(textRect.x
174: + ((Integer) UIManager
175: .get("Button.textShiftOffset")).intValue(),
176: textRect.y
177: + fm.getAscent()
178: + ((Integer) UIManager
179: .get("Button.textShiftOffset"))
180: .intValue() + offset,
181: textRect.width, 1);
182: }
183: }
184: }
|