-
Notifications
You must be signed in to change notification settings - Fork 3
/
TextIcon.java
325 lines (279 loc) · 7.87 KB
/
TextIcon.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import java.awt.*;
import java.beans.*;
import java.util.*;
import javax.swing.*;
import java.awt.font.*;
/**
* The TextIcon will paint a String of text as an Icon. The Icon
* can be used by any Swing component that supports icons.
*
* TextIcon supports two different layout styles:
* <ul>
* <li>Horizontally - does normal rendering of the text by using the
* Graphics.drawString(...) method
* <li>Vertically - Each character is displayed on a separate line
* </ul>
*
* TextIcon was designed to be rendered on a specific JComponent as it
* requires FontMetrics information in order to calculate its size and to do
* the rendering. Therefore, it should only be added to component it was
* created for.
*
* By default the text will be rendered using the Font and foreground color
* of its associated component. However, this class does allow you to override
* these properties. Also starting in JDK6 the desktop renderering hints will
* be used to renderer the text. For versions not supporting the rendering
* hints antialiasing will be turned on.
*/
public class TextIcon implements Icon, PropertyChangeListener
{
public enum Layout
{
HORIZONTAL,
VERTICAL;
}
private JComponent component;
private Layout layout;
private String text;
private Font font;
private Color foreground;
private int padding;
// Used for the implementation of Icon interface
private int iconWidth;
private int iconHeight;
// Used for Layout.VERTICAL to save reparsing the text every time the
// icon is repainted
private String[] strings;
private int[] stringWidths;
/**
* Convenience constructor to create a TextIcon with a HORIZONTAL layout.
*
* @param component the component to which the icon will be added
* @param text the text to be rendered on the Icon
*/
public TextIcon(JComponent component, String text)
{
this(component, text, Layout.HORIZONTAL);
}
/**
* Create a TextIcon specifying all the properties.
*
* @param component the component to which the icon will be added
* @param text the text to be rendered on the Icon
* @param layout specify the layout of the text. Must be one of
* the Layout enums: HORIZONTAL or VERTICAL
*/
public TextIcon(JComponent component, String text, Layout layout)
{
this.component = component;
this.layout = layout;
setText( text );
component.addPropertyChangeListener("font", this);
}
/**
* Get the Layout enum
*
* @return the Layout enum
*/
public Layout getLayout()
{
return layout;
}
/**
* Get the text String that will be rendered on the Icon
*
* @return the text of the Icon
*/
public String getText()
{
return text;
}
/**
* Set the text to be rendered on the Icon
*
* @param text the text to be rendered on the Icon
*/
public void setText(String text)
{
this.text = text;
calculateIconDimensions();
}
/**
* Get the Font used to render the text. This will default to the Font
* of the component unless the Font has been overridden by using the
* setFont() method.
*
* @return the Font used to render the text
*/
public Font getFont()
{
if (font == null)
return component.getFont();
else
return font;
}
/**
* Set the Font to be used for rendering the text
*
* @param font the Font to be used for rendering the text
*/
public void setFont(Font font)
{
this.font = font;
calculateIconDimensions();
}
/**
* Get the foreground Color used to render the text. This will default to
* the foreground Color of the component unless the foreground Color has
* been overridden by using the setForeground() method.
*
* @return the Color used to render the text
*/
public Color getForeground()
{
if (foreground == null)
return component.getForeground();
else
return foreground;
}
/**
* Set the foreground Color to be used for rendering the text
*
* @param foreground the foreground Color to be used for rendering the text
*/
public void setForeground(Color foreground)
{
this.foreground = foreground;
component.repaint();
}
/**
* Get the padding used when rendering the text
*
* @return the padding specified in pixels
*/
public int getPadding()
{
return padding;
}
/**
* By default the size of the Icon is based on the size of the rendered
* text. You can specify some padding to be added to the start and end
* of the text when it is rendered.
*
* @param padding the padding amount in pixels
*/
public void setPadding(int padding)
{
this.padding = padding;
calculateIconDimensions();
}
/**
* Calculate the size of the Icon using the FontMetrics of the Font.
*/
private void calculateIconDimensions()
{
Font font = getFont();
FontMetrics fm = component.getFontMetrics( font );
if (layout == Layout.HORIZONTAL)
{
iconWidth = fm.stringWidth( text ) + (padding * 2);
iconHeight = fm.getHeight();
}
else if (layout == Layout.VERTICAL)
{
int maxWidth = 0;
strings = new String[text.length()];
stringWidths = new int[text.length()];
// Find the widest character in the text string
for (int i = 0; i < text.length(); i++)
{
strings[i] = text.substring(i, i + 1);
stringWidths[i] = fm.stringWidth( strings[i] );
maxWidth = Math.max(maxWidth, stringWidths[i]);
}
// Add a minimum of 2 extra pixels, plus the leading value,
// on each side of the character.
iconWidth = maxWidth + ((fm.getLeading() + 2) * 2);
// Decrease then normal gap betweens lines of text by taking into
// account the descent.
iconHeight = (fm.getHeight() - fm.getDescent()) * text.length();
iconHeight += padding * 2;
}
component.revalidate();
}
//
// Implement the Icon Interface
//
/**
* Gets the width of this icon.
*
* @return the width of the icon in pixels.
*/
@Override
public int getIconWidth()
{
return iconWidth;
}
/**
* Gets the height of this icon.
*
* @return the height of the icon in pixels.
*/
@Override
public int getIconHeight()
{
return iconHeight;
}
/**
* Paint the icons of this compound icon at the specified location
*
* @param c The component to which the icon is added
* @param g the graphics context
* @param x the X coordinate of the icon's top-left corner
* @param y the Y coordinate of the icon's top-left corner
*/
@Override
public void paintIcon(Component c, Graphics g, int x, int y)
{
Graphics2D g2 = (Graphics2D)g.create();
// The "desktophints" is supported in JDK6
Toolkit toolkit = Toolkit.getDefaultToolkit();
Map map = (Map)(toolkit.getDesktopProperty("awt.font.desktophints"));
if (map != null)
{
g2.addRenderingHints(map);
}
else
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
g2.setFont( getFont() );
g2.setColor( getForeground() );
FontMetrics fm = g2.getFontMetrics();
if (layout == Layout.HORIZONTAL)
{
g2.translate(x, y + fm.getAscent());
g2.drawString(text, padding, 0);
}
else if (layout == Layout.VERTICAL)
{
int offsetY = fm.getAscent() - fm.getDescent() + padding;
int incrementY = fm.getHeight() - fm.getDescent();
for (int i = 0; i < text.length(); i++)
{
int offsetX = Math.round((getIconWidth() - stringWidths[i]) / 2.0f);
g2.drawString(strings[i], x + offsetX, y + offsetY);
offsetY += incrementY;
}
}
// g2.dispose();
}
//
// Implement the PropertyChangeListener interface
//
public void propertyChange(PropertyChangeEvent e)
{
// Handle font change when using the default font
if (font == null)
calculateIconDimensions();
}
}