-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultiLineLabel.java
272 lines (243 loc) · 6.99 KB
/
MultiLineLabel.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
/*
DEM -- A Geographic Information System for Line-Of-Sight Radio Communications.
Copyright (C) 1998, 1999 Jeffrey B. Otterson
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
For more information, to submit bugs, software changes, etc., please contact
Jeff Otterson / N1KDO
3543 Tritt Springs Way
Marietta, GA 30062
otterson@mindspring.com
*/
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
/**
* This class provides a multi-line label. Lines are separated with the \n character.
*
* @author Jeffrey B. Otterson
* @see java.awt.Label
*/
public class MultiLineLabel extends Component
{
String label;
int alignment;
private static final String base = "multilinelabel";
private static int nameCounter = 0;
/**
* Left alignment
*/
public final static int LEFT = 0;
/**
* Center alignment
*/
public final static int CENTER = 1;
/**
* Right alignment
*/
public final static int RIGHT = 2;
/**
* Creates a MultiLineLabel with no label text and default alignment (left justified).
*/
public MultiLineLabel()
{
this("", LEFT);
} /* MultiLineLabel constructor */
/**
* Creates a MultiLineLabel with the specified label and default alignment (left justified).
* @param label the text that makes up the label.
*/
public MultiLineLabel(String label)
{
this(label, LEFT);
} /* MultiLineLabel constructor */
/**
* Creates a MultiLineLabel with the specified label and aligmment. The
* alignment value must be one of LEFT, CENTER, or RIGHT.
* @param label the text that makes up the label.
* @param alignment the alignment value.
*/
public MultiLineLabel(String label, int alignment)
{
this.setName(base + nameCounter++);
this.label = label;
this.alignment = alignment;
} /* MultiLineLabel constructor */
/**
* Gets the text of this MultiLineLabel.
* @return the text of this MultiLineLabel.
* @see #setText
*/
public String getText()
{
return label;
} /* getText() */
/**
* Sets the text for this MultiLineLabel to the specified text.
* @param label text of the MultiLineLabel.
* @see #getText
*/
public synchronized void setText(String label)
{
this.label = label;
invalidate();
repaint();
} /* setLabel */
/**
* Gets the alignment of this MultiLineLabel
* @return the current aligmnent of this MultiLineLabel
* @see #setAlignment
*/
public int getAlignment()
{
return alignment;
} /* getAlignment() */
/**
* Sets the alignment of this MultiLineLabel to the specified alignment.
* @param alignment the alignment value.
* @see #getAlignment
*/
public synchronized void setAlignment(int alignment)
{
this.alignment = alignment;
invalidate();
repaint();
} /* setAlignment */
/**
* paints the MultiLineLabel
* @param g The graphics context to use for painting.
*/
public void paint(Graphics g)
{
Font f = getFont();
if (f != null)
{
if (label != null)
{
g.setFont(f);
FontMetrics fm = getFontMetrics(f);
int font_height = fm.getAscent() + fm.getDescent();
int index = 0;
int last_index = 0;
int num_lines = 1;
/* count the lines of text */
index = label.indexOf('\n');
while (index != -1)
{
num_lines++;
index = label.indexOf('\n', index + 1);
} /* while index != -1 */
Dimension size = getSize();
int text_height = num_lines * font_height;
int x;
int y = Math.max(0, (size.height - text_height) / 2) + fm.getAscent();
/* start drawing the lines of text */
index = label.indexOf('\n');
String line;
while (index != -1)
{
line = label.substring(last_index, index);
x = calculateOffset(fm.stringWidth(line), size.width);
g.drawString(line, x, y);
y += font_height;
last_index = index + 1;
index = label.indexOf('\n', last_index);
} /* while */
/* draw the last line of text */
line = label.substring(last_index);
if (line.length() > 0)
{
x = calculateOffset(fm.stringWidth(line), size.width);
g.drawString(line, x, y);
//y += font_height;
} /* if lastLine.length() > 0 */
} /* if label != null */
} /* if f != null */
} /* paint */
/**
* calculate the offset for a line of text based on the alignment.
* @param width the width of the line of text
* @param wholeWidth the width of the MultiLineLabel
* @return the calculated x offset for the line of text
*/
private int calculateOffset(int width, int wholeWidth)
{
int x;
switch (alignment)
{
case LEFT:
x = 0;
break;
case RIGHT:
x = wholeWidth - width;
break;
case CENTER:
x = (wholeWidth - width) / 2;
break;
default:
x = 0;
break;
} /* switch */
return(x);
} /* calculateOffset() */
/**
* Gets the preferred size of the MultiLineLabel.
* @return A dimension object indicating this MultiLineLabel's preferred size.
*/
public Dimension getPreferredSize()
{
Font f = getFont();
if (f != null)
{
FontMetrics fm = getFontMetrics(f);
int fontHeight = fm.getAscent() + fm.getDescent();
int x = 0;
int y = 0;
int index = 0;
int last_index = 0;
if (label != null)
{
index = label.indexOf('\n');
String line;
while (index != -1)
{
line = label.substring(last_index, index);
y += fontHeight;
x = Math.max(x, fm.stringWidth(line));
last_index = index + 1;
index = label.indexOf('\n', last_index);
} /* while */
line = label.substring(last_index);
if (line.length() > 0)
{
y += fontHeight;
x = Math.max(x, fm.stringWidth(line));
} /* if lastLine.length() > 0 */
} /* if label != null */
return new Dimension(x, y);
} /* if f != null */
else
{
return new Dimension(1, 1);
} /* if f != null */
} /* getPreferredSize() */
/**
* Gets the minimum size of the MultiLineLabel.
* @return A dimension object indicating this MultiLineLabel's minimum size.
*/
public Dimension getMinimumSize()
{
return(getPreferredSize());
} /* getMinimumSize() */
} /* class MultiLineLabel */