KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > util > ExtendedJLabel


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.util;
9
10 import javax.swing.*;
11 import java.awt.*;
12
13 /**
14     A <tt>JLabel</tt> that can be underlined, implements <tt>Scrollable</tt>,
15     may have a tooltip text equal to its text and exposes a few convenience methods
16     for <tt>setText()</tt>.
17  
18     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
19     @version $Revision: 1.3 $ $Date: 2003/08/18 07:47:20 $
20 */

21 public class ExtendedJLabel extends JLabel implements Scrollable {
22
23     private boolean underlined = false;
24     private boolean autoTooltip = false;
25
26     /**
27         Constructor.
28      */

29     public ExtendedJLabel() {
30     }
31
32     /**
33         Constructor.
34         @param text the label text.
35      */

36     public ExtendedJLabel(String JavaDoc text) {
37         super(text);
38     }
39     
40     public Dimension getPreferredScrollableViewportSize() {
41         return getSize();
42     }
43
44     public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
45         return getWidth() / 10;
46     }
47     
48     public boolean getScrollableTracksViewportWidth() {
49         return false;
50     }
51     
52     public boolean getScrollableTracksViewportHeight() {
53         return false;
54     }
55     
56     public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
57         return 10;
58     }
59     
60     /**
61         Check whether this label is underlined.
62         @return underlined or not
63      */

64     public boolean isUnderlined() {
65         return underlined;
66     }
67     
68     /**
69         Set whether this label is underlined.
70         @param underlined underlined or not
71      */

72     public void setUnderlined(boolean underlined) {
73         this.underlined = underlined;
74         repaint();
75     }
76     
77     /**
78         Check whether the tooltip text is automatically equal
79         to the text of this label or not.
80         @return equal or not
81      */

82     public boolean getAutoTooltip() {
83         return autoTooltip;
84     }
85
86     /**
87         Set whether the tooltip text is automatically equal
88         to the text of this label or not.
89         @param autoTooltip equal or not
90      */

91     public void setAutoTooltip(boolean autoTooltip) {
92         this.autoTooltip = autoTooltip;
93         setToolTipText(getText());
94     }
95     
96     public void setText(String JavaDoc text) {
97         super.setText(text);
98         if (autoTooltip) {
99             setToolTipText(text);
100         }
101     }
102     
103     /**
104         Convenience method for calling <tt>setText()</tt> with a <tt>short</tt>.
105         @param number the <tt>short</tt>
106      */

107     public void setText(short number) {
108         setText(String.valueOf(number));
109     }
110
111     /**
112         Convenience method for calling <tt>setText()</tt> with a <tt>int</tt>.
113         @param number the <tt>int</tt>
114      */

115     public void setText(int number) {
116         setText(String.valueOf(number));
117     }
118
119     /**
120         Convenience method for calling <tt>setText()</tt> with a <tt>double</tt>.
121         @param number the <tt>double</tt>
122      */

123     public void setText(double number) {
124         setText(String.valueOf(number));
125     }
126
127     /**
128         Convenience method for calling <tt>setText()</tt> with a <tt>float</tt>.
129         @param number the <tt>float</tt>
130      */

131     public void setText(float number) {
132         setText(String.valueOf(number));
133     }
134
135     /**
136         Convenience method for calling <tt>setText()</tt> with a <tt>long</tt>.
137         @param number the <tt>long</tt>
138      */

139     public void setText(long number) {
140         setText(String.valueOf(number));
141     }
142
143     public void paint(Graphics g) {
144         super.paint(g);
145         
146         if (underlined) {
147             Insets i = getInsets();
148             FontMetrics fm = g.getFontMetrics();
149
150             Rectangle textRect = new Rectangle();
151             Rectangle viewRect = new Rectangle(i.left, i.top, getWidth() - (i.right + i.left), getHeight() - (i.bottom + i.top) );
152
153             SwingUtilities.layoutCompoundLabel(
154                                 this,
155                                 fm,
156                                 getText(),
157                                 getIcon(),
158                                 getVerticalAlignment(),
159                                 getHorizontalAlignment(),
160                                 getVerticalTextPosition(),
161                                 getHorizontalTextPosition(),
162                                 viewRect,
163                                 new Rectangle(),
164                                 textRect,
165                                 getText() == null ? 0 : ((Integer JavaDoc)UIManager.get("Button.textIconGap")).intValue()
166                           );
167
168
169             int offset = 2;
170             if (UIManager.getLookAndFeel().isNativeLookAndFeel() && System.getProperty("os.name").startsWith("Windows")) {
171                 offset = 1;
172             }
173             g.fillRect(textRect.x + ((Integer JavaDoc)UIManager.get("Button.textShiftOffset")).intValue() ,
174                        textRect.y + fm.getAscent() + ((Integer JavaDoc)UIManager.get("Button.textShiftOffset")).intValue() + offset,
175                        textRect.width,
176                        1);
177         }
178     }
179 }
180
181
Popular Tags